MCP Sandbox
Execute Python code and install packages safely within isolated Docker containers.
MCP Sandbox
Feel free to try on mcp sandbox
δΈζζζ‘£ | English
Demo
Python MCP Sandbox is an interactive Python code execution tool that allows users and LLMs to safely execute Python code and install packages in isolated Docker containers.
Viby
Viby works with mcp sandbox
Features
- π³ Docker Isolation: Securely run Python code in isolated Docker containers
- π¦ Package Management: Easily install and manage Python packages with support for custom PyPI mirrors
- π File Generation: Support for generating files and accessing them via web links
- π Authentication: Optional API key-based authentication for multi-user environments
- π¨ Web UI: Built-in web interface for managing sandboxes and viewing execution results
- π SSE Support: Real-time communication via Server-Sent Events for MCP integration
Installation
# Clone the repository
git clone https://github.com/JohanLi233/python-mcp-sandbox.git
cd python-mcp-sandbox
# Install dependencies using uv
uv venv
uv sync
# Start the server
uv run main.py
The default SSE endpoint is http://127.0.0.1:8181/sse, and you can interact with it via the MCP Inspector through SSE or any other client that supports SSE connections.
Configuration
The server configuration can be customized in config.toml:
- Host: Default is
127.0.0.1(localhost only) - Port: Default is
8181 - PyPI Mirror: Configure your preferred Python package index mirror
To allow external access, change the host to 0.0.0.0 in the configuration file.
Available Tools
- create_sandbox: Creates a new Python Docker sandbox and returns its ID for subsequent code execution and package installation
- list_sandboxes: Lists all existing sandboxes (Docker containers) for reuse
- execute_python_code: Executes Python code in a specified Docker sandbox
- install_package_in_sandbox: Installs Python packages in a specified Docker sandbox
- check_package_installation_status: Checks if a package is installed or installation status in a Docker sandbox
- execute_terminal_command: Executes a terminal command in the specified Docker sandbox. Parameters:
sandbox_id(string),command(string). Returnsstdout,stderr,exit_code. - upload_file_to_sandbox: Uploads a local file to the specified Docker sandbox. Parameters:
sandbox_id(string),local_file_path(string),dest_path(string, optional, default:/app/results).
Project Structure
python-mcp-sandbox/
βββ main.py # Application entry point
βββ requirements.txt # Project dependencies
βββ Dockerfile # Docker configuration for Python containers
βββ results/ # Directory for generated files
βββ mcp_sandbox/ # Main package directory
β βββ __init__.py
β βββ models.py # Pydantic models
β βββ api/ # API related components
β β βββ __init__.py
β β βββ routes.py # API route definitions
β βββ core/ # Core functionality
β β βββ __init__.py
β β βββ docker_manager.py # Docker container management
β β βββ mcp_tools.py # MCP tools
β βββ utils/ # Utilities
β βββ __init__.py
β βββ config.py # Configuration constants
β βββ file_manager.py # File management
β βββ task_manager.py # Periodic task management
βββ README.md # Project documentation
Example Prompt
I've configured a Python code execution sandbox for you. You can run Python code using the following steps:
1. First, use the "list_sandboxes" tool to view all existing sandboxes (Docker containers).
- You can reuse an existing sandbox_id if a sandbox exists, do not create a new one.
- If you need a new sandbox, use the "create_sandbox" tool.
- Each sandbox is an isolated Python environment, and the sandbox_id is required for all subsequent operations.
2. If you need to install packages, use the "install_package_in_sandbox" tool
- Parameters: sandbox_id and package_name (e.g., numpy, pandas)
- This starts asynchronous installation and returns immediately with status
3. After installing packages, you can check their installation status using the "check_package_installation_status" tool
- Parameters: sandbox_id and package_name (name of the package to check)
- If the package is still installing, you need to check again using this tool
4. Use the "execute_python_code" tool to run your code
- Parameters: sandbox_id and code (Python code)
- Returns output, errors and links to any generated files
- All generated files are stored inside the sandbox, and file_links are direct HTTP links for inline viewing
Example workflow:
- Use list_sandboxes to check for available sandboxes, if no available sandboxes, use create_sandbox to create a new one β Get sandbox_id
- Use install_package_in_sandbox to install necessary packages (like pandas, matplotlib), with the sandbox_id parameter
- Use check_package_installation_status to verify package installation, with the same sandbox_id parameter
- Use execute_python_code to run your code, with the sandbox_id parameter
Code execution happens in a secure sandbox. Generated files (images, CSVs, etc.) will be provided as direct HTTP links, which can viewed inline in the browser.
Remember not to use plt.show() in your Python code. For visualizations:
- Save figures to files using plt.savefig() instead of plt.show()
- For data, use methods like df.to_csv() or df.to_excel() to save as files
- All saved files will automatically appear as HTTP links in the results, which you can open or embed directly.
MCP Example Config
Below is an example config for Claude Desktop:
{
"mcpServers": {
"mcpSandbox": {
"command": "npx",
"args": ["-y", "supergateway", "--sse", "http://127.0.0.1:8181/sse"]
}
}
}
If authentication is enabled, include the API key:
{
"mcpServers": {
"mcpSandbox": {
"command": "npx",
"args": ["-y", "supergateway", "--sse", "http://127.0.0.1:8181/sse?api_key=<YOUR_API_KEY>"]
}
}
}
MCP Example Config for Online Demo
{
"mcpServers": {
"mcpSandbox": {
"command": "npx",
"args": ["-y", "supergateway", "--sse", "http://115.190.87.78/sse?api_key=<API_KEY>"]
}
}
}
Modify the serverUrl as needed for your environment.
Related Servers
MCP Emulator Controller
Control emulators by opening/closing apps, capturing screenshots, and interacting with the screen.
Azure DevOps
Integrate with Azure DevOps services to manage work items, repositories, and pipelines.
Kubernetes Automated Installation
An agent for automatically installing Kubernetes in a Rocky Linux environment using MCP.
CryptoAnalysisMCP
Provides comprehensive cryptocurrency technical analysis, including real-time price data, technical indicators, chart pattern detection, and trading signals for over 2,500 cryptocurrencies.
Bonk MCP
Implements Solana blockchain functionality for the LetsBonk launchpad.
Jira Context MCP
MCP server to provide Jira Tickets information to AI coding agents like Cursor.
Coding Assistant Server
A coding assistant server that provides context-aware code suggestions, documentation integration, and technology detection.
PipeCD
Integrate with PipeCD to manage applications and deployments.
TCC
Automatically generates MCP servers from OpenAPI specifications, enabling conversational AI agents to interact with existing web systems.
gNMIBuddy
Retrieves essential network information from devices using gNMI and OpenConfig models.