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 "allOf": [
44 {
45 "if": {
46 "properties": { "action": { "const": "file" } },
47 "required": ["action"]
48 },
49 "then": { "required": ["action", "path"] }
50 },
51 {
52 "if": {
53 "properties": { "action": { "const": "delta" } },
54 "required": ["action"]
55 },
56 "then": { "required": ["action", "path"] }
57 }
58 ]
59 }),
60 )
61 }
62
63 fn handle(
64 &self,
65 args: &Map<String, Value>,
66 ctx: &ToolContext,
67 ) -> Result<ToolOutput, ErrorData> {
68 let action = get_str(args, "action").unwrap_or_else(|| "report".to_string());
69 let format = get_str(args, "format");
70 let path = if let Some(p) = ctx.resolved_path("path") {
71 Some(p.to_string())
72 } else if let Some(err) = ctx.path_error("path") {
73 return Err(ErrorData::invalid_params(format!("path: {err}"), None));
74 } else {
75 None
76 };
77 let root = if let Some(p) = ctx
78 .resolved_path("root")
79 .or(ctx.resolved_path("project_root"))
80 {
81 p
82 } else if let Some(err) = ctx.path_error("root").or(ctx.path_error("project_root")) {
83 return Err(ErrorData::invalid_params(format!("root: {err}"), None));
84 } else {
85 &ctx.project_root
86 };
87
88 let result =
89 crate::tools::ctx_quality::handle(&action, path.as_deref(), root, format.as_deref());
90
91 Ok(ToolOutput::simple(result))
92 }
93}