lean_ctx/tools/registered/
ctx_delta.rs1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8pub struct CtxDeltaTool;
9
10impl McpTool for CtxDeltaTool {
11 fn name(&self) -> &'static str {
12 "ctx_delta"
13 }
14
15 fn tool_def(&self) -> Tool {
16 tool_def(
17 "ctx_delta",
18 "Incremental diff — sends only changed lines since last read.",
19 json!({
20 "type": "object",
21 "properties": {
22 "path": { "type": "string", "description": "Absolute file path" }
23 },
24 "required": ["path"]
25 }),
26 )
27 }
28
29 fn handle(
30 &self,
31 _args: &Map<String, Value>,
32 ctx: &ToolContext,
33 ) -> Result<ToolOutput, ErrorData> {
34 let path = ctx
35 .resolved_path("path")
36 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?
37 .to_string();
38
39 tokio::task::block_in_place(|| {
40 let cache_lock = ctx
41 .cache
42 .as_ref()
43 .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
44 let mut cache = cache_lock.blocking_write();
45 let output = crate::tools::ctx_delta::handle(&mut cache, &path);
46 let original = cache.get(&path).map_or(0, |e| e.original_tokens);
47 let tokens = crate::core::tokens::count_tokens(&output);
48 drop(cache);
49
50 if let Some(session_lock) = ctx.session.as_ref() {
51 let mut session = session_lock.blocking_write();
52 session.mark_modified(&path);
53 }
54
55 let saved = original.saturating_sub(tokens);
56 Ok(ToolOutput {
57 text: output,
58 original_tokens: original,
59 saved_tokens: saved,
60 mode: Some("delta".to_string()),
61 path: Some(path),
62 changed: false,
63 })
64 })
65 }
66}