use rmcp::RoleServer;
use rmcp::model::{ProgressNotificationParam, ProgressToken};
use rmcp::service::Peer;
#[derive(Clone)]
pub struct ProgressSender {
peer: Peer<RoleServer>,
token: ProgressToken,
}
impl ProgressSender {
pub fn new(peer: Peer<RoleServer>, token: ProgressToken) -> Self {
Self { peer, token }
}
pub fn send(&self, progress: f64, total: Option<f64>, message: Option<String>) {
let mut params = ProgressNotificationParam::new(self.token.clone(), progress);
if let Some(total) = total {
params = params.with_total(total);
}
if let Some(message) = message {
params = params.with_message(message);
}
let peer = self.peer.clone();
tokio::spawn(async move {
if let Err(e) = peer.notify_progress(params).await {
tracing::debug!("[progress] notify failed: {e}");
}
});
}
}
pub type SharedProgressSender = std::sync::Arc<std::sync::Mutex<Option<ProgressSender>>>;
#[cfg(all(test, unix))]
mod over_mcp {
use rmcp::ServiceExt;
use serde_json::{Value, json};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use crate::tools::LeanCtxServer;
struct Host {
lines: tokio::io::Lines<BufReader<tokio::io::ReadHalf<tokio::io::DuplexStream>>>,
writer: tokio::io::WriteHalf<tokio::io::DuplexStream>,
}
impl Host {
async fn send(&mut self, message: Value) {
let mut line = message.to_string();
line.push('\n');
self.writer.write_all(line.as_bytes()).await.unwrap();
}
async fn answer(&mut self, id: u64) -> (Value, Vec<Value>) {
let mut tokens = Vec::new();
loop {
let line = tokio::time::timeout(
std::time::Duration::from_secs(60),
self.lines.next_line(),
)
.await
.expect("the server answers within a minute")
.expect("readable")
.expect("the connection stays open");
let message: Value = serde_json::from_str(&line).expect("JSON-RPC");
if message["method"] == "notifications/progress" {
tokens.push(message["params"]["progressToken"].clone());
} else if message["id"] == id {
return (message, tokens);
}
}
}
async fn shell(
&mut self,
id: u64,
command: &str,
token: Option<u64>,
) -> (String, Vec<Value>) {
let mut params = json!({ "name": "ctx_shell", "arguments": { "command": command } });
if let Some(token) = token {
params["_meta"] = json!({ "progressToken": token });
}
self.send(
json!({ "jsonrpc": "2.0", "id": id, "method": "tools/call", "params": params }),
)
.await;
let (response, tokens) = self.answer(id).await;
assert!(
response.get("error").is_none(),
"a JSON-RPC answer, no error: {response}"
);
assert_ne!(
response["result"]["isError"],
json!(true),
"no tool error: {response}"
);
let text = response["result"]["content"]
.as_array()
.into_iter()
.flatten()
.filter_map(|block| block["text"].as_str())
.collect::<String>();
(text, tokens)
}
}
async fn connect(root: &str) -> Host {
let server = LeanCtxServer::new_with_project_root(Some(root));
let (server_io, client_io) = tokio::io::duplex(1 << 16);
tokio::spawn(async move {
if let Ok(running) = Box::pin(server.serve(server_io)).await {
let _ = running.waiting().await;
}
});
let (reader, writer) = tokio::io::split(client_io);
let mut host = Host {
lines: BufReader::new(reader).lines(),
writer,
};
host.send(json!({
"jsonrpc": "2.0", "id": 1, "method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"capabilities": {},
"clientInfo": { "name": "test-host", "version": "1.0.0" }
}
}))
.await;
host.answer(1).await;
host.send(json!({ "jsonrpc": "2.0", "method": "notifications/initialized" }))
.await;
host
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn progress_goes_only_to_the_call_that_asked_for_it() {
let _data = lean_ctx::core::data_dir::isolated_data_dir();
let _cap = lean_ctx::test_env::VarGuard::set("LEAN_CTX_SHELL_FG_CAP_MS", "5600");
let _security = lean_ctx::test_env::VarGuard::set("LEAN_CTX_SHELL_SECURITY", "off");
let project = tempfile::tempdir().unwrap();
std::fs::create_dir(project.path().join(".git")).unwrap();
let mut host = connect(&project.path().to_string_lossy()).await;
let (text, tokens) = host.shell(2, "sleep 6; echo with_token", Some(9)).await;
assert!(
text.contains("auto-background"),
"detached at the cap, cleanly: {text}"
);
assert!(
!tokens.is_empty(),
"the call with a token hears its progress"
);
assert!(
tokens.iter().all(|t| *t == json!(9)),
"only under its own token: {tokens:?}"
);
let (text, tokens) = host.shell(3, "sleep 6; echo without_token", None).await;
assert!(
text.contains("auto-background"),
"detached at the cap, cleanly: {text}"
);
assert!(
tokens.is_empty(),
"a call without a token sends no progress, least of all under the \
finished call's token: {tokens:?}"
);
let (text, _) = host.shell(4, "echo still_connected", None).await;
assert!(text.contains("still_connected"), "{text}");
}
#[test]
fn the_default_foreground_cap_returns_before_hosts_abort_a_call() {
const { assert!(crate::tools::registered::ctx_shell::DEFAULT_FOREGROUND_SOFT_CAP_MS <= 90_000) };
}
}