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:
- Removes previously registered MCP tools from the tool server
- Re-fetches the full tool list from the MCP server
- Registers the updated tools with the tool server
§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.
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 fails, the initial tool fetch
fails, or tool registration with the tool server fails.
Trait Implementations§
Source§impl ClientHandler for McpClientHandler
impl ClientHandler for McpClientHandler
fn get_info(&self) -> ClientInfo
async fn on_tool_list_changed(&self, context: NotificationContext<RoleClient>)
fn ping( &self, context: RequestContext<RoleClient>, ) -> impl Future<Output = Result<(), ErrorData>> + Send
fn create_message( &self, params: CreateMessageRequestParams, context: RequestContext<RoleClient>, ) -> impl Future<Output = Result<CreateMessageResult, ErrorData>> + Send
fn list_roots( &self, context: RequestContext<RoleClient>, ) -> impl Future<Output = Result<ListRootsResult, ErrorData>> + Send
Source§fn create_elicitation(
&self,
request: CreateElicitationRequestParams,
context: RequestContext<RoleClient>,
) -> impl Future<Output = Result<CreateElicitationResult, ErrorData>> + Send
fn create_elicitation( &self, request: CreateElicitationRequestParams, context: RequestContext<RoleClient>, ) -> impl Future<Output = Result<CreateElicitationResult, ErrorData>> + Send
fn on_custom_request( &self, request: CustomRequest, context: RequestContext<RoleClient>, ) -> impl Future<Output = Result<CustomResult, ErrorData>> + Send
fn on_cancelled( &self, params: CancelledNotificationParam, context: NotificationContext<RoleClient>, ) -> impl Future<Output = ()> + Send
fn on_progress( &self, params: ProgressNotificationParam, context: NotificationContext<RoleClient>, ) -> impl Future<Output = ()> + Send
fn on_logging_message( &self, params: LoggingMessageNotificationParam, context: NotificationContext<RoleClient>, ) -> impl Future<Output = ()> + Send
fn on_resource_updated( &self, params: ResourceUpdatedNotificationParam, context: NotificationContext<RoleClient>, ) -> impl Future<Output = ()> + Send
fn on_resource_list_changed( &self, context: NotificationContext<RoleClient>, ) -> impl Future<Output = ()> + Send
fn on_prompt_list_changed( &self, context: NotificationContext<RoleClient>, ) -> impl Future<Output = ()> + Send
fn on_url_elicitation_notification_complete( &self, params: ElicitationResponseNotificationParam, context: NotificationContext<RoleClient>, ) -> impl Future<Output = ()> + Send
fn on_custom_notification( &self, notification: CustomNotification, context: NotificationContext<RoleClient>, ) -> impl Future<Output = ()> + Send
Auto Trait Implementations§
impl Freeze for McpClientHandler
impl !RefUnwindSafe for McpClientHandler
impl Send for McpClientHandler
impl Sync for McpClientHandler
impl Unpin for McpClientHandler
impl UnsafeUnpin for McpClientHandler
impl !UnwindSafe for McpClientHandler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<R, S> DynService<R> for Swhere
R: ServiceRole,
S: Service<R>,
impl<R, S> DynService<R> for Swhere
R: ServiceRole,
S: Service<R>,
fn handle_request( &self, request: <R as ServiceRole>::PeerReq, context: RequestContext<R>, ) -> Pin<Box<dyn Future<Output = Result<<R as ServiceRole>::Resp, ErrorData>> + Send + '_>>
fn handle_notification( &self, notification: <R as ServiceRole>::PeerNot, context: NotificationContext<R>, ) -> Pin<Box<dyn Future<Output = Result<(), ErrorData>> + Send + '_>>
fn get_info(&self) -> <R as ServiceRole>::Info
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more