lean_ctx/tools/registered/
ctx_quality.rs1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str};
6use crate::tool_defs::tool_def;
7
8pub struct CtxQualityTool;
9
10impl McpTool for CtxQualityTool {
11 fn name(&self) -> &'static str {
12 "ctx_quality"
13 }
14
15 fn tool_def(&self) -> Tool {
16 tool_def(
17 "ctx_quality",
18 "WORKFLOW: report (project score+hotspots+$ tax) → file (one file) → delta (vs HEAD).\n\
19 Code health = clean code as a token-cost lever: cognitive complexity, naming,\n\
20 and the estimated token 'quality tax' of over-threshold functions.\n\
21 ANTIPATTERN: NOT a linter/style checker — it scores navigability, not formatting.",
22 json!({
23 "type": "object",
24 "properties": {
25 "action": {
26 "type": "string",
27 "enum": ["report", "file", "delta"],
28 "description": "report|file|delta"
29 },
30 "path": {
31 "type": "string",
32 "description": "File to analyze (required for file|delta)"
33 },
34 "root": {
35 "type": "string",
36 "description": "Project root"
37 },
38 "format": {
39 "type": "string",
40 "description": "Output format (text|json)"
41 }
42 }
43 }),
44 )
45 }
46
47 fn handle(
48 &self,
49 args: &Map<String, Value>,
50 ctx: &ToolContext,
51 ) -> Result<ToolOutput, ErrorData> {
52 let action = get_str(args, "action").unwrap_or_else(|| "report".to_string());
53 let format = get_str(args, "format");
54 let path = if let Some(p) = ctx.resolved_path("path") {
55 Some(p.to_string())
56 } else if let Some(err) = ctx.path_error("path") {
57 return Err(ErrorData::invalid_params(format!("path: {err}"), None));
58 } else {
59 None
60 };
61 let root = if let Some(p) = ctx
62 .resolved_path("root")
63 .or(ctx.resolved_path("project_root"))
64 {
65 p
66 } else if let Some(err) = ctx.path_error("root").or(ctx.path_error("project_root")) {
67 return Err(ErrorData::invalid_params(format!("root: {err}"), None));
68 } else {
69 &ctx.project_root
70 };
71
72 let result =
73 crate::tools::ctx_quality::handle(&action, path.as_deref(), root, format.as_deref());
74
75 Ok(ToolOutput::simple(result))
76 }
77}