code_mode_transform
Transform raw MCP tool outputs by running custom JavaScript code in a secure sandbox to extract, filter, or reformat specific data fields as needed.
Instructions
A universal code-mode transformer. Takes RAW TEXT or JSON output from ANY MCP tool (GitHub, Firecrawl, chrome-devtools, camoufox, codegraphcontext, videoMcp, arxiv, etc.) and runs a custom JavaScript code string against it in a secure QuickJS sandbox. Use this as a second step after calling any tool that returns large payloads — pass the raw output as 'data' and a JS extraction script as 'code'. Your script reads the 'DATA' global variable (a string of the tool output) and uses console.log() to print only the fields you need. Typical use cases: extract only issue titles/IDs from GitHub list_issues, pull specific selectors from DOM snapshots, summarize crawl results, extract timestamps from video transcripts.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | The raw text or JSON output from another MCP tool to process. | |
| code | Yes | JavaScript code to execute. The 'DATA' global variable contains the raw data string. Use console.log() to output your extraction. Example: `var d = JSON.parse(DATA); console.log(d.items.map(function(i){return i.title}).join('\n'));` | |
| language | No | Language of the code. Only 'javascript' is supported. | javascript |
| source_tool | No | Optional. Name of the MCP tool that produced the data (for logging/metrics only). |
Implementation Reference
- src/tools/handlers.ts:148-184 (handler)The handler function `codeModeTransformHandler` that executes the code string against the provided data in a sandbox.
export async function codeModeTransformHandler(args: unknown) { if (!isCodeModeTransformArgs(args)) { throw new Error("Invalid arguments for code_mode_transform"); } const { data, code, language = "javascript", source_tool = "unknown" } = args; if (language.toLowerCase() !== "javascript") { return { content: [{ type: "text", text: "Unsupported language. Only 'javascript' is supported." }], isError: true, }; } const beforeSizeKB = (Buffer.byteLength(data, "utf8") / 1024).toFixed(1); console.error(`[code_mode_transform] source=${source_tool}, input=${beforeSizeKB}KB`); const { stdout, error, executionTimeMs } = await runInSandbox(data, code); if (error) { return { content: [{ type: "text", text: `Sandboxed Execution Failed:\n${error}` }], isError: true, }; } const finalOutput = stdout.trim() || "[No output from script]"; const afterSizeKB = (Buffer.byteLength(finalOutput, "utf8") / 1024).toFixed(1); const reductionPct = (100 - (Number(afterSizeKB) / Number(beforeSizeKB)) * 100).toFixed(1); const header = `[code-mode-transform (${source_tool}): ${beforeSizeKB}KB -> ${afterSizeKB}KB (${reductionPct}% reduction) in ${executionTimeMs}ms]\n\n`; return { content: [{ type: "text", text: header + finalOutput }], isError: false, }; } - src/tools/definitions.ts:181-214 (schema)The definition of the `CODE_MODE_TRANSFORM_TOOL` including its input schema and description.
export const CODE_MODE_TRANSFORM_TOOL: Tool = { name: "code_mode_transform", description: "A universal code-mode transformer. Takes RAW TEXT or JSON output from ANY MCP tool (GitHub, Firecrawl, chrome-devtools, camoufox, codegraphcontext, videoMcp, arxiv, etc.) " + "and runs a custom JavaScript code string against it in a secure QuickJS sandbox. " + "Use this as a second step after calling any tool that returns large payloads — pass the raw output as 'data' and a JS extraction script as 'code'. " + "Your script reads the 'DATA' global variable (a string of the tool output) and uses console.log() to print only the fields you need. " + "Typical use cases: extract only issue titles/IDs from GitHub list_issues, pull specific selectors from DOM snapshots, summarize crawl results, extract timestamps from video transcripts.", inputSchema: { type: "object", properties: { data: { type: "string", description: "The raw text or JSON output from another MCP tool to process.", }, code: { type: "string", description: "JavaScript code to execute. The 'DATA' global variable contains the raw data string. Use console.log() to output your extraction. " + "Example: `var d = JSON.parse(DATA); console.log(d.items.map(function(i){return i.title}).join('\\n'));`", }, language: { type: "string", description: "Language of the code. Only 'javascript' is supported.", default: "javascript", }, source_tool: { type: "string", description: "Optional. Name of the MCP tool that produced the data (for logging/metrics only).", }, }, required: ["data", "code"], }, }; - src/tools/definitions.ts:356-367 (helper)The `isCodeModeTransformArgs` type guard function used to validate the arguments for the tool.
export function isCodeModeTransformArgs( args: unknown ): args is { data: string; code: string; language?: string; source_tool?: string } { return ( typeof args === "object" && args !== null && "data" in args && typeof (args as { data: string }).data === "string" && "code" in args && typeof (args as { code: string }).code === "string" ); }