Code Mode MCP server

Code Mode MCP keeps the same MCP servers but changes how an agent uses them: instead of calling tools one by one, the model writes a small program that calls the tools, and that program runs in a sandbox. The payoff is far fewer tokens on tool-heavy tasks — Anthropic reported one workflow dropping from about 150,000 to 2,000 tokens.

The name comes from Cloudflare, which called the pattern “Code Mode”; Anthropic published the same idea as “code execution with MCP”. Both start from one observation: LLMs are better at writing code than at making long chains of tool calls.

How classic MCP works

In the classic setup, the MCP client loads every tool’s name, description and JSON schema into the model’s context. The model then calls tools directly, one call per step.

  1. The client lists all tools from all connected servers and puts their definitions in the prompt.
  2. The model picks a tool and emits a call with arguments.
  3. The full result comes back into the context.
  4. The model reads it, decides the next call, and repeats.

This is simple and works well with a handful of tools. It breaks down at scale in two ways:

  • Definition bloat. At roughly 150 tokens per tool, 1,000 tools cost about 150,000 tokens before the agent reads your request.
  • Intermediate results. Every result passes through the model, even when it only needs to be handed to the next tool. A 50,000-token document fetched from one tool and copied into another is paid for twice.

How Code Mode works

Code Mode turns MCP servers into a code API. The model sees one or two generic tools instead of hundreds, and writes code against typed functions.

  1. The client converts each server’s tool schemas into typed functions (Cloudflare generates TypeScript signatures).
  2. The model discovers only the functions it needs — for example by searching or browsing a file tree — rather than loading them all up front.
  3. The model writes a script that calls those functions, with loops, filters and conditionals.
  4. The script runs in a sandbox. Only the final, filtered output returns to the model.

URL Pattern sample

https://<subdomain>.<domain>/mcp?codemode=search_and_execute

A small example of the difference:

// Classic: 31 separate tool calls, each result read by the model
// Code Mode: one script
for (const day of daysInMonth) {
  await calendar.createEvent({ date: day, title: "Standup" });
}

Cloudflare’s own API server shows how far this goes: it exposes just two tools, search() and execute(), to cover more than 2,500 endpoints. Its MCP server portals now run in Code Mode by default, exposing a single code tool and keeping credentials out of the model’s context.

Side by side

AspectClassic MCPCode Mode MCP
What the model seesEvery tool’s full schemaOne or two generic tools; functions loaded on demand
How it actsOne tool call per stepWrites a script that calls many tools
Intermediate dataFlows through the context windowStays in the sandbox; only results return
Loops and filteringDone by the model, call by callDone in code (for, if, map, filter)
Token costGrows with tool count and data sizeRoughly flat; large savings on batch tasks
Output tokensLowerHigher, since the model writes code
InfrastructureJust client + serverNeeds a secure sandbox with limits and monitoring
DebuggingEach call is visible and auditableMust inspect generated code and its logs
CredentialsPassed via the clientCan be kept outside the model entirely

Reported savings vary by task: Cloudflare measured 32% fewer tokens on a single event and 81% on a 31-event batch; one independent benchmark found 78.5% fewer input tokens but 2.2× more output tokens, for about 70% lower cost overall.

When to use which

Pick classic MCP for small toolsets and simple, few-step tasks; pick Code Mode once you have dozens of tools, large payloads, or repetitive batch work.

  • Classic MCP: under ~20 tools, short interactions, environments where you cannot run untrusted code, or where every call must be individually approved.
  • Code Mode: many servers or hundreds of endpoints, data that should be filtered before the model sees it, multi-step workflows, and teams that already run sandboxes.

Neither replaces MCP itself. Code Mode is a different way for the client to use the same servers, so you can adopt it without rewriting them.

Sources