Node.js: connect, and every tool is an async method taking one object of arguments.
The client is the package bentrix for Node.js, with TypeScript types. 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
Node.js 16 or later, and Bentrix open on the same computer.
02Install
Two ways, the same package either way.
From npm. The SDK is the package bentrix on npm, its version the application's:
npm install bentrix
From its source on your computer. Its source is github.com/bentrixhq/bentrix-node, under the MIT licence; a clone of it is the SDK on your computer, installed into your project by its path:
git clone https://github.com/bentrixhq/bentrix-node
npm install ./bentrix-node
The client — require('bentrix/mcp') — needs nothing else. The engine in your own process — require('bentrix'), with Local and the Gym's TradingEnv — goes through the FFI package koffi:
npm install koffi
Check it:
node -e "console.log(require('bentrix/mcp').CATALOGUE_VERSION)"
03Connect
const { connect, ToolError } = require('bentrix/mcp');
(async () => {
const b = await connect({ name: 'my-agent' });
const state = await b.workspace.getState({});
const bench = state.laboratories[0].benches[0].uri;
const [home, owner, , name] = bench.replace('bentrix://', '').split('/');
const book = await b.trade.book({ home, owner, bench: name });
console.log(book.positions.length, 'positions open');
})();
04How a call looks
Every tool of the catalogue is a method, grouped by its domain: b.workspace, b.chart, b.study, b.trade … in camel case. A method takes the tool's arguments as one object, { name: value }, by the tool's own argument names, and answers the tool's answer as a promise of a plain object. 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 rejects with a ToolError carrying the product's own sentence; a request refused before running rejects with the server's error. 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: BUSY (-40001), BUSY_RETRIES (10) and BUSY_WAIT_MAX_MS (5000), exported by the client.
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.
const { connect } = require('bentrix/mcp');
(async () => {
const client = await connect({ name: 'my-agent' });
// Every laboratory, and the workbenches in it.
const state = await client.workspace.getState({ depth: "benches" });
for (const lab of state.laboratories) {
console.log("laboratory:", lab.name);
for (const bench of lab.benches) {
console.log(" workbench:", bench.uri);
}
}
})();Run it
node my_agent.jsThe 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 JavaScript 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.
'use strict';
const { connect, ToolError } = require('bentrix/mcp');
function check(held, what) {
if (!held) throw new Error(`FAILED: ${what}`);
}
(async () => {
const b = await connect({ name: 'node-walk' });
const state = await b.workspace.getState({});
const bench = state.laboratories[0].benches[0].uri;
const [home, owner, , name] = bench.replace('bentrix://', '').split('/');
const book = await b.trade.book({ home, owner, bench: name });
check(Array.isArray(book.positions), `the book did not read: ${JSON.stringify(book)}`);
let said = '';
try {
await b.call('no_such_tool', {});
} catch (e) {
said = String(e.message);
}
check(said.includes('no_such_tool'), `a refusal did not say its tool: ${said}`);
const made = await b.strategy.envMake({ bench, interval: 'H1', window: 8, episode: 'window', episode_bars: 12 });
const actions = [1, 1, 2, 0, 2, 2, 1, 0, 1, 2, 0, 1];
const start = await b.strategy.envReset({ env: made.env, seed: 5 });
check(start.obs.market.length === 8, 'the observation is not shaped as declared');
const rewards = [];
const hashes = [start.info.ledger_hash];
for (const a of actions) {
const s = await b.strategy.envStep({ env: made.env, action: a });
rewards.push(s.reward);
hashes.push(s.info.ledger_hash);
if (s.terminated || s.truncated) break;
}
await b.strategy.envClose({ env: made.env });
// A tool's own refusal (isError), not a request the wire refused (an unknown tool is a JSON-RPC error).
let refusal = null;
try {
await b.strategy.envStep({ env: made.env, action: 0 });
} catch (e) {
refusal = e;
}
check(refusal instanceof ToolError && refusal.message.includes('No environment'), `a closed environment's step was not refused in its words: ${refusal}`);
check(new Set(hashes).size > 1, 'the episode traded nothing');
console.log(JSON.stringify({ start_ms: start.info.start_ms, rewards, hashes }));
console.log('PASS');
})().catch((e) => {
console.error(e instanceof ToolError ? `${e.tool}: ${e.message}` : e.stack || String(e));
process.exit(1);
});