RFC: Standardized Interface For Isolated Python Execution Via MCP

by ADMIN 66 views

Background


The current process for executing Python code in an isolated Pyodide environment through MCP requires complex JSON-RPC formatting and multiple command-line flags. This creates unnecessary friction for developers and makes automation difficult. The existing workflow involves manual handling of dependencies, understanding of the internal MCP architecture, and multiple Deno flags. This complexity hinders the adoption of MCP for Python development and integration with Continuous Integration/Continuous Deployment (CI/CD) pipelines.

Problem Statement


Running Python code through the MCP runner requires:

  • Correct JSON-RPC payload formatting
  • Multiple Deno flags
  • Understanding of the internal MCP architecture
  • Manual handling of dependencies

The current process is demonstrated in the following example:

cat <<EOF > request.json
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "runPython",
  "params": {
    "files": [
      {
        "name": "main.py",
        "content": "print('Hello from Python in MCP!')",
        "active": true
      }
    ]
  }
}
EOF

cat request.json | deno run -N -R=node_modules -W=node_modules --node-modules-dir=auto --allow-read=. --allow-env jsr:@pydantic/mcp-run-python stdio

This example showcases the complexity of the current process, which involves creating a JSON-RPC payload, specifying multiple Deno flags, and manually handling dependencies.

Proposal


To address the complexity and friction associated with executing Python code through MCP, we propose creating a standardized interface that simplifies the process. The proposed interface will provide a simple CLI wrapper, direct string execution, stdin piping, dependency management, and module imports from the filesystem.

Simple CLI Wrapper


The proposed CLI wrapper will provide a simple and intuitive way to execute Python code through MCP. The wrapper will handle JSON-RPC formatting, configure appropriate Deno flags, and manage dependencies.

Example usage:

pymcp run code.py [--mode stdio|sse|warmup] [--port 3001]

Direct String Execution


The proposed interface will also support direct string execution, allowing developers to execute Python code without creating a separate file.

Example usage:

pymcp exec "print('Hello world')"

Stdin Piping


The proposed interface will also support stdin piping, allowing developers to pipe Python code from stdin to MCP.

Example usage:

cat code.py | pymcp run -

Dependency Management


The proposed interface will also manage dependencies, allowing developers to specify requirements and Python packages.

Example usage:

pymcp run code.py --requirements requirements.txt

Module Imports from Filesystem


The proposed interface will also support module imports from the filesystem, allowing developers to import modules from local directories.

Example usage:

pymcp run main.py --pythonpath ./algorithms

Implementation Details


The proposed wrapper will handle the following tasks:

  • Handle JSON-RPC formatting Configure appropriate Deno flags
  • Manage dependencies
  • Support module imports from the local filesystem
  • Format output for easier consumption
  • Provide proper error reporting

Benefits


The proposed standardized interface will provide several benefits, including:

  • Simplifies development workflow
  • Reduces learning curve
  • Facilitates automation and CI/CD integration
  • Standardizes the interface for Python execution
  • Creates a foundation for future enhancements (debugging, profiling)

Next Steps


To implement the proposed standardized interface, the following steps will be taken:

  1. Create prototype implementation
  2. Add tests for various use cases
  3. Document the interface
  4. Integrate with existing MCP tooling

Q: What is the current process for executing Python code in an isolated Pyodide environment through MCP?


A: The current process involves complex JSON-RPC formatting and multiple command-line flags, which creates unnecessary friction for developers and makes automation difficult.

Q: What are the main challenges associated with the current process?


A: The main challenges include:

  • Correct JSON-RPC payload formatting
  • Multiple Deno flags
  • Understanding of the internal MCP architecture
  • Manual handling of dependencies

Q: What is the proposed standardized interface for isolated Python execution through MCP?


A: The proposed interface will provide a simple CLI wrapper, direct string execution, stdin piping, dependency management, and module imports from the filesystem.

Q: What are the benefits of the proposed standardized interface?


A: The benefits include:

  • Simplifies development workflow
  • Reduces learning curve
  • Facilitates automation and CI/CD integration
  • Standardizes the interface for Python execution
  • Creates a foundation for future enhancements (debugging, profiling)

Q: How will the proposed interface handle JSON-RPC formatting?


A: The proposed interface will handle JSON-RPC formatting automatically, eliminating the need for manual formatting.

Q: How will the proposed interface manage dependencies?


A: The proposed interface will manage dependencies by allowing developers to specify requirements and Python packages.

Q: How will the proposed interface support module imports from the filesystem?


A: The proposed interface will support module imports from the filesystem by allowing developers to import modules from local directories.

Q: What are the next steps for implementing the proposed standardized interface?


A: The next steps include:

  1. Create prototype implementation
  2. Add tests for various use cases
  3. Document the interface
  4. Integrate with existing MCP tooling

Q: How will the proposed interface be integrated with existing MCP tooling?


A: The proposed interface will be integrated with existing MCP tooling through a series of APIs and interfaces, ensuring seamless integration and compatibility.

Q: What is the expected timeline for implementing the proposed standardized interface?


A: The expected timeline for implementing the proposed standardized interface is [insert timeline].

Q: Who will be responsible for implementing the proposed standardized interface?


A: The implementation of the proposed standardized interface will be led by [insert team/individual].

Q: How will the proposed interface be tested and validated?


A: The proposed interface will be tested and validated through a series of unit tests, integration tests, and user acceptance testing (UAT).

Q: What are the expected outcomes of implementing the proposed standardized interface?


A: The expected outcomes include:

  • Simplified development workflow
  • Reduced learning curve
  • Facilitated automation and CI/CD integration
  • Standardized interface for Python execution
  • Created foundation for future enhancements (debugging, profiling)

By addressing the challenges and complexities associated the current process, the proposed standardized interface will provide a simplified and streamlined experience for developers, facilitating automation and CI/CD integration, and creating a foundation for future enhancements.