Skip to main content

E2B MCP Server

Project description

E2B MCP Server (Python)

A production-grade Model Context Protocol server for executing code in secure, isolated E2B sandboxes.

Python 3.10+ License E2B

โœจ Features

  • ๐Ÿš€ Production-Ready: Comprehensive error handling, logging, and resource management
  • ๐Ÿ”’ Secure Isolation: Each sandbox runs in a separate, secure E2B environment
  • ๐ŸŽฏ Type-Safe: Full type annotations and Pydantic validation
  • ๐Ÿ“Š Observable: Detailed logging and sandbox statistics
  • ๐Ÿงน Auto Cleanup: Automatic resource cleanup and graceful shutdown
  • ๐Ÿ“ฆ Modular Design: Clean, maintainable code structure
  • โšก Easy to Use: Simple API with comprehensive documentation

๐Ÿ› ๏ธ Available Tools

The server provides 9 powerful tools for sandbox interaction:

Tool Description
create_sandbox Create a new E2B sandbox with configurable timeout
run_code Execute Python code using Jupyter notebook syntax
run_command Run shell commands in the sandbox
read_file Read file contents from the sandbox
write_file Write content to files in the sandbox
list_files List files in a sandbox directory
get_sandbox_url Get URL for accessing sandbox on specific port
get_file_download_url Get download URL for a file in the sandbox
kill_sandbox Terminate and cleanup a sandbox

๐Ÿ“‹ Requirements

๐Ÿš€ Quick Start

Installation

# Install with uv (recommended)
uv install

# Or with pip
pip install -e .

Configuration

  1. Get your E2B API key from e2b.dev
  2. Set up environment variables:
# Copy example config
cp .env.example .env

# Edit .env and add your API key
E2B_API_KEY=your_api_key_here

Running the Server

Standalone

# Run directly
python -m e2b_mcp_server

# Or programmatically
python -c "from e2b_mcp_server import main; import asyncio; asyncio.run(main())"

With Claude Desktop

Add to your Claude Desktop config:

MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "e2b-mcp-server": {
      "command": "uvx",
      "args": ["e2b-mcp-server"],
      "env": {
        "E2B_API_KEY": "your_api_key_here"
      }
    }
  }
}

โš™๏ธ Configuration

Environment variables (see .env.example):

Variable Default Description
E2B_API_KEY (required) Your E2B API key
LOG_LEVEL INFO Logging level (DEBUG, INFO, WARNING, ERROR)
MAX_ACTIVE_SANDBOXES 10 Maximum concurrent sandboxes

Advanced Configuration

The server uses these constants (can be modified in constants.py):

  • DEFAULT_SANDBOX_TIMEOUT_MS: 300000 (5 minutes)
  • MAX_SANDBOX_TIMEOUT_MS: 3600000 (1 hour)

๐Ÿ—๏ธ Architecture

Module Structure

e2b_mcp_server/
โ”œโ”€โ”€ constants.py      # Configuration constants
โ”œโ”€โ”€ exceptions.py     # Custom exception classes
โ”œโ”€โ”€ schemas.py        # Pydantic validation schemas
โ”œโ”€โ”€ utils.py          # Utility functions
โ”œโ”€โ”€ manager.py        # Sandbox lifecycle management
โ”œโ”€โ”€ handlers.py       # Tool handler implementations
โ”œโ”€โ”€ server.py         # MCP server setup and routing
โ””โ”€โ”€ __init__.py       # Package exports

Design Principles

  • Separation of Concerns: Each module has a single, clear responsibility
  • Type Safety: Full type hints and Pydantic validation
  • Error Handling: Comprehensive exception hierarchy
  • Resource Management: Automatic cleanup and lifecycle tracking
  • Testability: Modular design enables easy unit testing

Key Components

SandboxManager (manager.py)

Manages E2B sandbox lifecycle:

  • Creates and tracks sandboxes
  • Enforces resource limits
  • Handles cleanup on shutdown

Tool Handlers (handlers.py)

Implements business logic for each tool:

  • Validates inputs using schemas
  • Interacts with sandboxes
  • Returns standardized responses

Schemas (schemas.py)

Pydantic models for validation:

  • Type checking
  • Field validation
  • Auto-generated JSON schemas

๐Ÿ› Debugging

Use the MCP Inspector for debugging:

npx @modelcontextprotocol/inspector \
  uv \
  --directory . \
  run \
  e2b-mcp-server

The Inspector provides a web interface for:

  • Testing tool calls
  • Viewing request/response logs
  • Debugging connection issues

Enable Debug Logging

export LOG_LEVEL=DEBUG
python -m e2b_mcp_server

๐Ÿงช Development

Setup Development Environment

# Clone the repository
git clone https://github.com/yukkit/e2b-mcp-server
cd mcp-server/packages/python

# Install dependencies
uv install

# Copy environment config
cp .env.example .env
# Edit .env with your E2B_API_KEY

Project Structure

