Skip to main content
Glama
formulahendry

mcp-server-code-runner

run-code

Execute code snippets in multiple programming languages to test functionality and view results directly within the MCP server environment.

Instructions

Run code snippet and return the result.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesCode Snippet
languageIdYesLanguage ID

Implementation Reference

  • Core handler logic for executing the provided code snippet in the specified language by creating a temporary file and running it with the appropriate executor.
    async ({ code, languageId }) => {
      if (!code) {
        throw new Error("Code is required.");
      }
    
      if (!languageId) {
        throw new Error("Language ID is required.");
      }
    
      const executor = languageIdToExecutorMap[languageId as keyof typeof languageIdToExecutorMap];
    
      if (!executor) {
        throw new Error(`Language '${languageId}' is not supported.`);
      }
    
      const filePath = await createTmpFile(code, languageId);
      const command = `${executor} "${filePath}"`;
    
      const result = await executeCommand(command);
    
      return {
        content: [
          {
            type: "text",
            text: result,
          },
        ],
      };
    },
  • Zod input schema defining 'code' (string) and 'languageId' (enum from supported languages).
    {
      code: z.string().describe("Code Snippet"),
      languageId: z.enum(Object.keys(languageIdToExecutorMap) as [keyof typeof languageIdToExecutorMap]).describe("Language ID"),
    },
  • src/server.ts:15-51 (registration)
    Registration of the 'run-code' tool on the MCP server, including name, description, schema, and handler.
    server.tool(
      "run-code",
      "Run code snippet and return the result.",
      {
        code: z.string().describe("Code Snippet"),
        languageId: z.enum(Object.keys(languageIdToExecutorMap) as [keyof typeof languageIdToExecutorMap]).describe("Language ID"),
      },
      async ({ code, languageId }) => {
        if (!code) {
          throw new Error("Code is required.");
        }
    
        if (!languageId) {
          throw new Error("Language ID is required.");
        }
    
        const executor = languageIdToExecutorMap[languageId as keyof typeof languageIdToExecutorMap];
    
        if (!executor) {
          throw new Error(`Language '${languageId}' is not supported.`);
        }
    
        const filePath = await createTmpFile(code, languageId);
        const command = `${executor} "${filePath}"`;
    
        const result = await executeCommand(command);
    
        return {
          content: [
            {
              type: "text",
              text: result,
            },
          ],
        };
      },
    );
  • Helper function to execute shell commands and capture stdout, rejecting on error or stderr.
    async function executeCommand(command: string): Promise<string> {
      return new Promise((resolve, reject) => {
        console.debug(`Executing command: ${command}`);
        exec(command, (error: any, stdout: string, stderr: string) => {
          if (error) {
            reject(`Error: ${error.message}`);
          }
          if (stderr) {
            reject(`Stderr: ${stderr}`);
          }
          resolve(stdout);
        });
      });
  • Mapping of language IDs to shell executors used by the handler and schema.
    export const languageIdToExecutorMap = {
        javascript: "node",
        php: "php",
        python: "python -u",
        perl: "perl",
        perl6: "perl6",
        ruby: "ruby",
        go: "go run",
        lua: "lua",
        groovy: "groovy",
        powershell: "powershell -ExecutionPolicy ByPass -File",
        bat: "cmd /c",
        shellscript: "bash",
        fsharp: "fsi",
        csharp: "scriptcs",
        vbscript: "cscript //Nologo",
        typescript: "ts-node",
        coffeescript: "coffee",
        scala: "scala",
        swift: "swift",
        julia: "julia",
        crystal: "crystal",
        ocaml: "ocaml",
        r: "Rscript",
        applescript: "osascript",
        clojure: "lein exec",
        racket: "racket",
        scheme: "csi -script",
        ahk: "autohotkey",
        autoit: "autoit3",
        dart: "dart",
        haskell: "runhaskell",
        nim: "nim compile --verbosity:0 --hints:off --run",
        lisp: "sbcl --script",
        kit: "kitc --run",
        v: "v run",
        sass: "sass --style expanded",
        scss: "scss --style expanded",
    };
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions running code and returning results but lacks critical details like execution environment, security implications, timeout limits, error handling, or resource constraints, which are essential for safe and effective use.

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 a single, efficient sentence that front-loads the core purpose without unnecessary words. It is appropriately sized for the tool's function, making it highly concise and well-structured.

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

Completeness2/5

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

Given the complexity of code execution (which involves security, performance, and error handling), the description is insufficient. With no annotations, no output schema, and minimal behavioral details, it fails to provide complete context for safe and informed use, especially for a tool that could have significant side effects.

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 description does not add meaning beyond the input schema, which has 100% coverage with clear descriptions for both parameters (code and languageId). Since the schema fully documents the parameters, the baseline score of 3 is appropriate, as the description provides no additional semantic context.

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 'Run code snippet and return the result' clearly states the action (run) and resource (code snippet) with a specific outcome (return result). It distinguishes the tool's function effectively, though it lacks sibling differentiation since no sibling tools exist, making a 5 unnecessary.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool, such as for testing, debugging, or specific contexts, and offers no alternatives or exclusions. It only states what the tool does without usage context, leaving the agent to infer applicability.

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/formulahendry/mcp-server-code-runner'

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