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 {
62 ctx.resolved_path("path").map(String::from)
63 };
64
65 let root = ctx
66 .resolved_path("project_root")
67 .unwrap_or(&ctx.project_root)
68 .to_string();
69 let depth = get_int(args, "depth").map(|d| d as usize);
70 let kind = get_str(args, "kind");
71
72 let cache = ctx.cache.as_ref().unwrap();
73 let mut guard = tokio::task::block_in_place(|| cache.blocking_write());
74 let result = crate::tools::ctx_graph::handle(
75 &action,
76 path.as_deref(),
77 &root,
78 &mut guard,
79 ctx.crp_mode,
80 depth,
81 kind.as_deref(),
82 );
83
84 Ok(ToolOutput {
85 text: result,
86 original_tokens: 0,
87 saved_tokens: 0,
88 mode: Some(action),
89 path: None,
90 })
91 }
92}