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
impl McpServerManager
Sourcepub fn with_server(self, config: McpServerConfig) -> Self
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.
Sourcepub fn register_server(&mut self, config: McpServerConfig) -> &mut Self
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.
Sourcepub fn connected_server(
&self,
server_id: &McpServerId,
) -> Option<&McpServerHandle>
pub fn connected_server( &self, server_id: &McpServerId, ) -> Option<&McpServerHandle>
Returns the handle for a connected server, or None if it is not connected.
Sourcepub fn connected_servers(&self) -> Vec<&McpServerHandle>
pub fn connected_servers(&self) -> Vec<&McpServerHandle>
Returns handles for all currently connected servers.
Sourcepub async fn connect_server(
&mut self,
server_id: &McpServerId,
) -> Result<McpServerHandle, McpError>
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.
Sourcepub async fn connect_all(&mut self) -> Result<Vec<McpServerHandle>, McpError>
pub async fn connect_all(&mut self) -> Result<Vec<McpServerHandle>, McpError>
Sourcepub async fn refresh_server(
&mut self,
server_id: &McpServerId,
) -> Result<McpDiscoverySnapshot, McpError>
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.
Sourcepub async fn disconnect_server(
&mut self,
server_id: &McpServerId,
) -> Result<(), McpError>
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.
Sourcepub async fn resolve_auth(
&mut self,
resolution: AuthResolution,
) -> Result<(), McpError>
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.
Sourcepub async fn resolve_auth_and_resume(
&mut self,
resolution: AuthResolution,
) -> Result<McpOperationResult, McpError>
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.
Sourcepub async fn replay_auth_request(
&mut self,
request: &AuthRequest,
) -> Result<McpOperationResult, McpError>
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.
Sourcepub fn tool_registry(&self) -> ToolRegistry
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>.
Sourcepub fn capability_provider(&self) -> McpCapabilityProvider
pub fn capability_provider(&self) -> McpCapabilityProvider
Builds a combined McpCapabilityProvider from all connected servers,
merging their tools, resources, and prompts.