mcp-code-execution
Installation
SKILL.md
MCP via Code Execution — Cut Agent Token Consumption
This skill gives you an operating procedure for replacing direct MCP tool invocations with agent-generated code that calls MCP servers as code APIs. Direct tool calling has two token sinks: every tool definition is loaded into context up front, and every intermediate result flows through the model. Presenting MCP tools as files on a filesystem and writing code against them fixes both. On Anthropic's reference workflow (Google Drive → Salesforce), this cut token usage from 150,000 to 2,000 tokens — a 98.7% saving.
When to use
Switch to code execution with MCP when any of these hold:
- The agent has access to many tools — developers routinely wire agents into dozens of MCP servers exposing hundreds or thousands of tools. At that scale, definitions alone can consume hundreds of thousands of tokens before the agent reads the request.
- A workflow chains tools so one tool's output becomes another's input. With direct calls the full payload flows through the model twice (e.g., a 2-hour sales-meeting transcript ≈ an extra 50,000 tokens just to copy it from
gdrive.getDocumentintosalesforce.updateRecord). - Tool results are large and mostly irrelevant (10,000-row spreadsheets, long channel histories) and you only need a filtered slice.
- The workflow needs loops, conditionals, retries, or polling — chaining individual tool calls for these burns tokens and adds latency per round trip.
- Sensitive data (emails, phones, names) must move between systems without entering model context.
Do NOT use when:
- You cannot provide a secure execution environment. Running agent-generated code requires sandboxing, resource limits, and monitoring — no sandbox, no code execution.
- The infrastructure cost outweighs the gain. The source is explicit: code execution "introduces its own complexity"; weigh token/latency savings against implementation cost before adopting.