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