← All writing

AI Engineering

Code Mode for MCP: Two Tools Instead of 2,594

Mani G 3 min read

Swiss-style poster art of a towering stack of paper manuals casting a long shadow beside a small brass key and a magnifying glass.

Picture connecting an agent to a handful of MCP servers and asking it a simple question. Before it reads a word of your request, it has already chewed through thousands of tokens of tool descriptions. It's like hiring a contractor who insists on reading the full catalogue of every hardware shop in town before hanging a picture. Code Mode is the fix I keep coming back to, and the numbers behind it are silly in the best way.

The problem Code Mode fixes

Most MCP clients load every tool definition into the model's context up front. Each tool brings a name, a description and a JSON schema. Then every tool result also flows back through the model, even when the model only needs one field from it. Anthropic's engineering team described both problems in November 2025: tool definitions crowding the context, and intermediate results eating tokens twice as they pass through.

What Code Mode actually is

Cloudflare's idea, first published in September 2025, is that models are better at writing code than at making long chains of tool calls. So instead of exposing tools one by one, you give the model a typed API and let it write a short program. The program runs in a sandbox, does the loops and the arithmetic, and returns only the final answer.

Cloudflare then pushed it further with its own API. The Cloudflare API has more than 2,500 endpoints. Exposing every endpoint as a native MCP tool would cost about 1.17 million tokens of definitions, according to Cloudflare's docs. Its Code Mode server exposes two tools, search() and execute(), for about 1,000 tokens total. The agent writes JavaScript to search the OpenAPI spec for the endpoints it needs, then writes more JavaScript to call them. Both run inside a Dynamic Worker isolate, a lightweight V8 sandbox with no file system and outbound fetches off by default.

A quick example

Say you want every DNS zone whose name ends in .dev, plus a count of records in each. The old way is a tool call to list zones, a pile of JSON in context, then one call per zone. In Code Mode, the agent writes one async function that lists zones, filters them, loops with Promise.all to fetch records, and returns a five-line summary. The giant JSON never touches the model.

Who else does this

The pattern spread fast, but not as one standard. Goose, the open-source agent from Block, ships a Code Mode extension with three tools: list_functions, get_function_details and execute_javascript. TanStack AI's version, announced in April, gives the model a single execute_typescript tool and runs it in an isolate. TanStack's pitch includes my favourite line on the topic: LLMs are hilariously bad at math, so let a runtime do it.

So, about the "99% fewer tokens" claim floating around. It depends on what you compare. Cloudflare's first experiment, converting one MCP server into a TypeScript API, cut tokens by 81%. Anthropic's own example went from 150,000 tokens to 2,000, a 98.7% saving. The Cloudflare API case is well past 99%, because the alternative was absurd. Good results, but a range, not a single magic number.

One honest caveat from Anthropic's post: running model-written code needs real sandboxing, resource limits and monitoring. Code Mode trades token cost for infrastructure work. If you want a deeper look at keeping that code contained, my post on agent sandboxes covers the heavier VM end. Here's the stat that made me laugh: a native MCP version of the full Cloudflare API would be larger than the context window of most models. How big is your agent's tool list before you say hello?