Skip to main content

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
  • uv for 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:

  1. 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
  2. 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) and state (optional) parameters
    • Returns execution results and updated state
  • execute-persistent: Run Python code in a persistent Docker container

    • Takes code (required) and session_id (optional) parameters
    • Returns execution results
    • Maintains state between calls
  • install-package: Install Python packages in a container

    • Takes package_name (required) and session_id (optional) parameters
    • Uses uv for efficient package installation
    • Returns installation output
  • cleanup-session: Clean up a persistent session

    • Takes session_id (required) parameter
    • Stops and removes the associated Docker container

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

  1. Clone the repository:
git clone https://github.com/artivus/python-docker-mcp.git
cd python-docker-mcp
  1. Set up development environment:
uv venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
uv pip install -e ".[dev]"
  1. 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:

  1. Sync dependencies and update lockfile:
uv sync
  1. Build package distributions:
uv build
  1. 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.

Metadata

Release files for python-docker-mcp 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-docker-mcp 0.2.5
File Size Uploaded Provenance
python_docker_mcp-0.2.5.tar.gz 22.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for python-docker-mcp 0.2.5
File Interpreter ABI Platform
python_docker_mcp-0.2.5-py3-none-any.whl Python 3 none any Details

Total release size: 48.1 kB

Release files / python_docker_mcp-0.2.5.tar.gz

Download URL python_docker_mcp-0.2.5.tar.gz
Size 22.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4c8a5e579505c5fb024c5bc5205118277e7073f05ec57e0708de1a5373a189f7
BLAKE2b-256 checksum
How to use checksums
992b2fb6928d0a608fdc63598fce72da5dfb79e15857a4fba20f5846ce951f9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.23

Release files / python_docker_mcp-0.2.5-py3-none-any.whl

Download URL python_docker_mcp-0.2.5-py3-none-any.whl
Size 25.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e43b1ca58e8bc5e436a78ede4380cbe6c736d5226da8d8f918905cc534acddc
BLAKE2b-256 checksum
How to use checksums
26b71057a155ec43a29c04835bd73362cda12e67b8e2b7371b00b2a0c85bdb8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.23

Security for python-docker-mcp 0.2.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page