Mcp Vs Code Mode Comparison

MCP vs Code Mode: How AI Agents Cut Tool Tokens by Writing Code

Agents can call tools one at a time, or write a short program that calls them. The difference shows up on the token bill.

TL;DR: Code mode keeps MCP as the connection standard but lets an agent write code against it, so fewer tool definitions and results pass through the model.

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.

  1. A client reads the tool schemas from each connected MCP server.
  2. Those schemas become typed functions, grouped by server.
  3. Next, the model gets a way to browse or search the functions, plus a tool that runs code.
  4. For each task, the model writes a short script that calls the functions it needs.
  5. 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

QuestionDirect MCP tool callsCode mode
What the model loadsEvery tool definitionA few entry points, then only what it opens
How tools runOne call per model turnA script that can make many calls
Where results goBack through the modelStay in the sandbox unless returned
Loops and conditionsDecided by the model each turnWritten as normal code
What the team must runAn MCP clientAn MCP client and a secure code sandbox
Best fitA few tools, simple actionsMany 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.

Read the savings claims carefully

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.

Frequently Asked Questions

Does code mode replace MCP?
No. Code mode still uses MCP servers; it changes how the model calls their tools, by writing code instead of issuing one tool call at a time.
How much can code mode save in tokens?
Anthropic cites one example falling from 150,000 to 2,000 tokens. Real savings depend on how many tools are connected and how large the results are.
Do developers need a sandbox for code mode?
Yes. Running model-written code safely needs an isolated environment with limits, monitoring, and tight control of network access.
When are direct MCP tool calls the better choice?
Direct calls suit agents with a handful of tools, small results, and actions that people want to see and approve one by one.

Share this
Waqas Ahmad

About Author

Waqas Ahmad

Waqas Ahmad is a technology writer at Saganote and a software professional with more than a decade of experience in the software industry. He primarily writes about artificial intelligence, AI products, developer tools, software development, and emerging technologies.