mcp-multiplexer 0.1.0

One MCP server fronting many: 7 meta-tools instead of every tool schema in context
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use crate::config::Config;
use crate::model::ToolInfo;
use crate::search::search;
use crate::upstream::Upstreams;
use rmcp::handler::server::router::tool::ToolRouter;
use rmcp::handler::server::tool::ToolCallContext;
use rmcp::handler::server::wrapper::Parameters;
use rmcp::model::*;
use rmcp::service::RequestContext;
use rmcp::{RoleServer, ServerHandler, tool, tool_handler, tool_router};
use schemars::JsonSchema;
use serde::Serialize;
use std::sync::Arc;

#[derive(Serialize)]
pub struct ServerSummary {
    pub name: String,
    pub status: String,
    pub tool_count: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,
}

#[derive(Serialize)]
pub struct ToolSummary {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub annotations: Option<serde_json::Value>,
}

#[derive(Serialize)]
pub struct RefreshResult {
    pub name: String,
    pub status: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_count: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

#[derive(serde::Deserialize, JsonSchema)]
pub struct ServerArg {
    pub server: String,
}

#[derive(serde::Deserialize, JsonSchema)]
pub struct SearchArgs {
    pub query: String,
    pub server: Option<String>,
    pub limit: Option<usize>,
}

#[derive(serde::Deserialize, JsonSchema)]
pub struct RefreshArgs {
    pub server: Option<String>,
}

#[derive(serde::Deserialize, JsonSchema)]
pub struct DescribeArgs {
    pub server: String,
    pub tool: String,
}

#[derive(serde::Deserialize, JsonSchema)]
pub struct CallArgs {
    pub server: String,
    pub tool: String,
    pub arguments: Option<serde_json::Map<String, serde_json::Value>>,
}

#[derive(serde::Deserialize, JsonSchema)]
pub struct AuthorizeArgs {
    pub server: String,
    /// Headless completion: the final redirect URL (http://127.0.0.1:.../callback?code=...)
    pub pasted_url: Option<String>,
}

#[derive(Clone)]
pub struct Aggregator {
    cfg: Config,
    ups: Arc<Upstreams>,
    tool_router: ToolRouter<Self>,
}

fn internal(e: impl std::fmt::Display) -> ErrorData {
    ErrorData::internal_error(e.to_string(), None)
}

impl Aggregator {
    pub fn new(cfg: Config, ups: Arc<Upstreams>) -> Aggregator {
        Aggregator {
            cfg,
            ups,
            tool_router: Self::tool_router(),
        }
    }

    pub async fn list_servers(&self) -> anyhow::Result<Vec<ServerSummary>> {
        let mut out = Vec::new();
        for name in self.ups.server_names() {
            out.push(ServerSummary {
                // in-memory index only: list_servers must never connect (lazy startup)
                tool_count: self.ups.cached_tools(&name).len(),
                status: self.ups.status(&name).to_string(),
                instructions: self.ups.instructions(&name),
                name,
            });
        }
        Ok(out)
    }

    pub async fn list_tools(&self, server: String) -> anyhow::Result<Vec<ToolSummary>> {
        Ok(self
            .ups
            .tools(&server)
            .await?
            .into_iter()
            .map(|t| ToolSummary {
                name: t.name,
                description: t.description,
                annotations: t.annotations,
            })
            .collect())
    }

    pub async fn search_tools(
        &self,
        query: String,
        server: Option<String>,
        limit: usize,
    ) -> anyhow::Result<Vec<(String, ToolInfo)>> {
        let names = match server.as_deref() {
            Some(s) => {
                if !self.ups.server_names().iter().any(|n| n == s) {
                    anyhow::bail!(
                        "unknown server {s:?}; available: {}",
                        self.ups.server_names().join(", ")
                    );
                }
                vec![s.to_string()]
            }
            None => self.ups.server_names(),
        };
        // fetch concurrently: a cold/hanging server must not serialize N x connect-timeout into a search
        let mut set = tokio::task::JoinSet::new();
        for name in names {
            let ups = self.ups.clone();
            set.spawn(async move { (name.clone(), ups.tools(&name).await.unwrap_or_default()) });
        }
        let mut index = Vec::new();
        while let Some(pair) = set.join_next().await {
            index.push(pair?);
        }
        Ok(search(&index, &query, server.as_deref(), limit))
    }

