Skip to main content

MCP Code Execute (Python)

Executes Python code in a sandboxed environment using Pyodide (Python compiled to WebAssembly).

  • Tech: TypeScript, Deno, Pyodide (Python 3.12 in WASM)
  • Port: 8000
  • Tool name: run_python_code

Tool: run_python_code

Runs Python code in an isolated sandbox. Supports async Python, dependency management, and file creation.

Input Schema

FieldTypeRequiredDescription
codestringYesPython code to execute

Example

{
"name": "run_python_code",
"arguments": {
"code": "import math\nresult = math.sqrt(144)\nprint(f'Square root: {result}')\nresult"
}
}

Response

{
"stdout": "Square root: 12.0\n",
"stderr": "",
"result": 12.0,
"files": []
}

Features

Dependency Management

Dependencies can be declared using PEP 723 script metadata in the code:

# /// script
# dependencies = ["numpy", "pandas"]
# ///
import numpy as np
import pandas as pd

data = np.array([1, 2, 3, 4, 5])
print(f"Mean: {np.mean(data)}")

Pyodide installs the packages via micropip before execution.

File Creation

Code can create files in /tmp/mcp_run_python. After execution, all created files are automatically uploaded to Azure Blob Storage and their URLs are returned.

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('/tmp/mcp_run_python/chart.png')

Security

  • Code runs in WebAssembly (Pyodide) -- no access to the host filesystem or network
  • Execution is sandboxed by design
  • File output is restricted to the /tmp/mcp_run_python directory

Transports

Supports three MCP transport modes: stdio, SSE, and Streamable HTTP.