MCPAgent solves this with deferred tool loading: tools are registered in an internal registry but not sent to the model until the model explicitly searches for and selects them.
Two loading modes
- Normal mode
- Deferred mode
All tools are exposed to the model in every turn.Used when:
- Total tools (default + MCP) is below
tool_threshold(default15), and deferred_tools=False(the default)
Decision rules
MCPAgent applies the following logic after init_mcp_servers() completes:
Case 1: Small tool count (below threshold)
Case 1: Small tool count (below threshold)
If
(default tools + MCP tools) < tool_threshold and deferred_tools=False:- Agent stays in normal mode.
- All tools are sent to the model on every turn.
- No discovery step is required.
Case 2: Large tool count (at or above threshold)
Case 2: Large tool count (at or above threshold)
If
(default tools + MCP tools) >= tool_threshold and deferred_tools=False:_auto_deferredis set toTrue.- All tools are registered in
_tool_registry. - The model initially receives only
tool_search_regexplus any already-loaded tools. - Tools found via search are added to
_loaded_toolsand become callable in subsequent turns.
Case 3: Explicit deferred mode
Case 3: Explicit deferred mode
If
deferred_tools=True:- Deferred mode is enabled regardless of the total tool count.
- Useful when you always want controlled, incremental tool exposure.
Internal data structures
| Attribute | Type | Description |
|---|---|---|
_tool_registry | Dict[str, dict] | All known tool schemas, keyed by tool name. Populated from default tools and MCP tools during init. |
_loaded_tools | Set[str] | Names of tools currently exposed to the model. Grows as the model discovers tools via search. |
_auto_deferred | bool | True when deferred mode was triggered automatically by the threshold, rather than set explicitly. |
tool_threshold | int | The count at which auto-deferred mode activates. Default 15. |
The tool_search_regex tool
In deferred mode the model receives a single special tool:tool_search_regex, MCPAgent._execute_tool() intercepts it and runs _search_tools(), which:
- Compiles the pattern as a case-insensitive regex.
- Iterates
_tool_registryand matches against bothnameanddescription. - Adds matching tool names to
_loaded_tools. - Returns a result dict with
status,tools, andnewly_loaded.
Adding tools to the deferred registry
Programmatic registration
Preloading known-useful tools
Registering a function directly
Whenregister_tool_from_function() is called on an MCPAgent that is already in deferred mode, the tool is added to both internal_tools and _loaded_tools (preloaded), since custom tools are assumed to be immediately useful:
Inspecting registry state
Practical recommendations
Tools found via
tool_search_regex are permanently added to _loaded_tools for the lifetime of the agent instance. There is no built-in mechanism to unload a tool once it has been loaded. If you need to restrict tool access per session, manage that at the session creation layer instead.