LLM Python Code Sandbox

LLM Python Code Sandbox

Enables LLMs to execute Python code in isolated sandboxes with file operations and MCP integration, supporting multi-round execution and plot capture.

Category
Visit Server

README

LLM Python Code Sandbox ๐Ÿš€

Self-Hosting an E2B-like coding playground

Logo Description
<img src="./asset/logo.png" width="500px"> A Python code sandbox HTTP service crafted for LLMs, enabling isolated Python execution environments with code execution, file operations, and MCP integration. ๐ŸŽ›๏ธ

๐ŸŒŸ Awesome Features

  1. Create Sandbox ๐Ÿ†•: Spin up a Jupyter kernel and get a unique ID in a flash! โšก
  2. Execute Code ๐Ÿ’ป: Run code in a specified Jupyter kernel and receive stdout, stderr, errors, tracebacks, and even binary files like images. Supports Multi-Round Code Execution for complex workflows! ๐ŸŽจ
  3. File Operations ๐Ÿ“: Upload files to the sandbox workspace and download files from it seamlessly. ๐Ÿ“ค๐Ÿ“ฅ
  4. Close Sandbox ๐Ÿ—‘๏ธ: Safely shut down sandboxes and clean up resources when you're done. โ™ป๏ธ
  5. Sandbox Isolation ๐Ÿ”’: Each sandbox has its own Python virtual environment and working directory to prevent package conflicts. ๐Ÿ›ก๏ธ
  6. Auto-Cleanup ๐Ÿงน: Sandboxes automatically close after 24 hours with hourly cleanup of expired ones. No messy leftovers! ๐Ÿšฟ
  7. Virtual Environment Mirror ๐Ÿชž: Service auto-creates a base virtual environment image with common packages on startup, making new sandbox initialization super fast! โšก
  8. MCP Support ๐Ÿค–: Integrated with FastAPI-MCP, allowing the service to be directly called by AI models. ๐Ÿค

๐Ÿš€ Getting Started

Install Dependencies

Run this command in your project directory to install all required packages:

cd sandbox
pip install -r requirements.txt

Start the Service

Launch the sandbox service with this simple command:

python main.py --host 0.0.0.0 --port 8000

The service will be up and running at http://0.0.0.0:8000 ๐ŸŒ

๐Ÿ“ฑ Sandbox Client (E2B-like)

You can directly use the SandboxClient to interact with the sandbox service!

Basic Usage

# Create client instance
client = SandboxClient(base_url='http://0.0.0.0:8000')

# Create a new sandbox
 sandbox_id = client.create_sandbox()

# Execute some code
client.execute_code("print('Hello, Sandbox! ๐Ÿ‘‹')")

# Install required Python packages in the sandbox's virtual environment
client.install_package("numpy")

# View generated files
files = client.list_files()

# Download a generated CSV file
csv_file = next((f for f in files if f['path'].endswith('.csv')), None)
client.download_file(csv_file['path'])

# Upload a local file to the sandbox
client.upload_file('test_upload.txt')

# Close the sandbox when finished
client.close_sandbox()

# Check if all sandboxes are closed
client.list_all_sandboxes()

Multi-Round Code Execution Example ๐ŸŽญ

The sandbox supports executing multiple code blocks in the same session, preserving state between executions. Perfect for building complex programs step by step! ๐Ÿงฉ

# Create client and sandbox
client = SandboxClient(base_url='http://0.0.0.0:8000')
client.create_sandbox()

# Step 1: Import libraries and define initial data
client.execute_code("""import numpy as np
import pandas as pd

# Create sample data
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David'],
    'Age': [28, 32, 45, 36],
    'Salary': [8000, 12000, 15000, 9000]
}

# Create DataFrame
df = pd.DataFrame(data)
print(df)""")

# Step 2: Data processing and analysis (using data defined in step 1)
client.execute_code("""# Calculate average age and salary
avg_age = df['Age'].mean()
avg_salary = df['Salary'].mean()

print(f"Average Age: {avg_age:.1f}")
print(f"Average Salary: ${avg_age:.2f}")

# Add a new column
df['Age Group'] = pd.cut(df['Age'], bins=[20, 30, 40, 50], labels=['20-30s', '30-40s', '40-50s'])
print(df)""")


# Step 3: Save processed data
client.execute_code("""# Save to CSV file
df.to_csv('processed_data.csv', index=False)
print("Data saved to processed_data.csv ๐Ÿ’พ")
""")

# Download the generated file
files = client.list_files()
csv_file = next((f for f in files if f['path'] == 'processed_data.csv'), None)
if csv_file:
    client.download_file(csv_file['path'])

# Close the sandbox
client.close_sandbox()

Matplotlib Plot Capture Example ๐ŸŽจ

The sandbox can capture matplotlib plots and return them as image data, perfect for displaying or saving visualizations! ๐Ÿ“Š

# Create client and sandbox
client = SandboxClient(base_url='http://0.0.0.0:8000')
client.create_sandbox()

# Install required packages (if not in base environment)
client.install_package("matplotlib")
client.install_package("numpy")

# Execute plotting code
result = client.execute_code("""import numpy as np
import matplotlib.pyplot as plt

# Generate data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# Create plot
plt.figure(figsize=(10, 6))
plt.plot(x, y1, label='Sine Function')
plt.plot(x, y2, label='Cosine Function')
plt.title('Trigonometric Functions Demo ๐Ÿ“ˆ')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.legend()
plt.tight_layout()

# The sandbox will automatically capture the plot and return image data
""")

# The result will contain image data for the plot, which can be used for display or saving
# Image data is typically stored in result['results'] as base64 encoded strings

# Another way to save the plot as a file
client.execute_code("""# Save the plot to a file
plt.savefig('trigonometric_functions.png', dpi=300, bbox_inches='tight')
print("Plot saved as trigonometric_functions.png ๐ŸŽจ")
""")

# Download the generated image file
files = client.list_files()
img_file = next((f for f in files if f['path'] == 'trigonometric_functions.png'), None)
if img_file:
    client.download_file(img_file['path'])

# Close the sandbox
client.close_sandbox()

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured