import { Sandbox } from 'e2b'
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
// Check the keys before the sandbox exists
for (const name of ['ANTHROPIC_API_KEY', 'BROWSERBASE_API_KEY', 'GEMINI_API_KEY', 'BROWSERBASE_PROJECT_ID', 'EXA_API_KEY', 'AIRTABLE_API_KEY']) {
if (!process.env[name]) throw new Error(`Set ${name}`);
}
const sbx = await Sandbox.create({
envs: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY! },
mcp: {
browserbase: {
apiKey: process.env.BROWSERBASE_API_KEY!,
geminiApiKey: process.env.GEMINI_API_KEY!,
projectId: process.env.BROWSERBASE_PROJECT_ID!,
},
exa: {
apiKey: process.env.EXA_API_KEY!,
},
airtable: {
airtableApiKey: process.env.AIRTABLE_API_KEY!,
nodeenv: 'production',
},
},
});
try {
const mcpUrl = sbx.getMcpUrl();
const mcpToken = await sbx.getMcpToken();
// The gateway starts its servers after the sandbox is created. Listing the tools once
// waits until they are up: a client that connects earlier, like claude below, gets no tools.
const client = new Client({ name: 'e2b-mcp-client', version: '1.0.0' });
await client.connect(new StreamableHTTPClientTransport(new URL(mcpUrl), {
requestInit: { headers: { Authorization: `Bearer ${mcpToken}` } },
}));
const { tools } = await client.listTools();
console.log(`The gateway is ready with ${tools.length} tools`);
await client.close();
// You can now connect the gateway to any MCP client, for example claude:
// This also works for your local claude!
// Commands in this template run as root by default, and claude refuses
// --dangerously-skip-permissions as root, so both commands run as `user`.
await sbx.commands.run(`claude mcp add --transport http e2b-mcp-gateway ${mcpUrl} --header "Authorization: Bearer ${mcpToken}"`, { user: 'user', timeoutMs: 0, onStdout: console.log, onStderr: console.log });
await sbx.commands.run(
`echo 'Use browserbase and exa to research open positions at e2b.dev. Collect your findings in Airtable.' | claude -p --dangerously-skip-permissions`,
{ user: 'user', timeoutMs: 0, onStdout: console.log, onStderr: console.log }
)
} finally {
await sbx.kill();
}