Anthropic Code Execution with MCP: Boosting Agent Efficiency
TL;DR
Anthropic demonstrated that agents can interact with MCP servers via code execution instead of direct tool calls, reducing token consumption by up to 98.7% and lowering latency while preserving privacy and state.
Why MCP Needs a New Interaction Model
MCP (Model Context Protocol) is an open standard that lets AI agents call external tools through a universal API. Since its launch in November 2024, thousands of MCP servers and SDKs for major languages have been created, making MCP the de‑facto way to connect agents to tools and data.
However, as agents hook up to hundreds of servers, two token‑draining patterns emerge:
- Tool definitions flood the context window. Loading every tool description up‑front can require hundreds of thousands of tokens before the model even sees the user request.
- Intermediate results travel through the model. Each tool call’s raw output is sent back to the LLM, duplicated for every step, which can add tens of thousands of tokens for large documents.
Both patterns increase inference cost and latency, and they can exceed the model’s context limit.
Code Execution as a Token‑Efficient Bridge
Instead of feeding tool definitions directly to the LLM, agents can treat MCP servers as code APIs. The agent writes code that imports the needed tool modules, calls them, and processes results inside a sandboxed execution environment. This approach solves the two problems above:
- On‑demand loading: The agent reads only the files representing the required tools, shrinking the context from ~150 k tokens to ~2 k tokens (a 98.7% reduction).
- Local result processing: Data is filtered, aggregated, or transformed in code before any token is sent back to the model, preventing massive payloads from entering the context window.
A TypeScript example shows the agent generating a virtual file tree of available tools, then importing getDocument.ts and updateRecord.ts only when needed. The same pattern works in any language supported by the MCP SDKs.
Core Benefits of Code Execution with MCP
Progressive Disclosure
Agents can explore the filesystem (or a search_tools endpoint) to discover only the relevant tool definitions. Adding a detail level parameter (name only, name + description, full schema) further trims context usage.
Context‑Efficient Tool Results
Large data sets can be trimmed before reaching the model. For example, fetching a 10,000‑row spreadsheet and returning only five rows reduces token load dramatically. The same technique applies to joins, aggregations, and field extraction.
Powerful Control Flow
Loops, conditionals, and error handling are expressed in native code rather than a chain of individual tool calls. This eliminates the back‑and‑forth “call → wait → call” pattern, cutting time‑to‑first‑token latency.
Privacy‑Preserving Operations
Intermediate data stays inside the execution sandbox. Sensitive fields can be tokenized before any model exposure, ensuring PII never appears in the LLM’s context. The MCP client can automatically untokenize data when passing it to a downstream tool.
State Persistence and Reusable Skills
Agents can write intermediate results to files, enabling resumable workflows. Generated code can be saved as reusable functions and packaged with a SKILL.md manifest, forming Skills—folders of scripts and instructions that the model can invoke for specialized tasks.
Trade‑offs and Operational Considerations
Running agent‑generated code requires a secure sandbox with resource limits, monitoring, and proper isolation (see Anthropic’s code sandboxing documentation). These infrastructure costs must be weighed against the token‑savings, latency gains, and richer composition capabilities.
Practical Takeaways
- Adopt code execution when agents need to interact with many MCP tools or handle large data payloads.
- Use progressive disclosure (filesystem navigation or
search_tools) to keep context size minimal. - Leverage sandboxed execution to enforce privacy and maintain state across steps.
- Package frequently used code as Skills to accelerate future agent development.
Community Invitation
Anthropic encourages developers to experiment with this pattern and share results with the MCP community (link).