Java: Bentrix.connect, and every tool is a method taking its own arguments object.
The client is the package bentrix.mcp. 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
A JDK 21 or later — 22 or later for the engine in your own process — and Bentrix open on the same computer.
02Install
Two ways, the same client either way.
From Maven Central. The client is net.bentrix:bentrix, its version the application's. With Maven:
<dependency>
<groupId>net.bentrix</groupId>
<artifactId>bentrix</artifactId>
<version>0.1.382</version>
</dependency>
With Gradle: implementation("net.bentrix:bentrix:0.1.382"). The engine in your own process is net.bentrix:bentrix-engine beside it (Java 22 or later, run with --enable-native-access=ALL-UNNAMED).
From its source on your computer. Its source is github.com/bentrixhq/bentrix-java, under the MIT licence; a clone of it is the SDK on your computer, compiled with your program:
git clone https://github.com/bentrixhq/bentrix-java
javac -d out bentrix-java/src/bentrix/Json.java bentrix-java/src/bentrix/BentrixException.java bentrix-java/mcp/src/bentrix/mcp/*.java MyAgent.java
java -cp out MyAgent
03Connect
try (Bentrix b = Bentrix.connect("my-agent")) {
Object state = b.workspace.getState(new Bentrix.WorkspaceApi.GetStateArgs());
// ... read the workbench's uri from the state, then:
Object book = b.trade.book(new Bentrix.TradeApi.BookArgs(home, owner, bench));
}
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 one object of the tool's own arguments type (new Bentrix.ChartApi.OpenArgs(bench, symbol)), whose required arguments are its constructor's and whose optional ones are set by its methods, and answers the tool's answer as the JSON as maps and lists. 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 throws a Client.McpException carrying the product's own sentence. 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: Client.BUSY (-40001), Client.BUSY_RETRIES (10) and Client.BUSY_WAIT_MAX_MS (5000); RpcException.retryAfterMs carries the wait.
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.
import bentrix.mcp.Bentrix;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public final class MyAgent {
// at reads a field or an element of an answer: at(v, "laboratories", 0, "uri").
@SuppressWarnings("unchecked")
static Object at(Object v, Object... path) {
for (Object p : path) {
v = p instanceof Integer i ? ((List<Object>) v).get(i) : ((Map<String, Object>) v).get(p);
}
return v;
}
public static void main(String[] argv) throws Exception {
try (Bentrix client = Bentrix.connect("my-agent")) {
// Every laboratory, and the workbenches in it.
Object state = client.workspace.getState(new Bentrix.WorkspaceApi.GetStateArgs().depth("benches"));
for (Object lab : (List<Object>) at(state, "laboratories")) {
System.out.println("laboratory:" + " " + at(lab, "name"));
for (Object bench : (List<Object>) at(lab, "benches")) {
System.out.println(" workbench:" + " " + at(bench, "uri"));
}
}
}
}
}Run it
javac -d out MyAgent.java <the SDK's .java files> && java -cp out MyAgentThe 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 Java 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.
import bentrix.Json;
import bentrix.mcp.Bentrix;
import bentrix.mcp.Client;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public final class McpWalk {
private static void check(boolean held, String what) {
if (!held) {
throw new Client.McpException("FAILED: " + what);
}
}
@SuppressWarnings("unchecked")
private static Object at(Object v, Object... path) {
for (Object p : path) {
v = p instanceof Integer i ? ((List<Object>) v).get(i) : ((Map<String, Object>) v).get(p);
}
return v;
}
public static void main(String[] argv) {
try (Bentrix b = Bentrix.connect("java-walk")) {
String bench = (String) at(b.workspace.getState(new Bentrix.WorkspaceApi.GetStateArgs()), "laboratories", 0, "benches", 0, "uri");
String[] p = bench.replaceFirst("^bentrix://", "").split("/");
Object book = b.trade.book(new Bentrix.TradeApi.BookArgs(p[0], p[1], p[3]));
check(at(book, "positions") instanceof List<?>, "the book did not read: " + book);
String said = "";
try {
b.call("no_such_tool", null);
} catch (Client.McpException e) {
said = e.getMessage();
}
check(said.contains("no_such_tool"), "a refusal did not say its tool: " + said);
Object made = b.strategy.envMake(new Bentrix.StrategyApi.EnvMakeArgs(bench).interval("H1").window(8).episode("window").episodeBars(12));
String env = (String) at(made, "env");
Object start = b.strategy.envReset(new Bentrix.StrategyApi.EnvResetArgs(env).seed(5));
check(((List<?>) at(start, "obs", "market")).size() == 8, "the observation is not shaped as declared");
List<Double> rewards = new ArrayList<>();
List<Object> hashes = new ArrayList<>();
hashes.add(at(start, "info", "ledger_hash"));
for (int a : new int[] {1, 1, 2, 0, 2, 2, 1, 0, 1, 2, 0, 1}) {
Object s = b.strategy.envStep(new Bentrix.StrategyApi.EnvStepArgs(env, a));
rewards.add((Double) at(s, "reward"));
hashes.add(at(s, "info", "ledger_hash"));
if (Boolean.TRUE.equals(at(s, "terminated")) || Boolean.TRUE.equals(at(s, "truncated"))) {
break;
}
}
b.strategy.envClose(new Bentrix.StrategyApi.EnvCloseArgs(env));
// A tool's own refusal (isError), not a request the wire refused.
RuntimeException refusal = null;
try {
b.strategy.envStep(new Bentrix.StrategyApi.EnvStepArgs(env, 0));
} catch (RuntimeException e) {
refusal = e;
}
check(refusal instanceof Client.ToolException && refusal.getMessage().contains("No environment"), "a closed environment's step was not refused in its words: " + refusal);
check(hashes.stream().distinct().count() > 1, "the episode traded nothing");
Map<String, Object> episode = new LinkedHashMap<>();
episode.put("start_ms", ((Double) at(start, "info", "start_ms")).longValue());
episode.put("rewards", rewards);
episode.put("hashes", hashes);
// Double.toString writes the shortest digits that read back as the same double.
System.out.println(Json.write(episode));
System.out.println("PASS");
} catch (Client.McpException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}