forked from paiml/rust-mcp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_progress_notifications.rs
More file actions
78 lines (65 loc) · 2.44 KB
/
10_progress_notifications.rs
File metadata and controls
78 lines (65 loc) · 2.44 KB
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
//! Progress Notifications Example
//!
//! This example demonstrates how progress notifications work in MCP.
//! It shows the structure of progress notifications and tokens.
use pmcp::types::{ProgressNotification, ProgressToken};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize tracing
tracing_subscriber::fmt::init();
println!("=== MCP Progress Notifications Example ===");
// Create a progress tracker
let progress_tracker = Arc::new(Mutex::new(HashMap::<ProgressToken, f64>::new()));
// Simulate some progress notifications
let notifications = vec![
ProgressNotification::new(
ProgressToken::String("task-1".to_string()),
10.0,
Some("Starting task...".to_string()),
),
ProgressNotification::new(
ProgressToken::String("task-1".to_string()),
25.0,
Some("Processing data...".to_string()),
),
ProgressNotification::new(
ProgressToken::String("task-1".to_string()),
50.0,
Some("Halfway there...".to_string()),
),
ProgressNotification::new(
ProgressToken::String("task-1".to_string()),
100.0,
Some("Task completed!".to_string()),
),
];
println!("Simulating progress notifications:");
for notification in notifications {
// Create a progress bar visualization (assuming progress is 0-100)
let percentage = notification.progress;
let progress_bar = "█".repeat((percentage / 5.0) as usize);
let empty_bar = "░".repeat(20 - (percentage / 5.0) as usize);
println!(
"📊 Progress [{}{}] {:.1}% - {} (Token: {:?})",
progress_bar,
empty_bar,
percentage,
notification.message.as_deref().unwrap_or("Working..."),
notification.progress_token
);
// Track progress
progress_tracker
.lock()
.unwrap()
.insert(notification.progress_token.clone(), notification.progress);
// Simulate some delay
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
}
println!("✅ Progress tracking completed!");
// Show final state
let tracker = progress_tracker.lock().unwrap();
println!("Final progress state: {:?}", *tracker);
Ok(())
}