code_executor
Execute Python or Node.js code snippets in a sandboxed environment for quick calculations, data transformations, and API-free logic testing with isolated execution and resource limits.
Instructions
Executes a small Python or Node.js code snippet in a sandboxed child process. Hard limits: 15 second timeout, 64 KB output cap. The snippet runs in an isolated temp file — no persistent state between calls. Ideal for quick calculations, data transformations, and API-free logic.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| language | Yes | The language runtime to use: 'python' or 'node'. | |
| code | Yes | The code snippet to execute. Keep it self-contained. |
Implementation Reference
- src/tools/codeExecutor.ts:59-155 (handler)The handler function for the code_executor tool, which creates a temporary file, executes it with the specified interpreter (python or node), handles timeouts and output, and returns the result.
async handler(args: { language: Language; code: string }): Promise<string> { const { language, code } = args; const tmpFile = getTempFilePath(language); try { fs.writeFileSync(tmpFile, code, "utf-8"); } catch (err) { return `ERROR: Could not write temp file.\n${String(err)}`; } const { cmd, args: extraArgs } = getInterpreter(language); const startTime = Date.now(); const result = await new Promise<ExecutionResult>((resolve) => { const proc = spawn(cmd, [...extraArgs, tmpFile], { timeout: TIMEOUT_MS, shell: process.platform === "win32", // Required for Windows PATH resolution }); let stdout = ""; let stderr = ""; let timedOut = false; const stdoutTimer = setTimeout(() => { timedOut = true; proc.kill("SIGKILL"); }, TIMEOUT_MS); proc.stdout.on("data", (chunk: Buffer) => { stdout += chunk.toString(); if (stdout.length > MAX_OUTPUT_BYTES) { stdout = stdout.slice(0, MAX_OUTPUT_BYTES) + "\n[OUTPUT TRUNCATED]"; proc.kill("SIGKILL"); } }); proc.stderr.on("data", (chunk: Buffer) => { stderr += chunk.toString(); if (stderr.length > MAX_OUTPUT_BYTES) { stderr = stderr.slice(0, MAX_OUTPUT_BYTES) + "\n[STDERR TRUNCATED]"; } }); proc.on("close", (exitCode) => { clearTimeout(stdoutTimer); resolve({ language, stdout: stdout.trimEnd(), stderr: stderr.trimEnd(), exitCode, executionTimeMs: Date.now() - startTime, timedOut, }); }); proc.on("error", (err) => { clearTimeout(stdoutTimer); resolve({ language, stdout: "", stderr: `Failed to start process: ${err.message}\n\nMake sure '${cmd}' is installed and on your PATH.`, exitCode: -1, executionTimeMs: Date.now() - startTime, timedOut: false, }); }); }); // Cleanup temp file try { fs.unlinkSync(tmpFile); } catch { // Non-fatal } const lines: string[] = [ `## Code Execution Result`, `**Language:** ${result.language} | **Exit Code:** ${result.exitCode ?? "N/A"} | **Time:** ${result.executionTimeMs}ms`, ]; if (result.timedOut) { lines.push(`\n⚠️ **TIMED OUT** after ${TIMEOUT_MS / 1000} seconds.`); } if (result.stdout) { lines.push(`\n### stdout\n\`\`\`\n${result.stdout}\n\`\`\``); } else { lines.push(`\n### stdout\n*(empty)*`); } if (result.stderr) { lines.push(`\n### stderr\n\`\`\`\n${result.stderr}\n\`\`\``); } return lines.join("\n"); }, }; - src/tools/codeExecutor.ts:44-58 (schema)The input schema definition for the code_executor tool, defining the required language and code parameters.
inputSchema: { type: "object", properties: { language: { type: "string", enum: ["python", "node"], description: "The language runtime to use: 'python' or 'node'.", }, code: { type: "string", description: "The code snippet to execute. Keep it self-contained.", }, }, required: ["language", "code"], }, - src/tools/codeExecutor.ts:37-43 (registration)The registration of the tool, including its name and description.
return { name: "code_executor", description: "Executes a small Python or Node.js code snippet in a sandboxed child process. " + "Hard limits: 15 second timeout, 64 KB output cap. " + "The snippet runs in an isolated temp file — no persistent state between calls. " + "Ideal for quick calculations, data transformations, and API-free logic.",