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            }),
41        )
42    }
43
44    fn handle(
45        &self,
46        args: &Map<String, Value>,
47        ctx: &ToolContext,
48    ) -> Result<ToolOutput, ErrorData> {
49        let action = get_str(args, "action")
50            .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
51        let path = get_str(args, "path");
52        let depth = get_usize(args, "depth").map(|d| d.min(64));
53        let project_root = if let Some(p) = ctx
54            .resolved_path("project_root")
55            .or(ctx.resolved_path("root"))
56        {
57            p
58        } else if let Some(err) = ctx.path_error("project_root").or(ctx.path_error("root")) {
59            return Err(ErrorData::invalid_params(
60                format!("project_root: {err}"),
61                None,
62            ));
63        } else {
64            &ctx.project_root
65        };
66
67        let result =
68            crate::tools::ctx_review::handle(&action, path.as_deref(), project_root, depth);
69
70        Ok(ToolOutput::simple(result))
71    }
72}