Skip to content

Commit 52e731b

Browse files
authored
fix: Small improvement to progress demo (#898)
Signed-off-by: Dawid Nowak <nowakd@gmail.com>
1 parent 2536a05 commit 52e731b

3 files changed

Lines changed: 26 additions & 55 deletions

File tree

.githooks/commit-msg

Lines changed: 0 additions & 50 deletions
This file was deleted.

examples/servers/src/common/progress_demo.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rmcp::{
1111
};
1212
use serde_json::json;
1313
use tokio_stream::StreamExt;
14-
use tracing::debug;
14+
use tracing::{debug, info};
1515

1616
// a Stream data source that generates data in chunks
1717
#[derive(Clone)]
@@ -30,7 +30,7 @@ impl StreamDataSource {
3030
}
3131
}
3232
pub fn from_text(text: &str) -> Self {
33-
Self::new(text.as_bytes().to_vec(), 1)
33+
Self::new(text.as_bytes().to_vec(), 5)
3434
}
3535
}
3636

@@ -61,7 +61,7 @@ impl ProgressDemo {
6161
#[allow(dead_code)]
6262
pub fn new() -> Self {
6363
Self {
64-
data_source: StreamDataSource::from_text("Hello, world!"),
64+
data_source: StreamDataSource::from_text("1111122222333334444455555"),
6565
}
6666
}
6767
#[tool(description = "Process data stream with progress updates")]
@@ -70,6 +70,21 @@ impl ProgressDemo {
7070
ctx: RequestContext<RoleServer>,
7171
) -> Result<CallToolResult, McpError> {
7272
let mut counter = 0;
73+
info!(
74+
"Processing stream with progress token {:?}",
75+
ctx.meta.get_key_value("progressToken")
76+
);
77+
let Some((_, progress_token)) = ctx.meta.get_key_value("progressToken") else {
78+
return Err(McpError::internal_error(format!("No progress token"), None));
79+
};
80+
81+
let Ok(progress_token) = serde_json::from_value::<NumberOrString>(progress_token.clone())
82+
else {
83+
return Err(McpError::internal_error(
84+
format!("Invalid format of the progress token"),
85+
None,
86+
));
87+
};
7388

7489
let mut data_source = self.data_source.clone();
7590
loop {
@@ -83,9 +98,9 @@ impl ProgressDemo {
8398
counter += 1;
8499
// create progress notification param
85100
let progress_param = ProgressNotificationParam {
86-
progress_token: ProgressToken(NumberOrString::Number(counter)),
101+
progress_token: ProgressToken(progress_token.clone()),
87102
progress: counter as f64,
88-
total: None,
103+
total: Some(5.0),
89104
message: Some(chunk_str.to_string()),
90105
};
91106

@@ -104,6 +119,7 @@ impl ProgressDemo {
104119
));
105120
}
106121
}
122+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
107123
}
108124

109125
Ok(CallToolResult::success(vec![Content::text(format!(

examples/servers/src/progress_demo.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rmcp::{
1010

1111
mod common;
1212
use common::progress_demo::ProgressDemo;
13+
use tracing_subscriber::EnvFilter;
1314

1415
const HTTP_BIND_ADDRESS: &str = "127.0.0.1:8001";
1516

@@ -20,6 +21,10 @@ async fn main() -> anyhow::Result<()> {
2021
.nth(1)
2122
.unwrap_or_else(|| env::var("TRANSPORT_MODE").unwrap_or_else(|_| "stdio".to_string()));
2223

24+
tracing_subscriber::fmt()
25+
.with_env_filter(EnvFilter::from_default_env())
26+
.init();
27+
2328
match transport_mode.as_str() {
2429
"stdio" => run_stdio().await,
2530
"http" | "streamhttp" => run_streamable_http().await,

0 commit comments

Comments
 (0)