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
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Code Snippet | |
| languageId | Yes | Language 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, }, ], }; }, );
- src/server.ts:22-50 (handler)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, }, ], }; },
- src/server.ts:18-21 (schema)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"), },
- src/constants.ts:1-45 (helper)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", };
- src/server.ts:56-86 (helper)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); }); });