Skip to main content

lean_ctx/tools/registered/
ctx_tools.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
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 as ChoiceCards) | call (proxy a `server::tool`) | list (servers+counts) | refresh.\n\
23             Use find to discover, then call the chosen `server::tool`. Off by default ([gateway] config).",
24            json!({
25                "type": "object",
26                "properties": {
27                    "action": {
28                        "type": "string",
29                        "enum": ["find", "call", "list", "refresh"],
30                        "description": "Operation to perform (default: find)"
31                    },
32                    "query": { "type": "string", "description": "What you want to do; ranks the catalog (find)" },
33                    "tool": { "type": "string", "description": "A `server::tool` handle from find (call)" },
34                    "arguments": { "type": "object", "description": "Arguments forwarded verbatim to the downstream tool (call)" }
35                }
36            }),
37        )
38    }
39
40    fn handle(
41        &self,
42        args: &Map<String, Value>,
43        _ctx: &ToolContext,
44    ) -> Result<ToolOutput, ErrorData> {
45        match crate::tools::ctx_tools::run(args) {
46            Ok(text) => Ok(ToolOutput::simple(text)),
47            Err(e) => Err(ErrorData::invalid_params(e, None)),
48        }
49    }
50}