// Journal · Oct 26, 2026 · 7 min read

Code Mode vs MCP: when agents should write code

Code Mode vs MCP is not a choice between two protocols: Code Mode is a way of using MCP. With classic MCP tool calling, every tool definition sits in the model’s context and the model makes one call per turn, with each intermediate result flowing back through the window. With Code Mode, the same tools are presented as a typed TypeScript API; the model writes one script that chains the calls, filters the data, and returns only the answer, and that script runs in a sandbox. Use direct tool calls for small tool sets, single-step tasks and actions a human must see. Use Code Mode for large API surfaces, multi-step chains and big intermediate data. Most production agents should run both.

We build agents on MCP for the internal tools we ship, and this is the decision we now make on every one of them.

What is the difference between Code Mode and MCP tool calling?

MCP standardises how an agent reaches a tool: discovery, schemas, transport, auth. It says nothing about whether the model should call those tools one at a time.

Most agents do. The client loads each server’s tool definitions into the prompt, the model emits a tool call, the result comes back into context, and the model decides the next call. A five-step task is five model turns, and everything every step returned is still in the window.

Code Mode swaps that loop for code. Cloudflare’s Code Mode: the better way to use MCP (Kenton Varda and Sunil Pai, 26 September 2025) converts an MCP server’s schema into TypeScript declarations with doc comments and gives the model a single tool that executes TypeScript. Their argument in one line: “LLMs have seen a lot of code. They have not seen a lot of ’tool calls’.”

Why direct MCP tool calls get expensive

Two costs grow with every server you connect.

Tool definitions are paid for upfront. Anthropic’s advanced tool use write-up (24 November 2025) measured a five-server setup — GitHub, Slack, Sentry, Grafana, Splunk — at 58 tools and roughly 55,000 tokens before the conversation starts. GitHub alone was 35 tools and about 26,000 tokens.

Intermediate results pass through the model. In Code execution with MCP (4 November 2025), Anthropic’s example is copying a meeting transcript from Google Drive into Salesforce: the transcript flows through context twice, and rewriting the workflow as code took it from about 150,000 tokens to about 2,000. That is Anthropic’s measurement on one workflow, not a general rate — but the direction holds whenever a tool returns more data than the answer needs.

Both problems are context engineering problems. Whatever sits in the window decides cost, latency and accuracy.

How Code Mode works

The model receives a typed API instead of a tool list, writes a script against it, and only what the script logs or returns comes back. Against an orders system, the script might look like this:

// Written by the model against generated, typed bindings for two MCP servers.
const orders = await ordersApi.findOrders({ status: "open", limit: 500 });

const late = orders.filter(
  (o) => Date.now() - Date.parse(o.promisedAt) > 2 * 24 * 60 * 60 * 1000,
);

for (const o of late) {
  await crm.addNote({ customerId: o.customerId, text: `Order ${o.id} is late` });
}

// Only this line re-enters the model's context — not 500 orders.
console.log({ checked: orders.length, late: late.map((o) => o.id) });

Done with direct calls, the 500 orders land in context and the CRM note becomes a turn per late order. As a script, it is one execution.

The sandbox carries the safety. In Cloudflare’s version, each snippet runs in a fresh V8 isolate through the Worker Loader API; global fetch() and connect() throw, so the code can only reach the bindings it was given, and API keys stay with the supervisor rather than in generated code. Anthropic’s variant presents each server as a directory of TypeScript files the agent lists and reads on demand, so definitions load progressively instead of all at once.

Server-side Code Mode: search and execute

The 2026 development is servers doing this themselves. Cloudflare’s MCP server for its whole API (Matt Carey, 20 February 2026) covers more than 2,500 endpoints with two tools, search() over the OpenAPI spec and execute() against an API client. Cloudflare puts that at about 1,000 tokens, against 1.17 million for an equivalent server with one tool per endpoint, measured with tiktoken.

