Building Code Mode for APIs with MCP and Isolated Workers
Build a context-efficient MCP server for OpenAPI-described APIs
Hello and welcome! 👋
This series is a practical follow-up to 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.
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 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.
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 AI Agents and MCP Server: Teaming Up for the Agentic Web, especially its MCP server article.
Throughout 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.
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.
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.
Throughout this journey, we will:
- Beyond One Tool Call at a Time: Introducing Code Mode: Build two direct
Drivetools over a Markdown fixture and observe how the complete file content travels through the model's context before the next tool call. - Let the Model Orchestrate Tools with a Local Code Runtime: Replace the two direct tools with one generic
codetool, expose a typedDrivefacade, and introduce the executor interface that later parts will replace. The local evaluator based on theFunctionconstructor is intentionally a trusted development shortcut, not a production sandbox. - Searching OpenAPI Specifications Without Filling Context: Keep the OpenAPI document inside the server, resolve its local
$refvalues, and let generated code search a compact type facade instead of returning the complete specification to the model. - Calling APIs with Code Mode and Returning Only What Matters: Add a separate
executetool with a host-ownedapi.request()helper. We will validate request options, call the local MarketHub fixture, and return a narrow projection rather than the complete API response. - 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.
The companion repository, code-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.
Function constructor for development, while Part 5 provides a Cloudflare deployment scaffold and structural security evaluation.Chapters
1. Beyond One Tool Call at a Time: Introducing Code Mode
See why dependent direct MCP tool calls make intermediate content part of model context, and how Code Mode moves orchestration into generated code.
2. Let the Model Orchestrate Tools with a Local Code Runtime
Build a minimal trusted local Code Mode runtime with a typed Drive facade and an executor seam ready for isolation.
3. Searching OpenAPI Specifications Without Filling Context
Keep an OpenAPI specification server-side and search a compact typed facade without returning the complete document.
4. Calling APIs with Code Mode and Returning Only What Matters
Use a host-owned API request helper to validate and execute a narrow API call, then return only the useful projection.
5. Replacing the Evaluator with Isolated Cloudflare Workers
Replace a trusted local Function evaluator with isolated Cloudflare Dynamic Workers, controlled outbound access, and bounded execution results.
Meet the instructor
My name is Estéban Soubiran.
I'm a web engineer and technical writer who loves sharing with others.
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.
You can find me on X and LinkedIn , or email me at esteban@soubiran.dev .
What do you think?
Let's stay in touch
New articles, practical web development tips, and updates on my open source projects. Straight to your inbox.
Discussions
Loading discussions...
Add a comment
Checking your session...