Skip to main content

Model Context Protocol server to run Python code in a sandbox.

Project description

MCP Run Python

CI PyPI versions license Join Slack

MCP server to run Python code in a sandbox.

Code is executed using Pyodide in Deno and is therefore isolated from the rest of the operating system.

Features

  • Secure Execution: Run Python code in a sandboxed WebAssembly environment
  • Package Management: Automatically detects and installs required dependencies
  • Complete Results: Captures standard output, standard error, and return values
  • Asynchronous Support: Runs async code properly
  • Error Handling: Provides detailed error reports for debugging

(This code was previously part of Pydantic AI but was moved to a separate repo to make it easier to maintain.)

Usage

To use this server, you must have both Python and Deno installed.

The server can be run with deno installed using uvx:

uvx mcp-run-python [-h] [--version] [--port PORT] [--deps DEPS] {stdio,streamable-http,example}

where:

  • stdio runs the server with the Stdio MCP transport — suitable for running the process as a subprocess locally
  • streamable-http runs the server with the Streamable HTTP MCP transport - suitable for running the server as an HTTP server to connect locally or remotely. This supports stateful requests, but does not require the client to hold a stateful connection like SSE
  • example will run a minimal Python script using numpy, useful for checking that the package is working, for the code to run successfully, you'll need to install numpy using uvx mcp-run-python --deps numpy example

Usage in codes as an MCP server

pip install mcp-run-python
# or
uv add mcp-run-python

Then you can use mcp-run-python with Pydantic AI:

from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStdio
from mcp_run_python import deno_args_prepare

import logfire

logfire.configure()
logfire.instrument_mcp()
logfire.instrument_pydantic_ai()

server = MCPServerStdio('deno', args=deno_args_prepare('stdio'))
agent = Agent('claude-3-5-haiku-latest', toolsets=[server])


async def main():
    async with agent:
        result = await agent.run('How many days between 2000-01-01 and 2025-03-18?')
    print(result.output)
    #> There are 9,208 days between January 1, 2000, and March 18, 2025.w

if __name__ == '__main__':
    import asyncio
    asyncio.run(main())

Note: deno_args_prepare can take deps as a keyword argument to install dependencies. As well as returning the args needed to run mcp_run_python, deno_args_prepare installs the dependencies so they can be used by the server.

Usage in code with code_sandbox

mcp-run-python includes a helper function code_sandbox to allow you to easily run code in a sandbox.

from mcp_run_python import code_sandbox

code = """
import numpy
a = numpy.array([1, 2, 3])
print(a)
a
"""

async def main():
    async with code_sandbox(dependencies=['numpy']) as sandbox:
        result = await sandbox.eval(code)
        print(result)


if __name__ == '__main__':
    import asyncio

    asyncio.run(main())

Under the hood, code_sandbox runs an MCP server using stdio. You can run multiple code blocks with a single sandbox.

Logging

MCP Run Python supports emitting stdout and stderr from the python execution as MCP logging messages.

For logs to be emitted you must set the logging level when connecting to the server. By default, the log level is set to the highest level, emergency.

Dependencies

mcp_run_python uses a two step process to install dependencies while avoiding any risk that sandboxed code can edit the filesystem.

  • deno is first run with write permissions to the node_modules directory and dependencies are installed, causing wheels to be written to ``
  • deno is then run with read-only permissions to the node_modules directory to run untrusted code.

Dependencies must be provided when initializing the server so they can be installed in the first step.

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

mcp_run_python-0.0.2.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

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

mcp_run_python-0.0.2-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file mcp_run_python-0.0.2.tar.gz.

File metadata

  • Download URL: mcp_run_python-0.0.2.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.14

File hashes

Hashes for mcp_run_python-0.0.2.tar.gz
Algorithm Hash digest
SHA256 fbbdf0da83f82085e001026f12607e0a4dd6c69f19c1a2b3dc998b8e94f94b2a
MD5 eaa0323df37f9bffda587e151b869402
BLAKE2b-256 ef5aa1db3a6eef337fb6094b7a72c15981bdabba1c8dfa0ca802d9898e93e77c

See more details on using hashes here.

File details

Details for the file mcp_run_python-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_run_python-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1b0068e574a0eaa846b3751b40215d2b61e66818602f06ff205ac7b4b4420ca2
MD5 0a9d6ffa5f0a3d3f60dcf6bafd794566
BLAKE2b-256 586645e505962609919bdd1023720bc3d830b473a93791991ff28c422f80582c

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