|
| 1 | +# Dynamic Tool Discovery and Invocation |
| 2 | + |
| 3 | +The MCP Gateway & Registry provides a powerful **Dynamic Tool Discovery and Invocation** feature that enables AI agents to autonomously discover and execute tools beyond their initial capabilities. This feature uses advanced semantic search with FAISS indexing and sentence transformers to intelligently match natural language queries to the most relevant MCP tools across all registered servers. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Overview](#overview) |
| 8 | +- [How It Works](#how-it-works) |
| 9 | +- [Architecture](#architecture) |
| 10 | +- [Usage Examples](#usage-examples) |
| 11 | +- [Agent Integration](#agent-integration) |
| 12 | +- [API Reference](#api-reference) |
| 13 | +- [Technical Implementation](#technical-implementation) |
| 14 | +- [Demo](#demo) |
| 15 | + |
| 16 | +## Overview |
| 17 | + |
| 18 | +Traditional AI agents are limited to the tools they were initially configured with. The Dynamic Tool Discovery feature breaks this limitation by allowing agents to: |
| 19 | + |
| 20 | +1. **Discover new tools** through natural language queries |
| 21 | +2. **Automatically find** the most relevant tools from hundreds of available MCP servers |
| 22 | +3. **Dynamically invoke** discovered tools without prior configuration |
| 23 | +4. **Expand capabilities** on-demand based on user requests |
| 24 | + |
| 25 | +This enables agents to handle tasks they weren't originally designed for, making them truly adaptive and extensible. |
| 26 | + |
| 27 | +## How It Works |
| 28 | + |
| 29 | +The dynamic tool discovery process follows these steps: |
| 30 | + |
| 31 | +1. **Natural Language Query**: Agent receives a user request requiring specialized capabilities |
| 32 | +2. **Semantic Search**: The `intelligent_tool_finder` tool processes the query using sentence transformers |
| 33 | +3. **FAISS Index Search**: Searches through embeddings of all registered MCP tools |
| 34 | +4. **Relevance Ranking**: Returns tools ranked by semantic similarity to the query |
| 35 | +5. **Tool Invocation**: Agent uses the discovered tool information to invoke the appropriate MCP tool |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +## Architecture |
| 40 | + |
| 41 | +### Components |
| 42 | + |
| 43 | +```mermaid |
| 44 | +graph TB |
| 45 | + subgraph "Agent Layer" |
| 46 | + A[AI Agent] --> B[Natural Language Query] |
| 47 | + end |
| 48 | + |
| 49 | + subgraph "Discovery Layer" |
| 50 | + B --> C[intelligent_tool_finder] |
| 51 | + C --> D[Sentence Transformer] |
| 52 | + C --> E[FAISS Index] |
| 53 | + end |
| 54 | + |
| 55 | + subgraph "Registry Layer" |
| 56 | + E --> F[Tool Metadata] |
| 57 | + F --> G[Server Information] |
| 58 | + end |
| 59 | + |
| 60 | + subgraph "Execution Layer" |
| 61 | + G --> H[invoke_mcp_tool] |
| 62 | + H --> I[Target MCP Server] |
| 63 | + end |
| 64 | + |
| 65 | + I --> J[Tool Result] |
| 66 | + J --> A |
| 67 | +``` |
| 68 | + |
| 69 | +### Key Technologies |
| 70 | + |
| 71 | +- **FAISS (Facebook AI Similarity Search)**: High-performance vector similarity search |
| 72 | +- **Sentence Transformers**: Neural network models for semantic text understanding |
| 73 | +- **Cosine Similarity**: Mathematical measure of semantic similarity between queries and tools |
| 74 | +- **MCP Protocol**: Standardized communication with tool servers |
| 75 | + |
| 76 | +## Usage Examples |
| 77 | + |
| 78 | +### Basic Tool Discovery |
| 79 | + |
| 80 | +```python |
| 81 | +# Agent discovers time-related tools |
| 82 | +result = intelligent_tool_finder("current time in different timezone") |
| 83 | + |
| 84 | +# Returns information about relevant tools: |
| 85 | +# { |
| 86 | +# "tool_name": "current_time_by_timezone", |
| 87 | +# "service_path": "/currenttime", |
| 88 | +# "service_name": "Current Time Server", |
| 89 | +# "tool_schema": {...}, |
| 90 | +# "overall_similarity_score": 0.89 |
| 91 | +# } |
| 92 | +``` |
| 93 | + |
| 94 | +### Complete Workflow Example |
| 95 | + |
| 96 | +```python |
| 97 | +# 1. Discover tools for weather information |
| 98 | +weather_tools = intelligent_tool_finder("weather forecast for tomorrow") |
| 99 | + |
| 100 | +# 2. Use the discovered tool |
| 101 | +if weather_tools: |
| 102 | + tool_info = weather_tools[0] # Get the best match |
| 103 | + |
| 104 | + result = invoke_mcp_tool( |
| 105 | + mcp_registry_url="https://your-registry.com/mcpgw/sse", |
| 106 | + server_name=tool_info["service_path"], # e.g., "/weather" |
| 107 | + tool_name=tool_info["tool_name"], # e.g., "get_forecast" |
| 108 | + arguments={"location": "New York", "days": 1}, |
| 109 | + auth_token=auth_token, |
| 110 | + user_pool_id=user_pool_id, |
| 111 | + client_id=client_id, |
| 112 | + region=region, |
| 113 | + auth_method="m2m" |
| 114 | + ) |
| 115 | +``` |
| 116 | + |
| 117 | +## Agent Integration |
| 118 | + |
| 119 | +### System Prompt Configuration |
| 120 | + |
| 121 | +Agents are configured with instructions on how to use dynamic tool discovery: |
| 122 | + |
| 123 | +```text |
| 124 | +<tool_discovery> |
| 125 | +When a user requests something that requires a specialized tool you don't have direct access to, use the intelligent_tool_finder tool. |
| 126 | +
|
| 127 | +How to use intelligent_tool_finder: |
| 128 | +1. When you identify that a task requires a specialized tool (e.g., weather forecast, time information, etc.) |
| 129 | +2. Call the tool with a description of what you need: `intelligent_tool_finder("description of needed capability")`. |
| 130 | +3. The tool will return the most appropriate specialized tool along with usage instructions |
| 131 | +4. You can then use the invoke_mcp_tool to invoke this discovered tool by providing the MCP Registry URL, server name, tool name, and required arguments |
| 132 | +
|
| 133 | +Example workflow: |
| 134 | +1. Discover a tool: result = intelligent_tool_finder("current time timezone") |
| 135 | +2. The result provides details about a time tool on the "currenttime" MCP server. |
| 136 | +3. Always use the "service_path" path field for the server name while creating the arguments for the invoke_mcp_tool in the next step. |
| 137 | +4. Use invoke_mcp_tool to call it with ALL required auth parameters |
| 138 | +</tool_discovery> |
| 139 | +``` |
| 140 | + |
| 141 | +### Agent Implementation |
| 142 | + |
| 143 | +The agent implementation in [`agents/agent.py`](../agents/agent.py) shows how to: |
| 144 | + |
| 145 | +1. **Load MCP tools** from the registry |
| 146 | +2. **Combine built-in and discovered tools** |
| 147 | +3. **Handle authentication** for both session cookie and M2M methods |
| 148 | +4. **Process tool discovery results** |
| 149 | + |
| 150 | +Key code snippet: |
| 151 | + |
| 152 | +```python |
| 153 | +# Get available tools from MCP and display them |
| 154 | +mcp_tools = await client.get_tools() |
| 155 | +logger.info(f"Available MCP tools: {[tool.name for tool in mcp_tools]}") |
| 156 | + |
| 157 | +# Add the calculator and invoke_mcp_tool to the tools array |
| 158 | +# The invoke_mcp_tool function already supports authentication parameters |
| 159 | +all_tools = [calculator, invoke_mcp_tool] + mcp_tools |
| 160 | +logger.info(f"All available tools: {[tool.name if hasattr(tool, 'name') else tool.__name__ for tool in all_tools]}") |
| 161 | + |
| 162 | +# Create the agent with the model and all tools |
| 163 | +agent = create_react_agent(model, all_tools) |
| 164 | +``` |
| 165 | + |
| 166 | +## API Reference |
| 167 | + |
| 168 | +### intelligent_tool_finder |
| 169 | + |
| 170 | +Finds the most relevant MCP tool(s) across all registered and enabled services based on a natural language query. |
| 171 | + |
| 172 | +#### Parameters |
| 173 | + |
| 174 | +| Parameter | Type | Required | Description | |
| 175 | +|-----------|------|----------|-------------| |
| 176 | +| `natural_language_query` | `str` | Yes | Your query in natural language describing the task you want to perform | |
| 177 | +| `username` | `str` | No* | Username for mcpgw server authentication | |
| 178 | +| `password` | `str` | No* | Password for mcpgw server authentication | |
| 179 | +| `session_cookie` | `str` | No* | Session cookie for registry authentication | |
| 180 | +| `top_k_services` | `int` | No | Number of top services to consider from initial FAISS search (default: 3) | |
| 181 | +| `top_n_tools` | `int` | No | Number of best matching tools to return (default: 1) | |
| 182 | + |
| 183 | +*Either `session_cookie` OR (`username` AND `password`) must be provided for authentication. |
| 184 | + |
| 185 | +#### Returns |
| 186 | + |
| 187 | +```python |
| 188 | +List[Dict[str, Any]] |
| 189 | +``` |
| 190 | + |
| 191 | +A list of dictionaries, each describing a recommended tool: |
| 192 | + |
| 193 | +```python |
| 194 | +[ |
| 195 | + { |
| 196 | + "tool_name": "current_time_by_timezone", |
| 197 | + "tool_parsed_description": { |
| 198 | + "main": "Get current time for a specific timezone", |
| 199 | + "parameters": {...} |
| 200 | + }, |
| 201 | + "tool_schema": { |
| 202 | + "type": "object", |
| 203 | + "properties": {...} |
| 204 | + }, |
| 205 | + "service_path": "/currenttime", |
| 206 | + "service_name": "Current Time Server", |
| 207 | + "overall_similarity_score": 0.89 |
| 208 | + } |
| 209 | +] |
| 210 | +``` |
| 211 | + |
| 212 | +#### Example Usage |
| 213 | + |
| 214 | +```python |
| 215 | +# Basic usage with session cookie |
| 216 | +tools = await intelligent_tool_finder( |
| 217 | + natural_language_query="what time is it in Tokyo", |
| 218 | + session_cookie="your_session_cookie_here" |
| 219 | +) |
| 220 | + |
| 221 | +# Advanced usage with multiple results |
| 222 | +tools = await intelligent_tool_finder( |
| 223 | + natural_language_query="stock market information and financial data", |
| 224 | + username="admin", |
| 225 | + password="your_password", |
| 226 | + top_k_services=5, |
| 227 | + top_n_tools=3 |
| 228 | +) |
| 229 | +``` |
| 230 | + |
| 231 | +## Technical Implementation |
| 232 | + |
| 233 | +### FAISS Index Creation |
| 234 | + |
| 235 | +The registry automatically creates and maintains a FAISS index of all registered MCP tools: |
| 236 | + |
| 237 | +1. **Tool Metadata Collection**: Gathers tool descriptions, schemas, and server information |
| 238 | +2. **Text Embedding**: Uses sentence transformers to create vector embeddings |
| 239 | +3. **Index Building**: Constructs FAISS index for fast similarity search |
| 240 | +4. **Automatic Updates**: Refreshes index when servers are added/modified |
| 241 | + |
| 242 | +### Semantic Search Process |
| 243 | + |
| 244 | +```python |
| 245 | +# 1. Embed the natural language query |
| 246 | +query_embedding = await asyncio.to_thread(_embedding_model_mcpgw.encode, [natural_language_query]) |
| 247 | +query_embedding_np = np.array(query_embedding, dtype=np.float32) |
| 248 | + |
| 249 | +# 2. Search FAISS for top_k_services |
| 250 | +distances, faiss_ids = await asyncio.to_thread(_faiss_index_mcpgw.search, query_embedding_np, top_k_services) |
| 251 | + |
| 252 | +# 3. Collect tools from top services |
| 253 | +candidate_tools = [] |
| 254 | +for service in top_services: |
| 255 | + for tool in service.tools: |
| 256 | + tool_text = f"Service: {service.name}. Tool: {tool.name}. Description: {tool.description}" |
| 257 | + candidate_tools.append({ |
| 258 | + "text_for_embedding": tool_text, |
| 259 | + "tool_name": tool.name, |
| 260 | + "service_path": service.path, |
| 261 | + # ... other metadata |
| 262 | + }) |
| 263 | + |
| 264 | +# 4. Embed all candidate tool descriptions |
| 265 | +tool_embeddings = await asyncio.to_thread(_embedding_model_mcpgw.encode, tool_texts) |
| 266 | + |
| 267 | +# 5. Calculate cosine similarity and rank |
| 268 | +similarities = cosine_similarity(query_embedding_np, tool_embeddings_np)[0] |
| 269 | +ranked_tools = sorted(tools_with_scores, key=lambda x: x["similarity_score"], reverse=True) |
| 270 | +``` |
| 271 | + |
| 272 | +### Performance Optimizations |
| 273 | + |
| 274 | +- **Lazy Loading**: FAISS index and models are loaded on-demand |
| 275 | +- **Caching**: Embeddings and metadata are cached and reloaded only when files change |
| 276 | +- **Async Processing**: All embedding operations run in separate threads |
| 277 | +- **Memory Efficiency**: Uses float32 precision for embeddings to reduce memory usage |
| 278 | + |
| 279 | +### Model Configuration |
| 280 | + |
| 281 | +The system uses configurable sentence transformer models: |
| 282 | + |
| 283 | +```python |
| 284 | +# Default model (lightweight, fast) |
| 285 | +EMBEDDINGS_MODEL_NAME = 'all-MiniLM-L6-v2' # 384 dimensions |
| 286 | + |
| 287 | +# Model loading with caching |
| 288 | +model_cache_path = _registry_server_data_path.parent / ".cache" |
| 289 | +_embedding_model_mcpgw = SentenceTransformer(EMBEDDINGS_MODEL_NAME, cache_folder=model_cache_path) |
| 290 | +``` |
| 291 | + |
| 292 | +## Demo |
| 293 | + |
| 294 | +### Example Interaction |
| 295 | + |
| 296 | +**User Query**: "What's the current time in Tokyo?" |
| 297 | + |
| 298 | +**Agent Process**: |
| 299 | +1. Agent recognizes need for time information |
| 300 | +2. Calls `intelligent_tool_finder("current time in Tokyo")` |
| 301 | +3. Discovers `current_time_by_timezone` tool from `/currenttime` server |
| 302 | +4. Invokes tool with `{"tz_name": "Asia/Tokyo"}` |
| 303 | +5. Returns formatted time result |
| 304 | + |
| 305 | +**Result**: "The current time in Tokyo is 2024-01-15 14:30:45 JST" |
| 306 | + |
| 307 | +### Performance Metrics |
| 308 | + |
| 309 | +- **Search Speed**: < 100ms for typical queries |
| 310 | +- **Accuracy**: 85-95% relevance for well-described tools |
| 311 | +- **Scalability**: Handles 1000+ registered tools efficiently |
| 312 | +- **Memory Usage**: ~200MB for model and index data |
| 313 | + |
| 314 | +## Best Practices |
| 315 | + |
| 316 | +### For Tool Developers |
| 317 | + |
| 318 | +1. **Descriptive Names**: Use clear, descriptive tool names |
| 319 | +2. **Rich Descriptions**: Provide detailed tool descriptions with use cases |
| 320 | +3. **Proper Schemas**: Include comprehensive parameter schemas |
| 321 | +4. **Consistent Naming**: Follow naming conventions for better discoverability |
| 322 | + |
| 323 | +### For Agent Developers |
| 324 | + |
| 325 | +1. **Specific Queries**: Use specific, descriptive queries for better matches |
| 326 | +2. **Fallback Handling**: Implement fallbacks when no suitable tools are found |
| 327 | +3. **Authentication**: Always include proper authentication parameters |
| 328 | +4. **Error Handling**: Handle tool discovery and invocation errors gracefully |
| 329 | + |
| 330 | +### For System Administrators |
| 331 | + |
| 332 | +1. **Index Maintenance**: Monitor FAISS index updates and performance |
| 333 | +2. **Model Updates**: Consider updating sentence transformer models periodically |
| 334 | +3. **Server Health**: Ensure registered servers are healthy and responsive |
| 335 | +4. **Access Control**: Configure proper authentication and authorization |
0 commit comments