Skip to main content
Glama
yonaka15
by yonaka15

pyodide_execute

Execute Python code in a browser environment with Pyodide, capturing outputs and saving generated images to accessible file paths.

Instructions

Execute Python code using Pyodide with output capture. When generating images, they will be automatically saved to the output directory instead of being displayed. Images can be accessed from the saved file paths that will be included in the output.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesPython code to execute
timeoutNoExecution timeout in milliseconds (default: 5000)

Implementation Reference

  • Core implementation that executes Python code in Pyodide, handles timeout, captures output, performs memory cleanup, and formats the result or error.
    async executePython(code: string, timeout: number) {
      if (!this.pyodide) {
        return formatCallToolError("Pyodide not initialized");
      }
    
      try {
        const { result, output } = await withOutputCapture(
          this.pyodide,
          async () => {
            const executionResult = await Promise.race([
              this.pyodide!.runPythonAsync(code),
              new Promise((_, reject) =>
                setTimeout(() => reject(new Error("Execution timeout")), timeout)
              ),
            ]);
    
            // Memory cleanup
            this.pyodide!.globals.clear();
            await this.pyodide!.runPythonAsync("import gc; gc.collect()");
    
            return executionResult;
          },
          { suppressConsole: true }
        );
    
        return formatCallToolSuccess(
          output
            ? `Output:\n${output}\nResult:\n${String(result)}`
            : String(result)
        );
      } catch (error) {
        return formatCallToolError(error);
      }
    }
  • MCP server handler for pyodide_execute tool: validates input arguments and calls PyodideManager.executePython.
    case "pyodide_execute": {
      const executePythonArgs = isExecutePythonArgs(args);
      if (executePythonArgs instanceof type.errors) {
        throw executePythonArgs;
      }
      const { code, timeout = 5000 } = executePythonArgs;
      const results = await pyodideManager.executePython(code, timeout);
      return results;
    }
  • Tool definition including name, description, and inputSchema for the pyodide_execute tool.
    export const EXECUTE_PYTHON_TOOL: Tool = {
      name: "pyodide_execute",
      description:
        "Execute Python code using Pyodide with output capture. When generating images, they will be automatically saved to the output directory instead of being displayed. Images can be accessed from the saved file paths that will be included in the output.",
      inputSchema: {
        type: "object",
        properties: {
          code: {
            type: "string",
            description: "Python code to execute",
          },
          timeout: {
            type: "number",
            description: "Execution timeout in milliseconds (default: 5000)",
          },
        },
        required: ["code"],
      },
    };
  • Registers all tools including EXECUTE_PYTHON_TOOL (pyodide_execute) in the server's TOOLS list.
    const TOOLS: Tool[] = [
      tools.EXECUTE_PYTHON_TOOL,
      tools.INSTALL_PYTHON_PACKAGES_TOOL,
      tools.GET_MOUNT_POINTS_TOOL,
      tools.LIST_MOUNTED_DIRECTORY_TOOL,
      tools.READ_IMAGE_TOOL,
    ];
  • Arktype validation schema for pyodide_execute input arguments (code required, timeout optional).
    const isExecutePythonArgs = type({
      code: "string",
      "timeout?": "number",
    });
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively describes key behaviors: output capture, automatic image saving to an output directory, and inclusion of file paths in output. It also implies a sandboxed execution environment (Pyodide). However, it doesn't cover potential side effects like memory usage, error handling, or security restrictions, leaving some gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is highly concise and well-structured in two sentences. The first sentence states the core functionality, and the second adds important behavioral context about image handling. Every word earns its place with no redundancy or fluff.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (executing arbitrary Python code), no annotations, and no output schema, the description does a good job covering essential aspects: execution method, output capture, and image handling. However, it doesn't explain the return format or error behavior, which would be helpful for an AI agent. The absence of an output schema increases the need for more completeness.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, providing clear documentation for both parameters (code and timeout). The description doesn't add any parameter-specific information beyond what's in the schema, such as code syntax examples or timeout implications. According to the rules, with high schema coverage, the baseline is 3 even without param info in the description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Execute Python code using Pyodide with output capture.' It specifies the action (execute), resource (Python code), and key capability (output capture). However, it doesn't explicitly differentiate from sibling tools like pyodide_install-packages or pyodide_read-image, which prevents a perfect score.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides implied usage guidance by mentioning that 'When generating images, they will be automatically saved to the output directory instead of being displayed.' This suggests a specific use case for image handling. However, it lacks explicit guidance on when to use this tool versus alternatives like pyodide_get-mount-points or pyodide_list-mounted-directory, and doesn't mention prerequisites or exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/yonaka15/mcp-pyodide'

If you have feedback or need assistance with the MCP directory API, please join our Discord server