lean_ctx/tools/registered/
ctx_benchmark.rs1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str, require_resolved_path};
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 — measures token savings across all available modes for a file or project.\n\
19 WORKFLOW: use BEFORE ctx_read to pick the optimal compression strategy.\n\
20 Provide a file path, or use action=project for project-wide results.\n\
21 ANTIPATTERN: NOT for production profiling — measures compression, not runtime performance.",
22 json!({
23 "type": "object",
24 "properties": {
25 "path": { "type": "string", "description": "File path to benchmark (required for per-file mode)" },
26 "action": { "type": "string", "description": "Benchmark scope: omit for per-file, \"project\" for project-wide" },
27 "format": { "type": "string", "description": "Output format for project benchmarks: json|markdown|terminal (default terminal)" }
28 },
29 "required": ["path"]
30 }),
31 )
32 }
33
34 fn handle(
35 &self,
36 args: &Map<String, Value>,
37 ctx: &ToolContext,
38 ) -> Result<ToolOutput, ErrorData> {
39 let path = require_resolved_path(ctx, args, "path")?;
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}