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 mut guard = tokio::task::block_in_place(|| cache.blocking_write());
41            crate::tools::ctx_dedup::handle_action(&mut guard, &action)
42        } else {
43            let guard = tokio::task::block_in_place(|| cache.blocking_read());
44            crate::tools::ctx_dedup::handle(&guard)
45        };
46        Ok(ToolOutput::simple(result))
47    }
48}