Skip to main content
Glama

run-code

Execute code snippets in various programming languages and receive immediate results using the MCP server tool designed for efficient code execution.

Instructions

Run code snippet and return the result.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesCode Snippet
languageIdYesLanguage ID

Implementation Reference

  • src/server.ts:15-51 (registration)
    Registration of the 'run-code' tool, including the name, description, input schema, and the handler function.
    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, }, ], }; }, );
  • Handler function that validates input, retrieves the executor for the language, creates a temp file with the code, executes it, and returns the stdout as text content.
    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 schema defining the inputs: code (string) and languageId (enum of supported languages).
    { code: z.string().describe("Code Snippet"), languageId: z.enum(Object.keys(languageIdToExecutorMap) as [keyof typeof languageIdToExecutorMap]).describe("Language ID"), },
  • Constants mapping language IDs to executor commands and file extensions, used for schema enum and execution.
    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", }; export const languageIdToFileExtensionMap = { javascript: "js", typescript: "ts", powershell: "ps1", };
  • Helper functions to create temporary file for the code, get file extension, and execute the shell command, capturing stdout or error.
    async function createTmpFile(content: string, languageId: string) { const tmpDir = os.tmpdir(); const fileExtension = getFileExtension(languageId); const fileName = `tmp.${fileExtension}`; const filePath = path.join(tmpDir, fileName); await fs.promises.writeFile(filePath, content); console.debug(`Temporary file created at: ${filePath}`); return filePath; } function getFileExtension(languageId: string): string { const fileExtension = languageIdToFileExtensionMap[languageId as keyof typeof languageIdToFileExtensionMap]; return fileExtension ?? languageId; } 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); }); });

Other Tools

Related 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