Skip to content

Integrations

Claude Agent SDK

Plug in the hosted MCP server for every sandbox tool with no code, or register your own in-process tool.

Install

Terminal
npm i sandboxplease @anthropic-ai/claude-agent-sdk zod

Code

Copy it as claude-agent-sdk.ts. The model is the only part you would change.

claude-agent-sdk.ts
// Claude Agent SDK: connect the hosted SandboxPlease MCP server, or register your own in-process tool.
// Run: SANDBOXPLEASE_API_KEY=... ANTHROPIC_API_KEY=... npx tsx claude-agent-sdk.ts
import { createSdkMcpServer, type McpServerConfig, query, tool } from "@anthropic-ai/claude-agent-sdk";
import { type Sandbox, SandboxPlease } from "sandboxplease";
import { z } from "zod";

// Option 1: the hosted MCP server, every SandboxPlease tool with no code.
export const hostedMcp = (): McpServerConfig => ({
  type: "http",
  url: `${process.env.SANDBOXPLEASE_BASE_URL ?? "https://api.sandboxplease.com"}/mcp`,
  headers: { Authorization: `Bearer ${process.env.SANDBOXPLEASE_API_KEY}` },
});

// Option 2: your own tool, when you want one sandbox per agent run under your control.
export function sandboxServer(sp = new SandboxPlease()) {
  let sandbox: Promise<Sandbox> | undefined;
  const runCommand = tool(
    "run_command",
    "Run a shell command in a Linux sandbox with Python 3 and Node. Returns exit code, stdout and stderr.",
    { command: z.string().describe("Shell command, e.g. python3 -c 'print(1+1)'") },
    async ({ command }) => {
      sandbox ??= sp.sandboxes.create({ timeout: 600 });
      const { exitCode, stdout, stderr } = await (await sandbox).exec(command);
      return { content: [{ type: "text", text: JSON.stringify({ exitCode, stdout, stderr }) }] };
    },
  );
  return {
    server: createSdkMcpServer({ name: "sandbox", tools: [runCommand] }),
    runCommand,
    close: async () => (await sandbox)?.kill(),
  };
}

if (import.meta.url === `file://${process.argv[1]}`) {
  const prompt = "Compute 6*7 with Python in a SandboxPlease sandbox, then kill it.";
  for await (const message of query({
    prompt,
    options: { mcpServers: { sandboxplease: hostedMcp() }, allowedTools: ["mcp__sandboxplease__*"] },
  })) {
    if (message.type === "result" && message.subtype === "success") console.log(message.result);
  }
}

Run it

Terminal
export SANDBOXPLEASE_API_KEY=sp-v1-…
export ANTHROPIC_API_KEY=…
npx tsx claude-agent-sdk.ts

The agent runs Python in the sandbox and answers 42. Each sandbox shows up in Logs with its provider and cost.