Skip to main content

lean_ctx/tools/registered/
ctx_analyze.rs

1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, require_resolved_path};
6use crate::tool_defs::tool_def;
7
8pub struct CtxAnalyzeTool;
9
10impl McpTool for CtxAnalyzeTool {
11    fn name(&self) -> &'static str {
12        "ctx_analyze"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_analyze",
18            "Entropy analysis — recommends optimal compression mode for a file path.\n\
19            WORKFLOW: Use BEFORE ctx_read to pick the best mode (full/signatures/auto).\n\
20            Saves tokens by selecting the mode that minimizes size while retaining information.",
21            json!({
22                "type": "object",
23                "properties": {
24                    "path": { "type": "string", "description": "File path to analyze for optimal compression mode" }
25                },
26                "required": ["path"]
27            }),
28        )
29    }
30
31    fn handle(
32        &self,
33        args: &Map<String, Value>,
34        ctx: &ToolContext,
35    ) -> Result<ToolOutput, ErrorData> {
36        let path = require_resolved_path(ctx, args, "path")?;
37
38        let result = crate::tools::ctx_analyze::handle(&path, crate::tools::CrpMode::effective());
39
40        Ok(ToolOutput {
41            text: result,
42            original_tokens: 0,
43            saved_tokens: 0,
44            mode: None,
45            path: Some(path),
46            changed: false,
47            shell_outcome: None,
48        })
49    }
50}