An MCP server that executes Python code in containerized environments.
Project description
Containerized Code Executer MCP
A Model Context Protocol (MCP) server that provides secure Python code execution capabilities using Docker containers. This server allows clients to execute Python code in isolated Docker environments with customizable dependencies.
Features
- Secure Execution: Code runs in isolated Docker containers, preventing system interference
- Dynamic Dependencies: Install Python packages on-demand when starting the execution service
- Persistent State: Maintains execution state within the container session
- Socket Communication: Fast TCP socket communication between client and container
- MCP Integration: Built with FastMCP for seamless integration with MCP clients
Architecture
The project consists of two main components:
- MCP Server (
main.py): Exposes MCP tools for managing and communicating with code execution containers - Code Executor (
code_executer/executer.py): Python REPL server that runs inside Docker containers
Installation
Prerequisites
- Python 3.11 or higher
- Docker Desktop or Docker Engine
- UV package manager (recommended) or pip
Setup
- Clone or download this project
- Install dependencies:
# Using UV (recommended)
uv install
# Or using pip
pip install -r requirements.txt
Usage
Starting the MCP Server
Run the MCP server:
python main.py
The server will start and expose the following MCP tools:
Available Tools
start_code_executer
Starts a new code execution service in a Docker container.
Parameters:
dependencies(optional): List of Python packages to install in the container
Example:
# Start with no additional dependencies
start_code_executer()
# Start with specific packages
start_code_executer(dependencies=["numpy", "pandas", "matplotlib"])
execute_code
Executes Python code in the running container.
Parameters:
code: Python code string to execute
Example:
execute_code("print('Hello, World!')")
execute_code("import numpy as np; print(np.array([1, 2, 3]))")
stop_code_executer
Stops and removes the code execution container.
Example:
stop_code_executer()
How It Works
-
Container Creation: When
start_code_executeris called, the server:- Generates a Dockerfile with specified dependencies
- Builds a Docker image with Python 3.11 and the executor script
- Runs a container exposing port 8888
-
Code Execution: When
execute_codeis called:- The MCP server connects to the container via TCP socket (port 8888)
- Sends the Python code to the container
- The container executes the code and returns results as JSON
- Results include output, errors, and execution status
-
State Persistence: Each container maintains its own execution context, so variables and imports persist between code executions within the same session.
Security Features
- Isolation: Code runs in completely isolated Docker containers
- Network Isolation: Containers only expose the necessary port for communication
- Resource Limits: Docker provides built-in resource management and limits
- Temporary Containers: Containers are created with unique names and can be easily cleaned up
Configuration
Custom Dependencies
You can specify Python packages to be installed when starting the execution service:
start_code_executer(dependencies=["requests", "beautifulsoup4", "scikit-learn"])
Docker Configuration
The Docker container uses:
- Base image:
python:3.11-slim - Working directory:
/app - Exposed port: - Package manager:
uvfor fast dependency resolution
Development
Project Structure
├── main.py # MCP server with tool definitions
├── pyproject.toml # Project configuration and dependencies
├── README.md #
├── uv.lock # Locked dependencies
└── code_executer/
├── Dockerfile # Generated dynamically
└── executer.py # Python REPL server for containers
Adding New Features
- New MCP Tools: Add new functions decorated with
@mcp.tool()inmain.py - Executor Enhancements: Modify
code_executer/executer.pyfor new execution capabilities - Container Customization: Update the Dockerfile template in
main.py
Troubleshooting
Common Issues
- Docker Connection Errors: Ensure Docker is running and accessible
- Port Conflicts: Make sure port 8888 is available
- Container Build Failures: Check Docker logs for dependency installation issues
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file containerizedcodeexecutermcp-0.1.0.tar.gz.
File metadata
- Download URL: containerizedcodeexecutermcp-0.1.0.tar.gz
- Upload date:
- Size: 45.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcd826eb88e0ca94e414cc0dad6eec23285d11d9a50f956ca673c1218fa4ad7a
|
|
| MD5 |
8ddcb9bef6dd218d414686130bb32c83
|
|
| BLAKE2b-256 |
44fc2502367b685d2c687ede0fe68b65c8c40ac023919e7e3f645b7aa23166f7
|
File details
Details for the file containerizedcodeexecutermcp-0.1.0-py3-none-any.whl.
File metadata
- Download URL: containerizedcodeexecutermcp-0.1.0-py3-none-any.whl
- Upload date:
- Size: 54.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74f48add2f446bbfb989e3a012bc8433242e34090de1680a7f86d64a64cd9dcb
|
|
| MD5 |
445dec2022a82413451873a46151b363
|
|
| BLAKE2b-256 |
6486203c680a341e6e5ab4d950d504a709dfea7b78fdda8b0811991a16cdcd71
|