lean_ctx/tools/registered/
ctx_graph.rs1use 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 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 "File-level dependency graph queries.\n\
19 action=symbol path=\"file.rs::fnName\" returns the DEFINITION (not usages — \
20 use ctx_search for references). neighbors=imports±direction, \
21 impact=reverse-dep blast radius, path from→to=dependency chain, \
22 diff since=HEAD~1=git change impact, diagram kind=deps|calls (Mermaid).\n\
23 For understanding code use ctx_compose FIRST.",
24 json!({
25 "type": "object",
26 "properties": {
27 "action": {
28 "type": "string",
29 "description": "build|related|symbol|impact|status|enrich|context|diagram|neighbors|path|explain|diff"
30 },
31 "path": {
32 "type": "string",
33 "description": "Path; file::symbol for symbol action"
34 },
35 "to": { "type": "string", "description": "Target file (action=path)" },
36 "depth": { "type": "integer" },
37 "kind": { "type": "string", "description": "diagram: deps|calls" },
38 "format": { "type": "string", "description": "text|json" },
39 "since": { "type": "string", "description": "Git ref (default HEAD~1)" },
40 "project_root": { "type": "string" }
41 },
42 "required": ["action"]
43 }),
44 )
45 }
46
47 fn handle(
48 &self,
49 args: &Map<String, Value>,
50 ctx: &ToolContext,
51 ) -> Result<ToolOutput, ErrorData> {
52 let action = get_str(args, "action")
53 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
54
55 let path = if action == "diagram" {
56 get_str(args, "path")
57 } else if let Some(p) = ctx.resolved_path("path") {
58 Some(p.to_string())
59 } else if let Some(err) = ctx
60 .path_error("path")
61 .filter(|_| get_str(args, "path").is_some())
62 {
63 return Err(ErrorData::invalid_params(format!("path: {err}"), None));
64 } else {
65 None
66 };
67
68 let root = if let Some(p) = ctx.resolved_path("project_root") {
69 p.to_string()
70 } else if let Some(err) = ctx.path_error("project_root") {
71 return Err(ErrorData::invalid_params(
72 format!("project_root: {err}"),
73 None,
74 ));
75 } else {
76 ctx.project_root.clone()
77 };
78 let depth = get_usize(args, "depth").map(|d| d.min(64));
79 let kind = get_str(args, "kind");
80 let format = get_str(args, "format");
81 let since = get_str(args, "since");
83 let to = if let Some(p) = ctx.resolved_path("to") {
84 Some(p.to_string())
85 } else if let Some(err) = ctx
86 .path_error("to")
87 .filter(|_| get_str(args, "to").is_some())
88 {
89 return Err(ErrorData::invalid_params(format!("to: {err}"), None));
90 } else {
91 None
92 };
93
94 let cache = ctx
95 .cache
96 .as_ref()
97 .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
98 let Some(mut guard) = crate::server::bounded_lock::write(cache, "ctx_graph") else {
99 return Ok(ToolOutput::simple(
100 "[graph cache temporarily unavailable — retry in a moment]".to_string(),
101 ));
102 };
103 let result = crate::tools::ctx_graph::handle(
104 &action,
105 path.as_deref(),
106 &root,
107 &mut guard,
108 ctx.crp_mode,
109 depth,
110 kind.as_deref(),
111 to.as_deref(),
112 format.as_deref(),
113 since.as_deref(),
114 );
115
116 Ok(ToolOutput {
117 text: result,
118 original_tokens: 0,
119 saved_tokens: 0,
120 mode: Some(action),
121 path: None,
122 changed: false,
123 shell_outcome: None,
124 })
125 }
126}