Skip to content
Sandbox SDK
Esc
↑↓navigate↵open⌘Jpreview
On this page

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/mcp does not require zod or @modelcontextprotocol/sdk.
  • Lifecycle and protocol versions: requires a single initialize handshake before serving tools/list or tools/call (ping is allowed beforehand, and duplicate initialize calls return -32600 so the negotiated version stays fixed for the session). Negotiates 2025-06-18 (default), 2025-03-26, and 2024-11-05. Per the MCP specification, JSON-RPC batch arrays are accepted only on 2025-03-26 and rejected with -32600 on 2025-06-18 and 2024-11-05.
  • Typed isError: tool failures set isError: true from 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.handle and serveMcpStdio enforce maxInFlight (default 16) and maxQueued (default 256) per individual request (including 2025-03-26 batch members), returning -32000 Server busy on overflow. serveMcpStdio additionally enforces maxMessageBytes (default 1 MiB, exact UTF-8 byte accounting across chunk and surrogate-pair boundaries), applies output backpressure, handles per-request notifications/cancelled even when the queue is full, and stops promptly when signal aborts.

Security model

  • createSandboxMcpServer fails closed with SandboxError (invalid_argument) if passed an object not created by createSandbox or connectSandbox, so secret redaction can never be bypassed.
  • Registered secrets are redacted from command output, file reads, logs, and errors.
  • root scopes file tools and cwd with symlink checks, but shell commands (run, start_process) keep the sandbox’s own privileges. Rely on sandbox isolation and network policy as the security boundary. See Security.

Next: CLI reference or Agent tools.

Was this page helpful?