Expand description
Code Mode for Rust agents. The model writes a script, ghostmode runs it in a sandbox, and
the host provides the tools the script calls. The model calls only execute and poll.
Write a host, build a Sandbox, and give the model the execute and poll definitions.
Pass each execute call’s code to Sandbox::execute and send the Status JSON back:
use futures_util::future::{BoxFuture, FutureExt};
use ghostmode::host::{self, Call, Host, Tool};
use ghostmode::backend::Monty;
use ghostmode::{Builder, schema};
use serde_json::{Value, json};
struct Echo;
impl Host for Echo {
fn tools(&self) -> Vec<Tool> {
let input_schema = json!({
"type": "object",
"properties": { "text": { "type": "string" } },
"required": ["text"],
});
vec![Tool::new("echo", "Returns the text you pass it.", input_schema)]
}
fn call(&self, call: &Call) -> BoxFuture<'static, Result<Value, host::Error>> {
let text = call.args.get("text").cloned().ok_or_else(|| host::Error::Failed("text is required".into()));
async move { text }.boxed()
}
}
let mut sandbox = Builder::new(Echo, Monty::default()).build().await;
let definitions = schema::mcp_definitions(sandbox.tools());
let status = sandbox.execute("echo(text='hello')", false).await;
let reply = serde_json::to_value(&status).unwrap();A sandbox runs scripts in a backend::Backend. Each feature adds a backend or a host:
monty, on by default, addsbackend::Monty, which runs scripts inside the caller’s process. It isn’t crash-isolated: a script can panic, abort or hang the caller’s process.monty-pooladdsbackend::MontyPool, which runs each session in its own Monty worker process. Untrusted scripts belong there.rmcpaddsmcp::Server, a host that offers a connected MCP server’s tools to scripts.
Modules§
- backend
- The Python interpreters a sandbox can run its scripts in.
- host
- What the host provides: the tools a script calls, and the answers to those calls.
- mcp
rmcp - A host over a connected MCP client, so a script can call the MCP server’s tools.
- schema
- The two functions the model calls,
executeandpoll, with their JSON Schemas, in MCP’s and OpenAI’s shapes. The caller hands one of the two lists to its model.
Structs§
- Builder
- Sets up a
Sandbox. Made byBuilder::new, which names the backend. The built sandbox keeps its builder and runs with its host, backend, waits, limits and signing key. ItsDebugoutput never shows the signing key. - Limits
- What a script may use before the sandbox stops it with a
failedstatus. A stopped script leaves the session’s globals as it had set them, unless the backend lost them, which the next status reports asrestarted. - Sandbox
- One Python session that runs the model’s scripts in backend
B, and the host whose tools they call. Globals persist from oneexecuteto the next, and one run is in flight at a time.
Enums§
- Error
- Why the caller’s
Sandbox::dumporBuilder::restorefailed. - Status
- What
executeorpollanswers, from the sandbox to the caller. It serializes as the JSON the caller sends to the model, such as{"status": "running", "run_id": 1}.