lean_ctx/tools/registered/
ctx_multi_repo.rs1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{
6 McpTool, ToolContext, ToolOutput, get_str, get_str_array, get_usize,
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-repository — add, remove, search project directories.\n\
21 WORKFLOW: list_roots → add_root/remove_root → search.\n\
22 ANTI-PATTERN: not for single-repo projects — use ctx_search.\n\
23 Actions: add_root|remove_root|list_roots|search|status|save_config.\n\
24 Cross-repo search runs hybrid retrieval per root (BM25+dense+SPLADE)\n\
25 and merges with RRF; mode=\"bm25\" forces lexical-only.\n\
26 ctx_search/ctx_glob/ctx_tree/ctx_read all accept a repo=<alias>\n\
27 arg (not in their own schema) to target a registered root by\n\
28 alias instead of the project root — list_roots shows the aliases.",
29 json!({
30 "type": "object",
31 "properties": {
32 "action": {
33 "type": "string",
34 "enum": ["add_root", "remove_root", "list_roots", "search", "status", "save_config"],
35 "description": "add_root|remove_root|list_roots|search|status|save_config"
36 },
37 "path": {
38 "type": "string",
39 "description": "Repo path"
40 },
41 "alias": {
42 "type": "string",
43 "description": "Short alias (auto-derived if omitted)"
44 },
45 "query": {
46 "type": "string",
47 "description": "Search query (for search action)"
48 },
49 "roots": {
50 "type": "array",
51 "items": { "type": "string" },
52 "description": "Filter to specific repos by alias/path"
53 },
54 "max_results": {
55 "type": "integer",
56 "description": "Max results"
57 },
58 "mode": {
59 "type": "string",
60 "enum": ["hybrid", "bm25"],
61 "description": "Per-root ranking signal (default: hybrid; bm25 = lexical only)"
62 }
63 },
64 "required": ["action"],
65 "allOf": [
66 {
67 "if": { "properties": { "action": { "const": "add_root" } }, "required": ["action"] },
68 "then": { "required": ["action", "path"] }
69 },
70 {
71 "if": { "properties": { "action": { "const": "remove_root" } }, "required": ["action"] },
72 "then": { "required": ["action", "path"] }
73 },
74 {
75 "if": { "properties": { "action": { "const": "search" } }, "required": ["action"] },
76 "then": { "required": ["action", "query"] }
77 }
78 ]
79 }),
80 )
81 }
82
83 fn handle(
84 &self,
85 args: &Map<String, Value>,
86 _ctx: &ToolContext,
87 ) -> Result<ToolOutput, ErrorData> {
88 let action = get_str(args, "action")
89 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
90
91 let path = get_str(args, "path");
92 let alias = get_str(args, "alias");
93 let query = get_str(args, "query");
94 let roots_filter = get_str_array(args, "roots");
95 let max_results = get_usize(args, "max_results").unwrap_or(20).min(1000);
96 let mode = get_str(args, "mode");
97
98 let (result, original_tokens) = crate::tools::ctx_multi_repo::handle(
99 &action,
100 path.as_deref(),
101 alias.as_deref(),
102 query.as_deref(),
103 roots_filter.as_deref(),
104 max_results,
105 mode.as_deref(),
106 );
107
108 if result.starts_with("ERROR:") {
109 return Err(ErrorData::invalid_params(result, None));
110 }
111
112 let sent = crate::core::tokens::count_tokens(&result);
113 let saved = original_tokens.saturating_sub(sent);
114
115 Ok(ToolOutput {
116 text: result,
117 original_tokens,
118 saved_tokens: saved,
119 mode: Some("multi_repo".to_string()),
120 path,
121 changed: action == "add_root" || action == "remove_root",
122 shell_outcome: None,
123 content_blocks: None,
124 })
125 }
126}