5.2 KiB
MCP Agent Description Integration
Overview
This implementation adds support for fetching agent tool descriptions from MCP (Model Context Protocol) endpoints during agent selection. This allows the system to use the actual tool descriptions from MCP servers for intelligent agent routing instead of relying solely on static descriptions in the configuration file.
Architecture
Components Modified
-
MCP Client Module (
brightstaff/src/utils/mcp_client.rs)- New module that handles communication with MCP servers via SSE
- Fetches tool lists and descriptions from MCP endpoints
- Parses MCP URLs in multiple formats
-
Configuration Structs (
common/src/configuration.rs)- Added optional
toolfield toAgentstruct - Added optional
toolfield toAgentFilterstruct - Supports specifying which MCP tool to invoke
- Added optional
-
Agent Selector (
brightstaff/src/handlers/agent_selector.rs)- Enhanced to fetch tool descriptions from MCP endpoints
- Uses MCP descriptions for agent routing when available
- Falls back to configuration descriptions if MCP fetch fails
-
Agent Chat Completions (
brightstaff/src/handlers/agent_chat_completions.rs)- Updated to pass agent_map to agent selector
- Ensures agent information is available during selection
How It Works
1. Configuration
Agents can now be configured with MCP URLs and optional tool names:
agents:
- id: rag_agent
url: mcp://host.docker.internal:10501
tool: invoke # Optional: defaults to agent id
- id: travel_agent
url: mcp://host.docker.internal:10502
agent_filters:
- id: query_rewriter
url: mcp://host.docker.internal:10500
tool: rewrite_query_with_archgw # Optional
2. MCP URL Parsing
The MCP client supports three URL formats:
mcp://host:port # Basic format
mcp://host:port/tool_name # Tool in path
mcp://host:port?tool=tool_name # Tool as query param
3. Description Fetching
During agent selection:
- Agent selector checks if agent URL starts with
mcp:// - If yes, calls MCP client to fetch tool description from endpoint
- MCP client makes GET request to
http://host:port/sse/tools/list - Parses SSE response to extract tool descriptions
- Returns description for specified tool (from config or URL)
- Falls back to config description if MCP fetch fails
4. Agent Routing
The fetched MCP tool descriptions are used in routing preferences:
ModelUsagePreference {
model: agent_id,
routing_preferences: vec![RoutingPreference {
name: agent_id,
description: mcp_description, // From MCP endpoint
}],
}
The LLM router uses these descriptions to select the appropriate agent based on the user's request.
Key Features
-
Automatic Description Fetching: Tool descriptions are automatically fetched from MCP servers when agents have
mcp://URLs -
Graceful Fallback: If MCP endpoint is unavailable or doesn't return a description, falls back to the description in arch_config.yaml
-
Multiple URL Formats: Supports flexible MCP URL specification for tool names
-
Async Operation: All MCP fetching is done asynchronously to avoid blocking
-
Comprehensive Logging: Debug and warning logs track MCP interactions
Example Usage
Configuration (arch_config.yaml)
version: v0.3.0
agents:
- id: rag_agent
url: mcp://host.docker.internal:10501
# Description will be fetched from MCP endpoint
agent_filters:
- id: query_rewriter
url: mcp://host.docker.internal:10500
tool: rewrite_query_with_archgw
listeners:
- type: agent
port: 8001
router: arch_agent_router
agents:
- id: rag_agent
description: fallback description if MCP unavailable
filter_chain:
- query_rewriter
Flow
- User sends request to listener
- Agent selector creates agent map from configuration
- For each agent in listener:
- Checks if agent URL is MCP (
mcp://) - Fetches tool description from MCP endpoint
- Uses fetched description for routing
- Checks if agent URL is MCP (
- LLM router selects best agent based on descriptions
- Filter chain processes request using selected agent
Testing
Unit tests cover:
- MCP URL parsing (basic, path, query param formats)
- Agent selection with MCP descriptions
- Fallback to config descriptions
- Agent map creation
Integration tests verify:
- End-to-end agent selection flow
- Pipeline processing with MCP agents
Error Handling
The implementation handles:
- Invalid MCP URLs (returns error)
- Unreachable MCP endpoints (logs warning, uses config description)
- Missing tool in MCP response (returns error)
- Empty descriptions (logs warning, uses config description)
- Network errors (falls back to config description)
Backward Compatibility
The changes are fully backward compatible:
toolfield is optional on Agent and AgentFilter- Non-MCP agents work as before
- Config descriptions still work when MCP is not used
Future Enhancements
Possible improvements:
- Cache MCP tool descriptions to reduce network calls
- Support for MCP stdio transport in addition to SSE
- Periodic refresh of MCP descriptions
- Support for fetching tool schemas along with descriptions
- Metrics for MCP endpoint availability and response times