Skip to main content
Glama
pathakkhhimanshu

AI Dev Assistant

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
NameRequiredDescriptionDefault
languageYesThe language runtime to use: 'python' or 'node'.
codeYesThe code snippet to execute. Keep it self-contained.

Implementation Reference

  • 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");
      },
    };
  • 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"],
    },
  • 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.",
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 and effectively discloses key behavioral traits: it specifies hard limits (15-second timeout, 64 KB output cap), isolation (runs in isolated temp file), and statelessness (no persistent state between calls). It does not cover aspects like error handling or security implications, but provides substantial operational context.

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 front-loaded with core functionality and efficiently structured in three sentences: the first states purpose and constraints, the second details isolation, and the third provides usage context. Every sentence adds value without redundancy, making it appropriately sized and zero-waste.

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 moderate complexity (code execution with constraints), no annotations, and no output schema, the description is largely complete: it covers purpose, behavioral limits, and usage context. However, it lacks details on return values or error responses, which would be helpful for an agent invoking the tool.

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?

Schema description coverage is 100%, so the schema already documents both parameters thoroughly. The description adds minimal value beyond the schema, mentioning 'self-contained' for the code parameter but not elaborating on syntax or constraints. Baseline 3 is appropriate as the schema does the heavy lifting.

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

Purpose5/5

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

The description clearly states the tool executes code snippets in specific languages (Python/Node.js) in a sandboxed environment, distinguishing it from sibling tools like doc_search or terminal_commander by focusing on code execution rather than document retrieval or system commands. It specifies the action ('executes'), resource ('code snippet'), and context ('sandboxed child process').

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

Usage Guidelines4/5

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

The description provides clear context for when to use this tool ('ideal for quick calculations, data transformations, and API-free logic'), but does not explicitly state when not to use it or name alternatives among siblings. It implies usage for small, stateless tasks but lacks explicit exclusions or comparisons to other tools.

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/pathakkhhimanshu/MCP'

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