Skip to main content

lean_ctx/tools/registered/
ctx_review.rs

1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str, get_usize};
6use crate::tool_defs::tool_def;
7
8pub struct CtxReviewTool;
9
10impl McpTool for CtxReviewTool {
11    fn name(&self) -> &'static str {
12        "ctx_review"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_review",
18            "Automated code review with impact analysis, caller tracking, and test discovery.\n\
19             Actions: review (single file), diff-review (from git diff text),\n\
20             checklist (structured review questions). depth=N (default 3).\n\
21             WORKFLOW: run tests first, then use review for structured analysis.\n\
22             ANTIPATTERN: not a substitute for actual test execution.",
23            json!({
24                "type": "object",
25                "properties": {
26                    "action": {
27                        "type": "string",
28                        "enum": ["review", "diff-review", "checklist"]
29                    },
30                    "path": {
31                        "type": "string",
32                        "description": "File path (review/checklist) or git diff text (diff-review)"
33                    },
34                    "depth": {
35                        "type": "integer",
36                        "description": "Analysis breadth (default 3)"
37                    }
38                },
39                "required": ["action"],
40                "allOf": [
41                    {
42                        "if": {
43                            "properties": { "action": { "const": "review" } },
44                            "required": ["action"]
45                        },
46                        "then": { "required": ["action", "path"] }
47                    },
48                    {
49                        "if": {
50                            "properties": { "action": { "const": "diff-review" } },
51                            "required": ["action"]
52                        },
53                        "then": { "required": ["action", "path"] }
54                    },
55                    {
56                        "if": {
57                            "properties": { "action": { "const": "checklist" } },
58                            "required": ["action"]
59                        },
60                        "then": { "required": ["action", "path"] }
61                    }
62                ]
63            }),
64        )
65    }
66
67    fn handle(
68        &self,
69        args: &Map<String, Value>,
70        ctx: &ToolContext,
71    ) -> Result<ToolOutput, ErrorData> {
72        let action = get_str(args, "action")
73            .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
74        let path = get_str(args, "path");
75        let depth = get_usize(args, "depth").map(|d| d.min(64));
76        let project_root = if let Some(p) = ctx
77            .resolved_path("project_root")
78            .or(ctx.resolved_path("root"))
79        {
80            p
81        } else if let Some(err) = ctx.path_error("project_root").or(ctx.path_error("root")) {
82            return Err(ErrorData::invalid_params(
83                format!("project_root: {err}"),
84                None,
85            ));
86        } else {
87            &ctx.project_root
88        };
89
90        let result =
91            crate::tools::ctx_review::handle(&action, path.as_deref(), project_root, depth);
92
93        Ok(ToolOutput::simple(result))
94    }
95}