Code Mode: Solving the Agent Context Problem
Model Context Protocol (MCP) has become the standard for AI agents to access external tools, but traditional approaches suffer from a fundamental tension: agents need many tools to be useful, yet each tool description consumes tokens from the limited context window. Cloudflare's new Code Mode technique solves this by allowing models to write compact, executable code against a typed SDK instead of using individual tool descriptions.
What Changed: MCP Server Architecture
Cloudflare released a new MCP server for the entire Cloudflare API that exposes only two tools:
- search(): Query the full OpenAPI spec via JavaScript code to discover relevant endpoints
- execute(): Write JavaScript code to make API calls, handle pagination, and compose operations
Both tools run generated code inside a Dynamic Worker isolate—a lightweight V8 sandbox with no file system access and disabled external fetches by default.
Impact: 99.9% Token Reduction
The efficiency gains are dramatic. An equivalent MCP server using traditional tool descriptions would require 1.17 million tokens—exceeding the context window of advanced foundation models. The Code Mode approach uses only ~1,000 tokens, regardless of how many API endpoints exist. This fixed footprint remains constant even as the Cloudflare API grows.
How Developers Use It
Step 1 - Discovery: Agents call search() with JavaScript to filter the OpenAPI spec by product, path, tags, or metadata:
async () => {
const results = [];
for (const [path, methods] of Object.entries(spec.paths)) {
if (path.includes('/zones/') &&
(path.includes('firewall/waf') || path.includes('rulesets'))) {
for (const [method, op] of Object.entries(methods)) {
results.push({ method: method.toUpperCase(), path, summary: op.summary });
}
}
}
return results;
}
Step 2 - Execution: Agents call execute() to write code that makes authenticated API requests, chains operations, and returns results.
Open Source & Extensibility
Cloudflare is also open-sourcing a Code Mode SDK as part of the Cloudflare Agents SDK, allowing developers to apply the same approach to their own MCP servers and AI agents. This pattern was independently validated by Anthropic's Code Execution with MCP post, signaling broader industry adoption of the technique.