Files
agent-coordinator/DYNAMIC_TOOL_DISCOVERY.md
Ra 62319b87db feat: implement dynamic MCP tool discovery with shared server architecture
- Replace hardcoded tool lists with dynamic discovery via MCP tools/list
- Move MCPServerManager to application supervision tree for resource sharing
- Eliminate duplicate MCP server instances (one shared instance per server type)
- Add automatic tool refresh when servers restart
- Implement conditional VS Code tool loading based on module availability
- Add comprehensive test suite for dynamic discovery
- Update documentation with architecture improvements

Benefits:
- Full MCP protocol compliance
- Massive resource savings (shared servers vs per-agent instances)
- Zero maintenance overhead for tool list synchronization
- Automatic adaptation to server changes
- Improved reliability and performance

Closes: Dynamic tool discovery implementation
Fixes: Multiple MCP server instance resource waste
2025-08-23 15:47:12 -07:00

108 lines
4.1 KiB
Markdown

# Dynamic Tool Discovery Implementation Summary
## What We Accomplished
The Agent Coordinator has been successfully refactored to implement **fully dynamic tool discovery** following the MCP protocol specification, eliminating all hardcoded tool lists **and ensuring shared MCP server instances across all agents**.
## Key Changes Made
### 1. Removed Hardcoded Tool Lists
**Before**:
```elixir
coordinator_native = ~w[register_agent create_task get_next_task complete_task get_task_board heartbeat]
```
**After**:
```elixir
# Tools discovered dynamically by checking actual tool definitions
coordinator_tools = get_coordinator_tools()
if Enum.any?(coordinator_tools, fn tool -> tool["name"] == tool_name end) do
{:coordinator, tool_name}
end
```
### 2. Made VS Code Tools Conditional
**Before**: Always included VS Code tools even if not available
**After**:
```elixir
vscode_tools = try do
if Code.ensure_loaded?(AgentCoordinator.VSCodeToolProvider) do
AgentCoordinator.VSCodeToolProvider.get_tools()
else
[]
end
rescue
_ -> []
end
```
### 3. Added Shared MCP Server Management
**MAJOR FIX**: MCPServerManager is now part of the application supervision tree
**Before**: Each agent/test started its own MCP servers
- Multiple server instances for the same functionality
- Resource waste and potential conflicts
- Different OS PIDs per agent
**After**: Single shared MCP server instance
- Added to `application.ex` supervision tree
- All agents use the same MCP server processes
- Perfect resource sharing
### 4. Added Dynamic Tool Refresh
**New function**: `refresh_tools/0`
- Re-discovers tools from all running MCP servers
- Updates tool registry in real-time
- Handles both PID and Port server types properly
### 5. Enhanced Tool Routing
**Before**: Used hardcoded tool name lists for routing decisions
**After**: Checks actual tool definitions to determine routing## Test Results
✅ All tests passing with dynamic discovery:
```
Found 44 total tools:
• Coordinator tools: 6
• External MCP tools: 26+ (context7, filesystem, memory, sequential thinking)
• VS Code tools: 12 (when available)
```
**External servers discovered**:
- Context7: 2 tools (resolve-library-id, get-library-docs)
- Filesystem: 14 tools (read_file, write_file, edit_file, etc.)
- Memory: 9 tools (search_nodes, create_entities, etc.)
- Sequential Thinking: 1 tool (sequentialthinking)
## Benefits Achieved
1. **Perfect MCP Protocol Compliance**: No hardcoded assumptions, everything discovered via `tools/list`
2. **Shared Server Architecture**: Single MCP server instance shared by all agents (massive resource savings)
3. **Flexibility**: New MCP servers can be added via configuration without code changes
4. **Reliability**: Tools automatically re-discovered when servers restart
5. **Performance**: Only available tools included in routing decisions + shared server processes
6. **Maintainability**: No need to manually sync tool lists with server implementations
7. **Resource Efficiency**: No duplicate server processes per agent/session
8. **Debugging**: Clear visibility into which tools are available from which servers
## Files Modified
1. **`lib/agent_coordinator/mcp_server_manager.ex`**:
- Removed `get_coordinator_tool_names/0` function
- Modified `find_tool_server/2` to use dynamic discovery
- Added conditional VS Code tool loading
- Added `refresh_tools/0` and `rediscover_all_tools/1`
- Fixed Port vs PID handling for server aliveness checks
2. **Tests**:
- Added `test/dynamic_tool_discovery_test.exs`
- All existing tests still pass
- New tests verify dynamic discovery works correctly
## Impact
This refactoring makes the Agent Coordinator a true MCP-compliant aggregation server that follows the protocol specification exactly, rather than making assumptions about what tools servers provide. It's now much more flexible and maintainable while being more reliable in dynamic environments where servers may come and go.
The system now perfectly implements the user's original request: **"all tools will reply with what tools are available"** via the MCP protocol's `tools/list` method.