lean_ctx/tools/registered/
ctx_callgraph.rs1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_int, get_str};
6use crate::tool_defs::tool_def;
7
8pub struct CtxCallgraphTool;
9
10impl McpTool for CtxCallgraphTool {
11 fn name(&self) -> &'static str {
12 "ctx_callgraph"
13 }
14
15 fn tool_def(&self) -> Tool {
16 tool_def(
17 "ctx_callgraph",
18 "Callers/callees for one symbol (function call edges, not const/var refs).\n\
19 action=callers|callees symbol='fn' → every call site with file:line.\n\
20 action=trace from→to finds the path between two symbols (depth=N).\n\
21 For end-to-end flow understanding use ctx_compose FIRST.",
22 json!({
23 "type": "object",
24 "properties": {
25 "action": {
26 "type": "string",
27 "enum": ["callers", "callees", "trace", "risk"]
28 },
29 "symbol": { "type": "string" },
30 "file": { "type": "string", "description": "Scope results to file" },
31 "depth": { "type": "integer", "minimum": 1, "maximum": 5 },
32 "from": { "type": "string" },
33 "to": { "type": "string" }
34 },
35 "required": ["action"],
36 "allOf": [
37 { "if": { "properties": { "action": { "enum": ["callers", "callees", "risk"] } }, "required": ["action"] }, "then": { "required": ["symbol"] } },
38 { "if": { "properties": { "action": { "const": "trace" } }, "required": ["action"] }, "then": { "required": ["from", "to"] } }
39 ]
40 }),
41 )
42 }
43
44 fn handle(
45 &self,
46 args: &Map<String, Value>,
47 ctx: &ToolContext,
48 ) -> Result<ToolOutput, ErrorData> {
49 let action = get_str(args, "action").unwrap_or_else(|| "callers".to_string());
50
51 let action_normalized = match action.to_lowercase().as_str() {
52 "callers" | "caller" => "callers",
53 "callees" | "callee" => "callees",
54 "trace" => "trace",
55 "risk" => "risk",
56 _ => action.as_str(),
57 }
58 .to_string();
59
60 let symbol = get_str(args, "symbol");
61 let file = get_str(args, "file");
62 let depth = get_int(args, "depth").unwrap_or(1).clamp(1, 5) as usize;
63 let from = get_str(args, "from");
64 let to = get_str(args, "to");
65
66 let result = crate::tools::ctx_callgraph::handle(
67 &action_normalized,
68 symbol.as_deref(),
69 file.as_deref(),
70 &ctx.project_root,
71 depth,
72 from.as_deref(),
73 to.as_deref(),
74 );
75
76 Ok(ToolOutput {
77 text: result,
78 original_tokens: 0,
79 saved_tokens: 0,
80 mode: Some(action_normalized),
81 path: None,
82 changed: false,
83 shell_outcome: None,
84 content_blocks: None,
85 })
86 }
87}