Skip to main content

lean_ctx/server/
notifications.rs

1use rmcp::model::{
2    Notification, NotificationNoParam, ResourceUpdatedNotificationParam, ServerNotification,
3    ToolListChangedNotificationMethod,
4};
5use rmcp::service::{Peer, RoleServer};
6
7pub async fn send_resource_updated(peer: &Peer<RoleServer>, uri: &str) {
8    let notif = Notification::new(ResourceUpdatedNotificationParam::new(uri));
9    let server_notif = ServerNotification::ResourceUpdatedNotification(notif);
10    if let Err(e) = peer.send_notification(server_notif).await {
11        tracing::debug!("Failed to send resource updated notification: {e}");
12    }
13}
14
15pub async fn send_tools_list_changed(peer: &Peer<RoleServer>) {
16    let notif = NotificationNoParam {
17        method: ToolListChangedNotificationMethod,
18        extensions: rmcp::model::Extensions::default(),
19    };
20    let server_notif = ServerNotification::ToolListChangedNotification(notif);
21    if let Err(e) = peer.send_notification(server_notif).await {
22        tracing::debug!("Failed to send tools list changed notification: {e}");
23    }
24}
25
26pub const RESOURCE_URI_SUMMARY: &str = "lean-ctx://context/summary";
27pub const RESOURCE_URI_PRESSURE: &str = "lean-ctx://context/pressure";
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn notification_types_construct_correctly() {
35        let notif = Notification::new(ResourceUpdatedNotificationParam::new(RESOURCE_URI_SUMMARY));
36        let sn = ServerNotification::ResourceUpdatedNotification(notif);
37        assert!(matches!(
38            sn,
39            ServerNotification::ResourceUpdatedNotification(_)
40        ));
41
42        let notif = NotificationNoParam {
43            method: ToolListChangedNotificationMethod,
44            extensions: rmcp::model::Extensions::default(),
45        };
46        let sn = ServerNotification::ToolListChangedNotification(notif);
47        assert!(matches!(
48            sn,
49            ServerNotification::ToolListChangedNotification(_)
50        ));
51    }
52}