pub trait NotificationDefinition:
HasNotificationMetadata
+ HasNotificationPayload
+ HasNotificationRules {
// Provided methods
fn to_notification(&self) -> Notification { ... }
fn validate(&self) -> Result<(), String> { ... }
}
Expand description
Complete MCP Notification Creation - Build real-time event broadcasting systems.
This trait represents a complete, working MCP notification that can broadcast
real-time events to connected clients with structured payloads and routing rules.
When you implement the required metadata traits, you automatically get
NotificationDefinition
for free via blanket implementation.
§What You’re Building
A notification is a real-time event system that:
- Broadcasts events to connected clients instantly
- Carries structured JSON payloads with event data
- Supports routing rules for targeted delivery
- Provides reliable event ordering and delivery
§How to Create a Notification
Implement these three traits on your struct:
// This struct will automatically implement NotificationDefinition!
struct FileChangeNotification {
file_path: String,
change_type: String,
payload_data: Value,
}
impl FileChangeNotification {
fn new(file_path: String, change_type: String) -> Self {
let payload_data = json!({
"path": file_path,
"type": change_type,
"timestamp": "2024-01-01T00:00:00Z"
});
Self { file_path, change_type, payload_data }
}
}
impl HasNotificationMetadata for FileChangeNotification {
fn method(&self) -> &str {
"file/changed"
}
}
impl HasNotificationPayload for FileChangeNotification {
fn payload(&self) -> Option<&Value> {
Some(&self.payload_data)
}
}
impl HasNotificationRules for FileChangeNotification {
fn priority(&self) -> u32 {
1
}
}
// Now you can use it with the server:
let notification = FileChangeNotification::new(
"/workspace/src/main.rs".to_string(),
"modified".to_string(),
);
// The notification automatically implements NotificationDefinition
let base_notification = notification.to_notification();
§Key Benefits
- Real-Time: Instant event delivery to connected clients
- Structured Data: JSON payloads for rich event information
- Targeted Delivery: Client-specific routing rules
- MCP Compliant: Fully compatible with MCP 2025-06-18 specification
§Common Use Cases
- File system watch notifications
- Database change events
- User activity broadcasts
- System status updates
- Real-time collaboration events
Provided Methods§
Sourcefn to_notification(&self) -> Notification
fn to_notification(&self) -> Notification
Convert this notification definition to a base Notification