lean_ctx/tools/registered/
ctx_repomap.rs1use rmcp::ErrorData;
4use serde_json::{Map, Value, json};
5
6use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_int, get_str_array};
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 symbol map ranked by structural importance + session relevance.\n\
22 WORKFLOW: call for codebase-wide orientation at session start.\n\
23 ANTIPATTERN: not for task-scoped views — use ctx_overview instead.\n\
24 focus_files=['path/*.rs'] boosts specific areas; max_tokens controls size\n\
25 (default 2048). Saves tokens vs reading all files individually.",
26 json!({
27 "type": "object",
28 "properties": {
29 "path": { "type": "string", "description": "Project root" },
30 "max_tokens": { "type": "integer", "description": "Token budget", "default": 2048 },
31 "focus_files": {
32 "type": "array",
33 "items": { "type": "string" },
34 "description": "Boost ranking for relative paths"
35 }
36 }
37 }),
38 )
39 }
40
41 fn handle(
42 &self,
43 args: &Map<String, Value>,
44 ctx: &ToolContext,
45 ) -> Result<ToolOutput, ErrorData> {
46 let project_root = ctx
47 .resolved_path("path")
48 .map_or_else(|| ctx.project_root.clone(), String::from);
49
50 if project_root.is_empty() {
51 return Err(ErrorData::invalid_params(
52 "No project root available. Provide 'path' or ensure a project is open.",
53 None,
54 ));
55 }
56
57 let max_tokens =
58 get_int(args, "max_tokens").map_or(DEFAULT_MAX_TOKENS, |v| v.max(100) as usize);
59
60 let focus_files = get_str_array(args, "focus_files").unwrap_or_default();
61 let session_files = extract_session_files(ctx);
62
63 let result = crate::tools::ctx_repomap::handle(
64 &project_root,
65 max_tokens,
66 &focus_files,
67 &session_files,
68 );
69
70 let original_tokens = crate::core::tokens::count_tokens(&result);
71
72 Ok(ToolOutput {
73 text: result,
74 original_tokens,
75 saved_tokens: 0,
76 mode: Some("repomap".to_string()),
77 path: Some(project_root),
78 changed: false,
79 shell_outcome: None,
80 content_blocks: None,
81 })
82 }
83}
84
85fn extract_session_files(ctx: &ToolContext) -> Vec<String> {
86 let Some(ref session_arc) = ctx.session else {
87 return Vec::new();
88 };
89
90 let Ok(session) = session_arc.try_read() else {
91 return Vec::new();
92 };
93
94 session
95 .files_touched
96 .iter()
97 .map(|f| f.path.clone())
98 .collect()
99}