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 uses RRF to merge results.",
25            json!({
26                "type": "object",
27                "properties": {
28                    "action": {
29                        "type": "string",
30                        "enum": ["add_root", "remove_root", "list_roots", "search", "status", "save_config"],
31                        "description": "add_root|remove_root|list_roots|search|status|save_config"
32                    },
33                    "path": {
34                        "type": "string",
35                        "description": "Repo path"
36                    },
37                    "alias": {
38                        "type": "string",
39                        "description": "Short alias (auto-derived if omitted)"
40                    },
41                    "query": {
42                        "type": "string",
43                        "description": "Search query (for search action)"
44                    },
45                    "roots": {
46                        "type": "array",
47                        "items": { "type": "string" },
48                        "description": "Filter to specific repos by alias/path"
49                    },
50                    "max_results": {
51                        "type": "integer",
52                        "description": "Max results"
53                    }
54                },
55                "required": ["action"]
56            }),
57        )
58    }
59
60    fn handle(
61        &self,
62        args: &Map<String, Value>,
63        _ctx: &ToolContext,
64    ) -> Result<ToolOutput, ErrorData> {
65        let action = get_str(args, "action")
66            .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
67
68        let path = get_str(args, "path");
69        let alias = get_str(args, "alias");
70        let query = get_str(args, "query");
71        let roots_filter = get_str_array(args, "roots");
72        let max_results = get_usize(args, "max_results").unwrap_or(20).min(1000);
73
74        let (result, original_tokens) = crate::tools::ctx_multi_repo::handle(
75            &action,
76            path.as_deref(),
77            alias.as_deref(),
78            query.as_deref(),
79            roots_filter.as_deref(),
80            max_results,
81        );
82
83        if result.starts_with("ERROR:") {
84            return Err(ErrorData::invalid_params(result, None));
85        }
86
87        let sent = crate::core::tokens::count_tokens(&result);
88        let saved = original_tokens.saturating_sub(sent);
89
90        Ok(ToolOutput {
91            text: result,
92            original_tokens,
93            saved_tokens: saved,
94            mode: Some("multi_repo".to_string()),
95            path,
96            changed: action == "add_root" || action == "remove_root",
97            shell_outcome: None,
98        })
99    }
100}