Skip to main content

lean_ctx/tools/registered/
ctx_benchmark.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 CtxBenchmarkTool;
9
10impl McpTool for CtxBenchmarkTool {
11    fn name(&self) -> &'static str {
12        "ctx_benchmark"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_benchmark",
18            "Benchmark compression modes for a file or project.",
19            json!({
20                "type": "object",
21                "properties": {
22                    "path": { "type": "string" },
23                    "action": { "type": "string" },
24                    "format": { "type": "string" }
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 = ctx
37            .resolved_path("path")
38            .ok_or_else(|| ErrorData::invalid_params("path is required", None))?
39            .to_string();
40
41        let action = get_str(args, "action").unwrap_or_default();
42        let result = if action == "project" {
43            let fmt = get_str(args, "format").unwrap_or_default();
44            let bench = crate::core::benchmark::run_project_benchmark(&path);
45            match fmt.as_str() {
46                "json" => crate::core::benchmark::format_json(&bench),
47                "markdown" | "md" => crate::core::benchmark::format_markdown(&bench),
48                _ => crate::core::benchmark::format_terminal(&bench),
49            }
50        } else {
51            crate::tools::ctx_benchmark::handle(&path, crate::tools::CrpMode::effective())
52        };
53
54        Ok(ToolOutput::simple(result))
55    }
56}