Code Execution with MCP: Building More Efficient Agents
by Anthropic
Cut agent token usage ~98% by executing code over MCP.
Overview
This Anthropic engineering article explains a more efficient pattern for agents that connect to many MCP servers: instead of loading all tool definitions upfront and passing every intermediate result through the context window, present MCP servers as code APIs and let the agent write code to call them. It details techniques including filesystem-based progressive tool discovery (agents load only the tool modules they need on demand), in-environment data filtering before results reach the model, code-based control flow (loops and conditionals rather than long tool-call chains), privacy preservation via automatic PII tokenization, and persisting state and reusable skills as code. A worked example reduced a Google Drive-to-Salesforce workflow from roughly 150,000 tokens to about 2,000, a 98.7% reduction, at the cost of added infrastructure complexity requiring secure sandboxing and monitoring.
At a Glance
- Topic
- MCP
- Level
- Advanced
- Format
- Guide
- Cost
- Free
- Duration
- ~15 min read
- Provider
- Anthropic
- Hands-on
- No
- Certificate
- None
What You’ll Learn
- ✓Why loading all tool definitions upfront is costly at scale
- ✓Presenting MCP servers as code APIs for progressive discovery
- ✓Filtering data in the execution environment to save tokens
- ✓Privacy and state-persistence patterns for code execution
Highlights
- •Documents a ~98.7% token reduction in a real workflow
- •First-party guidance from Anthropic engineering
- •Covers the trade-offs (sandboxing, monitoring) honestly
Who It’s For
Best For
- ✓Engineers scaling agents across many MCP tools
- ✓Teams optimizing agent cost and latency
Prerequisites
- •Experience building tool-using agents
- •Understanding of MCP tools and context windows
FAQ
What is Code Execution with MCP: Building More Efficient Agents?
Anthropic engineering essay on presenting MCP servers as code APIs so agents write code instead of making many direct tool calls, dramatically reducing tokens and latency. For engineers scaling agents across many MCP tools.
Is Code Execution with MCP: Building More Efficient Agents free?
Code Execution with MCP: Building More Efficient Agents is free to access.
What level is Code Execution with MCP: Building More Efficient Agents for?
Code Execution with MCP: Building More Efficient Agents is aimed at a advanced audience. Recommended background: Experience building tool-using agents, Understanding of MCP tools and context windows.
How long does Code Execution with MCP: Building More Efficient Agents take?
Expect roughly ~15 min read. Most learners work through it at their own pace.
What will I learn from Code Execution with MCP: Building More Efficient Agents?
You'll learn: Why loading all tool definitions upfront is costly at scale; Presenting MCP servers as code APIs for progressive discovery; Filtering data in the execution environment to save tokens; Privacy and state-persistence patterns for code execution.