pub struct NotificationHandler { /* private fields */ }Expand description
Callback-based handler for server notifications.
Provides typed callback registration for each notification type,
without requiring a full ClientHandler trait implementation.
Server-initiated requests (sampling, elicitation, roots) are
rejected with method_not_found.
§Example
use tower_mcp::client::NotificationHandler;
let handler = NotificationHandler::new()
.on_progress(|p| {
println!("Progress: {}/{}", p.progress, p.total.unwrap_or(1.0));
})
.on_tools_changed(|| {
println!("Server tools changed!");
});Implementations§
Source§impl NotificationHandler
impl NotificationHandler
Sourcepub fn with_log_forwarding() -> Self
pub fn with_log_forwarding() -> Self
Create a handler that forwards MCP log messages to tracing.
Maps MCP log levels to tracing levels:
- Emergency, Alert, Critical ->
error! - Error ->
error! - Warning ->
warn! - Notice, Info ->
info! - Debug ->
debug!
Sourcepub fn on_progress(
self,
f: impl Fn(ProgressParams) + Send + Sync + 'static,
) -> Self
pub fn on_progress( self, f: impl Fn(ProgressParams) + Send + Sync + 'static, ) -> Self
Register a callback for progress notifications.
Sourcepub fn on_log_message(
self,
f: impl Fn(LoggingMessageParams) + Send + Sync + 'static,
) -> Self
pub fn on_log_message( self, f: impl Fn(LoggingMessageParams) + Send + Sync + 'static, ) -> Self
Register a callback for log message notifications.
Sourcepub fn on_resource_updated(
self,
f: impl Fn(String) + Send + Sync + 'static,
) -> Self
pub fn on_resource_updated( self, f: impl Fn(String) + Send + Sync + 'static, ) -> Self
Register a callback for resource updated notifications.
The callback receives the URI of the updated resource.
Sourcepub fn on_resources_changed(self, f: impl Fn() + Send + Sync + 'static) -> Self
pub fn on_resources_changed(self, f: impl Fn() + Send + Sync + 'static) -> Self
Register a callback for resources list changed notifications.
Sourcepub fn on_tools_changed(self, f: impl Fn() + Send + Sync + 'static) -> Self
pub fn on_tools_changed(self, f: impl Fn() + Send + Sync + 'static) -> Self
Register a callback for tools list changed notifications.
Sourcepub fn on_prompts_changed(self, f: impl Fn() + Send + Sync + 'static) -> Self
pub fn on_prompts_changed(self, f: impl Fn() + Send + Sync + 'static) -> Self
Register a callback for prompts list changed notifications.
Sourcepub fn on_task_status_changed(
self,
f: impl Fn(TaskStatusParams) + Send + Sync + 'static,
) -> Self
pub fn on_task_status_changed( self, f: impl Fn(TaskStatusParams) + Send + Sync + 'static, ) -> Self
Register a callback for legacy task status notifications.
Sourcepub fn on_final_task_status_changed(
self,
f: impl Fn(TaskStatusNotificationParams) + Send + Sync + 'static,
) -> Self
pub fn on_final_task_status_changed( self, f: impl Fn(TaskStatusNotificationParams) + Send + Sync + 'static, ) -> Self
Register a callback for final-protocol task status notifications.
Trait Implementations§
Source§impl ClientHandler for NotificationHandler
impl ClientHandler for NotificationHandler
Source§fn on_notification<'life0, 'async_trait>(
&'life0 self,
notification: ServerNotification,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn on_notification<'life0, 'async_trait>(
&'life0 self,
notification: ServerNotification,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn handle_create_message<'life0, 'async_trait>(
&'life0 self,
_params: CreateMessageParams,
) -> Pin<Box<dyn Future<Output = Result<CreateMessageResult, JsonRpcError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn handle_create_message<'life0, 'async_trait>(
&'life0 self,
_params: CreateMessageParams,
) -> Pin<Box<dyn Future<Output = Result<CreateMessageResult, JsonRpcError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
sampling/createMessage request from the server. Read moreSource§fn handle_elicit<'life0, 'async_trait>(
&'life0 self,
_params: ElicitRequestParams,
) -> Pin<Box<dyn Future<Output = Result<ElicitResult, JsonRpcError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn handle_elicit<'life0, 'async_trait>(
&'life0 self,
_params: ElicitRequestParams,
) -> Pin<Box<dyn Future<Output = Result<ElicitResult, JsonRpcError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
elicitation/create request from the server. Read moreSource§fn handle_list_roots<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<ListRootsResult, JsonRpcError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn handle_list_roots<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<ListRootsResult, JsonRpcError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
roots/list request from the server. Read more