Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.
Rust agent-skillsai-agentsclaude-codeclaude-code-plugincodexdeveloper-toolsffimcpmcp-servernapi-rsnodejsoptimizationperformanceprofilerpyo3pythonrewrite-it-in-rustrubyrusttypescript
mcp: send progress notifications during long tool calls (#45)
chessMane9e14715a25b
diff --git a/src/mcp.rs b/src/mcp.rsindex 88c3805..e2a83fe 100644--- a/src/mcp.rs+++ b/src/mcp.rs@@ -114,6 +114,24 @@ fn call(params: &Value) -> Result<Value> {})}+/// Sends each step of a long tool call (profiling, recording, building...) as a progress+/// notification, for clients that asked with a progressToken.+fn progress_notifier(token: Value) -> Box<dyn FnMut(&str) + Send> {+ let mut progress = 0u64;+ Box::new(move |message: &str| {+ progress += 1;+ let note = progress_note(&token, progress, message);+ let mut stdout = std::io::stdout().lock();+ let _ = writeln!(stdout, "{note}");+ let _ = stdout.flush();+ })+}++fn progress_note(token: &Value, progress: u64, message: &str) -> Value {+ json!({ "jsonrpc": "2.0", "method": "notifications/progress",+ "params": { "progressToken": token, "progress": progress, "message": message } })+}+fn handle(msg: &Value) -> Option<Value> {let id = msg.get("id")?.clone();let params = &msg["params"];@@ -126,12 +144,20 @@ fn handle(msg: &Value) -> Option<Value> {}),"ping" => json!({}),"tools/list" => json!({ "tools": tools() }),- "tools/call" => match call(params) {- Ok(v) => {- json!({ "content": [{ "type": "text", "text": serde_json::to_string_pretty(&v).unwrap_or_default() }] })+ "tools/call" => {+ let token = ¶ms["_meta"]["progressToken"];+ if token.is_string() || token.is_number() {+ ui::set_progress(Some(progress_notifier(token.clone())));+ }+ let result = call(params);+ ui::set_progress(None);+ match result {+ Ok(v) => {+ json!({ "content": [{ "type": "text", "text": serde_json::to_string_pretty(&v).unwrap_or_default() }] })+ }+ Err(e) => json!({ "content": [{ "type": "text", "text": format!("{e:#}") }], "isError": true }),}- Err(e) => json!({ "content": [{ "type": "text", "text": format!("{e:#}") }], "isError": true }),- },+ }method => {return Some(json!({ "jsonrpc": "2.0", "id": id, "error": { "code": -32601, "message": format!("unknown method {method}") } }),@@ -178,4 +204,25 @@ mod tests {let bad = handle(&json!({ "jsonrpc": "2.0", "id": 3, "method": "nope" })).unwrap();assert_eq!(bad["error"]["code"], -32601);}++ #[test]+ fn steps_become_progress_notifications() {+ let note = progress_note(&json!("tok-1"), 2, "building fa_fuzzy (release)");+ assert_eq!(note["method"], "notifications/progress");+ assert_eq!(+ note["params"],+ json!({ "progressToken": "tok-1", "progress": 2, "message": "building fa_fuzzy (release)" })+ );+ assert!(note.get("id").is_none(), "a notification has no id");++ let seen = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));+ let sink = seen.clone();+ ui::set_quiet(true);+ ui::set_progress(Some(Box::new(move |m: &str| sink.lock().unwrap().push(m.to_string()))));+ ui::step("profiling python app.py");+ ui::set_progress(None);+ ui::step("not reported");+ let seen = seen.lock().unwrap();+ assert!(seen.iter().any(|m| m == "profiling python app.py") && !seen.iter().any(|m| m == "not reported"));+ }}diff --git a/src/ui.rs b/src/ui.rsindex 8571a31..a5ae2ad 100644--- a/src/ui.rs+++ b/src/ui.rs@@ -2,12 +2,20 @@use std::fmt::Display;use std::io::IsTerminal;-use std::sync::OnceLock;use std::sync::atomic::{AtomicBool, Ordering};+use std::sync::{Mutex, OnceLock};static QUIET: AtomicBool = AtomicBool::new(false);static COLOR: OnceLock<bool> = OnceLock::new();+type Progress = Box<dyn FnMut(&str) + Send>;+static PROGRESS: Mutex<Option<Progress>> = Mutex::new(None);++/// While set, every step() line is passed here too, even when quiet (MCP progress notifications).+pub fn set_progress(progress: Option<Progress>) {+ *PROGRESS.lock().unwrap_or_else(|e| e.into_inner()) = progress;+}+pub fn set_quiet(quiet: bool) {QUIET.store(quiet, Ordering::Relaxed);}@@ -50,7 +58,11 @@ pub fn line(s: impl Display) {}pub fn step(s: impl Display) {- line(format!("{} {s}", rust("▸")));+ let text = s.to_string();+ if let Some(progress) = PROGRESS.lock().unwrap_or_else(|e| e.into_inner()).as_mut() {+ progress(&text);+ }+ line(format!("{} {text}", rust("▸")));}pub fn ok(s: impl Display) {