Skip to main content

lean_ctx/tools/registered/
ctx_multi_repo.rs

1use 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            }),
66        )
67    }
68
69    fn handle(
70        &self,
71        args: &Map<String, Value>,
72        _ctx: &ToolContext,
73    ) -> Result<ToolOutput, ErrorData> {
74        let action = get_str(args, "action")
75            .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
76
77        let path = get_str(args, "path");
78        let alias = get_str(args, "alias");
79        let query = get_str(args, "query");
80        let roots_filter = get_str_array(args, "roots");
81        let max_results = get_usize(args, "max_results").unwrap_or(20).min(1000);
82        let mode = get_str(args, "mode");
83
84        let (result, original_tokens) = crate::tools::ctx_multi_repo::handle(
85            &action,
86            path.as_deref(),
87            alias.as_deref(),
88            query.as_deref(),
89            roots_filter.as_deref(),
90            max_results,
91            mode.as_deref(),
92        );
93
94        if result.starts_with("ERROR:") {
95            return Err(ErrorData::invalid_params(result, None));
96        }
97
98        let sent = crate::core::tokens::count_tokens(&result);
99        let saved = original_tokens.saturating_sub(sent);
100
101        Ok(ToolOutput {
102            text: result,
103            original_tokens,
104            saved_tokens: saved,
105            mode: Some("multi_repo".to_string()),
106            path,
107            changed: action == "add_root" || action == "remove_root",
108            shell_outcome: None,
109        })
110    }
111}