Skip to content
Naditu
Get started

MCP Code Mode in Claude Code and Cloudflare explained

How Cloudflare and Claude Code MCP Code Mode shrink 2,594 tool schemas into 2 tools, and why scripts answer what you asked while missing unasked anomalies.

Naditu3 min read

Anyone scaling Model Context Protocol connectors past a handful of tools hits the same wall: schema bloat and multi-turn round-trip tax. Over the past year, both server builders and harness teams converged on a major architectural shift known as MCP Code Mode in Claude Code and Cloudflare. Instead of stuffing hundreds of JSON tool definitions into the prompt and reading every intermediate API payload line by line, the model writes a short JavaScript or Python program that calls tools as typed functions inside a sandbox and returns only the final shaped answer. Understanding where Code Mode saves 99 percent of context tokens, and what it quietly gives up on operational records, is essential for enterprise AI architects.

Why classic MCP tool calling hits a context wall at enterprise scale

In What is an AI agent harness? we looked at how an agent loop feeds tool definitions and tool results back into the context window on every turn. Two published measurements show how fast classic MCP breaks down on large APIs:

  • The definition footprint (Cloudflare, February 2026): exposing the full Cloudflare API as native MCP schemas requires 2,594 tools consuming 1,170,523 tokens (measured with tiktoken), or 585% of a 200,000-token context window before the user types a single word. Replacing those schemas with two Code Mode tools (search over the pre-resolved OpenAPI spec and execute in a sandboxed Worker) drops the footprint to 1,069 tokens, a 99.9% input-token reduction.
  • The intermediate payload tax (Anthropic, November 2025): copying a meeting transcript from Google Drive into Salesforce via classic tool calls forces the model to read and rewrite the entire document across turns. Moving that loop into code execution cut token usage in Anthropic's example from 150,000 tokens to 2,000 tokens, a 98.7% saving.

Server-side Code Mode versus client-side REPL in Claude Code

Code Mode now appears in two distinct places across the stack, and across adjacent harnesses like Browser Use 1.0 (which deleted 90 percent of its old library in favor of a Python REPL over raw CDP), Unharnessed, and Prime Agent:

  • Server-side sandboxes (Cloudflare Workers): generated JavaScript never receives API credentials, outbound fetch() and connect() are blocked by default, and connector calls cross the sandbox boundary strictly through Workers RPC.
  • Client-side harness REPL (Claude Code v2.1.323+): enabled via CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1, Claude Code exposes REPL and Poll so the model writes JavaScript that chains file reads, shell commands, and MCP calls in one turn while variables persist in memory across turns (up to 10 minutes when waiting, or 2 hours in background mode). For teams running this inside corporate networks, see our companion guide on self-hosted Claude Managed Agents architecture and limits.
Code Mode keeps intermediate pagination and join loops inside a sandbox so only the final shaped table enters the model's context.

The hidden trade-off: scripts answer only what they were asked

Code Mode looks strictly superior until you compare both modes on real business records. When evaluating classic multi-round MCP calls against single-turn scripts on a five-case commercial lending server (40,914 characters handled across four classic rounds on a link-integrity check versus 3,417 characters once a script knew the tools), a crucial qualitative difference emerged:

  1. A script answers the exact question it was written for: when a JavaScript filter maps six fields out of each loan record, every other field stays invisible inside the REPL and never reaches the model.
  2. A model reading full records notices unasked anomalies: in the classic sessions, without being prompted, the model flagged reminders that were never sent, statuses sitting behind their due dates, and a closed review carrying an empty checklist.
  3. Code Mode shrinks transport, not semantic ambiguity: giving an agent a fast JavaScript loop over raw ERP endpoints still leaves it guessing whether TRCODE = 8 is a wholesale invoice or a return, which status code blocks shipment, and which write call triggers a statutory tax seal.

The strongest production architecture combines both layers: use Code Mode so multi-step loops do not drown the context window, and back every tool call with an enterprise business ontology that surfaces explicit anomaly flags, verified entity joins, and human-gated write actions.

Pair Code Mode token efficiency with ontology-computed exception flags so scripts never filter away unasked operational risks.

Sources

  1. Code Mode: give agents an entire API in 1,000 tokens, Cloudflare Blog
  2. Code execution with MCP, Anthropic Engineering
  3. Code Mode documentation (REPL and Poll), Claude Code Docs
  4. MCP 2026-07-28 Specification Reference: Stateless HTTP, Tasks, Events, Apps & Elicitation, Open Enterprise Ontology Handbook (Ontoloji.org)

More on AI agent governance: Permissions, approval, audit and security for AI agents that read and act on company systems, including agents that work through MCP.

Keep reading

Follow what we ship.

New connectors, product updates and what we learn rolling AI out in enterprises, sent to your inbox.

Book a demo

Tell us where to reach you and we will reply with times.