techlist.io
Anthropic2025-11-044 min read

Curated summary

Code execution with MCP for efficient agents

Read original(opens in new tab)

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.

Continue with another curated summary.