pub struct McpServerHub { /* private fields */ }Expand description
A hub that aggregates multiple MCP servers into a single MCP server.
This allows you to:
- Connect to multiple external MCP servers
- Expose all their tools through a single unified server
- Wrap the hub with McpStdioServer or McpHttpServer
- Automatically restart servers on failure
- Parallel tool discovery for performance
Tools from connected servers are automatically discovered and made available through the hub’s server interface.
Implementations§
Source§impl McpServerHub
impl McpServerHub
Sourcepub fn with_timeout(name: impl Into<String>, timeout: Duration) -> Self
pub fn with_timeout(name: impl Into<String>, timeout: Duration) -> Self
Create a hub with a custom timeout.
Sourcepub async fn connect(
self: &Arc<Self>,
config: McpServerConnectionConfig,
) -> Result<(), McpTransportError>
pub async fn connect( self: &Arc<Self>, config: McpServerConnectionConfig, ) -> Result<(), McpTransportError>
Connect to an external MCP server.
This method:
- Creates the appropriate transport based on config
- Initializes the connection
- Discovers tools and creates proxy tools for them
- Starts a restart monitor if restart policy is enabled
Sourcepub fn trigger_restart(&self, server_name: &str)
pub fn trigger_restart(&self, server_name: &str)
Trigger an immediate restart for a specific server.
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 by name, routing to the correct server.
If the server restarts while a request is pending, the request will
immediately fail with a ServerRestarting error rather than timing out.
Uses circuit breaker to prevent cascading failures.
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>
List all tool definitions.
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.
Sourcepub async fn refresh_tools(&self) -> Result<(), McpTransportError>
pub async fn refresh_tools(&self) -> Result<(), McpTransportError>
Refresh tool cache by re-querying all servers (parallel).
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 is_alive(&self, server_name: &str) -> bool
pub async fn is_alive(&self, server_name: &str) -> bool
Check if a server is connected and alive.
Sourcepub async fn health_check(&self) -> Vec<(String, bool)>
pub async fn health_check(&self) -> Vec<(String, bool)>
Get health status of all servers.
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 async fn disconnect(
&self,
server_name: &str,
) -> Result<(), McpTransportError>
pub async fn disconnect( &self, server_name: &str, ) -> Result<(), McpTransportError>
Disconnect a specific server (stops restart monitor).
Sourcepub async fn shutdown_all(&self) -> Result<(), McpTransportError>
pub async fn shutdown_all(&self) -> Result<(), McpTransportError>
Shutdown all connected servers.
Sourcepub fn into_config(self, version: &str) -> McpServerConfig
pub fn into_config(self, version: &str) -> McpServerConfig
Convert this hub into an McpServerConfig that can be used with McpStdioServer or McpHttpServer.
This creates proxy tools that route calls to the connected external servers.
Sourcepub fn to_config(self: &Arc<Self>, version: &str) -> McpServerConfig
pub fn to_config(self: &Arc<Self>, version: &str) -> McpServerConfig
Create an McpServerConfig from this hub (keeps hub accessible).
Use this when you need to keep a reference to the hub for direct access.
Sourcepub fn proxy_tools(self: &Arc<Self>) -> Vec<DynTool> ⓘ
pub fn proxy_tools(self: &Arc<Self>) -> Vec<DynTool> ⓘ
Get proxy tools for all connected servers.
Use this when you want to combine hub tools with local tools:
let config = McpServerConfig::builder()
.name("my-server")
.version("1.0.0")
.register_tools_in_group("local") // Local tools
.with_tools(hub.proxy_tools()) // Proxied tools
.build();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.