Skip to main content

lean_ctx/tools/registered/
ctx_dedup.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{get_str, McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8pub struct CtxDedupTool;
9
10impl McpTool for CtxDedupTool {
11    fn name(&self) -> &'static str {
12        "ctx_dedup"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_dedup",
18            "Cross-file dedup: analyze or apply shared block references.",
19            json!({
20                "type": "object",
21                "properties": {
22                    "action": {
23                        "type": "string",
24                        "description": "analyze (default) or apply (register shared blocks for auto-dedup in ctx_read)",
25                        "default": "analyze"
26                    }
27                }
28            }),
29        )
30    }
31
32    fn handle(
33        &self,
34        args: &Map<String, Value>,
35        ctx: &ToolContext,
36    ) -> Result<ToolOutput, ErrorData> {
37        let action = get_str(args, "action").unwrap_or_default();
38        let cache = ctx.cache.as_ref().unwrap();
39        let result = if action == "apply" {
40            let Some(mut guard) = crate::server::bounded_lock::write(cache, "ctx_dedup:apply")
41            else {
42                return Ok(ToolOutput::simple(
43                    "[dedup unavailable — cache busy, retry]".to_string(),
44                ));
45            };
46            crate::tools::ctx_dedup::handle_action(&mut guard, &action)
47        } else {
48            let Some(guard) = crate::server::bounded_lock::read(cache, "ctx_dedup:status") else {
49                return Ok(ToolOutput::simple(
50                    "[dedup status unavailable — cache busy, retry]".to_string(),
51                ));
52            };
53            crate::tools::ctx_dedup::handle(&guard)
54        };
55        Ok(ToolOutput::simple(result))
56    }
57}