pub struct McpClientHandler { /* private fields */ }rmcp only.Expand description
An MCP client handler that automatically re-fetches the tool list when the
server sends a notifications/tools/list_changed notification.
This handler implements rmcp::ClientHandler and bridges the MCP
notification lifecycle with Rig’s ToolServer.
When the MCP server’s available tools change, this handler:
- Re-fetches the full tool list from the MCP server
- Replaces or removes registrations still owned by this handler
- Leaves newer local and peer-handler same-name registrations intact
§Usage
Use McpClientHandler::connect for a streamlined setup that handles
connection, initial tool fetch, and registration in one call:
let tool_server_handle = ToolServer::new().run();
let handler = McpClientHandler::new(client_info, tool_server_handle.clone());
let mcp_service = handler.connect(transport).await?;The returned RunningService keeps the MCP connection alive. When the
server updates its tools, the handler automatically syncs with the tool server.
Implementations§
Source§impl McpClientHandler
impl McpClientHandler
Sourcepub fn new(
client_info: ClientInfo,
tool_server_handle: ToolServerHandle,
) -> Self
pub fn new( client_info: ClientInfo, tool_server_handle: ToolServerHandle, ) -> Self
Create a new handler with the given client info and tool server handle.
The tool_server_handle should be a clone of the handle used by the agent,
so that tool updates are reflected in agent requests. Registered tools get
DEFAULT_MCP_TOOL_TIMEOUT; change it with McpClientHandler::with_timeout.
Sourcepub fn with_timeout(self, timeout: impl Into<Option<Duration>>) -> Self
pub fn with_timeout(self, timeout: impl Into<Option<Duration>>) -> Self
Set (or clear) the per-call timeout applied to every MCP tool this handler
registers. Pass a Duration to bound calls, or None to disable.
This applies the same setting to every tool managed by the handler.
Sourcepub fn with_refresh_timeout(self, timeout: Duration) -> Self
pub fn with_refresh_timeout(self, timeout: Duration) -> Self
Set the deadline for initial and list-changed tool-list fetches.
Sourcepub async fn connect<T, E, A>(
self,
transport: T,
) -> Result<RunningService<RoleClient, Self>, McpClientError>
pub async fn connect<T, E, A>( self, transport: T, ) -> Result<RunningService<RoleClient, Self>, McpClientError>
Connect to an MCP server, fetch the initial tool list, and register all tools with the tool server.
Returns the running MCP service. The connection stays alive as long as the
returned RunningService is held. When the server sends
notifications/tools/list_changed, this handler automatically re-fetches
and re-registers tools.
§Errors
Returns McpClientError if the connection or initial tool fetch fails.