Agentic AI Foundation Logo
Cartoon illustration of Alf the robot swerving a purple car towards a motorway exit at the last second, while Thomas Rooney leans out of the passenger window and points at the exit. The road sign above shows "No code mode" straight ahead and "Code mode" to the right.

Where code mode belongs in the MCP stack

Thomas RooneyNovember 24, 2026

A year ago, we said you don’t need code mode

A year ago this month, we published Reducing MCP token usage by 100x: you don’t need code mode. The argument was that MCP’s context problem could be solved inside the protocol, with progressive disclosure, without building a code execution environment.

A couple months ago, we ended up building code mode anyway. This is the story of where we were right, where we were wrong, and what makes gateways different.

We were right about progressive disclosure, but wrong about where it belongs

Our previous work found that building a server which used progressive disclosure reduced token consumption by 90% and substantially reduced context bloat. However, the server ended up not being the right place for progressive disclosure. The client was much better suited.

Instead of needing to rely on every server to be architected efficiently, it made much more sense for clients to put semantic search in front of the tool catalog. Shortly after that post, Anthropic shipped tool search in the Claude API. You mark a tool, or a whole MCP server, as deferred, and the model only loads its definition once it searches for it. OpenAI, GitHub Copilot and others offer their own versions, and major harnesses support some form of tool search for MCP today.

Diagram comparing server-side search per server versus client-side harness searching across all servers with unified tool indexing.

This raises an obvious question, one David Cramer (Sentry) asked recently:

David Cramer

Max Stoiber, a member of technical staff at OpenAI, agreed:

Max Stoiber

Stoiber argues that an additional search layer inside an MCP server can interfere with tool discovery in harnesses that already support it. At that point, progressive disclosure in the server may be a hat on a hat.

But where does this conversation leave “code mode” MCP servers?

Code mode: the post that launched a thousand agents

Code mode didn’t start with MCP, but MCP gave the idea a much bigger stage. To boil down the idea: AI is really good at writing code, so instead of structured tool calls, let the agent write code. The idea took off in September 2025 when Kenton Varda and Sunil Pai at Cloudflare published Code Mode: the better way to use MCP. They converted an MCP server’s tools into a TypeScript API, gave the agent a single tool that runs code, and executed that code in a sandboxed V8 isolate with no internet access. The only way out of the sandbox was through bindings to the connected MCP servers, so API keys never reached the model’s code.

Diagram showing one agent with two MCP servers: Cloudflare in code mode running sandboxed code and Linear in standard mode executing individual tool calls.

The excitement skipped over some real costs, which is why we were initially skeptical of code mode as opposed to a solution like progressive disclosure:

  • The server provider runs a lot more infrastructure. Running code an agent wrote means isolation, resource limits and monitoring, overhead that direct tool calls avoid. That’s not really a downside for a company like Cloudflare that sells ephemeral environments, but for most server providers it adds considerable complexity.
  • One script is harder to govern than many calls. With direct tool calls, each call is a discrete event you can log, approve or block. A script that makes ten calls in a loop is one opaque step, unless whatever runs it still checks every call against policy.

We maintain that for most companies, building a code mode MCP server adds more complexity than the benefits justify.

Back to the harness

So far this isn't a ringing endorsement for code mode. But much like progressive disclosure, code mode makes much more sense when it doesn't live on a single server. And that's what's starting to happen. Claude Code, pi and OpenCode have all added optional code mode support to their harnesses. It's safe to expect that this will spread across the rest of the popular harnesses.

Why did harnesses add support for code mode if they already had support for progressive disclosure? There's one case where code mode is particularly effective: handling intermediate tool results. Ask an agent to copy 5,000 records from your CRM into your warehouse, and with plain tool calls every record passes through the model on its way from one tool to the next, filling up the context. With code mode, the agent writes a short script that reads from one system and writes to the other, and the records never enter its context. The model only reads what the script returns, like a count, a summary, or the three rows that failed. That's fewer tokens and fewer turns, keeping the context window clean.

Animated diagram showing code mode in an agent harness, where a script transfers 5,000 CRM records to a warehouse, checks permissions on each call, and returns only the result to the model without exposing the records.

The same goes for any task that produces more data than the model needs to read: a search whose results need filtering, a log scanned for one error, or a batch of API calls fanned out in parallel. As the pi docs put it, "Only the script's output reaches the model, so a script can run calls in parallel and filter large results before the model sees them."

Code mode in the gateway

If the harness is the natural home for code mode, why did we add it to our gateway anyway? It's pretty simple: it uses infrastructure the gateway already has, and not every agent runs in a tier-1 harness.

A gateway already sits between an organization's agents and every downstream server: the CRM, ticketing, the data warehouse, internal APIs, etc. It already runs infrastructure, can manage upstream credentials and enforce policy on calls. Adding a sandbox to that is an incremental cost the gateway pays once, and everyone behind it benefits.

Teams building internal agents on raw model APIs, open-source frameworks or their own runtimes usually don't natively get code mode, and often no tool search either. When one of those agents connects to a gateway with thousands of tools behind it, it runs into two problems. Without tool search, its first request loads every tool definition into context. Without code mode, every intermediate result flows back through the model. These agents don't handle either problem by default, so the gateway is the natural place to solve both.

Animated diagram showing an MCP gateway running agent-written code in a sandbox, checking permissions for each call to CRM, warehouse and GitHub servers, logging allowed and denied actions, and returning only the result to the agent.

If your harness and your gateway both support code mode, don't enable both. If the agent runs in a harness with code mode, connect it to the gateway's tools directly and let the harness script them. The gateway still checks every call: each upstream call a script makes goes through the same identity, permission checks and audit logging as a regular tool call. If the harness has no code mode, use the gateway's code mode and keep the agent's tool list small.

That does not remove the risks of executing agent-written code. Prompt injection can influence what a script attempts, and the execution environment still needs isolation, resource limits and restricted network access. Per-call authorization helps govern access to upstream tools, but it is not a substitute for sandbox security.

So, do you need code mode?

If you run a single MCP server, probably not. Expose your tools plainly, write good descriptions, and let the harness handle discovery and code execution. The labs are post-training their models on their own tool search, and a second layer from you can get in the way.

If you run a gateway, it's worth offering. The harness is the best place for code mode, but plenty of agents don't run in a harness that has it. A gateway can apply identity, credential and policy controls to upstream calls from every agent that connects, while also providing code mode for agents whose harness doesn't support it. Just make sure each agent runs code mode in one place.

A year ago we said you don't need code mode. For most servers, that's still true. It belongs in the harness, and in the gateway for every agent whose harness doesn't have it yet.

About Speakeasy

Speakeasy is an AI control plane for securely scaling AI, giving enterprises visibility, security, and governance over agent actions inside an organization. Learn more at speakeasy.com.

Share

Author

subscription section bg
Subscribe

Subscribe to the AAIF Briefing

Weekly signal on standards, governance, and the people building the future. No fluff. Just what matters.

About AAIF