ContextBolt SEO Free for 7 days. Keywords, SERPs, backlinks and AI visibility, inside Claude. SEO data inside Claude. Start free trial
Technology · Code Mode MCP

Code Mode MCP: What It Fixes and When It Hurts

Code mode is a way of handing an agent your MCP tools. Instead of showing the model a list of tools and asking it to pick one, you show it a TypeScript API and ask it to write code. The code runs in a sandbox, and only the answer comes back.

The savings are real. Cloudflare says it cut the cost of exposing an API of more than 2,500 endpoints from 1.17 million tokens to roughly 1,000. Anthropic says a Google Drive to Salesforce job went from 150,000 tokens to 2,000.

They’re also the best cases. Anthropic’s own tool documentation says standard tool calling is the better fit below ten tools, and that line rarely gets quoted next to the 98.7% figure.

Quick answer
  • Code mode swaps tool calling for code writing. Your MCP tools become a TypeScript API and the model writes JavaScript against it in a sandbox.
  • The savings are real at scale. Cloudflare reports 1.17 million tokens down to about 1,000 for a 2,500-endpoint API.
  • Under ten tools it is a net loss. Anthropic’s tool search docs recommend plain tool calling below that line.
  • You trade a named, reviewable tool call for a block of code, so trusting the server matters more than the transport. ContextBolt SEO is hosted and only ever reads data, it never changes your site.

What is code mode MCP?

Code mode is a pattern where an agent writes and runs code that calls your tools, instead of calling those tools directly. Cloudflare coined the name in a September 26, 2025 post arguing that models are better at writing code to call MCP than at calling MCP directly.

The reasoning is training data. A model has read millions of lines of real code that calls real APIs, and a much smaller pile of contrived tool-call examples.

Mechanically, the runtime reads your MCP server’s schema, generates TypeScript type definitions with doc comments, and hands the model those definitions plus one tool that executes code. The model writes a snippet, the snippet runs in a disposable V8 isolate, and whatever it returns is what enters the conversation.

That last part is the whole trick. In normal tool calling, every result passes through the model. Pull 800 rows to find 3, and all 800 land in the context window. In code mode the filtering happens in the sandbox, and three rows come back.

How much does code mode save?

The published figures measure different things, so they differ a lot.

SourceBaselineAfterWhat was measured
Cloudflare, Feb 20, 20261.17M tokens~1,000 tokens2,500+ Cloudflare API endpoints exposed as tools
Anthropic, Nov 4, 2025150,000 tokens2,000 tokensA meeting transcript moved from Drive into Salesforce
Anthropic tool docs~55,000 tokens85%+ lessFive servers loaded at once, GitHub through Splunk

Read the last column first. Cloudflare’s 1.17 million baseline is what you’d spend loading 2,500 endpoints as separate tool definitions. Nobody shipped that, so it’s the cost of something code mode makes possible, not a setup people fixed.

Anthropic’s 150,000 is closer to a real workload. Moving a long transcript from one system to another sends it through the model twice for no reason. The Anthropic engineering post describes it as a 98.7% saving in time and cost.

The 55,000 figure is the most useful, because it’s what a working developer’s setup costs. Five connected servers, nothing exotic, and a third of a 200k context window is gone before anyone types a word.

Why does writing code beat picking a tool?

Several gains arrive together, and they get conflated.

Fewer definitions in context: the model sees one execute tool instead of two hundred named ones. That’s a load-time saving.

Smaller results coming back: the sandbox filters, sorts, joins and counts before anything reaches the model. On data-heavy work that’s a much larger saving than the first.

Control flow in one turn: a loop over 50 records is one code block, not 50 round trips.

There’s a fourth benefit. Data the model never sees can’t leak into a transcript. Anthropic points out that intermediate results stay in the execution environment, so personal data can be processed and even tokenized without entering the context window. If you’ve refused to connect a production database to an agent because of what would end up in the logs, that’s the argument that should move you.

What code mode costs you

Most of the costs aren’t about tokens.

You lose the reviewable prompt. With standard tool calling, the approval dialog says the agent wants to run delete_project with these arguments, and you can read that in a second. With code mode it shows fifteen lines of TypeScript, and you have to audit code under time pressure to decide whether to click yes.

Error handling gets vaguer. A failed tool call returns a specific error the model can reason about. A failed code block returns whatever the sandbox says, which is often generic.

You now run a sandbox. Anthropic says this in the same post that reports the 98.7% saving. Code execution needs secure isolation, resource limits and monitoring. Cloudflare’s implementation blocks outbound network access by default and gives the isolate no filesystem.

