run-python-code
Execute Python code securely in a sandbox environment, with options for pip install commands, JSON parsing, timeout, memory allocation, and saving output files.
Instructions
Execute Python code in a sandbox.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Python code | |
| commands | No | Pip install commands | |
| memory | No | Memory allocation in MB | |
| parseJson | No | Parse output as JSON | |
| saveOutputFiles | No | Save output files | |
| timeout | No | Execution timeout in ms |
Implementation Reference
- src/index.ts:1029-1051 (handler)The handler function that proxies the Python code execution request to the Dumpling AI API's /api/v1/run-python-code endpoint, handling authentication, error checking, and response formatting.async ({ code, commands, parseJson, timeout, memory, saveOutputFiles }) => { const apiKey = process.env.DUMPLING_API_KEY; if (!apiKey) throw new Error("DUMPLING_API_KEY not set"); const response = await fetch(`${NWS_API_BASE}/api/v1/run-python-code`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${apiKey}`, }, body: JSON.stringify({ code, commands, parseJson, timeout, memory, saveOutputFiles, }), }); if (!response.ok) throw new Error(`Failed: ${response.status} ${await response.text()}`); const data = await response.json(); return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] }; }
- src/index.ts:1021-1028 (schema)Zod schema defining the input parameters for the 'run-python-code' tool, including code, optional pip commands, JSON parsing, timeout, memory, and file saving options.{ code: z.string().describe("Python code"), commands: z.string().optional().describe("Pip install commands"), parseJson: z.boolean().optional().describe("Parse output as JSON"), timeout: z.number().optional().describe("Execution timeout in ms"), memory: z.number().optional().describe("Memory allocation in MB"), saveOutputFiles: z.boolean().optional().describe("Save output files"), },
- src/index.ts:1018-1052 (registration)Registration of the 'run-python-code' tool using server.tool(), including name, description, input schema, and handler function.server.tool( "run-python-code", "Execute Python code in a sandbox.", { code: z.string().describe("Python code"), commands: z.string().optional().describe("Pip install commands"), parseJson: z.boolean().optional().describe("Parse output as JSON"), timeout: z.number().optional().describe("Execution timeout in ms"), memory: z.number().optional().describe("Memory allocation in MB"), saveOutputFiles: z.boolean().optional().describe("Save output files"), }, async ({ code, commands, parseJson, timeout, memory, saveOutputFiles }) => { const apiKey = process.env.DUMPLING_API_KEY; if (!apiKey) throw new Error("DUMPLING_API_KEY not set"); const response = await fetch(`${NWS_API_BASE}/api/v1/run-python-code`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${apiKey}`, }, body: JSON.stringify({ code, commands, parseJson, timeout, memory, saveOutputFiles, }), }); if (!response.ok) throw new Error(`Failed: ${response.status} ${await response.text()}`); const data = await response.json(); return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] }; } );