Part 11 of 13 · 5 min read
Why agents need a code tool for MCP itself
Why agents need a code tool for MCP itself: tool-definition bloat, and code execution as the fix
Think of being handed the full 500-page vendor catalog before every single meeting, instead of a one-line directory that tells you which page to open. Then think of doing a spreadsheet's arithmetic yourself on a calculator, rather than reading every row aloud to someone on the phone who adds them up and reads the total back to you.
In plain words: every tool a module 2 server offers has to be described to the model before the model can choose to call it, and every result that comes back has to pass through the model's context too. Connect enough MCP servers and both costs add up: fifty-plus tools can cost tens of thousands of tokens in descriptions alone, before the model has read the actual request, and a single large tool result (ten thousand spreadsheet rows, say) burns through the rest. Two fixes attack each half of that cost. Loading tool descriptions on demand instead of all at once fixes the first. Letting the model write and run code that calls tools itself, so only the code's own filtered output returns to the model, fixes the second. This module's sandbox, from parts 1 to 16, is the wall that running that code safely requires.
A worked example. Anthropic's own account of this, published 4 November 2025, describes a task that used 150,000 tokens with ordinary tool calls (every intermediate row of a spreadsheet round-tripped through the model) and 2,000 tokens once the agent instead wrote code that called the same MCP tools directly, filtered the rows itself, and returned only the few that mattered: a 98.7% cut on that one task. A separate feature shipped two weeks later, on 20 November 2025, attacks the other half of the cost: a Tool Search Tool that lets a server mark its tool definitions defer_loading: true, so the model sees a short search tool instead of all fifty-plus descriptions, and only pulls in the three to five it actually ends up using. Anthropic's own benchmark for that feature reports about 77,000 tokens consumed before work begins with every definition loaded upfront, against about 8,700 tokens with search and on-demand loading, roughly an 85% reduction, with task accuracy on one internal benchmark rising from 49% to 74% alongside it. Cloudflare published an independent, similar finding under the name "Code Mode" two months earlier, in September 2025, which is the usual sign that a pattern is a real property of the problem and not one vendor's trick.
About this example About these numbers
These figures are reported by Anthropic's and Cloudflare's own published posts (links below), not reproduced by this module's toy code, which has no MCP server large enough to show bloat at this scale. Read them as direction, not as a benchmark you could rerun here. Last checked: 15 November 2026.
A sketch of the shape, not a key line from this module's code (the toy mini-mcp client from module 2 loads its few tools directly, since it never has enough of them to need either fix):
# illustrative only -- the pattern Anthropic's post describes, not code from this repo
# without deferred loading or code execution: every description, every row, through the model
for tool in mcp_client.list_tools(): # fifty-plus full descriptions into context
model_sees(tool.description)
rows = mcp_client.call_tool("get_sheet", {"id": "abc123"}) # 10,000 rows into context
model_sees(rows) # model must read all of it to find the few that matter
# with a search tool and code execution: only what is needed, ever reaches the model
tool = tool_search("which tool reads a spreadsheet by id") # one short description back
rows = run_in_sandbox(f"""
rows = {tool.name}(id="abc123") # the sandbox calls the real MCP tool
print([r for r in rows if r["status"] == "overdue"]) # filtering happens here, not in context
""")
model_sees(rows) # only the filtered few rows return
Tool-definition bloat, deferred loading, code execution with MCP
Tool-definition bloat is context consumed by tool descriptions and tool results that the model never actually needed to see in full, simply because the protocol's default is to load and return everything. Deferred loading (the Tool Search Tool's defer_loading setting) keeps a tool's full description out of context until a search step decides the model needs it. Code execution with MCP, also called programmatic tool calling, is the same idea applied to results instead of descriptions: the model writes code that calls MCP tools and filters their output inside the sandbox from parts 1 to 16 of this module, so only the code's own conclusion, not the raw tool result, ever reaches the model's context.
Example: the sandbox already built in this module is not a separate feature from MCP. It is the thing that makes "let the model write code that calls your tools" safe to ship at all; without it, code execution with MCP is just prompt injection with an API key attached.
For the curious Where this connects to module 2
Module 2 drew the line between a tool (the model decides) and a resource (the application decides). Code execution with MCP does not move that line: the model still decides that a tool gets called. What moves is where the result lands first — inside the sandbox's code, not directly in the model's context — and whether the model sees the tool's full description at all before it needs it.
Quick notes: what you must be able to say
- A resource limit is only as real as what enforces it;
RLIMIT_NPROCis ignored for root, so a sandbox running as root needs its own polling monitor. - Each sandbox layer stops exactly the attack it was built for: the environment scrub does not stop a path read, and network isolation does not stop a secret reaching the model.
- Filesystem and network isolation are both required: either alone leaves one half of exfiltration open.
- A file API must resolve names through every symbolic link, not just reject
..and absolute paths, or a confused-deputy leak slips through. - A tool result that names why a run stopped lets a model recover; a result that returns only output leaves every failure looking the same.