Dockerised Python execution environment for AI agents
Project description
python-docker-mcp
Dockerized Python execution environment for AI agents.
Overview
This MCP server provides a safe, sandboxed Python execution environment for LLM-powered agents. It allows agents to:
- Execute Python code in isolated Docker containers
- Choose between transient or persistent execution environments
- Install packages as needed for specific tasks
- Maintain state between execution steps
Installation
Requirements
- Docker must be installed and running on the host system
- Python 3.11 or later
uvfor package management (recommended)
Install from PyPI
# Using uv (recommended)
uv pip install python-docker-mcp
# Using pip
pip install python-docker-mcp
Install from Source
# Clone the repository
git clone https://github.com/artivus/python-docker-mcp.git
cd python-docker-mcp
# Install with uv
uv pip install -e .
# Or with pip
pip install -e .
Quick Start
Running the Server
The python-docker-mcp server can be started directly using the module:
python -m python_docker_mcp
This will start the MCP server and listen for JSONRPC requests on stdin/stdout.
Components
Docker Execution Environment
The server implements two types of execution environments:
-
Transient Environment
- Each execution is isolated in a fresh container
- State must be explicitly passed and returned between calls
- Safer for one-off code execution
-
Persistent Environment
- Maintains state between executions
- Variables defined in one execution are available in subsequent executions
- Suitable for interactive, stateful REPL-like sessions
Preinstalled Packages
The Docker containers come with several common scientific and data analysis libraries preinstalled:
- NumPy: For numerical computing
- Pandas: For data manipulation and analysis
- SciPy: For scientific computing and technical computing
- Matplotlib: For creating visualizations
- scikit-learn: For machine learning
- SymPy: For symbolic mathematics
These packages can be imported directly without installation, making it easier to perform common data science tasks.
Tools
The server provides the following tools:
-
execute-transient: Run Python code in a transient Docker container
- Takes
code(required) andstate(optional) parameters - Returns execution results and updated state
- Takes
-
execute-persistent: Run Python code in a persistent Docker container
- Takes
code(required) andsession_id(optional) parameters - Returns execution results
- Maintains state between calls
- Takes
-
install-package: Install Python packages in a container
- Takes
package_name(required) andsession_id(optional) parameters - Uses
uvfor efficient package installation - Returns installation output
- Takes
-
cleanup-session: Clean up a persistent session
- Takes
session_id(required) parameter - Stops and removes the associated Docker container
- Takes
Configuration
The server can be configured via a YAML configuration file. By default, it looks for a file at ~/.python-docker-mcp/config.yaml.
Configuration File Structure
Example configuration:
docker:
image: python:3.12.2-slim
working_dir: /app
memory_limit: 256m
cpu_limit: 0.5
timeout: 30
network_disabled: true
read_only: true
package:
installer: uv # or pip
index_url: null # Set to your PyPI mirror if needed
trusted_hosts: [] # List of trusted hosts for pip/uv
allowed_modules:
- math
- datetime
- random
- json
- re
- collections
blocked_modules:
- os
- sys
- subprocess
- shutil
- pathlib
Docker Configuration Options
| Option | Description | Default |
|---|---|---|
image |
Docker image to use for execution | python:3.12.2-slim |
working_dir |
Working directory inside container | /app |
memory_limit |
Memory limit for container | 256m |
cpu_limit |
CPU limit (0.0-1.0) | 0.5 |
timeout |
Execution timeout in seconds | 30 |
network_disabled |
Disable network access | true |
read_only |
Run container in read-only mode | true |
Container Pooling Options
The server supports container pooling to improve performance by reusing containers. These settings can be configured via environment variables:
| Environment Variable | Description | Default |
|---|---|---|
PYTHON_DOCKER_MCP_POOL_ENABLED |
Enable container pooling | true |
PYTHON_DOCKER_MCP_POOL_SIZE |
Maximum number of containers in the pool | 32 |
PYTHON_DOCKER_MCP_POOL_MAX_AGE |
Maximum container lifetime in seconds | 300 |
PYTHON_DOCKER_MCP_MAX_CONCURRENT_CREATIONS |
Maximum number of containers that can be created simultaneously | 5 |
PYTHON_DOCKER_MCP_MEMORY_LIMIT |
Memory limit for each container | 256m |
PYTHON_DOCKER_MCP_CPU_LIMIT |
CPU limit for each container (0.0-1.0) | 0.5 |
Package Installation Options
| Option | Description | Default |
|---|---|---|
installer |
Package installer to use (uv or pip) |
uv |
index_url |
Custom PyPI index URL | null |
trusted_hosts |
Trusted hosts for package installation | [] |
Integration with Claude and Anthropic Products
Claude Desktop
On MacOS: ~/Library/Application\ Support/Claude/claude_desktop_config.json
On Windows: %APPDATA%/Claude/claude_desktop_config.json
Development/Unpublished Servers Configuration
"mcpServers": {
"python-docker-mcp": {
"command": "uv",
"args": [
"--directory",
"/path/to/python-docker-mcp",
"run",
"python-docker-mcp"
]
}
}
Published Servers Configuration
"mcpServers": {
"python-docker-mcp": {
"command": "uvx",
"args": [
"python-docker-mcp"
]
}
}
Example MCP Usage
Transient Execution
# Calculate the factorial of 5
result = await call_tool("execute-transient", {
"code": "def factorial(n):\n return 1 if n <= 1 else n * factorial(n-1)\n\nresult = factorial(5)\nprint(f'The factorial of 5 is {result}')"
})
Persistent Session
# Create a persistent session and define a function
result = await call_tool("execute-persistent", {
"code": "def add(a, b):\n return a + b\n\nprint('Function defined')"
})
# Use the function in a subsequent call with the same session
result = await call_tool("execute-persistent", {
"session_id": "previous_session_id",
"code": "result = add(10, 20)\nprint(f'10 + 20 = {result}')"
})
Installing Packages
# Install NumPy in a persistent session
result = await call_tool("install-package", {
"session_id": "my_math_session",
"package_name": "numpy"
})
# Use NumPy in the session
result = await call_tool("execute-persistent", {
"session_id": "my_math_session",
"code": "import numpy as np\narr = np.array([1, 2, 3, 4, 5])\nprint(f'Mean: {np.mean(arr)}')"
})
Development
Development Setup
- Clone the repository:
git clone https://github.com/artivus/python-docker-mcp.git
cd python-docker-mcp
- Set up development environment:
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
uv pip install -e ".[dev]"
- Install pre-commit hooks:
pre-commit install
Running Tests
# Run all tests
pytest
# Run tests with coverage
pytest --cov=src/python_docker_mcp
# Run specific test categories
pytest tests/unit/
pytest tests/integration/
Building and Publishing
To prepare the package for distribution:
- Sync dependencies and update lockfile:
uv sync
- Build package distributions:
uv build
- Publish to PyPI:
uv publish
Debugging
Since MCP servers run over stdio, debugging can be challenging. For the best debugging experience, we strongly recommend using the MCP Inspector.
You can launch the MCP Inspector via npm with this command:
npx @modelcontextprotocol/inspector uv --directory /path/to/python-docker-mcp run python-docker-mcp
License
[License information]
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
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 python_docker_mcp-0.2.5.tar.gz.
File metadata
- Download URL: python_docker_mcp-0.2.5.tar.gz
- Upload date:
- Size: 22.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c8a5e579505c5fb024c5bc5205118277e7073f05ec57e0708de1a5373a189f7
|
|
| MD5 |
2ce4d9216638acc2a43525a8e2dacda3
|
|
| BLAKE2b-256 |
992b2fb6928d0a608fdc63598fce72da5dfb79e15857a4fba20f5846ce951f9d
|
File details
Details for the file python_docker_mcp-0.2.5-py3-none-any.whl.
File metadata
- Download URL: python_docker_mcp-0.2.5-py3-none-any.whl
- Upload date:
- Size: 25.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e43b1ca58e8bc5e436a78ede4380cbe6c736d5226da8d8f918905cc534acddc
|
|
| MD5 |
bad512716b84a67b3e6cb00dc6a1878f
|
|
| BLAKE2b-256 |
26b71057a155ec43a29c04835bd73362cda12e67b8e2b7371b00b2a0c85bdb8a
|