lean_ctx/tools/registered/
ctx_multi_repo.rs1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{
6 get_int, get_str, get_str_array, McpTool, ToolContext, ToolOutput,
7};
8use crate::tool_defs::tool_def;
9
10pub struct CtxMultiRepoTool;
11
12impl McpTool for CtxMultiRepoTool {
13 fn name(&self) -> &'static str {
14 "ctx_multi_repo"
15 }
16
17 fn tool_def(&self) -> Tool {
18 tool_def(
19 "ctx_multi_repo",
20 "Multi-repo management: add/remove roots, cross-repo search with Reciprocal Rank Fusion (RRF). Enables searching across multiple project directories simultaneously.",
21 json!({
22 "type": "object",
23 "properties": {
24 "action": {
25 "type": "string",
26 "enum": ["add_root", "remove_root", "list_roots", "search", "status", "save_config"],
27 "description": "Action to perform"
28 },
29 "path": {
30 "type": "string",
31 "description": "Repository path (for add_root/remove_root)"
32 },
33 "alias": {
34 "type": "string",
35 "description": "Short alias for the repo (for add_root). Auto-derived from directory name if omitted."
36 },
37 "query": {
38 "type": "string",
39 "description": "Search query (for search action)"
40 },
41 "roots": {
42 "type": "array",
43 "items": { "type": "string" },
44 "description": "Filter search to specific roots by alias or path (for search). Omit to search all."
45 },
46 "max_results": {
47 "type": "integer",
48 "description": "Maximum results to return (default: 20)"
49 }
50 },
51 "required": ["action"]
52 }),
53 )
54 }
55
56 fn handle(
57 &self,
58 args: &Map<String, Value>,
59 _ctx: &ToolContext,
60 ) -> Result<ToolOutput, ErrorData> {
61 let action = get_str(args, "action")
62 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
63
64 let path = get_str(args, "path");
65 let alias = get_str(args, "alias");
66 let query = get_str(args, "query");
67 let roots_filter = get_str_array(args, "roots");
68 let max_results = get_int(args, "max_results").unwrap_or(20) as usize;
69
70 let (result, original_tokens) = crate::tools::ctx_multi_repo::handle(
71 &action,
72 path.as_deref(),
73 alias.as_deref(),
74 query.as_deref(),
75 roots_filter.as_deref(),
76 max_results,
77 );
78
79 if result.starts_with("ERROR:") {
80 return Err(ErrorData::invalid_params(result, None));
81 }
82
83 let sent = crate::core::tokens::count_tokens(&result);
84 let saved = original_tokens.saturating_sub(sent);
85
86 Ok(ToolOutput {
87 text: result,
88 original_tokens,
89 saved_tokens: saved,
90 mode: Some("multi_repo".to_string()),
91 path,
92 changed: action == "add_root" || action == "remove_root",
93 })
94 }
95}