A production-ready Model Context Protocol (MCP) server that integrates AI assistants with E2B's sandboxed code execution environment.
This MCP server provides secure, sandboxed code execution capabilities for Python and JavaScript, along with file management and package installation features. Built for the Klavis AI coding assignment, it demonstrates enterprise-grade architecture with comprehensive error handling, security validation, and resource management.
execute_python- Execute Python code in sandboxed environmentexecute_javascript- Execute JavaScript/Node.js codecreate_file- Create files in sandboxread_file- Read files from sandboxlist_files- List directory contentsinstall_packages- Install Python (pip) or Node.js (npm) packagesget_sandbox_info- Get sandbox status and resource information
- Security-First Design: Input validation, output sanitization, and dangerous pattern detection
- Resource Management: Automatic sandbox cleanup, idle timeout handling, and resource monitoring
- Production Ready: Comprehensive logging, error handling, and graceful shutdown
- Multi-language Support: Both Python and JavaScript execution environments
- Persistent Sessions: Reuse sandboxes across tool calls for better performance
-
Clone and Install Dependencies
git clone <repository> cd my-mcp-server npm install
-
Set Environment Variables
export E2B_API_KEY="your-e2b-api-key" export LOG_LEVEL="info" # Optional: debug, info, warn, error export NODE_ENV="production" # Optional: enables file logging
-
Build the Project
npm run build
-
Test the Installation
npm test
Configure your MCP client to use this server:
{
"mcpServers": {
"e2b-server": {
"command": "node",
"args": ["/path/to/my-mcp-server/dist/index.js"],
"env": {
"E2B_API_KEY": "your-api-key"
}
}
}
}npm run dev# Run comprehensive tests
npm test
# Or run the built server directly
npm startExecute Python code in a sandboxed environment.
Parameters:
code(string, required): Python code to executesandbox_id(string, optional): Specific sandbox ID to reuse
Example:
print("Hello from E2B!")
import numpy as np
data = np.array([1, 2, 3, 4, 5])
print(f"Mean: {np.mean(data)}")Execute JavaScript/Node.js code in a sandboxed environment.
Parameters:
code(string, required): JavaScript code to executesandbox_id(string, optional): Specific sandbox ID to reuse
Example:
console.log("Hello from Node.js!");
const fs = require('fs');
const data = [1, 2, 3, 4, 5];
const mean = data.reduce((a, b) => a + b) / data.length;
console.log(`Mean: ${mean}`);Create a file in the sandbox environment.
Parameters:
path(string, required): File path to createcontent(string, required): File contentsandbox_id(string, optional): Specific sandbox ID
Example:
{
"path": "data/example.txt",
"content": "Hello, E2B MCP Server!"
}Read a file from the sandbox environment.
Parameters:
path(string, required): File path to readsandbox_id(string, optional): Specific sandbox ID
List files in a directory.
Parameters:
path(string, optional): Directory path (defaults to current directory)sandbox_id(string, optional): Specific sandbox ID
Install packages in the sandbox environment.
Parameters:
packages(array of strings, required): Package names to installlanguage(string, required): "python" or "javascript"sandbox_id(string, optional): Specific sandbox ID
Examples:
{
"packages": ["numpy", "pandas", "matplotlib"],
"language": "python"
}{
"packages": ["lodash", "axios", "moment"],
"language": "javascript"
}Get information about sandbox status and resource usage.
Parameters:
sandbox_id(string, optional): Specific sandbox ID (if not provided, lists all sandboxes)
- Code length limits (50KB max)
- Dangerous pattern detection
- Package name validation
- File path sanitization
- ANSI escape code removal
- Secret detection and masking
- Output length limits (10KB max)
- Execution timeout (30 seconds)
- Idle sandbox cleanup (5 minutes)
- Maximum file size (10MB)
- Package installation limits
The server monitors for potentially dangerous patterns:
- Network operations (
socket,requests,fetch) - System file access (
/etc,/root,/sys) - Process operations (
subprocess,child_process) - Secret patterns (API keys, passwords)
-
SandboxService (
src/sandbox-manager.ts)- Manages E2B sandbox lifecycle
- Handles resource cleanup and idle timeouts
- Provides sandbox pooling and reuse
-
E2BTools (
src/tools.ts)- Implements all MCP tool handlers
- Integrates with E2B API
- Handles execution and file operations
-
SecurityValidator (
src/security.ts)- Input validation and sanitization
- Pattern detection for dangerous code
- Output sanitization and secret masking
-
Logger (
src/logger.ts)- Structured logging with Winston
- Development and production configurations
- Security event logging
- Comprehensive try-catch blocks
- Graceful degradation
- Detailed error logging
- User-friendly error messages
- Automatic resource cleanup on failures
- Automatic sandbox cleanup on idle timeout
- Graceful shutdown handlers
- Memory and resource monitoring
- Connection pooling for better performance
| Variable | Required | Default | Description |
|---|---|---|---|
E2B_API_KEY |
Yes | - | Your E2B API key |
LOG_LEVEL |
No | info | Logging level (debug, info, warn, error) |
NODE_ENV |
No | development | Environment (enables file logging in production) |
src/
├── index.ts # Main MCP server entry point
├── sandbox-manager.ts # E2B sandbox lifecycle management
├── tools.ts # MCP tool implementations
├── security.ts # Security validation and sanitization
├── logger.ts # Logging configuration
├── types.ts # TypeScript type definitions
└── test.ts # Comprehensive test suite
npm run build# Mock tests (no API key required) - verify implementation structure
npm run test-mock
# Full functionality tests (requires E2B_API_KEY environment variable)
npm testMock Testing:
The test-mock script verifies that your implementation is structurally correct without requiring an E2B API key. This is useful for:
- Verifying tool definitions are correct
- Checking method signatures
- Testing implementation structure
- CI/CD environments where API keys aren't available
Full Testing:
The test script runs comprehensive functionality tests including:
- Python code execution
- JavaScript code execution
- File operations (create, read, list)
- Package installation (Python & JavaScript)
- Sandbox management
- Error handling
npx tsc --noEmitCreate a Dockerfile:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist/ ./dist/
ENV NODE_ENV=production
CMD ["npm", "start"]# Production environment variables
export NODE_ENV=production
export LOG_LEVEL=info
export E2B_API_KEY=your-production-api-keyThe server provides structured JSON logging suitable for log aggregation services:
{
"level": "info",
"message": "Executing Python code in sandbox abc-123",
"timestamp": "2024-01-01T12:00:00.000Z",
"service": "e2b-mcp-server"
}- Sandbox Reuse: Sandboxes are reused across tool calls to reduce latency
- Idle Cleanup: Automatic cleanup after 5 minutes of inactivity
- Output Limits: Output is truncated at 10KB to prevent memory issues
- Timeout Management: 30-second execution timeout with graceful error handling
- Resource Monitoring: Built-in sandbox resource usage tracking
-
"E2B_API_KEY environment variable is required"
- Ensure your E2B API key is set in environment variables
- Verify the key is valid and has appropriate permissions
-
"Sandbox creation failed"
- Check E2B service status
- Verify API key permissions
- Check network connectivity
-
"Security validation failed"
- Review code for dangerous patterns
- Check file paths for directory traversal
- Verify package names follow naming conventions
-
"Execution timeout"
- Code took longer than 30 seconds to execute
- Consider optimizing code or breaking into smaller chunks
✅ Code Execution Issue Resolved
The previous issue with Jupyter kernel port (49999) not being open has been completely resolved. The server now uses E2B's latest v1.5.1 SDK which provides direct code execution without relying on Jupyter kernels.
What was fixed:
- Updated from legacy Jupyter-based execution to E2B's modern code execution API
- Fixed
execute_python✅ - Now works with E2B v1.5.1 direct execution - Fixed
execute_javascript✅ - Now works with proper Node.js wrapping - Fixed
install_packages✅ - Now works for both Python (pip) and JavaScript (npm)
All features now working:
- ✅
execute_python- Execute Python code in sandboxed environment - ✅
execute_javascript- Execute JavaScript/Node.js code - ✅
create_file- Create files in sandbox - ✅
read_file- Read files from sandbox - ✅
list_files- List directory contents - ✅
install_packages- Install Python (pip) or Node.js (npm) packages - ✅
get_sandbox_info- Get sandbox status and resource information
Enable debug logging for detailed troubleshooting:
export LOG_LEVEL=debug
npm startThis project follows TypeScript strict mode and includes comprehensive error handling. When contributing:
- Follow the existing code style
- Add appropriate error handling
- Include security validation for new features
- Update tests for new functionality
- Add logging for important operations
MIT License - see LICENSE file for details.
For issues specific to this MCP server implementation, please check the troubleshooting section above. For E2B-related issues, consult the E2B documentation.