Code Execution with MCP: Building More Efficient Agents
by Anthropic
Stop paying a context tax for every MCP tool your agent might never call.
Overview
Published on Anthropic's engineering blog on 4 November 2025 by Adam Jones and Conor Kelly, this article diagnoses two costs that appear once an agent connects to many Model Context Protocol servers: every tool definition is loaded into the context window upfront, and every intermediate result flows back through the model during multi-step work. Anthropic's answer is to stop presenting MCP tools as a flat list of callable functions and instead expose each server as a directory of typed TypeScript modules on a filesystem the agent can explore - servers/google-drive, servers/salesforce and so on, one file per tool with a typed interface. The agent then writes code that imports only the handful of tools a task actually needs, so tool discovery becomes progressive disclosure rather than a fixed context tax paid on every request. The worked example combines fetching a Google Drive document and updating a Salesforce record in a single script rather than a chain of model round-trips, and the headline figure is a scenario dropping from roughly 150,000 tokens to 2,000, which the authors present as a 98.7% time and cost saving. The piece also covers filtering large datasets inside the execution environment before anything reaches the model, tokenizing sensitive fields so private data never enters context, and persisting state to files across steps.
At a Glance
- Topic
- MCP
- Level
- Intermediate
- Format
- Guide
- Cost
- Free
- Duration
- ~20 min read
- Provider
- Anthropic
- Hands-on
- No
- Certificate
- None
What You’ll Learn
- ✓Diagnose context bloat caused by loading every MCP tool definition upfront
- ✓Present MCP servers as a filesystem tree of typed TypeScript modules
- ✓Apply progressive disclosure so the agent loads only the tools needed
- ✓Filter and aggregate large tool results inside the execution environment
- ✓Chain multi-server workflows in code instead of sequential model round-trips
- ✓Keep sensitive fields out of model context using tokenization techniques
- ✓Persist intermediate state across steps so long tasks survive interruptions
Highlights
- •Quantifies the win concretely: about 150,000 tokens reduced to roughly 2,000
- •Comes from the team that authored the Model Context Protocol itself
- •Shows a real filesystem layout and typed interfaces, not abstract architecture advice
- •Triggered a wave of community implementations tracked in the MCP discussions
- •Covers privacy and state persistence, not only the token-count headline
Who It’s For
Best For
- ✓Engineers whose agents connect to many MCP servers at once
- ✓Teams hitting context-window limits from tool-definition bloat
- ✓MCP server authors deciding how to expose their tool surface
Prerequisites
- •Working knowledge of the Model Context Protocol and tool calling
- •Comfort reading TypeScript and running a sandboxed code executor
FAQ
What is Code Execution with MCP: Building More Efficient Agents?
An Anthropic engineering article on why agents connected to many Model Context Protocol servers burn their context window on tool definitions and intermediate results, and how presenting those servers as a filesystem of typed code modules instead lets an agent load only what a task needs. Written for engineers whose multi-server agents have hit context limits or latency walls.
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 intermediate audience. Recommended background: Working knowledge of the Model Context Protocol and tool calling, Comfort reading TypeScript and running a sandboxed code executor.
How long does Code Execution with MCP: Building More Efficient Agents take?
Expect roughly ~20 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: Diagnose context bloat caused by loading every MCP tool definition upfront; Present MCP servers as a filesystem tree of typed TypeScript modules; Apply progressive disclosure so the agent loads only the tools needed; Filter and aggregate large tool results inside the execution environment; Chain multi-server workflows in code instead of sequential model round-trips; Keep sensitive fields out of model context using tokenization techniques; Persist intermediate state across steps so long tasks survive interruptions.
Topics
Sources
This page was written from 3 sources, 2 on domains other than anthropic.com.