    /// Reconnect one or all servers and rebuild the tool index. Per-server
    /// errors are reported, not propagated — one dead server must not block
    /// refreshing the others.
    pub async fn refresh_tools(
        &self,
        server: Option<String>,
    ) -> anyhow::Result<Vec<RefreshResult>> {
        let names = match server.as_deref() {
            Some(s) => {
                if !self.ups.server_names().iter().any(|n| n == s) {
                    anyhow::bail!(
                        "unknown server {s:?}; available: {}",
                        self.ups.server_names().join(", ")
                    );
                }
                vec![s.to_string()]
            }
            None => self.ups.server_names(),
        };
        let mut set = tokio::task::JoinSet::new();
        for name in names {
            let ups = self.ups.clone();
            set.spawn(async move {
                match ups.refresh(&name).await {
                    Ok(()) => RefreshResult {
                        tool_count: Some(ups.cached_tools(&name).len()),
                        status: "ok".into(),
                        error: None,
                        name,
                    },
                    Err(e) => RefreshResult {
                        tool_count: None,
                        status: "error".into(),
                        error: Some(e.to_string()),
                        name,
                    },
                }
            });
        }
        let mut out = Vec::new();
        while let Some(r) = set.join_next().await {
            out.push(r?);
        }
        out.sort_by(|a, b| a.name.cmp(&b.name));
        Ok(out)
    }

    pub async fn describe_tool(&self, server: String, tool: String) -> anyhow::Result<ToolInfo> {
        if let Some(sc) = self.cfg.mcp_servers.get(&server)
            && !sc.is_allowed(&tool)
        {
            anyhow::bail!("tool {tool:?} on server {server:?} is blocked by config");
        }
        self.ups
            .tools(&server)
            .await?
            .into_iter()
            .find(|t| t.name == tool)
            .ok_or_else(|| anyhow::anyhow!("unknown tool {tool:?} on server {server:?}"))
    }

    pub async fn call_tool(
        &self,
        server: String,
        tool: String,
        arguments: Option<serde_json::Map<String, serde_json::Value>>,
    ) -> Result<CallToolResult, ErrorData> {
        self.ups
            .call(&server, &tool, arguments)
            .await
            .map_err(internal)
    }

    pub async fn authorize_server(
        &self,
        server: String,
        pasted_url: Option<String>,
    ) -> anyhow::Result<String> {
        match pasted_url {
            Some(u) => {
                self.ups.oauth_complete(&server, &u).await?;
                self.ups.refresh(&server).await?;
                Ok(format!("authorized; tool index refreshed for {server}"))
            }
            None => match self.ups.oauth_begin(&server).await? {
                None => Ok(format!("{server} is already authorized")),
                Some(url) => Ok(format!(
                    "Authorization required for {server}.\n\
                     1. Open in a browser: {url}\n\
                     2. Approve access — the redirect completes automatically.\n\
                     3. Retry your call.\n\
                     Headless? Open the URL anywhere, then call authorize_server again \
                     with pasted_url set to the final redirect URL."
                )),
            },
        }
    }

    /// Split a `server__tool` name into (server, tool) by longest matching
    /// expose-server prefix — server names may themselves contain `__`.
    pub fn route_exposed<'n>(&self, name: &'n str) -> Option<(&str, &'n str)> {
        self.cfg
            .mcp_servers
            .iter()
            .filter(|(_, sc)| sc.expose)
            .filter_map(|(n, _)| {
                name.strip_prefix(&format!("{n}__"))
                    .map(|t| (n.as_str(), t))
            })
            .max_by_key(|(n, _)| n.len())
    }
}

