feat: Add progressive tool discovery system#1644
feat: Add progressive tool discovery system#1644sidhantu123 wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
Implements optional progressive disclosure of MCP tools through semantic grouping
and lazy-loading. Servers can organize tools into semantic groups with gateway
tools that load actual tools on-demand, achieving significant reduction in context
token usage.
## Architecture
### Core Components
- ToolGroup: Semantic organization of related tools
- ToolGroupRegistry & ToolGroupManager: Discovery infrastructure
- Server.enable_discovery_with_groups(): Simple API to enable discovery
- is_discovery_enabled: Property to check discovery status
- Client-side tool refresh: Automatic handling via ToolListChangedNotification
### Key Features
- Hybrid mode: Mix direct tools with grouped tools
- Lazy loading: Tools load only when needed
- Non-blocking refresh: Tool refresh happens in background
- Backward compatible: Discovery is entirely opt-in
- No protocol changes: Works with existing MCP clients
## Usage Example
```python
from mcp.server import Server
from mcp import ToolGroup, Tool
math_group = ToolGroup(
name='math',
description='Mathematical operations',
tools=[
Tool(name='add', description='Add numbers', inputSchema={...}),
Tool(name='subtract', description='Subtract numbers', inputSchema={...}),
]
)
server = Server('my-service')
server.enable_discovery_with_groups([math_group])
```
## Testing
- 5 new discovery-specific tests: All passing
- 41/44 total tests passing (3 pre-existing unrelated failures)
- Backward compatibility verified with SDK examples
- Real-world examples with live weather APIs
## Files Changed
New files:
- src/mcp/server/discovery/__init__.py
- src/mcp/server/discovery/manager.py
- src/mcp/server/discovery/tool_group.py
- tests/test_discovery.py
- tests/test_discovery_integration.py
- examples/discovery/ (with server, agent, and README)
Modified files:
- src/mcp/__init__.py (export ToolGroup)
- src/mcp/client/session.py (callback support)
- src/mcp/client/session_group.py (tool refresh handling)
- src/mcp/server/lowlevel/server.py (discovery integration)
- tests/client/test_session_group.py (5 new tests)
## Benefits
- Token efficiency: Significant reduction in context token usage for large tool sets
- Scalability: Supports servers with many tools
- LLM autonomy: LLM decides which tools to load
- Clean architecture: Semantic grouping is explicit
- Backward compatible: No breaking changes, fully opt-in
|
Updated with latest commit: This includes:
Example server now demonstrates clean, focused progressive tool discovery with zero complexity. |
06878f8 to
2883104
Compare
|
Hey thank you for putting this all together, it's an interesting take on solving this problem. For now we won't be able to merge this into the SDK as this isn't something that's part of the spec. Although these problems are very real, the way to solve them are actively being discussed and planned out without any decision yet. If you'd like to track or contribute to those discussions you can take a look at these:
Although, I definitely think the SDK should be able to support additions like this on top of the methods the SDK provides, and could be reworked/refactored to be able to better support these sort of low level additions/modifications of server behaviour (while still staying within the bounds of the MCP spec itself). So for now I'm going to close the PR for the above reasons. But, please take a look at the above links and see if there are any you'd like to contribute your ideas to, that'd actually be super helpful! |
[DRAFT PR FOR OBSERVABILITY] Overview
This PR implements optional progressive disclosure of MCP tools through semantic grouping and lazy-loading. Servers can organize tools into semantic groups with gateway tools that load actual tools on-demand, achieving significant reduction in context token usage at the start of conversations.
Problem Being Solved
MCP servers with 50+ tools currently expose all tool definitions upfront, causing significant context bloat. This wastes valuable context budget and makes it harder for LLMs to reason effectively. Additionally, server developers struggle to unify disparate tools across different domains into a coherent organization system.
Solution
Progressive tool discovery allows servers to:
This enables server developers to organize disparate tools (like GitHub's repo management, pull requests, code changes, collaboration features) into semantic groups that make sense for users and LLMs.
Key Features
is_discovery_enableddefaults to FalseImplementation
New components:
Example Use Case
Testing
All tests pass:
Backward Compatibility
100% compatible:
Benefits
1. Improved LLM Reasoning & Reduced Hallucination
With fewer tools in context, LLMs have better focus and make fewer mistakes about which tools are available. Significantly reduces the likelihood of LLMs hallucinating about non-existent tools or choosing irrelevant ones.
2. Lower API Costs
Fewer tokens per request means reduced billing for users. This is especially impactful for high-volume applications where token savings directly translate to cost savings.
3. Faster Initial Connection
Quicker
listTools()response times at conversation start (500 tokens instead of 4,000+). Provides better user experience with lower latency on the critical first interaction.4. Extensibility for Large Codebases
Makes it trivial to add new tools without overwhelming LLMs with choices. Enables servers to grow from 10 tools to 100+ tools without degradation in LLM performance.
5. Aligns with Industry Standards
Follows the same pattern as OpenAI's function calling for large tool sets. Reflects best practices in API design and progressive disclosure that users expect.
6. Multi-Agent Ready
Different agents can be given different tool subsets based on their responsibilities. Enables more sophisticated orchestration patterns and agent specialization.
7. Better Composability
Makes it easier to combine multiple MCP servers and organize their tools coherently. Solves the "tool management" problem for complex deployments with many integrated services.
Impact
This feature addresses a fundamental scalability challenge in MCP as server toolsets grow larger. It enables the ecosystem to handle production-scale deployments with hundreds of tools while maintaining optimal LLM performance and user experience.