Bentrix Docs
HomePricingDashboard

1. Connect and look around

Your first program — connect to Bentrix, list your laboratories and workbenches, and find the workbench you have selected.

Before a program can do anything, it connects. Then the first thing it usually wants is to know what is there: which laboratories you have, which workbenches are in them, and which workbench you have selected in the window.

01Before you start

  1. Open Bentrix on this computer.
  2. Install the SDK for your language (its page: Python, JavaScript, C#, Go, Java, C++, Julia, R, Rust).
  3. Make a file and paste the program at the end of this page into it.

02Step 1 — connect

connect finds the Bentrix running on this computer and pairs with it under the name you give, here my-agent. The first time, the program appears in ☰ › Settings › Agents under that name, with the scopes read, lab and paper. Every later run reuses the pairing.

If Bentrix is not open, connect stops with Bentrix is not running: open it and run again.

03Step 2 — read the workspace

get_state answers what the left rail shows: your laboratories, the workbenches in each, your real accounts, and which workbench is selected. depth says how much: "benches" includes every workbench, "charts" also every chart window.

Python

state = client.workspace.get_state(depth="benches")

JSJavaScript

const state = await client.workspace.getState({ depth: "benches" });

C#C#

var state = await client.Workspace.GetState(depth: "benches");

GOGo

state := must(client.Workspace.GetState(ctx, mcp.WorkspaceGetStateArgs{Depth: mcp.Ptr[string]("benches")}))

Java

Object state = client.workspace.getState(new Bentrix.WorkspaceApi.GetStateArgs().depth("benches"));

C++C++

auto state = client.workspace.get_state(WorkspaceApi::GetStateArgs().depth("benches"));

Julia

state = client.workspace.get_state(; depth = "benches")

RR

state <- client$workspace$get_state(depth = "benches")

RRust

let state = client.workspace().get_state(mcp::WorkspaceGetStateArgs::new().depth("benches"))?;

The answer is an object. Its laboratories is a list; each laboratory has a name, a uri (its address, such as bentrix://lab/lab-1) and its benches; each workbench has its own uri, its markets, its cursor_ms (the moment its clock is at, in UTC milliseconds) and its engine (ready once it can trade).

04Step 3 — go through it

Python

for lab in state["laboratories"]:
    print("laboratory:", lab["name"])
    for wb in lab["benches"]:
        print("  workbench:", wb["uri"])
        print("    markets:", wb["markets"])

JSJavaScript

for (const lab of state.laboratories) {
  console.log("laboratory:", lab.name);
  for (const wb of lab.benches) {
    console.log("  workbench:", wb.uri);
    console.log("    markets:", wb.markets);
  }
}

C#C#

foreach (var lab in state!["laboratories"]!.AsArray())
{
    Console.WriteLine($"laboratory: {lab!["name"]!}");
    foreach (var wb in lab!["benches"]!.AsArray())
    {
        Console.WriteLine($"  workbench: {wb!["uri"]!}");
        Console.WriteLine($"    markets: {wb!["markets"]!}");
    }
}

GOGo

for _, lab := range at(state, "laboratories").([]any) {
	fmt.Println("laboratory:", at(lab, "name"))
	for _, wb := range at(lab, "benches").([]any) {
		fmt.Println("  workbench:", at(wb, "uri"))
		fmt.Println("    markets:", at(wb, "markets"))
	}
}

Java

for (Object lab : (List<Object>) at(state, "laboratories")) {
    System.out.println("laboratory:" + " " + at(lab, "name"));
    for (Object wb : (List<Object>) at(lab, "benches")) {
        System.out.println("  workbench:" + " " + at(wb, "uri"));
        System.out.println("    markets:" + " " + at(wb, "markets"));
    }
}

C++C++

for (const Json &lab : state["laboratories"].arr()) {
    std::cout << "laboratory:" << " " << lab["name"].dump() << std::endl;
    for (const Json &wb : lab["benches"].arr()) {
        std::cout << "  workbench:" << " " << wb["uri"].dump() << std::endl;
        std::cout << "    markets:" << " " << wb["markets"].dump() << std::endl;
    }
}

Julia

for lab in state["laboratories"]
    println("laboratory:", " ", lab["name"])
    for wb in lab["benches"]
        println("  workbench:", " ", wb["uri"])
        println("    markets:", " ", wb["markets"])
    end
end

RR

for (lab in state$laboratories) {
  cat("laboratory:", format(lab$name), "\n")
  for (wb in lab$benches) {
    cat("  workbench:", format(wb$uri), "\n")
    cat("    markets:", format(wb$markets), "\n")
  }
}

RRust

for lab in state["laboratories"].as_array().into_iter().flatten() {
    println!("laboratory: {}", lab["name"]);
    for wb in lab["benches"].as_array().into_iter().flatten() {
        println!("  workbench: {}", wb["uri"]);
        println!("    markets: {}", wb["markets"]);
    }
}

05Step 4 — the selected workbench

selected is the address of the workbench selected in the window, or nothing when none is. Most guides start here: they work on the workbench you have chosen.

Python

bench = state["selected"]
print("selected:", bench)

JSJavaScript

const bench = state.selected;
console.log("selected:", bench);

C#C#

var bench = state!["selected"]?.GetValue<string>() ?? "";
Console.WriteLine($"selected: {bench}");

GOGo

bench := text(at(state, "selected"))
fmt.Println("selected:", bench)

Java

String bench = (String) at(state, "selected");
System.out.println("selected:" + " " + bench);

C++C++

auto bench = (state["selected"].is_string() ? state["selected"].str() : std::string());
std::cout << "selected:" << " " << bench << std::endl;

Julia

bench = state["selected"]
println("selected:", " ", bench)

RR

bench <- state$selected
cat("selected:", format(bench), "\n")

RRust

let bench = state["selected"].as_str().unwrap_or_default().to_string();
println!("selected: {}", bench);
i

An address such as bentrix://lab/lab-1/bench/bench-2 is how every tool names a workbench, a chart or a laboratory. Keep it as text and pass it on.

06The whole program

Python

import bentrix

client = bentrix.connect(name="my-agent")

state = client.workspace.get_state(depth="benches")
for lab in state["laboratories"]:
    print("laboratory:", lab["name"])
    for wb in lab["benches"]:
        print("  workbench:", wb["uri"])
        print("    markets:", wb["markets"])
bench = state["selected"]
print("selected:", bench)

JSJavaScript

const { connect } = require('bentrix/mcp');

(async () => {
  const client = await connect({ name: 'my-agent' });

  const state = await client.workspace.getState({ depth: "benches" });
  for (const lab of state.laboratories) {
    console.log("laboratory:", lab.name);
    for (const wb of lab.benches) {
      console.log("  workbench:", wb.uri);
      console.log("    markets:", wb.markets);
    }
  }
  const bench = state.selected;
  console.log("selected:", bench);
})();

C#C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bentrix.Mcp;

using var client = await BentrixClient.ConnectAsync("my-agent");

var state = await client.Workspace.GetState(depth: "benches");
foreach (var lab in state!["laboratories"]!.AsArray())
{
    Console.WriteLine($"laboratory: {lab!["name"]!}");
    foreach (var wb in lab!["benches"]!.AsArray())
    {
        Console.WriteLine($"  workbench: {wb!["uri"]!}");
        Console.WriteLine($"    markets: {wb!["markets"]!}");
    }
}
var bench = state!["selected"]?.GetValue<string>() ?? "";
Console.WriteLine($"selected: {bench}");

GOGo

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)
	}

	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 _, wb := range at(lab, "benches").([]any) {
			fmt.Println("  workbench:", at(wb, "uri"))
			fmt.Println("    markets:", at(wb, "markets"))
		}
	}
	bench := text(at(state, "selected"))
	fmt.Println("selected:", bench)
	_ = []any{state, bench}
}

