Skip to main content

McpServerHub

Struct McpServerHub 

Source
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

Source

pub fn new(name: impl Into<String>) -> Self

Create a new hub with the given name.

Source

pub fn with_timeout(name: impl Into<String>, timeout: Duration) -> Self

Create a hub with a custom timeout.

Source

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

Connect to an external MCP server.

This method:

  1. Creates the appropriate transport based on config
  2. Initializes the connection
  3. Discovers tools and creates proxy tools for them
  4. Starts a restart monitor if restart policy is enabled
Source

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

Trigger an immediate restart for a specific server.

Source

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.

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>

List all tool definitions.

Source

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

Discover tools from all servers in parallel.

Source

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

Refresh tool cache by re-querying all servers (parallel).

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 is_alive(&self, server_name: &str) -> bool

Check if a server is connected and alive.

Source

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

Get health status of all servers.

Source

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

Get the server name that provides a specific tool.

Source

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

Disconnect a specific server (stops restart monitor).

Source

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

Shutdown all connected servers.

Source

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.

Source

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.

Source

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();
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.

Auto Trait Implementations§

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