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
§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 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.
Sourcepub async fn list_tools(
&self,
) -> Result<Vec<(String, ToolDefinition)>, McpTransportError>
pub async fn list_tools( &self, ) -> Result<Vec<(String, ToolDefinition)>, McpTransportError>
List all tools from all connected servers.
Sourcepub async fn list_all_tools(
&self,
) -> Result<Vec<ToolDefinition>, McpTransportError>
pub async fn list_all_tools( &self, ) -> Result<Vec<ToolDefinition>, McpTransportError>
Get all registered tools as a flat list.
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.
Sourcepub fn register_tool_sync(&self, tool_name: &str, server_name: &str)
pub fn register_tool_sync(&self, tool_name: &str, server_name: &str)
Manually register a tool in the cache.
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 fn health_check(&self) -> Vec<(String, bool)>
pub 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.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for McpHub
impl RefUnwindSafe for McpHub
impl Send for McpHub
impl Sync for McpHub
impl Unpin for McpHub
impl UnwindSafe for McpHub
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more