Java

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")) {

            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 wb : (List<Object>) at(lab, "benches")) {
                    System.out.println("  workbench:" + " " + at(wb, "uri"));
                    System.out.println("    markets:" + " " + at(wb, "markets"));
                }
            }
            String bench = (String) at(state, "selected");
            System.out.println("selected:" + " " + bench);
        }
    }
}

C++C++

#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");

    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 &wb : lab["benches"].arr()) {
            std::cout << "  workbench:" << " " << wb["uri"].dump() << std::endl;
            std::cout << "    markets:" << " " << wb["markets"].dump() << std::endl;
        }
    }
    auto bench = (state["selected"].is_string() ? state["selected"].str() : std::string());
    std::cout << "selected:" << " " << bench << std::endl;
}

Julia

using BentrixMCP

client = BentrixMCP.connect("my-agent")

state = client.workspace.get_state(; depth = "benches")
for lab in state["laboratories"]
    println("laboratory:", " ", lab["name"])
    for wb in lab["benches"]
        println("  workbench:", " ", wb["uri"])
        println("    markets:", " ", wb["markets"])
    end
end
bench = state["selected"]
println("selected:", " ", bench)

RR

library(bentrixmcp)

client <- connect("my-agent")

state <- client$workspace$get_state(depth = "benches")
for (lab in state$laboratories) {
  cat("laboratory:", format(lab$name), "\n")
  for (wb in lab$benches) {
    cat("  workbench:", format(wb$uri), "\n")
    cat("    markets:", format(wb$markets), "\n")
  }
}
bench <- state$selected
cat("selected:", format(bench), "\n")

RRust

use bentrix as mcp;

fn main() -> Result<(), mcp::Error> {
    let client = mcp::connect(Some("my-agent"))?;

    let state = client.workspace().get_state(mcp::WorkspaceGetStateArgs::new().depth("benches"))?;
    for lab in state["laboratories"].as_array().into_iter().flatten() {
        println!("laboratory: {}", lab["name"]);
        for wb in lab["benches"].as_array().into_iter().flatten() {
            println!("  workbench: {}", wb["uri"]);
            println!("    markets: {}", wb["markets"]);
        }
    }
    let bench = state["selected"].as_str().unwrap_or_default().to_string();
    println!("selected: {}", bench);
    Ok(())
}

07Run it

Save it, run it (each language's page shows the command), and it prints your laboratories and workbenches. If you have none yet, the next guide makes one.

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