Go: mcp.Connect, and every tool is a method taking a context and an arguments struct.
The client is the module bentrix, 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
Go 1.23 or later, and Bentrix open on the same computer.
02Install
The client is the module bentrix, package bentrix/mcp. Its source is github.com/bentrixhq/bentrix-go, under the MIT licence; a clone of it is the SDK on your computer:
git clone https://github.com/bentrixhq/bentrix-go
The module's path is bentrix, so your own module reaches it through a -replace that points at the clone:
go mod init myagent
go mod edit -require [email protected] -replace bentrix=../bentrix-go
03Connect
ctx := context.Background()
b, err := mcp.Connect(ctx, "my-agent")
if err != nil {
log.Fatal(err)
}
state, err := b.Workspace.GetState(ctx, mcp.WorkspaceGetStateArgs{})
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 Pascal case. A method takes the tool's arguments as a context and one struct named after the tool (mcp.ChartOpenArgs{Bench: …, Symbol: …}); an optional field is a pointer, set with mcp.Ptr(value), and answers the tool's answer as any and an error. 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 answers an error that errors.As reads as a *mcp.ToolError, carrying the product's own sentence in Msg. 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: mcp.Busy (-40001), mcp.BusyRetries (10) and mcp.BusyWaitMax (5 s); RPCError.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.
package main
import (
"context"
"encoding/json"
"fmt"
"bentrix/mcp"
)
// at reads a field or an element of an answer: at(v, "laboratories", 0, "uri").
func at(v any, path ...any) any {
for _, p := range path {
switch k := p.(type) {
case string:
m, _ := v.(map[string]any)
v = m[k]
case int:
a, _ := v.([]any)
if k >= len(a) {
return nil
}
v = a[k]
}
}
return v
}
// must stops the program with Bentrix's own sentence when a call is refused.
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
// num reads a number of an answer, however the client decoded it.
func num(v any) float64 {
switch n := v.(type) {
case json.Number:
f, _ := n.Float64()
return f
case float64:
return n
}
return 0
}
// text reads a text of an answer; nothing is "".
func text(v any) string {
s, _ := v.(string)
return s
}
func main() {
ctx := context.Background()
client, err := mcp.Connect(ctx, "my-agent")
if err != nil {
panic(err)
}
// Every laboratory, and the workbenches in it.
state := must(client.Workspace.GetState(ctx, mcp.WorkspaceGetStateArgs{Depth: mcp.Ptr[string]("benches")}))
for _, lab := range at(state, "laboratories").([]any) {
fmt.Println("laboratory:", at(lab, "name"))
for _, bench := range at(lab, "benches").([]any) {
fmt.Println(" workbench:", at(bench, "uri"))
}
}
_ = []any{state}
}Run it
go run .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 Go 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.
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"bentrix/mcp"
)
func check(held bool, what string) {
if !held {
fmt.Fprintln(os.Stderr, "FAILED:", what)
os.Exit(1)
}
}
func must[T any](v T, err error) T {
check(err == nil, fmt.Sprint(err))
return v
}
func obj(v any) map[string]any { m, _ := v.(map[string]any); return m }
func main() {
ctx := context.Background()
b := must(mcp.Connect(ctx, "go-walk"))
state := obj(must(b.Workspace.GetState(ctx, mcp.WorkspaceGetStateArgs{})))
bench := obj(obj(state["laboratories"].([]any)[0])["benches"].([]any)[0])["uri"].(string)
p := strings.Split(strings.TrimPrefix(bench, "bentrix://"), "/")
book := obj(must(b.Trade.Book(ctx, mcp.TradeBookArgs{Home: p[0], Owner: p[1], Bench: p[3]})))
_, isList := book["positions"].([]any)
check(isList, fmt.Sprint("the book did not read: ", book))
_, err := b.Call(ctx, "no_such_tool", nil)
check(err != nil && strings.Contains(err.Error(), "no_such_tool"), fmt.Sprint("a refusal did not say its tool: ", err))
made := obj(must(b.Strategy.EnvMake(ctx, mcp.StrategyEnvMakeArgs{Bench: bench, Interval: mcp.Ptr("H1"), Window: mcp.Ptr(int64(8)), Episode: mcp.Ptr("window"), EpisodeBars: mcp.Ptr(int64(12))})))
env := made["env"].(string)
start := obj(must(b.Strategy.EnvReset(ctx, mcp.StrategyEnvResetArgs{Env: env, Seed: mcp.Ptr(int64(5))})))
check(len(obj(start["obs"])["market"].([]any)) == 8, "the observation is not shaped as declared")
info := obj(start["info"])
rewards := []float64{}
hashes := []string{info["ledger_hash"].(string)}
for _, a := range []int{1, 1, 2, 0, 2, 2, 1, 0, 1, 2, 0, 1} {
s := obj(must(b.Strategy.EnvStep(ctx, mcp.StrategyEnvStepArgs{Env: env, Action: a})))
rewards = append(rewards, s["reward"].(float64))
hashes = append(hashes, obj(s["info"])["ledger_hash"].(string))
if s["terminated"].(bool) || s["truncated"].(bool) {
break
}
}
must(b.Strategy.EnvClose(ctx, mcp.StrategyEnvCloseArgs{Env: env}))
// A tool's own refusal (isError), not a request the wire refused.
_, err = b.Strategy.EnvStep(ctx, mcp.StrategyEnvStepArgs{Env: env, Action: 0})
var refusal *mcp.ToolError
check(errors.As(err, &refusal) && strings.Contains(refusal.Msg, "No environment"), fmt.Sprint("a closed environment's step was not refused in its words: ", err))
distinct := map[string]bool{}
for _, h := range hashes {
distinct[h] = true
}
check(len(distinct) > 1, "the episode traded nothing")
// encoding/json writes a float64 in the shortest digits that read back as the same double.
out, _ := json.Marshal(map[string]any{"start_ms": int64(info["start_ms"].(float64)), "rewards": rewards, "hashes": hashes})
fmt.Println(string(out))
fmt.Println("PASS")
}