Skip to main content

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:

  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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

containerizedcodeexecutermcp-0.1.0.tar.gz (45.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

containerizedcodeexecutermcp-0.1.0-py3-none-any.whl (54.1 kB view details)

Uploaded Python 3

File details

Details for the file containerizedcodeexecutermcp-0.1.0.tar.gz.

File metadata

File hashes

Hashes for containerizedcodeexecutermcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bcd826eb88e0ca94e414cc0dad6eec23285d11d9a50f956ca673c1218fa4ad7a
MD5 8ddcb9bef6dd218d414686130bb32c83
BLAKE2b-256 44fc2502367b685d2c687ede0fe68b65c8c40ac023919e7e3f645b7aa23166f7

See more details on using hashes here.

File details

Details for the file containerizedcodeexecutermcp-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for containerizedcodeexecutermcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 74f48add2f446bbfb989e3a012bc8433242e34090de1680a7f86d64a64cd9dcb
MD5 445dec2022a82413451873a46151b363
BLAKE2b-256 6486203c680a341e6e5ab4d950d504a709dfea7b78fdda8b0811991a16cdcd71

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page