Skip to main content

ghostmode/
lib.rs

1//! Code Mode for Rust agents. The model writes a script, ghostmode runs it in a sandbox, and
2//! the host provides the tools the script calls. The model calls only `execute` and `poll`.
3//!
4//! Write a host, build a [`Sandbox`], and give the model the `execute` and `poll` definitions.
5//! Pass each `execute` call's code to [`Sandbox::execute`] and send the [`Status`] JSON back:
6//!
7//! ```no_run
8//! use futures_util::future::{BoxFuture, FutureExt};
9//! use ghostmode::host::{self, Call, Host, Tool};
10//! # #[cfg(feature = "monty")]
11//! use ghostmode::backend::Monty;
12//! use ghostmode::{Builder, schema};
13//! use serde_json::{Value, json};
14//!
15//! struct Echo;
16//!
17//! impl Host for Echo {
18//!     fn tools(&self) -> Vec<Tool> {
19//!         let input_schema = json!({
20//!             "type": "object",
21//!             "properties": { "text": { "type": "string" } },
22//!             "required": ["text"],
23//!         });
24//!         vec![Tool::new("echo", "Returns the text you pass it.", input_schema)]
25//!     }
26//!
27//!     fn call(&self, call: &Call) -> BoxFuture<'static, Result<Value, host::Error>> {
28//!         let text = call.args.get("text").cloned().ok_or_else(|| host::Error::Failed("text is required".into()));
29//!         async move { text }.boxed()
30//!     }
31//! }
32//!
33//! # #[cfg(feature = "monty")]
34//! # async fn execute_echo() {
35//! let mut sandbox = Builder::new(Echo, Monty::default()).build().await;
36//! let definitions = schema::mcp_definitions(sandbox.tools());
37//! let status = sandbox.execute("echo(text='hello')", false).await;
38//! let reply = serde_json::to_value(&status).unwrap();
39//! # }
40//! ```
41//!
42//! A sandbox runs scripts in a [`backend::Backend`]. Each feature adds a backend or a host:
43//!
44//! - `monty`, on by default, adds `backend::Monty`, which runs scripts inside the caller's
45//!   process. It isn't crash-isolated: a script can panic, abort or hang the caller's process.
46//! - `monty-pool` adds `backend::MontyPool`, which runs each session in its own Monty worker
47//!   process. Untrusted scripts belong there.
48//! - `rmcp` adds `mcp::Server`, a host that offers a connected MCP server's tools to scripts.
49
50#![cfg_attr(docsrs, feature(doc_cfg))]
51
52pub mod backend;
53pub mod host;
54#[cfg(feature = "rmcp")]
55pub mod mcp;
56mod sandbox;
57pub mod schema;
58
59pub use sandbox::{Builder, Error, Limits, Sandbox, Status};
60
61#[cfg(all(doctest, feature = "monty"))]
62#[doc = include_str!("../docs/extending.md")]
63struct ExtendingGuideDoctests;