pub struct McpHub { /* private fields */ }Expand description
Central hub for MCP tool routing across multiple servers.
The McpHub provides:
- Connection management for multiple MCP servers
- Tool discovery and caching
- Automatic routing of tool calls to the correct server
- Circuit breaker protection for resilience
- Parallel tool discovery for performance
§Example
use mcp::{McpHub, McpServerConnectionConfig};
let hub = McpHub::new();
// Connect to an external server
let config = McpServerConnectionConfig::stdio("my-server", "node", vec!["server.js".into()]);
hub.connect(config).await?;
// List all available tools
let tools = hub.list_all_tools().await?;
// Call a tool (automatically routed to correct server)
let result = hub.call_tool("my_tool", serde_json::json!({"arg": "value"})).await?;Implementations§
Source§impl McpHub
impl McpHub
Sourcepub fn with_discovery_timeout(timeout: Duration) -> Self
pub fn with_discovery_timeout(timeout: Duration) -> Self
Create a hub with a custom discovery timeout.
Sourcepub async fn connect(
&self,
config: McpServerConnectionConfig,
) -> Result<Arc<dyn McpTransport>, McpTransportError>
pub async fn connect( &self, config: McpServerConnectionConfig, ) -> Result<Arc<dyn McpTransport>, McpTransportError>
Connect to an MCP server.
This method:
- Creates the appropriate transport based on config
- Initializes the connection
- Discovers tools and caches the mapping
- Returns the transport for direct access if needed
Sourcepub async fn call_tool(
&self,
name: &str,
args: Value,
) -> Result<Value, McpTransportError>
pub async fn call_tool( &self, name: &str, args: Value, ) -> Result<Value, McpTransportError>
Call a tool, automatically routing to the correct server.
Uses circuit breaker to prevent cascading failures - if a server is unhealthy, requests will be rejected immediately.
Sourcepub async fn list_tools(
&self,
) -> Result<Vec<(String, McpToolDefinition)>, McpTransportError>
pub async fn list_tools( &self, ) -> Result<Vec<(String, McpToolDefinition)>, McpTransportError>
List all tools from all connected servers.
Sourcepub async fn list_all_tools(
&self,
) -> Result<Vec<McpToolDefinition>, McpTransportError>
pub async fn list_all_tools( &self, ) -> Result<Vec<McpToolDefinition>, McpTransportError>
Get all registered tools as a flat list.
Sourcepub async fn discover_tools_parallel(
&self,
) -> Result<Vec<(String, McpToolDefinition)>, McpTransportError>
pub async fn discover_tools_parallel( &self, ) -> Result<Vec<(String, McpToolDefinition)>, McpTransportError>
Discover tools from all servers in parallel.
This is faster than sequential discovery when connecting to many servers.
Sourcepub async fn refresh_tool_cache(&self) -> Result<(), McpTransportError>
pub async fn refresh_tool_cache(&self) -> Result<(), McpTransportError>
Populate the tool cache by querying all servers (parallel).
Sourcepub async fn shutdown_all(&self) -> Result<(), McpTransportError>
pub async fn shutdown_all(&self) -> Result<(), McpTransportError>
Shutdown all connected servers.
Sourcepub async fn disconnect(
&self,
server_name: &str,
) -> Result<(), McpTransportError>
pub async fn disconnect( &self, server_name: &str, ) -> Result<(), McpTransportError>
Disconnect a specific server.
Sourcepub fn list_servers(&self) -> Vec<String>
pub fn list_servers(&self) -> Vec<String>
Get list of connected server names.
Sourcepub fn is_connected(&self, server_name: &str) -> bool
pub fn is_connected(&self, server_name: &str) -> bool
Check if a server is connected.
Sourcepub async fn health_check(&self) -> Vec<(String, bool)>
pub async fn health_check(&self) -> Vec<(String, bool)>
Get health status of all servers (includes circuit breaker state).
Sourcepub fn server_for_tool(&self, tool_name: &str) -> Option<String>
pub fn server_for_tool(&self, tool_name: &str) -> Option<String>
Get the server name that provides a specific tool.
Sourcepub fn circuit_breaker_stats(
&self,
server_name: &str,
) -> Option<CircuitBreakerStats>
pub fn circuit_breaker_stats( &self, server_name: &str, ) -> Option<CircuitBreakerStats>
Get circuit breaker statistics for a server.
Sourcepub fn reset_circuit_breaker(&self, server_name: &str)
pub fn reset_circuit_breaker(&self, server_name: &str)
Reset circuit breaker for a server (e.g., after manual recovery).