Python Sandbox MCP server

Provides a browser-compatible Python execution environment with package management capabilities for running code snippets safely without requiring a backend Python installation.
Back to servers
Setup instructions
Provider
Pydantic
Release date
Jun 21, 2024
Language
TypeScript
Package
Stats
3.9K downloads
12.4K stars

MCP Run Python is a server implementation of the Model Context Protocol designed to safely execute Python code. It uses Pyodide in Deno to provide an isolated sandbox environment, preventing any access to your operating system for secure code execution.

Installation

To use MCP Run Python, you need to have Deno installed on your system. Once Deno is available, you can run the server directly without any additional installation steps.

Running the Server

You can start the server with Deno using the following command:

deno run \
  -N -R=node_modules -W=node_modules --node-modules-dir=auto \
  jsr:@pydantic/mcp-run-python [stdio|streamable_http|sse|warmup]

Command Parameters Explained

The parameters in the command serve the following purposes:

  • -N -R=node_modules -W=node_modules: This gives the server network access and read/write permissions to the ./node_modules directory, which is needed for downloading and caching Python libraries
  • --node-modules-dir=auto: Instructs Deno to use a local node_modules directory
  • The last parameter specifies the transport mode:
    • stdio: Uses the Stdio MCP transport, ideal for running as a local subprocess
    • streamable_http: Uses the Streamable HTTP MCP transport for running as an HTTP server with support for stateful requests
    • sse: Uses the SSE MCP transport (deprecated but supported for backward compatibility)
    • warmup: Runs a minimal Python script to download and cache the Python standard library and verify proper server operation

Usage Example

Here's how to integrate MCP Run Python with Pydantic AI:

from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStdio

import logfire

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

server = MCPServerStdio('deno',
    args=[
        'run',
        '-N',
        '-R=node_modules',
        '-W=node_modules',
        '--node-modules-dir=auto',
        'jsr:@pydantic/mcp-run-python',
        '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.

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

This example shows how to:

  1. Create an MCP server using the stdio transport
  2. Connect it to a Pydantic AI agent
  3. Execute Python code through natural language requests

Additional Information

For comprehensive documentation, visit https://ai.pydantic.dev/mcp/run-python/ or refer to the Model Context Protocol specification.

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "mcp-run-python" '{"command":"deno","args":["run","-N","-R=node_modules","-W=node_modules","--node-modules-dir=auto","jsr:@pydantic/mcp-run-python","stdio"]}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "mcp-run-python": {
            "command": "deno",
            "args": [
                "run",
                "-N",
                "-R=node_modules",
                "-W=node_modules",
                "--node-modules-dir=auto",
                "jsr:@pydantic/mcp-run-python",
                "stdio"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "mcp-run-python": {
            "command": "deno",
            "args": [
                "run",
                "-N",
                "-R=node_modules",
                "-W=node_modules",
                "--node-modules-dir=auto",
                "jsr:@pydantic/mcp-run-python",
                "stdio"
            ]
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later