MCP
Expose Sandbox SDK tools over the Model Context Protocol with @opencoredev/sandbox-sdk/mcp or the sandbox-sdk mcp CLI command.
@opencoredev/sandbox-sdk/mcp turns any sandbox handle into a Model Context Protocol (MCP) server with zero runtime or peer dependencies. Clients such as Claude Code, Cursor, and Codex can run commands, edit files, and manage background processes inside an isolated sandbox with the same secret redaction and path guards as sandboxTools.
Quick start with the CLI
Run the built-in stdio MCP server directly from the sandbox-sdk CLI. Stdout carries JSON-RPC frames only; diagnostics go to stderr.
SANDBOX_PROVIDER=e2b E2B_API_KEY=... npx @opencoredev/sandbox-sdk mcp
Pass --key <name> or --id <sandbox-id> to reuse a persistent sandbox instead of creating an ephemeral one. Pass --secret-env NAME (repeatable) to inject secrets from your host environment without exposing them in command-line arguments:
GITHUB_TOKEN=... npx @opencoredev/sandbox-sdk mcp \
--provider local \
--root /workspace/app \
--allow run,read_file,write_file,edit_file,search,list_files \
--secret-env GITHUB_TOKEN
Programmatic MCP server
Use createSandboxMcpServer and serveMcpStdio when your own application creates or manages the sandbox handle.
import { createSandbox } from "@opencoredev/sandbox-sdk";
import { local } from "@opencoredev/sandbox-sdk/local";
import { createSandboxMcpServer, serveMcpStdio } from "@opencoredev/sandbox-sdk/mcp";
await using box = await createSandbox(local(), {
secrets: { API_KEY: process.env.API_KEY ?? "example-secret" },
});
const server = createSandboxMcpServer(box, {
root: "/workspace/app",
allow: ["run", "read_file", "write_file", "edit_file", "search", "list_files"],
// Audit hook only (not a security boundary).
beforeRun: (command) => (console.error(`[mcp] ${command}`), true),
});
await serveMcpStdio(server);
This creates a local sandbox, binds the selected tools with redaction enabled, and serves newline-delimited JSON-RPC over stdin and stdout until the client closes stdin.
Handle messages directly
server.handle(message, context?) processes a parsed JSON-RPC 2.0 object (or a batch array on protocol 2025-03-26) without stdio, which makes it easy to embed over HTTP or unit-test with memory():
import { createSandbox } from "@opencoredev/sandbox-sdk";
import { memory } from "@opencoredev/sandbox-sdk/memory";
import { createSandboxMcpServer } from "@opencoredev/sandbox-sdk/mcp";
await using box = await createSandbox(memory());
const server = createSandboxMcpServer(box);
await server.handle({
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {
protocolVersion: "2025-06-18",
capabilities: {},
clientInfo: { name: "my-client", version: "1.0.0" },
},
});
const response = await server.handle({
jsonrpc: "2.0",
id: 2,
method: "tools/list",
});
console.log(response);
Protocol and transport guarantees
- Zero peers:
@opencoredev/sandbox-sdk/mcpdoes not requirezodor@modelcontextprotocol/sdk. - Lifecycle and protocol versions: requires a single
initializehandshake before servingtools/listortools/call(pingis allowed beforehand, and duplicateinitializecalls return-32600so the negotiated version stays fixed for the session). Negotiates2025-06-18(default),2025-03-26, and2024-11-05. Per the MCP specification, JSON-RPC batch arrays are accepted only on2025-03-26and rejected with-32600on2025-06-18and2024-11-05. - Typed
isError: tool failures setisError: truefrom the structured tool outcome rather than inspecting formatted text. Protocol errors for unknown methods or tools return fixed diagnostic messages without echoing untrusted names. - Bounded concurrency and stdio: both
server.handleandserveMcpStdioenforcemaxInFlight(default 16) andmaxQueued(default 256) per individual request (including2025-03-26batch members), returning-32000Server busy on overflow.serveMcpStdioadditionally enforcesmaxMessageBytes(default 1 MiB, exact UTF-8 byte accounting across chunk and surrogate-pair boundaries), applies output backpressure, handles per-requestnotifications/cancelledeven when the queue is full, and stops promptly whensignalaborts.
Security model
createSandboxMcpServerfails closed withSandboxError(invalid_argument) if passed an object not created bycreateSandboxorconnectSandbox, so secret redaction can never be bypassed.- Registered secrets are redacted from command output, file reads, logs, and errors.
rootscopes file tools andcwdwith symlink checks, but shell commands (run,start_process) keep the sandbox’s own privileges. Rely on sandbox isolation andnetworkpolicy as the security boundary. See Security.
Next: CLI reference or Agent tools.