It doesn’t do your authorization for you. Cloudflare’s own documentation states that code execution does not replace authorization, and that permissions must still be enforced inside the upstream tool handlers. Sandboxing the code doesn’t sandbox the credentials it’s allowed to use.

Anthropic’s tool search tool attacks the same problem from the other end. You send every tool definition as usual, mark the rarely used ones with defer_loading: true, and the model searches for what it needs instead of carrying all of it. The docs report over 85% less context spent on definitions, with only the 3 to 5 relevant tools loaded on demand.

Fewer toolsTool searchCode mode
Fixes definition bloatYesYesYes
Fixes large tool resultsNoNoYes
Keeps the reviewable tool callYesYesNo
Needs a sandboxNoNoYes
Work to adoptDesign workOne flag per toolNew infrastructure
Best atUnder 10 tools10 to 1,000+ toolsData-heavy pipelines

Work through it right to left. Cut the tools you don’t need, starting with the servers that cost the most tokens in our seven-server test. If you still have too many, defer-load them. If the problem is the size of what comes back rather than the tool list, code mode is the one that helps.

When is code mode worth it?

Reach for it when at least one of these is true:

  • You’re aggregating many servers and the tool count runs into the hundreds. Tool selection accuracy starts degrading past 30 to 50 tools, per Anthropic’s own measurements.
  • Your workflow moves large payloads between systems. Transcripts, exports and log files, where the model is acting as a very expensive pipe.
  • You need real control flow, such as loops over records, joins across two systems, or conditional retries.
  • Sensitive data must not enter the transcript.

Skip it when your server and its results are small. Anthropic’s guidance is specific: standard tool calling is the better fit below 10 tools, when every tool gets used on every request, or when the definitions total under 100 tokens.

Most MCP servers don’t have this problem. They have the problem of being installed alongside eleven others. The 55,000-token figure is a fact about someone who connected GitHub, Slack, Sentry, Grafana and Splunk and left all five switched on. The fix that costs nothing is turning off the four you aren’t using today. A tool count is a design decision too, and a server with 47 tools is usually one that never decided what it was for.

We build MCP servers, so take ours as the example. ContextBolt SEO covers keyword research, rank and backlink data, page and site audits, AI visibility checks and Search Console, so by tool count it sits where code mode is supposed to start paying off. It still wouldn’t. Code mode would add a sandbox, remove the readable approval prompt, and save tokens of definitions that were never the bottleneck.

How to try it

Cloudflare ships the pattern in its Agents SDK, in two shapes. codeMcpServer() wraps an existing MCP server so the model gets generated TypeScript definitions plus a single execute tool. openApiMcpServer() handles the very large case, exposing search() and execute() against an OpenAPI document, which is the configuration behind the 1,000-token claim.

On the Anthropic side, the lighter move is tool search. Add tool_search_tool_regex_20251119 or the BM25 variant to your tools array, set defer_loading: true on everything you don’t need immediately, and leave your three to five most-used tools loaded. If your tools arrive through the MCP connector, that flag goes on the toolset’s default_config instead of on individual definitions.

Measure before and after. The published numbers span three orders of magnitude because everyone measures a different baseline, and yours is the only one that decides anything.

Where next

Frequently asked questions

Something else on your mind? Get in touch.

What is code mode in MCP?

Code mode converts an MCP server's tools into a TypeScript API, then asks the model to write JavaScript against that API instead of calling tools one at a time. The code runs in a sandbox. Only the final result enters the model's context window.

How much does code mode actually save?

Cloudflare reports its 2,500-endpoint API dropping from 1.17 million tokens to roughly 1,000. Anthropic reports a two-server data pipeline dropping from 150,000 tokens to 2,000. Both figures come from very large tool surfaces, so a small server will see nothing like them.

Is code mode better than the tool search tool?

They fix different halves. Tool search shrinks the tool definitions loaded up front. Code mode shrinks the results coming back. If your problem is a bloated tool list, start with tool search. If your problem is huge intermediate data, code mode is the one that helps.

Is code mode safe?

It runs model-written code, so it needs a real sandbox. Cloudflare uses V8 isolates with outbound network access blocked by default, and its docs state plainly that code execution does not replace authorization. You still enforce permissions inside the upstream tool handlers.

Do I need code mode for my own MCP server?

Probably not. Anthropic's tool search documentation says plain tool calling is the better fit when you have fewer than 10 tools, when every tool is used on every request, or when the definitions total under 100 tokens. Most single-purpose servers sit well inside that. See what is MCP for the baseline.