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.
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,
Called when the server sends a notification. Read more
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,
Handle a
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,
Handle an
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,
Handle a
roots/list request from the server. Read moreSource§impl Debug for NotificationHandler
impl Debug for NotificationHandler
Auto Trait Implementations§
impl Freeze for NotificationHandler
impl !RefUnwindSafe for NotificationHandler
impl Send for NotificationHandler
impl Sync for NotificationHandler
impl Unpin for NotificationHandler
impl UnsafeUnpin for NotificationHandler
impl !UnwindSafe for NotificationHandler
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
Mutably borrows from an owned value. Read more