techlist.io

Code Execution

2 posts

Cloudflare2026-02-203 min readCurated summary

Code Mode: give agents an entire API in 1,000 tokens

Code Mode addresses a central MCP problem: giving agents broad tool access without overwhelming their context windows. Instead of exposing thousands of individually described tools, it provides a typed SDK and lets agents write sandboxed JavaScript to search specifications and execute API calls. Cloudflare’s new MCP server applies this approach to its entire API using only `search()` and `execute()`, reducing context usage by 99.9% to roughly 1,000 tokens. ## The Context-Window Problem - MCP makes external tools available to AI agents, but each tool’s description consumes model context. - Large APIs can require enormous token budgets merely to describe their endpoints. - A conventional MCP server for the Cloudflare API would consume approximately 1.17 million tokens. - This can exceed the context window of even advanced foundation models. ## Code Mode as a Compact API Interface - Agents write code against a typed SDK rather than selecting from thousands of separate tools. - Generated code serves as a compact, composable plan. - Agents can: - Explore available operations. - Chain multiple API calls. - Handle pagination and errors. - Return only the data needed for the task. - The tool footprint remains fixed as the underlying API grows. ## Cloudflare’s Two-Tool MCP Server - The server exposes only: - `search()` — searches the Cloudflare OpenAPI specification. - `execute()` — runs JavaScript against the Cloudflare API. - The full OpenAPI specification stays outside the model context. - `$refs` are pre-resolved, allowing agents to inspect endpoint metadata directly through code. - Agents can filter endpoints by product, path, tags, or other metadata. ## Sandboxed Execution - Both tools execute code inside a Dynamic Worker isolate based on lightweight V8 sandboxes. - The environment has: - No filesystem access. - No environment variables exposed to generated code. - External fetches disabled by default. - Outbound requests can be explicitly enabled through controlled fetch handlers. ## Example: Finding DDoS Protection Endpoints - For a request to protect an origin from DDoS attacks, an agent can first consult documentation or skills. - It then uses `search()` to inspect the OpenAPI specification. - A JavaScript query can locate zone endpoints related to WAF and rulesets. - The search narrows more than 2,500 Cloudflare endpoints to relevant operations such as: - Listing and updating WAF packages and rules. - Listing and creating zone rulesets. - Reading and updating entry-point rulesets. - Creating and updating rules within a ruleset. - Once the relevant endpoints are identified, `execute()` can perform coordinated API requests in one sandboxed run. ## SDK Availability - Cloudflare is open-sourcing a Code Mode SDK as part of the Cloudflare Agents SDK. - The same pattern can be used to build more context-efficient MCP servers and AI agents. Code Mode is most useful for large, evolving APIs: expose a small, stable tool surface, let agents discover capabilities programmatically, and execute only the focused operations required for each task.

Anthropic2025-11-044 min readCurated summary

Code execution with MCP for efficient agents

Connecting AI agents to many tools through the Model Context Protocol (MCP) becomes inefficient at scale, because loading every tool definition upfront and passing intermediate results through the model's context window consumes large numbers of tokens. The post argues that presenting MCP servers as code APIs, which agents interact with by writing and running code, lets them load only the tools they need and process data outside the model. In the author's example, this cut token usage from about 150,000 to 2,000, a 98.7% reduction. ## Why Tool Use Becomes Expensive - **Tool definitions crowd the context window.** Most MCP clients load every tool's schema, description, and parameters into context up front. Agents connected to thousands of tools may need to process hundreds of thousands of tokens before they even read a request. - **Intermediate results pass through the model twice.** When the model moves data from one tool to another, such as copying a Google Drive transcript into a Salesforce record, the full content flows into context and then must be written out again. A two-hour meeting transcript can add around 50,000 tokens, and very large documents can exceed context limits entirely. Copying long data also increases the chance of errors. ## Presenting MCP Servers as Code - **Filesystem layout.** Each MCP server becomes a directory (e.g., `servers/google-drive/`, `servers/salesforce/`), and each tool becomes a typed TypeScript file such as `getDocument.ts` or `updateRecord.ts`, which wraps a call to `callMCPTool`. - **Workflows as scripts.** The Drive-to-Salesforce task becomes a short script that reads the document content into a variable and passes it directly to `updateRecord`, so the transcript never enters the model's context. - **Cloudflare's "Code Mode"** reached similar conclusions, based on the idea that LLMs write code well. ## Benefits of Code Execution - **Progressive disclosure.** The agent explores the directory tree to find servers and reads only the tool files it needs. Alternatively, a `search_tools` tool can return results at adjustable detail levels, such as name only, name plus description, or full schema. - **Filtering before the model sees data.** When fetching a 10,000-row spreadsheet, the script can filter for pending orders and log only a count and a sample of five rows, so the model sees a tiny fraction of the data. - **Native control flow.** Loops, conditionals, and retries run in code rather than through repeated model turns. For example, polling a Slack channel for a deployment message can happen in a single script with a sleep between checks, avoiding a model round-trip for each check. - **Privacy-preserving operations.** Intermediate results stay in the execution environment by default. An MCP client can also tokenize PII such as emails, phone numbers, and names before they reach the model, then restore the real values when data moves to the next tool. This keeps sensitive data out of the model while still letting it flow between systems, and it supports deterministic rules about where data may go. - **State persistence and reusable skills.** Agents can write intermediate files to a workspace to resume work later, and they can save working code as reusable functions. Adding a `SKILL.md` file turns these saved functions into structured skills that build up a toolbox over time. ## Trade-offs to Weigh Code execution requires a secure sandbox with resource limits and monitoring, which adds operational overhead and security concerns that direct tool calls avoid. Teams should weigh the savings in token cost, latency, and tool composition against these infrastructure costs. The author encourages anyone implementing this approach to share their findings with the MCP community.