lean_ctx/tools/registered/
ctx_smells.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 CtxSmellsTool;
9
10impl McpTool for CtxSmellsTool {
11 fn name(&self) -> &'static str {
12 "ctx_smells"
13 }
14
15 fn tool_def(&self) -> Tool {
16 tool_def(
17 "ctx_smells",
18 "WORKFLOW: rules (list detectors) → scan (run on project).\n\
19 Code smell detection: dead_code, long_function, god_file, complexity, etc.\n\
20 rule='name' or path='file' to filter.\n\
21 ANTIPATTERN: NOT a linter — no style/format enforcement.",
22 json!({
23 "type": "object",
24 "properties": {
25 "action": {
26 "type": "string",
27 "enum": ["scan", "summary", "rules", "file"],
28 "description": "scan|summary|rules|file"
29 },
30 "rule": {
31 "type": "string",
32 "description": "Filter by rule name (for scan)"
33 },
34 "path": {
35 "type": "string",
36 "description": "Filter by file path"
37 },
38 "root": {
39 "type": "string",
40 "description": "Project root"
41 },
42 "format": {
43 "type": "string",
44 "description": "Output format (text|json)"
45 }
46 }
47 }),
48 )
49 }
50
51 fn handle(
52 &self,
53 args: &Map<String, Value>,
54 ctx: &ToolContext,
55 ) -> Result<ToolOutput, ErrorData> {
56 let action = get_str(args, "action").unwrap_or_else(|| "summary".to_string());
57 let rule = get_str(args, "rule");
58 let path = get_str(args, "path");
59 let format = get_str(args, "format");
60 let root = if let Some(p) = ctx
61 .resolved_path("root")
62 .or(ctx.resolved_path("project_root"))
63 {
64 p
65 } else if let Some(err) = ctx.path_error("root").or(ctx.path_error("project_root")) {
66 return Err(ErrorData::invalid_params(format!("root: {err}"), None));
67 } else {
68 &ctx.project_root
69 };
70
71 let result = crate::tools::ctx_smells::handle(
72 &action,
73 rule.as_deref(),
74 path.as_deref(),
75 root,
76 format.as_deref(),
77 );
78
79 Ok(ToolOutput::simple(result))
80 }
81}