The Dodo Payments MCP server follows the same pattern by default: search_docs to look up SDK methods, and execute, which runs TypeScript against a pre-authenticated SDK client in a sandbox with no network or host filesystem access. Keys are injected server-side and never appear in tool parameters the model sees.

For the agent builder, server-side Code Mode needs no sandbox on your side. The server operator runs it.

Does Code Mode replace MCP?

No. The goose team’s Code Mode doesn’t replace MCP compares the two to HTTP and REST: MCP is the protocol, Code Mode a pattern on top of it. Nordic APIs lands in the same place, calling it “an integration pattern, not a protocol.” Cloudflare’s original post is explicit that MCP still earns its place through uniform connectivity, authorisation and documentation.

The question that is still open is tool design. At AGNTCon + MCPCon Japan on 10 September 2026, Postman’s Ruben Casas gave Do tools still matter? MCP tool design in the age of code mode, comparing curated workflow tools, tool search, and search-and-execute. Those are the three real options, and they are not mutually exclusive.

Code Mode vs MCP: when to use which

Use direct MCP tool calls when

  • The tool set is small. Anthropic’s guidance is that tool search pays off past 10 tools or 10,000 tokens of definitions; below that, plain definitions are fine.
  • The task is one or two calls. The goose team found Code Mode no faster on small tasks, because the model has to discover tools and write code first.
  • The model should see every result. If the reasoning depends on reading each intermediate output, hiding it in a script defeats the point.
  • Each action must be approvable. A discrete issue_refund call can be shown to a human, logged with its arguments, and blocked. A loop inside a script is harder to gate.

Use Code Mode when

  • There are three or more dependent calls. That is Anthropic’s threshold for programmatic tool calling, which cut average tokens on complex research tasks from 43,588 to 27,297 in their tests.
  • Results are large and the answer is small. Anthropic’s example — filtering a 10,000-row spreadsheet down to the five rows that matter — belongs in code, not in context.
  • The API surface is big. Hundreds of endpoints should be search-and-execute, not hundreds of tool definitions.
  • The session has to run long. Less context spent per step means more room before the agent degrades.

The hybrid we default to

For client agents we split by risk, not by fashion. Reads, lookups and aggregation go through an execute-style tool in a sandbox, using a read-only credential. Writes stay as a few named, typed MCP tools that a client can display, log and route through a human review queue. The read path gets Code Mode’s efficiency; the write path keeps per-call auditability.

What Code Mode costs you

Anthropic is direct about it: running agent-generated code “requires a secure execution environment with appropriate sandboxing,” and those requirements “add operational overhead and security considerations that direct tool calls avoid.” Add to that harder debugging of generated code, a dependency on one language runtime, and tooling that is still moving — Cloudflare’s @cloudflare/codemode package is marked experimental, and Dodo’s local sandbox currently requires Deno 2.8 or earlier. Pin versions and budget for it.

If you are building the server side, the advice from how to build an MCP server for an internal tool still holds: few tools, least privilege, writes behind a person. Code Mode changes how the model calls those tools, not who is allowed to do what.

Havoric is an AI automation and web development agency: we automate repetitive manual processes and build the web and mobile apps around them. Choosing between direct tool calls and Code Mode is now a routine design decision in our AI automation work, and we make it per tool, by risk.

Common questions

What is the difference between Code Mode and MCP?
MCP is the protocol that connects an agent to tools. Code Mode is a way of using it: instead of loading every MCP tool definition into the model’s context and calling one tool per turn, the tools are presented as a typed TypeScript API, the model writes one script that chains the calls and filters the data, and the script runs in a sandbox that returns only the result.
Does Code Mode replace MCP?
No. Code Mode still calls MCP tools underneath and still relies on MCP for connectivity, authorisation and tool documentation. It changes how the model invokes tools, not the protocol that exposes them.
When should you use direct MCP tool calls instead of Code Mode?
When the agent has a small tool set, the task is one or two calls, the model needs to reason about every intermediate result, or each action must be individually visible and approvable — writes that move money or message customers, for example. Direct calls need no sandbox and are easier to audit.