Skip to main content

lean_ctx_server/server/
progress.rs

1// SPDX-License-Identifier: Apache-2.0
2use rmcp::RoleServer;
3use rmcp::model::{ProgressNotificationParam, ProgressToken};
4use rmcp::service::Peer;
5
6/// Sends MCP progress notifications to the client during long-running tool operations.
7#[derive(Clone)]
8pub struct ProgressSender {
9    peer: Peer<RoleServer>,
10    token: ProgressToken,
11}
12
13impl ProgressSender {
14    pub fn new(peer: Peer<RoleServer>, token: ProgressToken) -> Self {
15        Self { peer, token }
16    }
17
18    pub fn send(&self, progress: f64, total: Option<f64>, message: Option<String>) {
19        // ProgressNotificationParam is #[non_exhaustive] since rmcp 2.0 — build via ctor.
20        let mut params = ProgressNotificationParam::new(self.token.clone(), progress);
21        if let Some(total) = total {
22            params = params.with_total(total);
23        }
24        if let Some(message) = message {
25            params = params.with_message(message);
26        }
27        let peer = self.peer.clone();
28        tokio::spawn(async move {
29            if let Err(e) = peer.notify_progress(params).await {
30                tracing::debug!("[progress] notify failed: {e}");
31            }
32        });
33    }
34}
35
36pub type SharedProgressSender = std::sync::Arc<std::sync::Mutex<Option<ProgressSender>>>;