R: connect, and every tool is a function under b$domain$.
The client is the package bentrixmcp. It finds the Bentrix running on this computer through the agents folder Bentrix writes at every start, pairs with it under the name you give — the name the person sees in ☰ › Settings › Agents, where the pairing's scopes are set — and talks to it over MCP. Nothing is typed: no address, no key.
01What you need
R 4 or later with the packages curl and jsonlite, and Bentrix open on the same computer.
02Install
The client is the package bentrixmcp of the SDK. Its source is github.com/bentrixhq/bentrix-r, under the MIT licence; a clone of it is the SDK on your computer:
git clone https://github.com/bentrixhq/bentrix-r
and install the package once:
R CMD INSTALL bentrix-r/bentrixmcp
03Connect
library(bentrixmcp)
b <- connect("my-agent")
state <- b$workspace$get_state()
bench <- state$laboratories[[1]]$benches[[1]]$uri
p <- strsplit(sub("^bentrix://", "", bench), "/", fixed = TRUE)[[1]]
book <- b$trade$book(p[1], p[2], p[4])
04How a call looks
Every tool of the catalogue is a method, grouped by its domain: b$workspace, b$chart, b$study, b$trade …. A method takes the tool's arguments as arguments — the required ones by position, the optional ones by name, and answers the tool's answer as a list. The whole list is at the end of this page; each method links to its tool's full entry — the arguments with their types and defaults, the scope a pairing must grant, and the plan.
05When Bentrix says no
A tool that runs and refuses signals an error condition carrying the product's own sentence; tryCatch reads it. The client also checks, when it connects, that the catalogue it was generated from is the one the running Bentrix serves: a different major version is refused before any call, so a program never calls a tool with arguments that no longer fit.
Busy is waited out, not raised. Bentrix answers one program at most 20 calls a second (a burst of 60), and answers a call past that busy (-40001, with the wait in retryAfterMs) before running anything. The client waits that long and sends the call again — so an order is never sent twice by it — up to ten times, each wait at most five seconds, and only then raises the refusal: .bx_busy (-40001), .bx_busy_retries (10) and .bx_busy_wait_max_s (5).
06Long work and streams
A tool that starts long work — a strategy run, a batch, a question put to the person in Bentrix's own window — answers a job; its handle is read with job_get until it ends. A stream (bars as they close, ticks, quotes, deals, the Journal) is followed with wait_for, which answers what came after the cursor it is given and the cursor to give next.
Your first program
Save this as a file, with Bentrix open on this computer. It connects, lists your laboratories and their workbenches, and prints the selected workbench’s markets.
library(bentrixmcp)
client <- connect("my-agent")
# Every laboratory, and the workbenches in it.
state <- client$workspace$get_state(depth = "benches")
for (lab in state$laboratories) {
cat("laboratory:", format(lab$name), "\n")
for (bench in lab$benches) {
cat(" workbench:", format(bench$uri), "\n")
}
}Run it
Rscript my_agent.RThe first run pairs the program: it appears in ☰ › Settings › Agents under the name it gave (my-agent), with the scopes read, lab and paper. If it says Bentrix is not running, open Bentrix and run it again.
A larger program, the one the tests run
This is the program Bentrix’s own tests run in R against a running Bentrix: it reads the workspace and a workbench’s book, is refused in the product’s words, and runs a reinforcement-learning episode through the tools.
library(bentrixmcp)
check <- function(held, what) if (!isTRUE(held)) {
message("FAILED: ", what)
quit(status = 1)
}
b <- connect("r-walk")
state <- b$workspace$get_state()
bench <- state$laboratories[[1]]$benches[[1]]$uri
p <- strsplit(sub("^bentrix://", "", bench), "/", fixed = TRUE)[[1]]
book <- b$trade$book(p[1], p[2], p[4])
check(is.list(book$positions), "the book did not read")
said <- tryCatch({
b$call("no_such_tool")
""
}, bentrix_error = function(e) conditionMessage(e))
check(grepl("no_such_tool", said, fixed = TRUE), paste("a refusal did not say its tool:", said))
made <- b$strategy$env_make(bench, interval = "H1", window = 8, episode = "window", episode_bars = 12)
start <- b$strategy$env_reset(made$env, seed = 5)
check(length(start$obs$market) == 8, "the observation is not shaped as declared")
rewards <- numeric(0)
hashes <- start$info$ledger_hash
for (a in c(1, 1, 2, 0, 2, 2, 1, 0, 1, 2, 0, 1)) {
s <- b$strategy$env_step(made$env, a)
rewards <- c(rewards, s$reward)
hashes <- c(hashes, s$info$ledger_hash)
if (isTRUE(s$terminated) || isTRUE(s$truncated)) break
}
b$strategy$env_close(made$env)
# A tool's own refusal (isError), not a request the wire refused.
refusal <- tryCatch({
b$strategy$env_step(made$env, 0)
NULL
}, bentrix_tool_error = function(e) conditionMessage(e))
check(!is.null(refusal) && grepl("No environment", refusal, fixed = TRUE), paste("a closed environment's step was not refused in its words:", refusal))
check(length(unique(hashes)) > 1, "the episode traded nothing")
# Every reward at 17 significant digits reads back as the same double.
cat(sprintf(
"{\"start_ms\": %s, \"rewards\": [%s], \"hashes\": [%s]}\n",
format(start$info$start_ms, scientific = FALSE), paste(sprintf("%.17g", rewards), collapse = ", "),
paste0("\"", hashes, "\"", collapse = ", ")
))
cat("PASS\n")