Skip to main content

lean_ctx/tools/registered/
ctx_review.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{get_int, get_str, McpTool, ToolContext, ToolOutput};
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: combines impact analysis, caller tracking, and test discovery. \
19             Actions: review (single file), diff-review (from git diff), checklist (structured review questions).",
20            json!({
21                "type": "object",
22                "properties": {
23                    "action": {
24                        "type": "string",
25                        "enum": ["review", "diff-review", "checklist"],
26                        "description": "Review action"
27                    },
28                    "path": {
29                        "type": "string",
30                        "description": "File path to review (or git diff text for diff-review)"
31                    },
32                    "depth": {
33                        "type": "integer",
34                        "description": "Impact analysis depth (default: 3)"
35                    }
36                },
37                "required": ["action"]
38            }),
39        )
40    }
41
42    fn handle(
43        &self,
44        args: &Map<String, Value>,
45        ctx: &ToolContext,
46    ) -> Result<ToolOutput, ErrorData> {
47        let action = get_str(args, "action")
48            .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
49        let path = get_str(args, "path");
50        let depth = get_int(args, "depth").map(|d| d as usize);
51        let project_root = if let Some(p) = ctx
52            .resolved_path("project_root")
53            .or(ctx.resolved_path("root"))
54        {
55            p
56        } else if let Some(err) = ctx.path_error("project_root").or(ctx.path_error("root")) {
57            return Err(ErrorData::invalid_params(
58                format!("project_root: {err}"),
59                None,
60            ));
61        } else {
62            &ctx.project_root
63        };
64
65        let result =
66            crate::tools::ctx_review::handle(&action, path.as_deref(), project_root, depth);
67
68        Ok(ToolOutput::simple(result))
69    }
70}