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
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Python code to execute | |
| timeout | No | Execution 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); } }
- src/handlers/index.ts:87-95 (handler)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; }
- src/tools/index.ts:3-21 (schema)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"], }, };
- src/handlers/index.ts:32-38 (registration)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, ];
- src/handlers/index.ts:40-43 (schema)Arktype validation schema for pyodide_execute input arguments (code required, timeout optional).const isExecutePythonArgs = type({ code: "string", "timeout?": "number", });