feat(agent): deferred MCP tool loading via tool_search - #3511
Open
etano wants to merge 1 commit into
Open
Conversation
When many MCP servers are connected, every tool's schema is loaded into the model's context on every turn, which can consume tens of thousands of tokens before any work happens and degrades tool-selection accuracy past ~30-50 tools. This adds an opt-in `tool_search` meta-tool that keeps MCP tool schemas out of context until the model discovers them on demand. Enabled via `options.tool_search`. When the number of MCP tools exceeds the configured threshold (default 20), MCP tools are deferred: the model sees only built-in tools plus `tool_search`, calls it with keywords to load matching tools, and those become callable on the next turn. Activated tools persist for the conversation (until the MCP catalog changes). Implementation lives entirely in internal/agent and leans on the existing per-step tool recomputation (PrepareStep), so activated tools appear on the next turn with no restart. It is provider-agnostic (works with any model), applies to the top-level agent only (sub-agents are unaffected), preserves PreToolUse hooks on activated tools, and keeps a stable sorted tool ordering.
Contributor
|
Thank you for your submission. We really appreciate it! Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request comment same as the below format. I have read the Contributor License Agreement (CLA) and hereby sign the CLA. Ethan Brown seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds opt-in deferred tool loading for MCP tools via a built-in
tool_searchmeta-tool, so setups with many MCP servers don't pay the full tool-schema context cost on every turn.Why
Every connected MCP server's tool schemas are loaded into context on every request. A handful of servers can add tens of thousands of tokens before any work happens, and tool-selection accuracy degrades once the toolset grows past ~30–50 tools. Deferring MCP tools behind a search keeps the per-turn context small and the visible toolset focused.
How it works
options.tool_searchconfig:{ "enabled": bool, "threshold": int (default 20) }.threshold,buildToolspartitions tools into an always-loaded core (built-ins) and a deferred catalog (MCP tools). The model sees only core + atool_searchtool.tool_searchwith keywords; matching catalog tools are activated and pushed onto the running agent viaSetTools. Because tools are recomputed each step inPrepareStep, they become callable on the next turn — no restart.Notes / scope
Testing
go build ./...clean,schema.jsonregenerated; verified end-to-end against a live setup with 40+ MCP tools — schemas deferred out of context, model discovers and calls tools on demand.Config example
{ "options": { "tool_search": { "enabled": true, "threshold": 20 } } }