Code execution with MCP: Building more efficient agents
01:00 · November 4, 2025 · Antropic Engineering Blog

Direct tool calls consume context for each definition and result. Agents scale better by writing code to call tools instead. Here's how it works with MCP.
Summary
The Model Context Protocol provides a standardized way for AI agents to connect to external tools and data sources, replacing the need for custom integrations per pairing. As adoption has grown since its November 2024 launch, agents now routinely access hundreds or thousands of tools across multiple MCP servers. This scale exposes two inefficiencies: every tool definition must be loaded into the context window at the outset, and every intermediate result returned by a tool must pass back through the model, driving up token counts and latency.
Code execution environments address these issues by exposing MCP servers as programmatic APIs rather than direct function calls. An agent can generate a filesystem view of available tools, list directories to discover relevant servers, and read only the specific definition files it needs for the current task. In one TypeScript implementation, this progressive disclosure reduced token usage from roughly 150,000 to 2,000 for a given workflow. The same environment lets the agent filter, aggregate, or transform large result sets locally before any data reaches the model.
Control flow also improves. Loops, conditionals, and error handling execute inside the code runtime instead of requiring repeated round-trips between the model and individual tool calls. Intermediate data can remain inside the execution environment, so only explicitly returned values enter the context window. For sensitive information, the client can automatically tokenize personally identifiable data before it reaches the model and restore the original values on subsequent tool calls.
These gains come with added operational requirements. Executing agent-written code demands sandboxing, resource limits, and monitoring that direct tool invocation does not. The article weighs the resulting reductions in token cost and latency against this infrastructure overhead, noting that the approach reuses established software-engineering patterns for context management and state persistence across sessions.
Why it matters
Highly actionable for Product Teams and Builders with concrete implementation patterns, code snippets, and measurable efficiency gains (e.g., 98.7% token reduction). Directly addresses model/product updates in agent tooling and context management.






