My Summary of "Code Execution with MCP" blogpost by Anthropic team
Introduction
Problems with direct MCP usage
- Many MCP servers are connected
- All tool definitions (from each MCP server) are added in context
- Intermediate output from mcp
Alternative approach
- Quoting the blogpost:
LLMs are adept at writing code and developers should take advantage of this..- So instead of calling mcp, tell agents to write code for an api wrapper around mcp tools (aka Code Execution with MCP, or Code Mode)
What makes the Alternative approach better?
These benefits are especially useful for devs to remember so that you can tell LLM to write code that can handle such things in real world use cases:
- Read tool definitions on demand (by either letting model navigate filesystem or using some tool like
search_tools) - "Code Mode" can filter/transform/aggregate/join/extractColumns on "intermediate output" before giving it to LLM model
- Instead of chaining mcp tool calls via prompt engineering (which can be unreliable), "Code Mode" can do loops, sleeps, conditionals, and error handling
- Reduced "Time to first token" latency using conditional tree (if-elif-else), as code can execute it in microseconds (v/s seconds via LLM)
- Intermediate output stays in execution environments (which are essentially 'temporary' sandboxes). (e.g. fetch+count users via code execution with MCP, without exposing email addresses to model)
- You can also tokenize more sensitive data (e.g. tokenize when fetching from a CRM, untokenize before logging it into a spreadsheet)
- Give filesystem access, let agent write intermediate results to files: for logging (i.e. track progress) and state persistence (i.e. resuming work)
- Save "working code with MCP" as reusable functions for future use (in their example in the blogpost, Anthropic team is putting it in
.skillsfolder)- Further quoting blogpost:
Adding a SKILL.md file to these saved functions creates a structured skill that models can reference and use... build a toolbox of higher-level capabilities
- Further quoting blogpost:
- Save "working code with MCP" as reusable functions for future use (in their example in the blogpost, Anthropic team is putting it in
Overhead in "Code Execution via MCP" approach
Quoting this section of the blogpost, because it's well written and quite concise already:
Note that code execution introduces its own complexity. Running agent-generated code requires a secure execution environment with appropriate sandboxing, resource limits, and monitoring. These infrastructure requirements add operational overhead and security considerations that direct tool calls avoid. The benefits of code execution—reduced token costs, lower latency, and improved tool composition—should be weighed against these implementation costs.
Levaraging familiarity with Software Engineering for your own benefit
Again, quoting another section of the blogpost, because it's well written and quite concise already:
Although many of the problems here feel novel—context management, tool composition, state persistence—they have known solutions from software engineering. Code execution applies these established patterns to agents, letting them use familiar programming constructs to interact with MCP servers more efficiently.
Until next time...