packages/python/
โ”œโ”€โ”€ e2b_mcp_server/       # Main package
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ server.py         # MCP server
โ”‚   โ”œโ”€โ”€ manager.py        # Sandbox management
โ”‚   โ”œโ”€โ”€ handlers.py       # Tool handlers
โ”‚   โ”œโ”€โ”€ schemas.py        # Validation schemas
โ”‚   โ”œโ”€โ”€ constants.py      # Configuration
โ”‚   โ”œโ”€โ”€ exceptions.py     # Custom exceptions
โ”‚   โ””โ”€โ”€ utils.py          # Utilities
โ”œโ”€โ”€ tests/                # Test suite (coming soon)
โ”œโ”€โ”€ examples/             # Usage examples
โ”œโ”€โ”€ .env.example          # Environment template
โ”œโ”€โ”€ pyproject.toml        # Package configuration
โ””โ”€โ”€ README.md             # This file

Code Style

The codebase follows:

  • PEP 8 style guidelines
  • Type hints for all functions
  • Comprehensive docstrings
  • Modular, testable design

Running Tests

# Coming soon
pytest tests/

๏ฟฝ Building and Packaging

Quick Build

# Method 1: Using Poetry (recommended)
poetry install
poetry build

# Method 2: Using build.sh script
./build.sh

# Generated files in dist/:
# - e2b_mcp_server-0.1.1-py3-none-any.whl
# - e2b_mcp_server-0.1.1.tar.gz

Local Installation Testing

# Install from built wheel
pip install dist/*.whl

# Test the installation
python -m e2b_mcp_server --help

Publishing to PyPI

# Configure PyPI token
poetry config pypi-token.pypi YOUR_TOKEN

# Publish
poetry publish

# Or build and publish together
poetry publish --build

For detailed packaging instructions, see PACKAGING.md.

๏ฟฝ๐Ÿ“Š Resource Management

The server includes production-grade resource management:

Sandbox Limits

  • Maximum concurrent sandboxes: Configurable (default: 10)
  • Automatic limit enforcement
  • Clear error messages when limits reached

Automatic Cleanup

  • Graceful shutdown handling
  • All sandboxes cleaned up on exit
  • Temporary sandboxes auto-cleaned after use

Monitoring

# Get current statistics
from e2b_mcp_server.manager import sandbox_manager

stats = sandbox_manager.get_stats()
# Returns: {
#   "active_sandboxes": 3,
#   "max_sandboxes": 10,
#   "sandbox_ids": ["sbx_123", "sbx_456", "sbx_789"]
# }

๐Ÿ”’ Security

  • Isolated Execution: Each sandbox runs in isolated E2B environment
  • Resource Limits: Configurable timeout and sandbox limits
  • API Key Protection: Credentials loaded from environment
  • Input Validation: All inputs validated with Pydantic

๐Ÿ“„ API Reference

Tool Schemas

All tools use Pydantic schemas for validation. See schemas.py for details.

create_sandbox

{
  "timeoutMs": 300000  # Optional, milliseconds
}

run_code

{
  "code": "print('hello')",      # Required
  "sandboxId": "sbx_..."          # Optional
}

run_command

{
  "command": "ls -la",            # Required
  "sandboxId": "sbx_...",         # Required
  "background": false             # Optional
}

See inline documentation in schemas.py for complete API details.

๐Ÿค Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests (when available)
  5. Submit a pull request

๐Ÿ“ License

Apache 2.0 - See LICENSE file for details.

๐Ÿ”— Links

๐Ÿ’ฌ Support

๐Ÿ™ Acknowledgments

Built with:

  • E2B - Secure code execution sandboxes
  • MCP - Model Context Protocol
  • Pydantic - Data validation
  • Python - Programming language

Made with โค๏ธ by the E2B team

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yukkit_e2b_mcp_server-0.3.0.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yukkit_e2b_mcp_server-0.3.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file yukkit_e2b_mcp_server-0.3.0.tar.gz.

File metadata

  • Download URL: yukkit_e2b_mcp_server-0.3.0.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.12.12 Linux/6.8.0-1044-azure

File hashes

Hashes for yukkit_e2b_mcp_server-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3d4816745d088a9586f41497ec74e8accb50f1ff8d3aa05fb8b3f104d812abb2
MD5 f74cb3042eb475fe2115e96985a094dc
BLAKE2b-256 87853537f2ce5d827953af9f7cffa486fe4ec7f8154ebb6ea9e0aeadccc1f8d6

See more details on using hashes here.

File details

Details for the file yukkit_e2b_mcp_server-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for yukkit_e2b_mcp_server-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b62ffe5d192bf5dc4d519ba7db51f04f3ce49e8f5cb7859d6c1a98fae79e615
MD5 b02d2904ecb6b81990fbec276a025025
BLAKE2b-256 673d27eb3bb7906a81c92701ab4de024afb4369b34d0cd2e97c369f580978198

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page