Skip to main content
Glama

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", };

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