Using Pydantic MCP Run Python as an Open Source Alternative to OpenAI Code Interpreter


In the last blog I discussed how I use OpenAI Code Interpreter to do RAG over data (CSV, Excel, etc.) files. OpenAI Code Interpreter is a managed offering and it does have some limitations. So, I was looking for an open source alternative. I discovered Pydantic team’s MCP Run Python package. It is an MCP server that allows agents to execute Python code in a secure, sandboxed environment. It uses Pyodide to run Python code in a JavaScript environment with Deno, isolating execution from the host system.

Installing Deno

You start by installing Deno.

curl -fsSL https://deno.land/install.sh | sh

Reload your shell or restart it and then you can run:

deno --version

You will see output as shown below:

deno 2.3.7 (stable, release, x86_64-apple-darwin)
v8 13.7.152.6-rusty
typescript 5.8.3

Starting the MCP Server

Once you have Deno installed you can run the MCP server using the code below:

import subprocess
import time


def start_mcp_server(port=3001):
    print(f"🚀 Starting MCP server on port {port}...")

    cmd = [
        "deno", "run", "-N", "-R=node_modules", "-W=node_modules",
        "--node-modules-dir=auto", "jsr:@pydantic/mcp-run-python",
        "sse", "--port", str(port)
    ]

    process = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True
    )

    print("⏳ Waiting for server to start...")
    time.sleep(5)  # Give server time to start

    # Check if server started successfully
    if process.poll() is not None:
        stdout, stderr = process.communicate()
        print("❌ Server failed to start:")
        print("STDOUT:", stdout)
        print("STDERR:", stderr)
        return None, None

    return process, f"http://localhost:{port}/sse"


process, url = start_mcp_server()
print(f"Started server at URL {url}")

Code Explanation

This function automates the process of starting the Pydantic MCP Run Python server:

  1. Command Construction: The function builds a Deno command to run the MCP server with specific permissions and configurations
  2. Process Management: It uses subprocess.Popen to start the server as a background process, capturing both stdout and stderr for monitoring
  3. Startup Verification: After a 5-second wait period, it checks if the process is still running to ensure successful startup
  4. Error Handling: If the server fails to start, it captures and displays the error output for debugging
  5. URL Generation: On success, it returns both the process handle and the server URL for further use

The Deno command does the following:

  • -N -R=node_modules -W=node_modules (alias of --allow-net --allow-read=node_modules --allow-write=node_modules) allows network access and read+write access to ./node_modules. These are required so Pyodide can download and cache the Python standard library and packages
  • --node-modules-dir=auto tells Deno to use a local node_modules directory
  • sse runs the server with the SSE MCP transport — running the server as an HTTP server to connect locally or remotely

Exposing the Server with ngrok

We will be using OpenAI’s Responses API. The call to the MCP server will be done from OpenAI’s cloud so we have to remotely host the server. We will use ngrok to expose the local 3001 port.

ngrok http 3001

ngrok Explanation

ngrok is a tunneling service that creates a secure tunnel from a public endpoint to your locally running service. When you run ngrok http 3001, it:

  1. Creates a Public URL: Generates a temporary public HTTPS URL (like https://abc123.ngrok.io) that forwards to your local port 3001
  2. Enables Remote Access: Allows OpenAI’s servers to access your locally running MCP server from anywhere on the internet
  3. Provides Security: Uses encrypted tunnels to ensure secure communication between the public endpoint and your local service
  4. Offers Monitoring: Shows real-time traffic and request logs in the ngrok dashboard

This is essential because OpenAI’s Responses API needs to reach your MCP server from their cloud infrastructure, and your local server isn’t directly accessible from the internet.

Using the MCP Server with OpenAI

Below is an example of how we can use Pydantic MCP Run Python tool:

import os
from openai import OpenAI

client = OpenAI()

# Define the CSV file path (accessible to your MCP server)
# csv_file_path = 'data/aviation_grievance.csv'
csv_url = "http://localhost:12312/aviation_grievance.csv"
user_query = "which airline has the most checkin and delay related issues?"

response = client.responses.create(
    model="o4-mini",
    instructions="""You are a data analyst with access to Python via the run_python_code tool.

        You can download and analyze CSV files from HTTP URLs. Use pandas to read the CSV data 
        and perform the requested analysis focusing on check-in and delay related issues.""",
    input=f"""I have a CSV file available at: {csv_url}

Please use the run_python_code tool to:
1. Download and read the CSV file from the URL using pandas
2. Analyze the structure of the file by reading first few records
3. Generate Python code to answer the user query
4. Provide a clear answer

Query: {user_query}
""",
    tools=[
        {
            "type": "mcp",
            "server_label": "python_executor",
            "server_url": "<<ngrok_url>>/sse",
            "require_approval": "never"
        }
    ],
)

# Print response
print(response)
print(response.output_text)

OpenAI Integration Code Explanation

This code demonstrates how to integrate the Pydantic MCP Run Python server with OpenAI’s Responses API:

  1. Client Setup: Initializes the OpenAI client with your API credentials
  2. Data Source: Defines a CSV file URL that contains aviation grievance data for analysis
  3. Query Definition: Sets up a specific analytical question about airline check-in and delay issues
  4. Response Creation: Uses the client.responses.create() method to:
  • Model Selection: Specifies “o4-mini” as the language model
  • Instructions: Provides system-level instructions defining the AI’s role as a data analyst with Python capabilities
  • Input Prompt: Gives detailed instructions for the analysis workflow
  • Tool Configuration: Configures the MCP server as an available tool with:
    • type: "mcp": Specifies this is an MCP server tool
    • server_label: A human-readable identifier for the tool
    • server_url: The ngrok URL pointing to your MCP server
    • require_approval: "never": Allows automatic execution without manual approval
  1. Output: Prints both the full response object and the extracted text output

The AI will use the MCP server to execute Python code, download the CSV data, perform pandas-based analysis, and provide insights about which airlines have the most check-in and delay-related issues.

Conclusion

The Pydantic MCP Run Python tool provides a powerful open-source alternative to OpenAI Code Interpreter, offering:

  • Security: Sandboxed Python execution environment
  • Flexibility: Integration with any MCP-compatible AI system
  • Control: Full control over the execution environment and data processing
  • Cost-effectiveness: No additional fees beyond your OpenAI API usage

This setup allows you to perform sophisticated data analysis while maintaining control over your computing resources and data processing pipeline.


Discover more from Shekhar Gulati

Subscribe to get the latest posts sent to your email.

Leave a comment

Discover more from Shekhar Gulati

Subscribe now to keep reading and get access to the full archive.

Continue reading