Skip to main content

McpHub

Struct McpHub 

Source
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

Source

pub fn new() -> Self

Create a new empty hub.

Source

pub fn with_discovery_timeout(timeout: Duration) -> Self

Create a hub with a custom discovery timeout.

Source

pub async fn connect( &self, config: McpServerConnectionConfig, ) -> Result<Arc<dyn McpTransport>, McpTransportError>

Connect to an MCP server.

This method:

  1. Creates the appropriate transport based on config
  2. Initializes the connection
  3. Discovers tools and caches the mapping
  4. Returns the transport for direct access if needed
Source

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.

Source

pub async fn list_tools( &self, ) -> Result<Vec<(String, McpToolDefinition)>, McpTransportError>

List all tools from all connected servers.

Source

pub async fn list_all_tools( &self, ) -> Result<Vec<McpToolDefinition>, McpTransportError>

Get all registered tools as a flat list.

Source

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.

Source

pub async fn refresh_tool_cache(&self) -> Result<(), McpTransportError>

Populate the tool cache by querying all servers (parallel).

Source

pub async fn shutdown_all(&self) -> Result<(), McpTransportError>

Shutdown all connected servers.

Source

pub async fn disconnect( &self, server_name: &str, ) -> Result<(), McpTransportError>

Disconnect a specific server.

Source

pub fn list_servers(&self) -> Vec<String>

Get list of connected server names.

Source

pub fn is_connected(&self, server_name: &str) -> bool

Check if a server is connected.

Source

pub async fn health_check(&self) -> Vec<(String, bool)>

Get health status of all servers (includes circuit breaker state).

Source

pub fn server_for_tool(&self, tool_name: &str) -> Option<String>

Get the server name that provides a specific tool.

Source

pub fn circuit_breaker_stats( &self, server_name: &str, ) -> Option<CircuitBreakerStats>

Get circuit breaker statistics for a server.

Source

pub fn reset_circuit_breaker(&self, server_name: &str)

Reset circuit breaker for a server (e.g., after manual recovery).

Trait Implementations§

Source§

impl Default for McpHub

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more