← Back
Cloudflare
Cloudflare launches MCP server using Code Mode, reducing API context requirements by 99.9%
Cloudflare WorkersCloudflare · integrationapifeaturesdkopen-source · blog.cloudflare.com ↗

Code Mode: Efficient API Access for AI Agents

Cloudflare announced a new MCP server that gives AI agents access to the entire Cloudflare API while consuming only around 1,000 tokens—a 99.9% reduction compared to traditional MCP implementations that would require 1.17 million tokens.

How Code Mode Works

Instead of exposing thousands of individual API tools, the server uses Code Mode—a technique that lets models write and execute code against a typed SDK. The MCP server exposes just two tools:

  • search() — Allows agents to query the Cloudflare OpenAPI spec by writing JavaScript code to filter endpoints by product, path, tags, or metadata
  • execute() — Enables agents to write JavaScript code that makes Cloudflare API requests, handles pagination, chains operations, and returns results

Both tools run generated code inside a Dynamic Worker isolate—a lightweight V8 sandbox with no file system access, no environment variable leakage, and disabled external fetches by default.

Key Benefits

  • Massive context savings: Provides access to hundreds of API endpoints without inflating model context windows
  • Type-safe SDK: Agents write code against a fully typed representation of the API, improving reliability
  • Composable operations: Agents can chain multiple API calls and handle complex logic in a single execution
  • Security by design: Code runs in an isolated sandbox with explicit fetch controls

Getting Started

The Cloudflare MCP server is available today and can be used with any MCP-compatible AI agent. Cloudflare is also open-sourcing the Code Mode SDK in the Cloudflare Agents SDK, enabling developers to apply the same pattern to their own MCP servers and AI agents.

The approach addresses a fundamental tension in agent design: agents need many tools to do useful work, but each tool consumes precious context window space. Code Mode solves this by letting the model itself determine which operations to perform through executable code rather than pre-defining tool boundaries.