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, require_resolved_path, 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 = require_resolved_path(ctx, args, "path")?;
37
38        let action = get_str(args, "action").unwrap_or_default();
39        let result = if action == "project" {
40            let fmt = get_str(args, "format").unwrap_or_default();
41            let bench = crate::core::benchmark::run_project_benchmark(&path);
42            match fmt.as_str() {
43                "json" => crate::core::benchmark::format_json(&bench),
44                "markdown" | "md" => crate::core::benchmark::format_markdown(&bench),
45                _ => crate::core::benchmark::format_terminal(&bench),
46            }
47        } else {
48            crate::tools::ctx_benchmark::handle(&path, crate::tools::CrpMode::effective())
49        };
50
51        Ok(ToolOutput::simple(result))
52    }
53}