lean_ctx/tools/registered/
ctx_repomap.rs1use rmcp::ErrorData;
4use serde_json::{json, Map, Value};
5
6use crate::server::tool_trait::{get_int, get_str_array, McpTool, ToolContext, ToolOutput};
7use crate::tool_defs::tool_def;
8
9pub struct CtxRepomapTool;
10
11const DEFAULT_MAX_TOKENS: usize = 2048;
12
13impl McpTool for CtxRepomapTool {
14 fn name(&self) -> &'static str {
15 "ctx_repomap"
16 }
17
18 fn tool_def(&self) -> rmcp::model::Tool {
19 tool_def(
20 "ctx_repomap",
21 "PageRank-based repo map showing the most important symbols across the codebase, ranked by structural importance and session relevance.",
22 json!({
23 "type": "object",
24 "properties": {
25 "path": { "type": "string", "description": "Project root path (default: session project root)" },
26 "max_tokens": { "type": "integer", "description": "Token budget for output (default: 2048)", "default": 2048 },
27 "focus_files": {
28 "type": "array",
29 "items": { "type": "string" },
30 "description": "Files to boost in ranking (relative paths)"
31 }
32 }
33 }),
34 )
35 }
36
37 fn handle(
38 &self,
39 args: &Map<String, Value>,
40 ctx: &ToolContext,
41 ) -> Result<ToolOutput, ErrorData> {
42 let project_root = ctx
43 .resolved_path("path")
44 .map_or_else(|| ctx.project_root.clone(), String::from);
45
46 if project_root.is_empty() {
47 return Err(ErrorData::invalid_params(
48 "No project root available. Provide 'path' or ensure a project is open.",
49 None,
50 ));
51 }
52
53 let max_tokens =
54 get_int(args, "max_tokens").map_or(DEFAULT_MAX_TOKENS, |v| v.max(100) as usize);
55
56 let focus_files = get_str_array(args, "focus_files").unwrap_or_default();
57
58 let session_files = extract_session_files(ctx);
60
61 let result = crate::tools::ctx_repomap::handle(
62 &project_root,
63 max_tokens,
64 &focus_files,
65 &session_files,
66 );
67
68 let original_tokens = crate::core::tokens::count_tokens(&result);
69
70 Ok(ToolOutput {
71 text: result,
72 original_tokens,
73 saved_tokens: 0,
74 mode: Some("repomap".to_string()),
75 path: Some(project_root),
76 changed: false,
77 })
78 }
79}
80
81fn extract_session_files(ctx: &ToolContext) -> Vec<String> {
83 let Some(ref session_arc) = ctx.session else {
84 return Vec::new();
85 };
86
87 let Ok(session) = session_arc.try_read() else {
88 return Vec::new();
89 };
90
91 session
92 .files_touched
93 .iter()
94 .map(|f| f.path.clone())
95 .collect()
96}