Skip to main content

Crate ghostmode

Crate ghostmode 

Source
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, adds backend::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-pool adds backend::MontyPool, which runs each session in its own Monty worker process. Untrusted scripts belong there.
  • rmcp adds mcp::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.
mcprmcp
A host over a connected MCP client, so a script can call the MCP server’s tools.
schema
The two functions the model calls, execute and poll, 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 by Builder::new, which names the backend. The built sandbox keeps its builder and runs with its host, backend, waits, limits and signing key. Its Debug output never shows the signing key.
Limits
What a script may use before the sandbox stops it with a failed status. A stopped script leaves the session’s globals as it had set them, unless the backend lost them, which the next status reports as restarted.
Sandbox
One Python session that runs the model’s scripts in backend B, and the host whose tools they call. Globals persist from one execute to the next, and one run is in flight at a time.

Enums§

Error
Why the caller’s Sandbox::dump or Builder::restore failed.
Status
What execute or poll answers, from the sandbox to the caller. It serializes as the JSON the caller sends to the model, such as {"status": "running", "run_id": 1}.