E2B Cookbook

repository·main·Indexed 23 days ago

https://github.com/e2b-dev/e2b-cookbook

A collection of example code and guides for building agentic workflows and code interpreters using the E2B SDK. It includes practical implementations for various LLM providers and AI frameworks, featuring examples such as the AgentKit Coding Agent, running Anthropic Claude Code in E2B sandboxes (JavaScript and Python), and implementing Anthropic Managed Agents with orchestrator and webhook worker patterns.

Tokens
63.2K
Snippets
162
Records
248
Agent score
79%

What's inside e2b-cookbook

  1. Run Sandbox Agent in E2B Sandbox (JavaScript)

    main

    The Sandbox Agent SDK allows you to run coding agents (like Claude Code, Codex, or Cursor) inside E2B sandboxes and manage them via HTTP from your backend. This provides a unified interface for multiple agents and a standardized event schema for normalized session data.

    To use this implementation, you must provide an E2B_API_KEY and at least one LLM provider key (e.g., OPENAI_API_KEY or ANTHROPIC_API_KEY).

  2. Run Anthropic Managed Agents in self-hosted E2B sandboxes

    main

    You can run Anthropic Managed Agents in self-hosted environments using E2B sandboxes. This setup provides a queue of work that your own worker handles. You can implement this using two primary patterns:

    1. Direct Polling: A worker stays connected and continuously polls the Anthropic environment queue for work.
    2. Webhooks: Use webhooks to wake your infrastructure when a session starts running.

    In a self-hosted setup, you must implement a handler that starts or finds the correct sandbox, runs the EnvironmentWorker, and maps files, logs, outputs, and cleanup to your specific use case.

  3. Browserbase MCP Example Overview

    main

    The Browserbase MCP Example demonstrates how to integrate the Browserbase Model Context Protocol (MCP) server with E2B sandboxes. This setup allows an AI agent (specifically using OpenAI Agents) to perform web automation tasks within a secure sandbox environment.

    Key Capabilities:

    • Connect to a Browserbase MCP server through an E2B sandbox.
    • Automate web tasks using OpenAI Agents.
    • Capture screenshots of web pages.
    • Stream results in real-time.
  4. Run Anthropic Managed Agents with E2B Workers (JavaScript)

    main

    This project provides a way to run Anthropic Managed Agents self-hosted environment workers within E2B sandboxes using TypeScript. The implementation uses an E2B template that includes Node.js, the Anthropic SDK, tsx, shell tools, and a writable /mnt/session working directory.

    There are three primary deployment patterns available:

    1. Orchestrator (orchestrator/): Your application or CLI starts and manages a long-running E2B worker sandbox that polls the environment queue.
    2. Webhooks (webhooks/): Anthropic webhooks wake an auto-resumable E2B sandbox, which then starts the worker on demand.
    3. App Webhooks (app-webhooks/): Your application receives Anthropic webhooks and then starts or reconnects the E2B worker sandbox.

    The worker sandbox has access to tools like bash, read, write, edit, glob, and grep, operating within the /mnt/session directory.

  5. Understand State Scope and Persistence

    main

    The persistence of the /mnt/session directory depends on the deployment pattern used:

    • Orchestrator: One worker sandbox polls the environment queue. /mnt/session persists for that specific sandbox and can be shared by any session the worker claims.
    • Webhooks: One auto-resumable webhook router starts or reconnects worker sandboxes. Using the default session routing scope provides each session with its own persistent /mnt/session.
    • App Webhooks: The app routes work to a sandbox based on APP_SANDBOX_ROUTING_SCOPE. The default session scope provides each session with its own persistent /mnt/session.
  6. How the MCP Research Agent works

    main

    The MCP Research Agent is an OpenAI-powered agent designed for research tasks using Model Context Protocol (MCP) servers.

    Workflow:

    1. Sandbox Creation: It initializes an E2B sandbox equipped with arXiv and DuckDuckGo MCP servers.
    2. Agent Integration: It configures an OpenAI agent with MCP integration, allowing the agent to call tools provided by the MCP servers.
    3. Task Execution: The agent performs research (e.g., finding LLM papers and author information) by interacting with the MCP tools.
    4. Output: Results are streamed directly to the console.
  7. How Claude Code CLI with MCP works

    main

    This integration automates a research workflow by combining the Claude Code CLI with E2B sandboxes and MCP servers. The workflow follows these steps:

    1. Sandbox Provisioning: An E2B sandbox is created containing specific MCP servers, such as arXiv and DuckDuckGo.
    2. CLI Integration: The Claude Code CLI is configured to interact with these MCP servers.
    3. Task Execution: The system runs a research task (e.g., finding LLM papers and author information).
    4. Result Hosting: The final research results are compiled into a web page and hosted within the sandbox environment.
  8. How the Code Interpreter implementation works

    main

    The code interpreter in this example is implemented using the evaluateCode method (found in app/api/chat/codeInterpreter.ts).

    Key Behaviors:

    • Session Management: The evaluateCode method accepts code (Python) and a sessionID. It uses the sessionID to either reconnect to an existing sandbox or create a new one if none exists.
    • Stateful Execution: Code execution is stateful because it uses Jupyter Notebook underneath. This allows for per-session persistence where you can refer to variables defined in previous executions or use functions defined earlier in the same session.
    • Lifecycle & Reuse: After code execution, the method disconnects from the sandbox and calls .keelAlive to ensure the sandbox remains available for reuse for a specified duration.
  9. Understand the Managed Agents worker and session lifecycle

    main

    The Managed Agents architecture involves several moving parts:

    1. Orchestrator Flow: The App starts an E2B worker, updates Anthropic environment metadata with the worker's sandbox ID, and then creates a session. The worker polls the environment work queue, executes tools (like bash, read, or write) in /mnt/session, and sends user.tool_result events back to Anthropic.
    2. Webhook Flow: Anthropic sends a POST request to an E2B auto-resume sandbox. The sandbox resumes, the webhook handler verifies the signature, and the worker is started to service the session work.

    Key Data Formats:

    • Anthropic Streams: Anthropic sends agent.tool_use events. The E2B worker executes these and responds with user.tool_result events.
    • Session Status: Successful runs involve transitions between session.status_running and session.status_idle (with stop reasons like requires_action or end_turn).