Skip to main content

lean_ctx/tools/registered/
ctx_compress.rs

1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_bool};
6use crate::tool_defs::tool_def;
7
8pub struct CtxCompressTool;
9
10impl McpTool for CtxCompressTool {
11    fn name(&self) -> &'static str {
12        "ctx_compress"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_compress",
18            "Compress read cache to free token budget. Does not affect session state or knowledge.\n\
19            WORKFLOW: check budget with ctx_context first, then reclaim space.",
20            json!({
21                "type": "object",
22                "properties": {
23                    "include_signatures": { "type": "boolean", "description": "Keep function/method signatures in compressed output (default: true)" }
24                }
25            }),
26        )
27    }
28
29    fn handle(
30        &self,
31        args: &Map<String, Value>,
32        ctx: &ToolContext,
33    ) -> Result<ToolOutput, ErrorData> {
34        let include_sigs = get_bool(args, "include_signatures").unwrap_or(true);
35        let cache = ctx
36            .cache
37            .as_ref()
38            .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
39        let Some(guard) = crate::server::bounded_lock::read(cache, "ctx_compress") else {
40            return Ok(ToolOutput::simple(
41                "[cache temporarily unavailable — retry in a moment]".to_string(),
42            ));
43        };
44        let result = crate::tools::ctx_compress::handle(&guard, include_sigs, ctx.crp_mode);
45        Ok(ToolOutput::simple(result))
46    }
47}