lean_ctx/tools/registered/
ctx_graph.rs1use 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 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 Some(mut guard) = crate::server::bounded_lock::write(cache, "ctx_graph") else {
87 return Ok(ToolOutput::simple(
88 "[graph cache temporarily unavailable — retry in a moment]".to_string(),
89 ));
90 };
91 let result = crate::tools::ctx_graph::handle(
92 &action,
93 path.as_deref(),
94 &root,
95 &mut guard,
96 ctx.crp_mode,
97 depth,
98 kind.as_deref(),
99 );
100
101 Ok(ToolOutput {
102 text: result,
103 original_tokens: 0,
104 saved_tokens: 0,
105 mode: Some(action),
106 path: None,
107 changed: false,
108 })
109 }
110}