MCP server to run Python code in a sandbox.
Configuration
View docs{
"mcpServers": {
"pydantic-mcp-run-python": {
"command": "uvx",
"args": [
"mcp-run-python@latest",
"stdio"
],
"env": {
"UVX": "<UVX>",
"MCP_LOG": "emergency"
}
}
}
}MCP Run Python provides a secure way to execute Python code inside a sandboxed environment, enabling you to run Python scripts and capture their output within an MCP-based workflow. It emphasizes safe isolation, automatic dependency handling, and easy integration with MCP clients to enable dynamic Python-enabled tooling and workflows.
You can use this server with an MCP client to run Python code in a sandbox and receive complete results, including standard output, standard error, and return values. Choose the Streamable MCP transport for interactive or stateful interactions, or use the stdio transport for running the server as a subprocess locally. You can also run small, self-contained Python blocks via a sandbox helper and integrate the server into larger agent workflows.
To start the MCP server locally using the stdio transport, run the dedicated startup command that launches the MCP server through the package manager tooling. A practical configuration shown in code usage uses the uvx tool to run the Python server script in stdio mode.
You can also prepare a Deno-based environment to host the server, enabling dependency installation and a ready-to-run runtime. This is helpful when you want to install dependencies in a controlled two-step process that protects the sandboxed code from filesystem manipulation.
Code sandboxing is supported via a helper that spins up an MCP server using stdio under the hood, allowing you to evaluate code blocks with a shared sandbox lifecycle. This makes it easy to run multiple code blocks without restarting the server each time.
Prerequisites you need before installing the MCP server: Python and Deno must be available on your system. You also need a package manager to install the server client and the MCP server package.
Install the MCP Run Python package using your preferred method.
pip install mcp-run-python
# or using the package manager helper
uv add mcp-run-pythonIf you want to run Deno-based workflows or prepare a ready environment for the server, you can use the provided helpers to set up the Deno environment and install dependencies.
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStdio
from mcp_run_python import async_prepare_deno_env
import logfire
logfire.configure()
logfire.instrument_mcp()
logfire.instrument_pydantic_ai()
async def main():
async with async_prepare_deno_env('stdio') as deno_env:
server = MCPServerStdio('deno', args=deno_env.args, cwd=deno_env.cwd, timeout=10)
agent = Agent('claude-3-5-haiku-latest', toolsets=[server])
async with agent:
result = await agent.run('How many days between 2000-01-01 and 2025-03-18?')
print(result.output)
if __name__ == '__main__':
import asyncio
asyncio.run(main())Security and reliability notes: Python code running in the sandbox is isolated from the host system. The two-step dependency installation process is designed to limit filesystem access during untrusted code execution. When using this server, be mindful of the potential risks associated with running untrusted Python code and ensure you only allow safe, vetted code paths.
Usage notes: The server supports emitting stdout and stderr as MCP logging messages. To enable logs, set the desired logging level when connecting to the server. By default, the logging level is the highest level, emergency.
Usage with Pydantic AI demonstrates how to wire the MCP server into an agent workflow. You can start a server in stdio mode and connect an agent to it to run queries and capture results.
Code sandbox helper: The code_sandbox function lets you run code blocks in a sandboxed MCP server, reusing a single sandbox for multiple evaluations.
Runs Python code in a sandboxed WebAssembly environment ensuring isolation from the host system.
Automatically detects and installs required Python dependencies for the sandboxed code.
Captures standard output, standard error, and return values so you can inspect results precisely.
Properly runs asynchronous Python code blocks within the sandbox.
Provides detailed error reports for debugging and safe failure handling.
Helper that enables evaluating code in a reusable sandbox via a dedicated MCP server.
Assists in preparing a Deno-based environment to host the server and install dependencies safely.