MCP vs code mode is a choice about how an AI agent uses its tools, not about which protocol it speaks. Both rely on the Model Context Protocol, the shared standard that connects models to outside services. Direct MCP use hands the model every tool and lets it call them one by one. Code mode asks the model to write a small program that calls those tools, then runs that program in a sandbox.
Cloudflare put the name Code Mode on the idea in September 2025. Anthropic described the same pattern that November, under the heading of code execution with MCP. Both companies were answering one complaint: an AI agent with many connected servers spends a large share of its budget before it reads the actual request.
Direct Tool Calls Fill the Context Window Twice
Every connected tool arrives with a name, a description, and a schema. Most MCP clients load all of them up front, and each one takes space in the context window. Anthropic's post says agents wired to thousands of tools can burn hundreds of thousands of tokens that way. Developers adding one more server from a list of useful MCP servers pay that cost on every request, whether the agent uses the server or not.
Results cause the second leak. A model that copies a document from one system into another reads the whole text, then writes it out again as the next tool input. Anthropic's example is a meeting transcript moving from Google Drive to Salesforce, where a two-hour meeting could add about 50,000 tokens. Long files raise a second risk. They can overflow the window or arrive with copying mistakes.
Code Mode Turns Tools Into a Programming Interface
Code mode changes what the model sees. Instead of a long menu of tools, the agent gets code it can import, much as a developer imports a library. Cloudflare converts a server's schema into a TypeScript API. Anthropic lays tools out as files in folders, so the agent opens only the ones a task needs.
- A client reads the tool schemas from each connected MCP server.
- Those schemas become typed functions, grouped by server.
- Next, the model gets a way to browse or search the functions, plus a tool that runs code.
- For each task, the model writes a short script that calls the functions it needs.
- A sandbox runs the script and sends back only what the script prints or returns.
Here is the shape of such a script. Names below are invented for illustration, and real function names come from the connected servers.
import * as crm from "./servers/crm";
import * as docs from "./servers/docs";
const transcript = await docs.getDocument({ id: "meeting-notes" });
await crm.updateRecord({
id: "account-42",
notes: transcript.content,
});
console.log("Saved", transcript.content.length, "characters");Only one line of output returns to the model. Meeting text travels from one system to the other inside the sandbox, so its text never enters the conversation. A developer who has built a first agent will recognise the difference: the model plans the work, and ordinary code carries the data.
MCP vs Code Mode Differs Most in What the Model Reads
| Question | Direct MCP tool calls | Code mode |
|---|---|---|
| What the model loads | Every tool definition | A few entry points, then only what it opens |
| How tools run | One call per model turn | A script that can make many calls |
| Where results go | Back through the model | Stay in the sandbox unless returned |
| Loops and conditions | Decided by the model each turn | Written as normal code |
| What the team must run | An MCP client | An MCP client and a secure code sandbox |
| Best fit | A few tools, simple actions | Many tools, large results, multi-step jobs |
Control flow is where the gap widens. A script can wait for a message, retry a failed request, or filter 10,000 spreadsheet rows down to five before the model sees any of them. Anthropic uses that spreadsheet case in its own post. Direct calls would push every row through the model first.
Token Savings Are Large, but the Headline Numbers Are Examples
Anthropic reports one case where usage fell from 150,000 tokens to 2,000, a 98.7% saving. Its post does not describe a benchmark behind the figure, so the number shows what is possible rather than what every agent will get. Cloudflare's later figure is bigger. Its code mode server covers more than 2,500 API endpoints with two tools, search and execute, in about 1,000 tokens.
Cloudflare compares its server with a traditional one at 1.17 million tokens in the article body, and elsewhere says more than 2 million. Both are estimates for one very large API, not for a typical agent.
Teams can estimate their own case with a simple count. Add up the tokens in the tool definitions, then add the size of the results that only pass from one tool to another. Multiply by the requests per day and by the rates in an AI API price comparison. Small agents with five tools may find the saving too minor to justify new infrastructure. Fable 5.2 pricing shows why per-task cost can move differently from the per-token price.
Running Model-Written Code Brings New Duties
Code written by a model needs a locked room to run in. Anthropic lists sandboxing, resource limits, and monitoring as the price of the approach, none of which a plain tool call requires. Cloudflare runs each script in a fresh V8 isolate on its Workers platform, with no file system and outside network access switched off by default. Firms weighing that design may also watch how Cloudflare's Deno acquisition affects running Workers on their own hardware.
Secrets get a different treatment too. Cloudflare's design hands the script a binding instead of an API key, and the supervising code adds authorisation on the way out. Anthropic also sketches a client that swaps personal details for placeholders, so the model handles a token such as [EMAIL_1] while the real address moves between tools. Neither idea removes the need to review what the script can reach. Sandbox choices are becoming a product of their own, as Vercel's Eve and Sandbox release showed.
- Limit what each script can call, and block open network access.
- Cap run time and memory so a looping script cannot run forever.
- Log every tool call the script makes, not only its final answer.
- Ask for approval before actions that spend money, send messages, or delete data.
Direct MCP Calls Still Suit Small, Simple Agents
Plenty of agents never hit the problem. A support bot with three tools and short answers gains little from a sandbox. Consumer products such as Google Home's MCP support deal in small commands, where one direct call is easy to check. Apple's Safari MCP server and Meta's WhatsApp Business server show how many vendors now publish tools, which is exactly why the tool list on a busy client keeps growing.
Searching for tools is a middle path. Some clients, Claude Code among them, load a tool's details only after a search finds it, which trims the menu without asking the model to write programs. Results still pass through the model with that method. Developers who already package repeat work as Claude Code skills can go a step further, since Anthropic notes that a working script can be saved and reused as a skill.
My reading of the two company posts is that MCP vs code mode will rarely be an either-or decision in practice. A client can keep a few safety-critical actions as direct calls, where each one is visible and easy to approve, and move bulk data work into scripts. Background products such as Claude Duet and plugin-based runtimes such as DeepSeek Harness point toward agents with ever more tools, and that is the setting where code starts to pay off.
Cloudflare's own post names one gap that remains open. Connecting several servers still adds context pressure, and the company pointed to a gateway product as its planned answer. Wider adoption of standards such as the Universal Commerce Protocol would add yet more tools for agents to sort through.
