Too Many Tools: Tool Search and Code Mode for MCP & Tool Heavy AI Agents
Agents collect tools from several places. A coding agent ships with its own for working with files and the shell, and you add web search and a few functions of your own on top. Then the user connects MCP servers, and the count jumps: a small server adds 20 or 30 tools, and a large cloud or SaaS API can add several hundred.
where an agent's tools come from (illustrative)
built-in read, edit, shell, grep 12 ─┐
web fetch, search 3 ─┤
custom your own functions 6 ─┼─► 228 tools,
MCP git host, chat, db, cloud 207 ─┘ every request
▲
└─ 90% come from MCP
Every tool comes with a description and a JSON schema, and the agent sends all of them to the model on every request, including the many requests that use none of them. By the time the agent reads its first file, tool definitions already fill a large part of the context.
You can keep most of them out in two ways: let the agent search for tools when it needs one, or let it write code that calls them. MCP is where the problem is worst, so most examples below use MCP tools, but both approaches work for any tool the agent has.
Tool definitions take up space
The description tells the model when to use a tool, and the schema tells it how to call it. Together they usually come to a few hundred tokens per tool, and tools with large nested parameters take more. Here are the MCP servers from the diagram above:
MCP tools loaded at startup (illustrative)
git host ██████ 35 tools
chat ████ 24 tools
database █ 8 tools
cloud api ███████████████████████ 140 tools
──────────────────────────────────
207 tools × ~400 tokens ≈ 80K tokens
sent with every request
Against a 200K window, that looks like this:
┌ 200K context window ─────────────────────────────┐
│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░│
└──────────────────────────────────────────────────┘
▓ MCP tool schemas: 40%, before the user types
░ everything else: files, output, the conversation
The tokens cost money, but they also cause two problems that are harder to see:
- With 200 tools in front of it, many with similar names like
create_issueandcreate_ticket, the model picks the wrong one more often. - The schemas stay in the window for the whole session, so the agent hits its limit and needs compaction sooner. I wrote about compaction here.
The list also keeps growing, because people add servers and rarely remove them. Any single task, though, only needs a few of these tools, and both approaches below rely on that.
1. Tool search
Instead of loading every tool at startup, the harness loads the agent's own tools (built-in, web and custom) plus one extra tool, search_tools. It defers the MCP tools: it knows about them, but it doesn't send their schemas to the model. A short index in the system prompt tells the model which servers exist and what they cover.
before
┌ all 228 tools loaded, ~91K tokens ───────────────┐
│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░│
└──────────────────────────────────────────────────┘
after
┌ 21 tools + search_tools + index, ~10K ───────────┐
│▓▓▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░│
└──────────────────────────────────────────────────┘
▓ loaded at startup ▒ 2 schemas found by search
░ free for the task
When the agent needs a tool it doesn't have, it calls search_tools with a query. The harness matches the query against tool names and descriptions, adds the schemas of the top matches to the context, and from then on the agent calls them like any other tool.
user: "file a bug for the failing deploy"
│
▼
search_tools("create issue bug")
│ ┌ 207 deferred tools ────────┐
├───► │ github.create_issue ✓ │
│ │ github.list_issues │
│ │ tracker.create_ticket ✓ │
│ │ … 204 more, never loaded │
│ └────────────────────────────┘
▼
2 schemas appended to the end of the context
│
▼
github.create_issue(...)
A few details decide how well this works:
- The model can only search for tools it knows exist. Put a one-line index of each server in the system prompt, with what the server covers and how many tools it has.
- Keyword search (BM25) over names and descriptions handles most queries. Embeddings help when the user's wording differs from the tool author's.
- Return full schemas, so the agent can call the tool without a second lookup.
- Append loaded schemas to the end of the context. Inserting them earlier invalidates the prompt cache from that point on.
- Keep loaded tools for the rest of the session, so the agent doesn't search for the same tool every turn.
The cost is an extra round trip the first time the agent uses a tool, and a dependence on search quality. If search misses, the agent carries on without the right tool and may never notice.
You'll want this once an agent has more than a few dozen tools, which covers most agents with MCP servers connected.
2. Code mode
Tool calls also fill the context with their results, and tool search does nothing about that.
With normal tool calls, every call goes through the model. The model asks for a call, the harness runs it, and the harness adds the full result to the context. Ten calls cost ten model turns, and every intermediate result stays in the window.
In code mode the agent gets a single tool, run_code. It writes a short program, in JavaScript or whatever language the harness supports, and the harness runs it in a sandbox where each tool the agent may use is an async function. The program does its looping and filtering inside the sandbox, and the model only sees what the program prints.
┌ agent ───────────┐ ┌ QuickJS sandbox ─────┐
│ writes ~10 lines │ run_code │ no fs, no network │
│ of JS │ ────────► │ mcp.ci.* read │
│ │ │ mcp.github.* read │
│ │ │ │
│ sees 3 lines │ ◄──────── │ 40 calls, 2 MB of │
│ of output │ stdout │ results stay here │
└──────────────────┘ └──────────────────────┘
bound tools only │
▼
┌ MCP servers ───┐
│ ci github │
└────────────────┘
Say the agent needs to find out why recent CI runs on main failed. With tool calls, it lists the runs and then fetches each failing log in a separate turn. In code mode it writes:
const runs = await mcp.ci.listRuns({ branch: "main" });
const failed = runs.filter(r => r.status === "failed");
for (const run of failed) {
const log = await mcp.ci.getLog({ id: run.id });
const error = log.split("\n").find(l => l.includes("Error"));
console.log(run.id, run.commit.slice(0, 7), error);
}
tool calls code mode
model ─► list_runs model ─► run_code
model ◄─ 50 runs (12K) ┊ list_runs
model ─► get_log #1 ┊ get_log × 6
model ◄─ log (9K) ┊ filter, format
model ─► get_log #2 model ◄─ 6 lines (300)
model ◄─ log (9K)
… × 6
7 round trips 1 round trip
~66K tokens into context ~300 tokens into context
Models handle this well, since they have seen far more code that calls APIs than tool-call JSON. A loop over failing runs is an easy program to write.
Keeping the sandbox safe
You are running code a model wrote, so the sandbox should expose as little as possible. An embedded JavaScript engine like QuickJS suits this:
- It has no file system, network or process access unless you add it. Code inside can only call the functions you pass in.
- It starts in milliseconds and runs in the same process, so you don't spin up a container for each run.
- You can cap memory, run time and the number of tool calls per run.
- You can generate TypeScript declarations from each tool's schema, so the model knows the exact function signatures.
Because the sandbox can only reach what you give it, permissions come down to which functions you bind. The simplest setup binds MCP tools and nothing else, so code in the sandbox can't touch the shell or edit files, and the agent's built-in tools keep their normal permissions outside it.
You can go further and bind only read-only tools. MCP tools can declare a readOnlyHint annotation, so the harness can bind just those and let the agent run dozens of reads without asking. Anything that writes, deletes or sends a message still goes through a normal tool call that the user approves.
all MCP tools (207)
│
├─ readOnlyHint: true ───► bound in the sandbox
│ run freely, no prompts
│
└─ writes, deletes, sends ► normal tool calls
one approval each
The model has to write code that runs, and a failed run means a retry. It is also harder for a user to follow 40 calls inside one program than 40 separate tool calls, so log each call the sandbox makes and show it in the UI.
Code mode pays off on tasks that touch many items or take many steps, where the agent would otherwise loop over tool results one turn at a time.
Using both
The two approaches cut different costs, so they combine well: tool search keeps schemas out of the context, and code mode keeps results out.
┌ search_tools ──────┐ ┌ run_code ──────────────┐
│ find the 3 tools │ ─► │ call them 40 times, │
│ out of 207 │ │ return 3 lines │
└────────────────────┘ └────────────────────────┘
keeps schemas out keeps results out
In code mode, search_tools can return function signatures instead of tool schemas. The agent looks up what it needs and then writes a program that calls it.
How I'd choose:
- Few tools and simple tasks: plain tool calls are fine.
- Many tools and simple tasks: tool search.
- Few tools and bulk or multi-step work: code mode.
- Many tools and bulk work: both.
Code mode with simpler tasks can be overkill, prompting the model to write scripts for a 1-2 turn job. Choosing the right mode is important. Tool search is relatively easier to recommend.
A good harness handles this for you.