Skip to main content

lean_ctx/tools/registered/
ctx_callgraph.rs

1use 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            }),
36        )
37    }
38
39    fn handle(
40        &self,
41        args: &Map<String, Value>,
42        ctx: &ToolContext,
43    ) -> Result<ToolOutput, ErrorData> {
44        let action = get_str(args, "action").unwrap_or_else(|| "callers".to_string());
45
46        let action_normalized = match action.to_lowercase().as_str() {
47            "callers" | "caller" => "callers",
48            "callees" | "callee" => "callees",
49            "trace" => "trace",
50            "risk" => "risk",
51            _ => action.as_str(),
52        }
53        .to_string();
54
55        let symbol = get_str(args, "symbol");
56        let file = get_str(args, "file");
57        let depth = get_int(args, "depth").unwrap_or(1).clamp(1, 5) as usize;
58        let from = get_str(args, "from");
59        let to = get_str(args, "to");
60
61        let result = crate::tools::ctx_callgraph::handle(
62            &action_normalized,
63            symbol.as_deref(),
64            file.as_deref(),
65            &ctx.project_root,
66            depth,
67            from.as_deref(),
68            to.as_deref(),
69        );
70
71        Ok(ToolOutput {
72            text: result,
73            original_tokens: 0,
74            saved_tokens: 0,
75            mode: Some(action_normalized),
76            path: None,
77            changed: false,
78            shell_outcome: None,
79            content_blocks: None,
80        })
81    }
82}