Skip to main content

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:

  1. MCP Server (main.py): Exposes MCP tools for managing and communicating with code execution containers
  2. 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

  1. Clone or download this project
  2. 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

  1. Container Creation: When start_code_executer is 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
  2. Code Execution: When execute_code is 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
  3. 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: uv for 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

  1. New MCP Tools: Add new functions decorated with @mcp.tool() in main.py
  2. Executor Enhancements: Modify code_executer/executer.py for new execution capabilities
  3. Container Customization: Update the Dockerfile template in main.py

Troubleshooting

Common Issues

  1. Docker Connection Errors: Ensure Docker is running and accessible
  2. Port Conflicts: Make sure port 8888 is available
  3. Container Build Failures: Check Docker logs for dependency installation issues

Metadata

Release files for containerizedcodeexecutermcp 0.1.0

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

Source distribution (sdist)

Source distribution for containerizedcodeexecutermcp 0.1.0
File Size Uploaded Provenance
containerizedcodeexecutermcp-0.1.0.tar.gz 45.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for containerizedcodeexecutermcp 0.1.0
File Interpreter ABI Platform
containerizedcodeexecutermcp-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 99.6 kB

Release files / containerizedcodeexecutermcp-0.1.0.tar.gz

Download URL containerizedcodeexecutermcp-0.1.0.tar.gz
Size 45.6 kB
Tags Source
SHA-256 checksum
How to use checksums
bcd826eb88e0ca94e414cc0dad6eec23285d11d9a50f956ca673c1218fa4ad7a
BLAKE2b-256 checksum
How to use checksums
44fc2502367b685d2c687ede0fe68b65c8c40ac023919e7e3f645b7aa23166f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.4

Release files / containerizedcodeexecutermcp-0.1.0-py3-none-any.whl

Download URL containerizedcodeexecutermcp-0.1.0-py3-none-any.whl
Size 54.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
74f48add2f446bbfb989e3a012bc8433242e34090de1680a7f86d64a64cd9dcb
BLAKE2b-256 checksum
How to use checksums
6486203c680a341e6e5ab4d950d504a709dfea7b78fdda8b0811991a16cdcd71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.4

Security for containerizedcodeexecutermcp 0.1.0

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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