Skip to main content

lean_ctx/tools/registered/
ctx_tools.rs

1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8/// `ctx_tools` — MCP Tool-Catalog Gateway (#210). Aggregates downstream MCP
9/// servers and returns a per-query top-N shortlist instead of injecting every
10/// downstream schema, then proxies the real call.
11pub struct CtxToolsTool;
12
13impl McpTool for CtxToolsTool {
14    fn name(&self) -> &'static str {
15        "ctx_tools"
16    }
17
18    fn tool_def(&self) -> Tool {
19        tool_def(
20            "ctx_tools",
21            "Gateway to downstream MCP servers — unlimited external tools at ~constant context cost.\n\
22             actions: find (query → top-N relevant tools) | call (proxy a server::tool) |\n\
23             list (servers+counts) | refresh.\n\
24             WORKFLOW: find to discover, then call the chosen server::tool.\n\
25             ANTIPATTERN: not for built-in tools — use those directly.",
26            json!({
27                "type": "object",
28                "properties": {
29                    "action": {
30                        "type": "string",
31                        "enum": ["find", "call", "list", "refresh"],
32                        "description": "find|call|list|refresh"
33                    },
34                    "query": { "type": "string", "description": "What you want to do (for find)" },
35                    "tool": { "type": "string", "description": "`server::tool` handle (for call)" },
36                    "arguments": { "type": "object", "description": "Arguments for downstream tool (call)" }
37                },
38                "allOf": [
39                    {
40                        "if": { "properties": { "action": { "const": "call" } }, "required": ["action"] },
41                        "then": { "required": ["action", "tool"] }
42                    }
43                ]
44            }),
45        )
46    }
47
48    fn handle(
49        &self,
50        args: &Map<String, Value>,
51        ctx: &ToolContext,
52    ) -> Result<ToolOutput, ErrorData> {
53        // `project_root` is threaded through so the gateway's L3 consolidation
54        // (#1095) can write addon output into the project's BM25/graph/knowledge
55        // stores. Empty (one-shot CLI ctx) disables project-scoped indexing.
56        match crate::tools::ctx_tools::run(args, &ctx.project_root) {
57            Ok(text) => Ok(ToolOutput::simple(text)),
58            Err(e) => Err(ErrorData::invalid_params(e, None)),
59        }
60    }
61}