[{"data":1,"prerenderedAt":322},["ShallowReactive",2],{"series-\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers":3,"series-context-\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers":188,"series-chapters-\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers":316},{"id":4,"title":5,"body":6,"contentId":162,"date":163,"description":164,"extension":165,"github":166,"meetTheInstructor":168,"meta":169,"navigation":170,"part":171,"path":172,"rawbody":173,"robots":171,"schemaOrg":171,"seo":174,"seriesSlug":175,"sitemap":176,"stem":177,"subtitle":178,"topics":179,"__hash__":187},"series\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers.md","Building Code Mode for APIs with MCP and Isolated Workers",{"type":7,"value":8,"toc":157},"minimark",[9,13,22,30,43,59,62,65,68,127,135,146,151],[10,11,12],"p",{},"Hello and welcome! 👋",[10,14,15,16,21],{},"This series is a practical follow-up to ",[17,18,20],"a",{"href":19},"\u002Fposts\u002Fcode-mode-two-tools-and-an-mcp-can-save-your-llm-context","Code Mode, Two Tools, and an MCP Can Save Your LLM Context",". That article introduced the idea: instead of making a model orchestrate every dependent tool call, let it write the orchestration as code and return only the result needed for its next decision.",[10,23,24,25,29],{},"Here, we will build that idea step by step. We will start with a small local file transformation, move to an OpenAPI-described API, and then replace the trusted local evaluator based on the ",[26,27,28],"code",{},"Function"," constructor with an isolated runtime. The final result will be a locally runnable MCP server that can search an API specification and execute a narrow request against the API.",[10,31,32,33,37,38,42],{},"MCP fundamentals are outside the scope of this series. If you need an introduction to the protocol or to the basic relationship between an AI agent and an MCP server, begin with ",[17,34,36],{"href":35},"\u002Fseries\u002Fai-agents-and-mcp-server-teaming-up-for-the-agentic-web","AI Agents and MCP Server: Teaming Up for the Agentic Web",", especially ",[17,39,41],{"href":40},"\u002Fseries\u002Fai-agents-and-mcp-server-teaming-up-for-the-agentic-web\u002Fmcp-to-provide-additional-capabilities-to-the-ai-agent","its MCP server article",".",[10,44,45,46,50,51,54,55,58],{},"Throughout the series, the ",[47,48,49],"strong",{},"model"," generates tool calls or code, the ",[47,52,53],{},"MCP client"," (or agent) runs the model loop, and the ",[47,56,57],{},"MCP server"," provides the capabilities. Keeping these roles separate is important: the host, not generated code, defines which capabilities are available.",[10,60,61],{},"The central idea is simple: with direct MCP tools, the model must call one tool, receive the complete intermediate result, and then use that result to decide how to call the next tool. With Code Mode, the model writes the orchestration as code, the code accesses the capabilities it needs inside the execution environment, and the model receives only the desired final result to continue.",[10,63,64],{},"Code Mode is not automatically the right choice. Direct MCP tools remain simpler for small, obvious actions. The extra runtime is worthwhile when a task contains dependent calls, requires local data processing, or needs to navigate a large and evolving API surface, like an OpenAPI specification that can be larger than the model's context window.",[10,66,67],{},"Throughout this journey, we will:",[69,70,71,82,97,107,121],"ol",{},[72,73,74,77,78,81],"li",{},[47,75,76],{},"Beyond One Tool Call at a Time: Introducing Code Mode",": Build two direct ",[26,79,80],{},"Drive"," tools over a Markdown fixture and observe how the complete file content travels through the model's context before the next tool call.",[72,83,84,87,88,90,91,93,94,96],{},[47,85,86],{},"Let the Model Orchestrate Tools with a Local Code Runtime",": Replace the two direct tools with one generic ",[26,89,26],{}," tool, expose a typed ",[26,92,80],{}," facade, and introduce the executor interface that later parts will replace. The local evaluator based on the ",[26,95,28],{}," constructor is intentionally a trusted development shortcut, not a production sandbox.",[72,98,99,102,103,106],{},[47,100,101],{},"Searching OpenAPI Specifications Without Filling Context",": Keep the OpenAPI document inside the server, resolve its local ",[26,104,105],{},"$ref"," values, and let generated code search a compact type facade instead of returning the complete specification to the model.",[72,108,109,112,113,116,117,120],{},[47,110,111],{},"Calling APIs with Code Mode and Returning Only What Matters",": Add a separate ",[26,114,115],{},"execute"," tool with a host-owned ",[26,118,119],{},"api.request()"," helper. We will validate request options, call the local MarketHub fixture, and return a narrow projection rather than the complete API response.",[72,122,123,126],{},[47,124,125],{},"Replacing the Evaluator with Isolated Cloudflare Workers",": Swap only the executor boundary for fresh Dynamic Workers. Search will run without outbound access, execute will use an allowlisted gateway, and credentials will remain outside generated code. We will also cover result bounds, resource limits, and host deadlines.",[10,128,129,130,134],{},"The companion repository, ",[131,132],"git-hub-link",{"repo":133},"barbapapazes\u002Fcode-mode",", is an executable textbook for the series. Each article has a complete checkpoint branch, a small evaluation, and a focused diff from the previous step. I recommend that you check out a branch and inspect the implementation for a better understanding of the concepts, as the articles will not cover every detail.",[136,137,140],"callout",{"color":138,"icon":139},"info","i-ph-info-duotone",[10,141,142,143,145],{},"This series assumes familiarity with TypeScript, JavaScript promises, HTTP APIs, and basic MCP concepts. The local checkpoints contain no credentials. Parts 2 through 4 use a trusted local evaluator based on the ",[26,144,28],{}," constructor for development, while Part 5 provides a Cloudflare deployment scaffold and structural security evaluation.",[147,148,150],"h2",{"id":149},"chapters","Chapters",[152,153],"series-chapters-list",{"className":154},[155,156],"mx-auto","max-w-5xl",{"title":158,"searchDepth":159,"depth":159,"links":160},"",2,[161],{"id":149,"depth":159,"text":150},"8f6d8d8d-9cc8-4ed3-9f2d-4e3d0b7a4d91","2026-07-12","Build an MCP server for an OpenAPI-described API, then replace a trusted local evaluator with isolated Cloudflare Dynamic Workers.","md",{"name":167,"repository":133},"Code Mode","I created this series to turn the Code Mode idea into a practical implementation. We will start with two ordinary MCP tools, move the orchestration into generated code, and finish by replacing a trusted local evaluator based on the Function constructor with isolated Cloudflare Dynamic Workers.",{},true,null,"\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers","---\ncontentId: 8f6d8d8d-9cc8-4ed3-9f2d-4e3d0b7a4d91\ntitle: Building Code Mode for APIs with MCP and Isolated Workers\nsubtitle: Build a context-efficient MCP server for OpenAPI-described APIs\ndescription: Build an MCP server for an OpenAPI-described API, then replace a trusted local evaluator with isolated Cloudflare Dynamic Workers.\ndate: 2026-07-12\ntopics:\n  - ai\n  - ai-agents\n  - mcp\n  - code-mode\n  - openapi\n  - cloudflare\n  - typescript\ngithub:\n  name: Code Mode\n  repository: barbapapazes\u002Fcode-mode\nmeetTheInstructor: I created this series to turn the Code Mode idea into a practical implementation. We will start with two ordinary MCP tools, move the orchestration into generated code, and finish by replacing a trusted local evaluator based on the Function constructor with isolated Cloudflare Dynamic Workers.\nseriesSlug: building-code-mode-for-apis-with-mcp-and-isolated-workers\n---\n\nHello and welcome! 👋\n\nThis series is a practical follow-up to [Code Mode, Two Tools, and an MCP Can Save Your LLM Context](\u002Fposts\u002Fcode-mode-two-tools-and-an-mcp-can-save-your-llm-context). That article introduced the idea: instead of making a model orchestrate every dependent tool call, let it write the orchestration as code and return only the result needed for its next decision.\n\nHere, we will build that idea step by step. We will start with a small local file transformation, move to an OpenAPI-described API, and then replace the trusted local evaluator based on the `Function` constructor with an isolated runtime. The final result will be a locally runnable MCP server that can search an API specification and execute a narrow request against the API.\n\nMCP fundamentals are outside the scope of this series. If you need an introduction to the protocol or to the basic relationship between an AI agent and an MCP server, begin with [AI Agents and MCP Server: Teaming Up for the Agentic Web](\u002Fseries\u002Fai-agents-and-mcp-server-teaming-up-for-the-agentic-web), especially [its MCP server article](\u002Fseries\u002Fai-agents-and-mcp-server-teaming-up-for-the-agentic-web\u002Fmcp-to-provide-additional-capabilities-to-the-ai-agent).\n\nThroughout the series, the **model** generates tool calls or code, the **MCP client** (or agent) runs the model loop, and the **MCP server** provides the capabilities. Keeping these roles separate is important: the host, not generated code, defines which capabilities are available.\n\nThe central idea is simple: with direct MCP tools, the model must call one tool, receive the complete intermediate result, and then use that result to decide how to call the next tool. With Code Mode, the model writes the orchestration as code, the code accesses the capabilities it needs inside the execution environment, and the model receives only the desired final result to continue.\n\nCode Mode is not automatically the right choice. Direct MCP tools remain simpler for small, obvious actions. The extra runtime is worthwhile when a task contains dependent calls, requires local data processing, or needs to navigate a large and evolving API surface, like an OpenAPI specification that can be larger than the model's context window.\n\nThroughout this journey, we will:\n\n1. **Beyond One Tool Call at a Time: Introducing Code Mode**: Build two direct `Drive` tools over a Markdown fixture and observe how the complete file content travels through the model's context before the next tool call.\n2. **Let the Model Orchestrate Tools with a Local Code Runtime**: Replace the two direct tools with one generic `code` tool, expose a typed `Drive` facade, and introduce the executor interface that later parts will replace. The local evaluator based on the `Function` constructor is intentionally a trusted development shortcut, not a production sandbox.\n3. **Searching OpenAPI Specifications Without Filling Context**: Keep the OpenAPI document inside the server, resolve its local `$ref` values, and let generated code search a compact type facade instead of returning the complete specification to the model.\n4. **Calling APIs with Code Mode and Returning Only What Matters**: Add a separate `execute` tool with a host-owned `api.request()` helper. We will validate request options, call the local MarketHub fixture, and return a narrow projection rather than the complete API response.\n5. **Replacing the Evaluator with Isolated Cloudflare Workers**: Swap only the executor boundary for fresh Dynamic Workers. Search will run without outbound access, execute will use an allowlisted gateway, and credentials will remain outside generated code. We will also cover result bounds, resource limits, and host deadlines.\n\nThe companion repository, \u003CGitHubLink repo=\"barbapapazes\u002Fcode-mode\" \u002F>, is an executable textbook for the series. Each article has a complete checkpoint branch, a small evaluation, and a focused diff from the previous step. I recommend that you check out a branch and inspect the implementation for a better understanding of the concepts, as the articles will not cover every detail.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nThis series assumes familiarity with TypeScript, JavaScript promises, HTTP APIs, and basic MCP concepts. The local checkpoints contain no credentials. Parts 2 through 4 use a trusted local evaluator based on the `Function` constructor for development, while Part 5 provides a Cloudflare deployment scaffold and structural security evaluation.\n::\n\n## Chapters\n\n::series-chapters-list\n---\nclass: mx-auto max-w-5xl\n---\n::\n",{"title":5,"description":164},"building-code-mode-for-apis-with-mcp-and-isolated-workers",{"loc":172},"series\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers","Build a context-efficient MCP server for OpenAPI-described APIs",[180,181,182,183,184,185,186],"ai","ai-agents","mcp","code-mode","openapi","cloudflare","typescript","WgDRYOu5CDIYY78O1GSEOv6BKbIsrTVwVf8cTOkdsOA",{"overview":189,"chapters":281},{"id":4,"title":5,"body":190,"contentId":162,"date":163,"description":164,"extension":165,"github":276,"meetTheInstructor":168,"meta":277,"navigation":170,"part":171,"path":172,"rawbody":173,"robots":171,"schemaOrg":171,"seo":278,"seriesSlug":175,"sitemap":279,"stem":177,"subtitle":178,"topics":280,"__hash__":187},{"type":7,"value":191,"toc":273},[192,194,198,202,208,216,218,220,222,258,262,268,270],[10,193,12],{},[10,195,15,196,21],{},[17,197,20],{"href":19},[10,199,24,200,29],{},[26,201,28],{},[10,203,32,204,37,206,42],{},[17,205,36],{"href":35},[17,207,41],{"href":40},[10,209,45,210,50,212,54,214,58],{},[47,211,49],{},[47,213,53],{},[47,215,57],{},[10,217,61],{},[10,219,64],{},[10,221,67],{},[69,223,224,230,240,246,254],{},[72,225,226,77,228,81],{},[47,227,76],{},[26,229,80],{},[72,231,232,87,234,90,236,93,238,96],{},[47,233,86],{},[26,235,26],{},[26,237,80],{},[26,239,28],{},[72,241,242,102,244,106],{},[47,243,101],{},[26,245,105],{},[72,247,248,112,250,116,252,120],{},[47,249,111],{},[26,251,115],{},[26,253,119],{},[72,255,256,126],{},[47,257,125],{},[10,259,129,260,134],{},[131,261],{"repo":133},[136,263,264],{"color":138,"icon":139},[10,265,142,266,145],{},[26,267,28],{},[147,269,150],{"id":149},[152,271],{"className":272},[155,156],{"title":158,"searchDepth":159,"depth":159,"links":274},[275],{"id":149,"depth":159,"text":150},{"name":167,"repository":133},{},{"title":5,"description":164},{"loc":172},[180,181,182,183,184,185,186],[282,289,295,302,309],{"contentId":283,"title":76,"description":284,"path":285,"part":286,"github":287,"topics":171},"6f0c6f18-8d31-4b4d-aea6-9b2df779a0d1","See why dependent direct MCP tool calls make intermediate content part of model context, and how Code Mode moves orchestration into generated code.","\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Fbeyond-one-tool-call-at-a-time-introducing-code-mode",1,{"branch":288},"part-1-direct-tools",{"contentId":290,"title":86,"description":291,"path":292,"part":159,"github":293,"topics":171},"2a9e7d4c-1f6b-4c83-9d52-7e0a6b3f4189","Build a minimal trusted local Code Mode runtime with a typed Drive facade and an executor seam ready for isolation.","\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Flet-the-model-orchestrate-tools-with-a-local-code-runtime",{"branch":294},"part-2-generic-code-mode",{"contentId":296,"title":101,"description":297,"path":298,"part":299,"github":300,"topics":171},"8c4a1e72-bf39-4d65-a08e-2c7f91b6d543","Keep an OpenAPI specification server-side and search a compact typed facade without returning the complete document.","\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Fsearching-openapi-specification-without-filling-the-context",3,{"branch":301},"part-3-search",{"contentId":303,"title":111,"description":304,"path":305,"part":306,"github":307,"topics":171},"1d7b5e93-6a24-4f80-bc19-3e8d0a572f16","Use a host-owned API request helper to validate and execute a narrow API call, then return only the useful projection.","\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Fcalling-apis-with-code-mode-and-returning-only-what-matters",4,{"branch":308},"part-4-local-execute",{"contentId":310,"title":125,"description":311,"path":312,"part":313,"github":314,"topics":171},"5e3a9c71-2b48-4d06-8f95-c7a1e0b6342d","Replace a trusted local Function evaluator with isolated Cloudflare Dynamic Workers, controlled outbound access, and bounded execution results.","\u002Fseries\u002Fbuilding-code-mode-for-apis-with-mcp-and-isolated-workers\u002Freplacing-local-eval-with-isolated-cloudflare-workers",5,{"branch":315},"part-5-dynamic-workers",[317,318,319,320,321],{"contentId":283,"title":76,"description":284,"path":285,"part":286,"topics":171},{"contentId":290,"title":86,"description":291,"path":292,"part":159,"topics":171},{"contentId":296,"title":101,"description":297,"path":298,"part":299,"topics":171},{"contentId":303,"title":111,"description":304,"path":305,"part":306,"topics":171},{"contentId":310,"title":125,"description":311,"path":312,"part":313,"topics":171},1795016665549]