#[tool_router]
impl Aggregator {
    #[tool(
        name = "list_servers",
        description = "List connected MCP servers: name, status, tool count, instructions"
    )]
    async fn list_servers_tool(&self) -> Result<String, ErrorData> {
        let v = self.list_servers().await.map_err(internal)?;
        serde_json::to_string_pretty(&v).map_err(internal)
    }

    #[tool(
        name = "list_tools",
        description = "List a server's tools: name, one-line description, annotations. No schemas."
    )]
    async fn list_tools_tool(
        &self,
        Parameters(p): Parameters<ServerArg>,
    ) -> Result<String, ErrorData> {
        let v = self.list_tools(p.server).await.map_err(internal)?;
        serde_json::to_string_pretty(&v).map_err(internal)
    }

    #[tool(
        name = "search_tools",
        description = "Search tools across servers by name/description. Returns matches WITH full input schemas."
    )]
    async fn search_tools_tool(
        &self,
        Parameters(p): Parameters<SearchArgs>,
    ) -> Result<String, ErrorData> {
        let v = self
            .search_tools(p.query, p.server, p.limit.unwrap_or(5).min(25))
            .await
            .map_err(internal)?;
        serde_json::to_string_pretty(&v).map_err(internal)
    }

    #[tool(
        name = "refresh_tools",
        description = "Reconnect one server (or all) and rebuild its tool index. Use when a server's tools changed."
    )]
    async fn refresh_tools_tool(
        &self,
        Parameters(p): Parameters<RefreshArgs>,
    ) -> Result<String, ErrorData> {
        let v = self.refresh_tools(p.server).await.map_err(internal)?;
        serde_json::to_string_pretty(&v).map_err(internal)
    }

    #[tool(
        name = "describe_tool",
        description = "Get the full input schema of one exact tool"
    )]
    async fn describe_tool_tool(
        &self,
        Parameters(p): Parameters<DescribeArgs>,
    ) -> Result<String, ErrorData> {
        let v = self
            .describe_tool(p.server, p.tool)
            .await
            .map_err(internal)?;
        serde_json::to_string_pretty(&v).map_err(internal)
    }

    #[tool(
        name = "call_tool",
        description = "Call a tool on an upstream MCP server. Arguments must match its schema."
    )]
    async fn call_tool_tool(
        &self,
        Parameters(p): Parameters<CallArgs>,
    ) -> Result<CallToolResult, ErrorData> {
        self.call_tool(p.server, p.tool, p.arguments).await
    }

    #[tool(
        name = "authorize_server",
        description = "OAuth-authorize a server. Without pasted_url: returns the authorization URL or 'already authorized'. With pasted_url: completes a headless flow from the final redirect URL."
    )]
    async fn authorize_server_tool(
        &self,
        Parameters(p): Parameters<AuthorizeArgs>,
    ) -> Result<String, ErrorData> {
        self.authorize_server(p.server, p.pasted_url)
            .await
            .map_err(internal)
    }
}

#[tool_handler]
impl ServerHandler for Aggregator {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
            .with_instructions("Multiplexed MCP servers. Use list_servers → list_tools/search_tools → describe_tool → call_tool. refresh_tools re-indexes after upstream tool changes. authorize_server handles OAuth login. Tools named server__tool are directly exposed.")
    }

    async fn list_tools(
        &self,
        _req: Option<PaginatedRequestParams>,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, ErrorData> {
        let mut tools = self.tool_router.list_all();
        for (name, sc) in &self.cfg.mcp_servers {
            if !sc.expose {
                continue;
            }
            if let Ok(list) = self.ups.tools(name).await {
                for t in list {
                    let schema = match t.schema {
                        serde_json::Value::Object(m) => Arc::new(m),
                        _ => Arc::new(serde_json::Map::new()),
                    };
                    let mut tool = Tool::new_with_raw(
                        format!("{name}__{}", t.name),
                        t.description.map(std::borrow::Cow::Owned),
                        schema,
                    );
                    tool.annotations = t.annotations.and_then(|v| serde_json::from_value(v).ok());
                    tools.push(tool);
                }
            }
        }
        Ok(ListToolsResult::with_all_items(tools))
    }

    async fn call_tool(
        &self,
        req: CallToolRequestParams,
        ctx: RequestContext<RoleServer>,
    ) -> Result<CallToolResponse, ErrorData> {
        if let Some((server, tool)) = self.route_exposed(&req.name) {
            let tool = tool.to_string();
            return self
                .ups
                .call(server, &tool, req.arguments)
                .await
                .map(CallToolResponse::from)
                .map_err(internal);
        }
        let tcc = ToolCallContext::new(self, req, ctx);
        self.tool_router.call(tcc).await
    }
}