Bentrix Docs
HomePricingDashboard

The C++ SDK

C++: a header-only client — Bentrix::connect, and every tool is a method taking its own arguments struct.

The client is the headers bentrix_mcp.hpp and bentrix_mcp_generated.hpp (namespace 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 C++17 compiler, and Bentrix open on the same computer.

02Install

The client is header-only: mcp/bentrix_mcp.hpp and mcp/bentrix_mcp_generated.hpp of the SDK. Its source is github.com/bentrixhq/bentrix-cpp, under the MIT licence; a clone of it is the SDK on your computer:

git clone https://github.com/bentrixhq/bentrix-cpp

Include the generated header and compile:

c++ -std=c++17 -I bentrix-cpp/mcp my_agent.cpp -o my_agent        # on Windows add -lws2_32

03Connect

#include "bentrix_mcp_generated.hpp"
using namespace bentrix::mcp;

int main() {
    auto b = Bentrix::connect("my-agent");
    Json state = b.workspace.get_state(WorkspaceApi::GetStateArgs());
    std::string bench = state["laboratories"][0]["benches"][0]["uri"].str();
}

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 struct of the tool's arguments (ChartApi::OpenArgs(bench, symbol)), and answers the tool's answer as a Json value. 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 bentrix::mcp::Error 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: bentrix::mcp::BUSY (-40001), BUSY_RETRIES (10) and BUSY_WAIT_MAX_MS (5000); RpcError::retry_after_ms 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.

#include <algorithm>
#include <chrono>
#include <iostream>
#include <thread>
#include "bentrix_mcp_generated.hpp"
using namespace bentrix::mcp;

int main() {
    auto client = Bentrix::connect("my-agent");

    // Every laboratory, and the workbenches in it.
    auto state = client.workspace.get_state(WorkspaceApi::GetStateArgs().depth("benches"));
    for (const Json &lab : state["laboratories"].arr()) {
        std::cout << "laboratory:" << " " << lab["name"].dump() << std::endl;
        for (const Json &bench : lab["benches"].arr()) {
            std::cout << "  workbench:" << " " << bench["uri"].dump() << std::endl;
        }
    }
}

Run it

g++ -std=c++17 -I <the SDK's cpp/mcp folder> my_agent.cpp -o my_agent && ./my_agent

The 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 C++ 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.

#include <cstdio>
#include <set>

#include "bentrix_mcp_generated.hpp"

using namespace bentrix::mcp;

static void check(bool held, const std::string &what) {
    if (!held) throw Error("FAILED: " + what);
}

int main() {
    try {
        auto b = Bentrix::connect("cpp-walk");
        Json state = b.workspace.get_state(WorkspaceApi::GetStateArgs());
        std::string bench = state["laboratories"][0]["benches"][0]["uri"].str();
        std::vector<std::string> p;
        {
            std::string rest = bench.substr(std::string("bentrix://").size()), part;
            std::istringstream in(rest);
            while (std::getline(in, part, '/')) p.push_back(part);
        }
        Json book = b.trade.book(TradeApi::BookArgs(p[0], p[1], p[3]));
        check(book["positions"].is_array(), "the book did not read: " + book.dump());
        std::string said;
        try {
            b.call("no_such_tool");
        } catch (const Error &e) {
            said = e.what();
        }
        check(said.find("no_such_tool") != std::string::npos, "a refusal did not say its tool: " + said);

        Json made = b.strategy.env_make(StrategyApi::EnvMakeArgs(bench).interval("H1").window(8).episode("window").episode_bars(12));
        std::string env = made["env"].str();
        Json start = b.strategy.env_reset(StrategyApi::EnvResetArgs(env).seed(5));
        check(start["obs"]["market"].arr().size() == 8, "the observation is not shaped as declared");
        Array rewards;
        Array hashes{start["info"]["ledger_hash"]};
        for (int a : {1, 1, 2, 0, 2, 2, 1, 0, 1, 2, 0, 1}) {
            Json s = b.strategy.env_step(StrategyApi::EnvStepArgs(env, Json(a)));
            rewards.push_back(Json(s["reward"].num()));
            hashes.push_back(s["info"]["ledger_hash"]);
            if (s["terminated"].boolean() || s["truncated"].boolean()) break;
        }
        b.strategy.env_close(StrategyApi::EnvCloseArgs(env));
        // A tool's own refusal (isError), not a request the wire refused.
        std::string refusal;
        try {
            b.strategy.env_step(StrategyApi::EnvStepArgs(env, Json(0)));
        } catch (const ToolError &e) {
            refusal = e.what();
        }
        check(refusal.find("No environment") != std::string::npos, "a closed environment's step was not refused in its words: " + refusal);
        std::set<std::string> distinct;
        for (const auto &h : hashes) distinct.insert(h.str());
        check(distinct.size() > 1, "the episode traded nothing");
        Json episode(Object{{"start_ms", Json(int64_t(start["info"]["start_ms"].num()))}, {"rewards", Json(rewards)}, {"hashes", Json(hashes)}});
        // A reward is written in the shortest of 15 or 17 digits that reads back as the same double.
        std::printf("%s\nPASS\n", episode.dump().c_str());
        return 0;
    } catch (const std::exception &e) {
        std::fprintf(stderr, "%s\n", e.what());
        return 1;
    }
}

Next

Bentrix 0.1.382Every number, name and default on these pages is read from the application itself.What is comingWhat changed