AI agents · Deep dive

Is MCP being replaced by "Code Mode"?

I kept seeing the same idea: AI companies are moving away from MCP, the standard way agents use tools, toward a new design called Code Mode. I got curious, so I read the original Cloudflare and Anthropic posts. Then I measured it myself on 150 real tools.

The problem: tools fill up the model's memory

An AI model can only "see" a limited amount of text at once, called its context window. Everything it reads costs money and time, measured in tokens (a token is roughly three quarters of a word).

MCP, the Model Context Protocol, is the open standard most AI apps use to connect a model to tools like GitHub, Google Drive or a company database.5 The usual way to use it causes two kinds of waste:

1. Every tool's instructions sit in context the whole time. Each tool comes with a written description and a list of inputs. Connect a few servers and that's hundreds of tools, all read again on every step, even though a task uses two of them.

2. Every result passes back through the model. Say the agent looks up a customer, then fetches their orders. The full customer record goes into the model. The model copies the ID into the next request. Then the full order history goes into the model too, just so it can add up some numbers.3

What Code Mode does instead

In September 2025, Cloudflare described a different setup. Instead of giving the model a list of tools, it turns the tools into a TypeScript programming interface. The model gets one tool: "run this code". It writes a short script that calls the tools, and the script runs in a locked-down sandbox with no internet access. Only what the script prints comes back to the model.1

Two months later, Anthropic published the same pattern, which it calls code execution with MCP. Its version adds one key step: the tools are stored as files, and the model opens only the ones it needs. Anthropic reports one example dropping from 150,000 tokens to 2,000.3

Here's the script from my test below. The model writes this, and the sandbox runs it:

The 30 orders it fetches never reach the model. Only the last line does:

The common claims, checked against the sources

This is how Code Mode usually gets summed up. I went back to the original posts for each part.

MisleadingAI companies are abandoning MCP for a new architecture.

Code Mode still runs on MCP servers. Cloudflare's agent toolkit reads the MCP server's tool list and converts it to code.1 Anthropic's post is about making MCP agents more efficient, not replacing MCP.3 What changes is what the model sees.

TrueWith usual tool calling, every result goes back through the model, and every tool definition stays in context.

These are exactly the two problems Anthropic names: tool definitions overloading the context, and intermediate results flowing through the model.3

PartlyCloudflare built Code Mode because thousands of tools broke the usual approach.

Cloudflare's first post (Sept 2025) gives no numbers and doesn't describe hitting a wall.1 Its later post (Feb 2026) does: its API has over 2,500 endpoints, and listing each as a tool would take about 1.17 million tokens. Its Code Mode server takes about 1,000.2

TrueIn Code Mode the model sees one tool, "run code", and its script runs in a sandbox.

True for the 2025 design. The sandbox is a V8 isolate (the engine inside Chrome), with no general internet access; API keys stay outside it.1 Cloudflare's 2026 API server uses two tools, search() and execute().2

TrueIntermediate results never reach the model, only the final answer.

By default, yes. Only what the script prints is returned.1,3 If the script prints everything, you're back where you started.

Their argumentModels are better at writing code than at calling tools.

Both companies say this. Cloudflare's reason: models have seen huge amounts of real code in training, but very few tool calls. Its post gives no benchmark for it, though.1,3

TrueAnthropic cut one example from 150,000 tokens to 2,000 with the same pattern.

It's in Anthropic's Nov 4, 2025 post, a 98.7% saving, for a task that moves a meeting transcript from Google Drive into Salesforce. The post doesn't say how it measured the 150,000.3 So I measured my own.

Demo 1

How much do tool instructions cost?

I took the 150 real tools in GitHub's official MCP server4, plus 2 customer-database tools, and counted the tokens each setup puts in front of the model before it reads your question. For more than 152 tools I reuse the real GitHub tools under new names. Pick a number of tools.

Demo 2

Watch one task fill the context

The task: "How much did Priya Shah spend in the last 90 days? Exclude refunded orders." The agent has all 152 tools connected.

0Model calls so far
0Tokens in context now
0Input tokens billed, total
What the model is reading right now

The model is re-read from the top on every call, so the tool list is paid for again each time. In the usual setup, the 30-order history (3,317 tokens) also lands in context, and the model has to add 26 prices in its head. In Code Mode, the sandbox does the adding, and the model reads one line.

My results

