lean_ctx/tools/registered/
ctx_dedup.rs1use 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
39 .cache
40 .as_ref()
41 .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
42 let result = if action == "apply" {
43 let Some(mut guard) = crate::server::bounded_lock::write(cache, "ctx_dedup:apply")
44 else {
45 return Ok(ToolOutput::simple(
46 "[dedup unavailable — cache busy, retry]".to_string(),
47 ));
48 };
49 crate::tools::ctx_dedup::handle_action(&mut guard, &action)
50 } else {
51 let Some(guard) = crate::server::bounded_lock::read(cache, "ctx_dedup:status") else {
52 return Ok(ToolOutput::simple(
53 "[dedup status unavailable — cache busy, retry]".to_string(),
54 ));
55 };
56 crate::tools::ctx_dedup::handle(&guard)
57 };
58 Ok(ToolOutput::simple(result))
59 }
60}