Skip to main content

McpServerManager

Struct McpServerManager 

Source
pub struct McpServerManager { /* private fields */ }
Expand description

Manages the lifecycle of one or more MCP servers: registration, connection, discovery, refresh, disconnection, and auth resolution.

This is the primary entry point for integrating MCP servers into an agentkit application. Register server configurations, connect them, and then obtain a combined ToolRegistry or McpCapabilityProvider for use in an agent loop.

§Example

use agentkit_mcp::{
    McpServerConfig, McpServerManager, McpTransportBinding, StdioTransportConfig,
};

let mut manager = McpServerManager::new()
    .with_server(McpServerConfig::new(
        "filesystem",
        McpTransportBinding::Stdio(
            StdioTransportConfig::new("npx")
                .with_arg("-y")
                .with_arg("@modelcontextprotocol/server-filesystem"),
        ),
    ))
    .with_server(McpServerConfig::new(
        "github",
        McpTransportBinding::Stdio(
            StdioTransportConfig::new("npx")
                .with_arg("-y")
                .with_arg("@modelcontextprotocol/server-github"),
        ),
    ));

let handles = manager.connect_all().await?;
let registry = manager.tool_registry();
println!("tools: {:?}", registry.specs().iter().map(|s| &s.name).collect::<Vec<_>>());

Implementations§

Source§

impl McpServerManager

Source

pub fn new() -> Self

Creates an empty server manager with no registered servers.

Source

pub fn with_server(self, config: McpServerConfig) -> Self

Registers a server configuration and returns self for chaining.

The server is not connected until connect_server or connect_all is called.

Source

pub fn register_server(&mut self, config: McpServerConfig) -> &mut Self

Registers a server configuration by mutable reference.

The server is not connected until connect_server or connect_all is called.

Source

pub fn connected_server( &self, server_id: &McpServerId, ) -> Option<&McpServerHandle>

Returns the handle for a connected server, or None if it is not connected.

Source

pub fn connected_servers(&self) -> Vec<&McpServerHandle>

Returns handles for all currently connected servers.

Source

pub async fn connect_server( &mut self, server_id: &McpServerId, ) -> Result<McpServerHandle, McpError>

Connects a single registered server by its identifier.

Performs the MCP handshake and full capability discovery.

§Errors

Returns McpError::UnknownServer if the server ID has not been registered, or other McpError variants if connection or discovery fails.

Source

pub async fn connect_all(&mut self) -> Result<Vec<McpServerHandle>, McpError>

Connects all registered servers sequentially.

Returns a handle for each server in registration order. If any server fails to connect, the error is returned immediately and remaining servers are not attempted.

§Errors

Returns the first McpError encountered during connection.

Source

pub async fn refresh_server( &mut self, server_id: &McpServerId, ) -> Result<McpDiscoverySnapshot, McpError>

Re-discovers capabilities for a connected server, updating the stored snapshot.

Call this after the server’s capabilities may have changed (e.g. after installing a plugin).

§Errors

Returns McpError::UnknownServer if the server is not connected, or other McpError variants if discovery fails.

Source

pub async fn disconnect_server( &mut self, server_id: &McpServerId, ) -> Result<(), McpError>

Disconnects a server and removes it from the active connections.

The server configuration remains registered and can be reconnected later with connect_server.

§Errors

Returns McpError::UnknownServer if the server is not connected.

Source

pub async fn resolve_auth( &mut self, resolution: AuthResolution, ) -> Result<(), McpError>

Stores or clears authentication credentials for a server and, if already connected, updates the live connection as well.

§Errors

Returns McpError::UnknownServer if the server ID from the resolution does not match any registered server.

Source

pub async fn resolve_auth_and_resume( &mut self, resolution: AuthResolution, ) -> Result<McpOperationResult, McpError>

Resolves authentication and immediately replays the operation that originally triggered the auth challenge.

This is a convenience method combining resolve_auth and replay_auth_request.

§Errors

Returns McpError if auth resolution or the replayed operation fails.

Source

pub async fn replay_auth_request( &mut self, request: &AuthRequest, ) -> Result<McpOperationResult, McpError>

Replays an auth request’s original MCP operation using stored credentials.

For connect operations the server is (re)connected. For tool calls, resource reads, and prompt retrievals the request is re-issued on the existing or newly established connection.

§Errors

Returns McpError if the operation cannot be replayed.

Source

pub fn tool_registry(&self) -> ToolRegistry

Builds a combined ToolRegistry containing McpToolAdapters for every tool discovered across all connected servers.

Tool names are prefixed as mcp_<server_id>_<tool_name>.

Source

pub fn capability_provider(&self) -> McpCapabilityProvider

Builds a combined McpCapabilityProvider from all connected servers, merging their tools, resources, and prompts.

Trait Implementations§

Source§

impl Default for McpServerManager

Source§

fn default() -> McpServerManager

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

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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<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