Skip to main content

lean_ctx/tools/registered/
ctx_graph.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 CtxGraphTool;
9
10impl McpTool for CtxGraphTool {
11    fn name(&self) -> &'static str {
12        "ctx_graph"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_graph",
18            "Unified code graph. Actions: build (index), related (connected files), symbol (def/usages), \
19impact (blast radius), status (stats), enrich (add commits+tests+knowledge), context (task-based query), diagram (Mermaid deps/calls).",
20            json!({
21                "type": "object",
22                "properties": {
23                    "action": {
24                        "type": "string",
25                        "enum": ["build", "related", "symbol", "impact", "status", "enrich", "context", "diagram"],
26                        "description": "Graph operation"
27                    },
28                    "path": {
29                        "type": "string",
30                        "description": "File path (related/impact) or file::symbol_name (symbol)"
31                    },
32                    "depth": {
33                        "type": "integer",
34                        "description": "Optional depth for action=diagram (default: 2)"
35                    },
36                    "kind": {
37                        "type": "string",
38                        "description": "Optional kind for action=diagram: deps|calls"
39                    },
40                    "project_root": {
41                        "type": "string",
42                        "description": "Project root directory (default: .)"
43                    }
44                },
45                "required": ["action"]
46            }),
47        )
48    }
49
50    fn handle(
51        &self,
52        args: &Map<String, Value>,
53        ctx: &ToolContext,
54    ) -> Result<ToolOutput, ErrorData> {
55        let action = get_str(args, "action")
56            .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
57
58        // For diagram action, pass the raw path; for others, use the resolved path.
59        let path = if action == "diagram" {
60            get_str(args, "path")
61        } else if let Some(p) = ctx.resolved_path("path") {
62            Some(p.to_string())
63        } else if ctx.path_error("path").is_some() && get_str(args, "path").is_some() {
64            return Err(ErrorData::invalid_params(
65                format!("path: {}", ctx.path_error("path").unwrap()),
66                None,
67            ));
68        } else {
69            None
70        };
71
72        let root = if let Some(p) = ctx.resolved_path("project_root") {
73            p.to_string()
74        } else if let Some(err) = ctx.path_error("project_root") {
75            return Err(ErrorData::invalid_params(
76                format!("project_root: {err}"),
77                None,
78            ));
79        } else {
80            ctx.project_root.clone()
81        };
82        let depth = get_int(args, "depth").map(|d| d as usize);
83        let kind = get_str(args, "kind");
84
85        let cache = ctx.cache.as_ref().unwrap();
86        let mut guard = tokio::task::block_in_place(|| cache.blocking_write());
87        let result = crate::tools::ctx_graph::handle(
88            &action,
89            path.as_deref(),
90            &root,
91            &mut guard,
92            ctx.crp_mode,
93            depth,
94            kind.as_deref(),
95        );
96
97        Ok(ToolOutput {
98            text: result,
99            original_tokens: 0,
100            saved_tokens: 0,
101            mode: Some(action),
102            path: None,
103            changed: false,
104        })
105    }
106}