1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Notification broadcasting for MCP servers
//!
//! This module provides a broadcast channel for file change events
//! that can be shared between file watchers and multiple MCP server instances.
use std::path::PathBuf;
use tokio::sync::broadcast;
#[derive(Debug, Clone)]
pub enum FileChangeEvent {
FileReindexed { path: PathBuf },
FileCreated { path: PathBuf },
FileDeleted { path: PathBuf },
IndexReloaded, // Entire index was reloaded from disk
}
/// Manages notification broadcasting to multiple MCP server instances
#[derive(Clone)]
pub struct NotificationBroadcaster {
sender: broadcast::Sender<FileChangeEvent>,
}
impl NotificationBroadcaster {
/// Create a new broadcaster with specified channel capacity
pub fn new(capacity: usize) -> Self {
let (sender, _) = broadcast::channel(capacity);
Self { sender }
}
/// Send a file change event to all subscribers
pub fn send(&self, event: FileChangeEvent) {
match self.sender.send(event.clone()) {
Ok(count) => {
crate::debug_event!("broadcast", "sent", "{event:?} to {count} subscribers");
}
Err(_) => {
// No receivers, this is fine
crate::debug_event!("broadcast", "dropped", "no subscribers for {event:?}");
}
}
}
/// Subscribe to receive notifications
pub fn subscribe(&self) -> broadcast::Receiver<FileChangeEvent> {
self.sender.subscribe()
}
}
/// Extension trait for MCP server to handle notifications
impl super::CodeIntelligenceServer {
/// Start listening for broadcast notifications and forward them via MCP
pub async fn start_notification_listener(
&self,
mut receiver: broadcast::Receiver<FileChangeEvent>,
) {
use rmcp::model::{
CustomNotification, LoggingLevel, LoggingMessageNotificationParam,
ResourceUpdatedNotificationParam, ServerNotification,
};
crate::debug_event!("mcp-notify", "listening");
loop {
match receiver.recv().await {
Ok(event) => {
crate::debug_event!("mcp-notify", "received", "{event:?}");
let peer_guard = self.peer.lock().await;
if let Some(peer) = peer_guard.as_ref() {
match event {
FileChangeEvent::FileReindexed { path } => {
let path_str = path.display().to_string();
// Send standard MCP resource updated notification (backwards compatible)
let _ = peer
.notify_resource_updated(ResourceUpdatedNotificationParam {
uri: format!("file://{path_str}"),
})
.await;
// Send logging message (backwards compatible)
let _ = peer
.notify_logging_message(LoggingMessageNotificationParam {
level: LoggingLevel::Info,
logger: Some("codanna".to_string()),
data: serde_json::json!({
"action": "re-indexed",
"file": path_str
}),
})
.await;
// Send custom notification (new)
let _ = peer
.send_notification(ServerNotification::CustomNotification(
CustomNotification::new(
"notifications/codanna/file-reindexed",
Some(serde_json::json!({
"path": path_str
})),
),
))
.await;
crate::debug_event!(
"mcp-notify",
"sent",
"FileReindexed {path_str}"
);
}
FileChangeEvent::FileCreated { path } => {
let path_str = path.display().to_string();
let _ = peer.notify_resource_list_changed().await;
// Send custom notification
let _ = peer
.send_notification(ServerNotification::CustomNotification(
CustomNotification::new(
"notifications/codanna/file-created",
Some(serde_json::json!({
"path": path_str
})),
),
))
.await;
crate::debug_event!(
"mcp-notify",
"sent",
"FileCreated {}",
path.display()
);
}
FileChangeEvent::FileDeleted { path } => {
let path_str = path.display().to_string();
let _ = peer.notify_resource_list_changed().await;
// Send custom notification
let _ = peer
.send_notification(ServerNotification::CustomNotification(
CustomNotification::new(
"notifications/codanna/file-deleted",
Some(serde_json::json!({
"path": path_str
})),
),
))
.await;
crate::debug_event!(
"mcp-notify",
"sent",
"FileDeleted {}",
path.display()
);
}
FileChangeEvent::IndexReloaded => {
let _ = peer.notify_resource_list_changed().await;
// Send custom notification
let _ = peer
.send_notification(ServerNotification::CustomNotification(
CustomNotification::new(
"notifications/codanna/index-reloaded",
Some(serde_json::json!({})),
),
))
.await;
crate::debug_event!("mcp-notify", "sent", "IndexReloaded");
}
}
} else {
crate::debug_event!("mcp-notify", "dropped", "no peer");
}
}
Err(broadcast::error::RecvError::Lagged(n)) => {
tracing::warn!("[mcp-notify] lagged by {n} messages");
}
Err(broadcast::error::RecvError::Closed) => {
crate::debug_event!("mcp-notify", "channel closed");
break;
}
}
}
}
}