All numbers are input tokens counted with cl100k_base, the same tokenizer family Cloudflare used.2 Claude counts tokens a little differently, so trust the ratios more than the exact counts.

Setup (152 tools, the task in Demo 2)Model callsInput tokensSaved vs usualOutput tokensData passed through model
Usual tool calling3103,632–733,725
Code Mode, whole API as TypeScript256,73245.3%16019
Code Mode, load only needed tools43,43696.7%19119

The headline number holds up. Code Mode with on-demand loading cut input tokens by 96.7% (103,632 → 3,436). That's close to Anthropic's 98.7%. At about 220 tokens per GitHub tool, usual tool calling would pass 150,000 tokens of instructions alone at roughly 680 tools.

But TypeScript isn't the trick. Rewriting all 152 tool definitions as TypeScript only shrank them by 14.7% (33,119 → 28,264 tokens). The big saving comes from two other things: the model loads only the 2 tool files it needs (96.8% smaller), and big results stay in the sandbox (3,725 → 19 tokens). Cloudflare's first post admitted it still loaded the whole API into context, and said searching it was future work.1 Its 2026 server added that search.2

The answer was right. The script's total, $15,146.20 across 26 orders, matched the true total I computed separately.

The catch

You have to run code an AI wrote. That needs a secure sandbox with limits and monitoring. Anthropic says plainly that this adds work and security risks that plain tool calls avoid.3

The model writes more. Output went from 73 tokens to 191, about 2.6×, because the model writes a script instead of short tool calls. Output tokens usually cost several times more than input tokens, though here the input savings are far larger.

Not always fewer steps. On-demand loading took 4 model calls here instead of 3: list the tools, open two files, run the script, answer. For short tasks that can mean more waiting. The step savings show up on longer tasks with loops.

Caching softens the old way. Many AI providers cache repeated text like tool lists, which makes it cheaper to re-read. But cached tokens still take up room in the context window.

Why I care

I built an LLM inference server from scratch, with continuous batching and a paged KV cache. On the serving side, every token in context costs real compute to read in and real GPU memory to hold while the model answers. A tool list re-read on every step is waste you pay for on every request. Code Mode is a design change on the agent side that removes most of that load from the server.

You can rerun my test. The script and raw results are in their own repo: mcp-code-mode-experiment. It reads the tool files from GitHub's MCP server (commit 9a72d8d, Nov 20, 2026) and runs the script in a Node sandbox. The customer and order data are made up, from a fixed random seed.

FAQ

Are companies abandoning MCP?

No. Code Mode still uses MCP servers to reach the tools. It changes what the model sees: a code interface it writes scripts against, instead of a list of tools it calls one at a time.

Is the 150,000 → 2,000 figure real?

Yes. It's in Anthropic's Nov 4, 2025 engineering post, for one example task. The post doesn't explain how the baseline was measured. My own test on 152 real tools got a 96.7% drop.

Where do the savings actually come from?

Mostly from loading tool instructions only when needed and keeping large results out of the model. Converting tools to TypeScript saved only 14.7% in my test.

What's the downside?

You need a secure sandbox to run AI-written code, and the model writes more output. In my test, output went from 73 to 191 tokens.

Should I switch my agent to Code Mode?

If it has many tools or moves large data between them, the savings are big. If it uses a handful of tools with small results, the sandbox may not be worth the extra work.

References

  1. K. Varda, S. Pai. Code Mode: the better way to use MCP. Cloudflare blog, Sept 26, 2025. blog.cloudflare.com/code-mode
  2. Cloudflare. Code Mode: give agents an entire API in 1,000 tokens. Cloudflare blog, Feb 20, 2026. blog.cloudflare.com/code-mode-mcp
  3. Anthropic. Code execution with MCP: Building more efficient agents. Anthropic Engineering, Nov 4, 2025. anthropic.com/engineering/code-execution-with-mcp
  4. GitHub. GitHub MCP Server, tool definition snapshots. Commit 9a72d8d, Nov 20, 2026. github.com/github/github-mcp-server
  5. Model Context Protocol. Official documentation. modelcontextprotocol.io
About me

Devanshu Dixit is a computer science graduate student and research assistant at Virginia Tech. I build machine learning to catch telecom and bank fraud, including SIM-farm detection on a 5G testbed I built, and I built an LLM inference server from scratch. Before grad school I was a data engineer at Novvum and a founding-team software engineer at Sprect.