synwire-mcp-adapters 0.1.0

High-level MCP adapters: multi-server client, tool conversion, interceptors, and provider abstraction
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! Multi-server MCP client.
//!
//! [`MultiServerMcpClient`] connects to N named MCP servers simultaneously,
//! aggregates their tools, and routes tool calls to the correct server.

use std::collections::HashMap;
use std::sync::Arc;

use futures_util::future::join_all;
use serde_json::Value;
use synwire_core::mcp::traits::{McpServerStatus, McpTransport};
use tokio::sync::RwLock;

use crate::callbacks::McpCallbacks;
use crate::error::McpAdapterError;
use crate::session::McpClientSession;

// ---------------------------------------------------------------------------
// Connection configuration
// ---------------------------------------------------------------------------

/// Configuration for connecting to a single MCP server.
///
/// Each variant describes a different transport mechanism.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Connection {
    /// Launch a subprocess and communicate over its stdin/stdout.
    Stdio {
        /// Executable path.
        command: String,
        /// Command-line arguments.
        args: Vec<String>,
        /// Environment variables.
        env: HashMap<String, String>,
    },

    /// Connect via Server-Sent Events (SSE) transport.
    Sse {
        /// SSE endpoint URL.
        url: String,
        /// Optional Bearer token.
        auth_token: Option<String>,
        /// Connection timeout in seconds.
        timeout_secs: Option<u64>,
    },

    /// Connect via Streamable HTTP (MCP 2025-03-26 spec).
    StreamableHttp {
        /// HTTP endpoint URL.
        url: String,
        /// Optional Bearer token.
        auth_token: Option<String>,
        /// Connection timeout in seconds.
        timeout_secs: Option<u64>,
    },

    /// Connect via WebSocket.
    WebSocket {
        /// WebSocket URL (ws:// or wss://).
        url: String,
        /// Optional Bearer token.
        auth_token: Option<String>,
    },
}

impl Connection {
    /// Creates a transport for this connection configuration.
    ///
    /// Returns a `Box<dyn McpTransport>` suitable for use with
    /// [`McpClientSession`].
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying transport cannot be initialised
    /// (e.g. HTTP client TLS failure).
    pub fn into_transport(
        self,
        name: &str,
    ) -> Result<Box<dyn McpTransport>, synwire_core::agents::error::AgentError> {
        match self {
            Self::Stdio { command, args, env } => Ok(Box::new(
                synwire_agent::mcp::StdioMcpTransport::new(name, command, args, env),
            )),
            Self::Sse {
                url,
                auth_token,
                timeout_secs,
            }
            | Self::StreamableHttp {
                url,
                auth_token,
                timeout_secs,
            } => Ok(Box::new(synwire_agent::mcp::HttpMcpTransport::try_new(
                name,
                url,
                auth_token,
                timeout_secs,
            )?)),
            Self::WebSocket { url, auth_token } => Ok(Box::new(
                crate::transport::WebSocketMcpTransport::new(name, url, auth_token),
            )),
        }
    }
}

// ---------------------------------------------------------------------------
// Server entry
// ---------------------------------------------------------------------------

struct ServerEntry {
    session: McpClientSession,
    /// Optional prefix applied to all tool names from this server.
    tool_name_prefix: Option<String>,
}

impl std::fmt::Debug for ServerEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ServerEntry")
            .field("session", &self.session)
            .field("tool_name_prefix", &self.tool_name_prefix)
            .finish()
    }
}

// ---------------------------------------------------------------------------
// MultiServerMcpClient
// ---------------------------------------------------------------------------

/// Configuration used to build a [`MultiServerMcpClient`].
#[derive(Debug, Default)]
pub struct MultiServerMcpClientConfig {
    /// Named server connections.
    pub servers: HashMap<String, Connection>,
    /// Optional prefix applied to all aggregated tool names.
    ///
    /// Per-server prefixes can be set via [`with_server_prefix`](Self::with_server_prefix).
    pub global_tool_prefix: Option<String>,
    /// Per-server tool name prefixes (override the global prefix for a specific server).
    pub server_prefixes: HashMap<String, String>,
}

impl MultiServerMcpClientConfig {
    /// Creates an empty configuration.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds a named server to the configuration.
    #[must_use]
    pub fn with_server(mut self, name: impl Into<String>, connection: Connection) -> Self {
        let _ = self.servers.insert(name.into(), connection);
        self
    }

    /// Sets a per-server tool name prefix.
    #[must_use]
    pub fn with_server_prefix(
        mut self,
        server_name: impl Into<String>,
        prefix: impl Into<String>,
    ) -> Self {
        let _ = self
            .server_prefixes
            .insert(server_name.into(), prefix.into());
        self
    }

    /// Sets the global tool name prefix applied to all servers that lack a
    /// per-server prefix.
    #[must_use]
    pub fn with_global_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.global_tool_prefix = Some(prefix.into());
        self
    }
}

/// A client that connects to multiple MCP servers simultaneously and
/// aggregates their tools under a unified interface.
///
/// # Connection
///
/// Call [`connect`](Self::connect) to establish connections to all configured
/// servers in parallel. Tools become available immediately after connection.
///
/// # Tool naming
///
/// Each server may have an optional prefix. When a prefix is configured,
/// tool names are exposed as `{prefix}/{tool_name}` to avoid collisions
/// across servers. The original server-local name is preserved for routing.
pub struct MultiServerMcpClient {
    servers: Arc<RwLock<HashMap<String, ServerEntry>>>,
    /// Callbacks for logging, progress, and elicitation events.
    callbacks: Arc<McpCallbacks>,
}

impl std::fmt::Debug for MultiServerMcpClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MultiServerMcpClient")
            .field("callbacks", &self.callbacks)
            .finish_non_exhaustive()
    }
}

impl MultiServerMcpClient {
    /// Connects to all servers in `config` simultaneously and returns a
    /// fully initialised client.
    ///
    /// Connection failures for individual servers are logged and their status
    /// will show as `Disconnected`. Call [`health`](Self::health) to inspect
    /// per-server status.
    ///
    /// # Errors
    ///
    /// Returns [`McpAdapterError`] only if configuration is fundamentally
    /// invalid. Individual server failures are accumulated silently.
    pub async fn connect(
        config: MultiServerMcpClientConfig,
        callbacks: McpCallbacks,
    ) -> Result<Self, McpAdapterError> {
        let callbacks = Arc::new(callbacks);

        // Destructure config to allow partial moves.
        let MultiServerMcpClientConfig {
            servers,
            server_prefixes,
            global_tool_prefix,
        } = config;

        // Connect all servers in parallel
        let connect_futures: Vec<_> = servers
            .into_iter()
            .map(|(name, conn)| {
                let prefix = server_prefixes
                    .get(&name)
                    .cloned()
                    .or_else(|| global_tool_prefix.clone());
                let transport_result = conn.into_transport(&name);
                async move {
                    let transport: Arc<dyn McpTransport> = match transport_result {
                        Ok(t) => Arc::from(t),
                        Err(e) => {
                            tracing::error!(server = %name, error = %e, "Failed to build transport");
                            return None;
                        }
                    };
                    match McpClientSession::connect(name.clone(), transport).await {
                        Ok(mut session) => {
                            // Best-effort tool cache population
                            if let Err(e) = session.populate_tool_cache().await {
                                tracing::warn!(
                                    server = %name,
                                    error = %e,
                                    "Failed to populate tool cache"
                                );
                            }
                            Some((
                                name,
                                ServerEntry {
                                    session,
                                    tool_name_prefix: prefix,
                                },
                            ))
                        }
                        Err(e) => {
                            tracing::error!(
                                server = %name,
                                error = %e,
                                "Failed to connect to MCP server"
                            );
                            None
                        }
                    }
                }
            })
            .collect();

        let results = join_all(connect_futures).await;
        let servers: HashMap<String, ServerEntry> = results.into_iter().flatten().collect();

        tracing::info!(connected = servers.len(), "MultiServerMcpClient connected");

        Ok(Self {
            servers: Arc::new(RwLock::new(servers)),
            callbacks,
        })
    }

    /// Returns all aggregated tool descriptors from all connected servers.
    ///
    /// Tool names are prefixed when a server prefix is configured.
    pub async fn get_tool_descriptors(&self) -> Vec<AggregatedToolDescriptor> {
        let servers = self.servers.read().await;
        let mut tools = Vec::new();

        for (server_name, entry) in servers.iter() {
            for descriptor in entry.session.cached_tools() {
                let exposed_name = entry.tool_name_prefix.as_ref().map_or_else(
                    || descriptor.name.clone(),
                    |prefix| format!("{prefix}/{}", descriptor.name),
                );
                tools.push(AggregatedToolDescriptor {
                    exposed_name,
                    server_name: server_name.clone(),
                    original_name: descriptor.name.clone(),
                    description: descriptor.description.clone(),
                    input_schema: descriptor.input_schema.clone(),
                });
            }
        }
        drop(servers);

        tools
    }

    /// Returns health status for all servers.
    #[allow(clippy::significant_drop_tightening)]
    pub async fn health(&self) -> Vec<McpServerStatus> {
        let servers = self.servers.read().await;
        let status_futures: Vec<_> = servers
            .values()
            .map(|entry| entry.session.status())
            .collect();
        join_all(status_futures).await
    }

    /// Calls a tool by its exposed name (including any prefix).
    ///
    /// # Errors
    ///
    /// - [`McpAdapterError::ToolNotFound`] if no server exposes the given name.
    /// - [`McpAdapterError::Transport`] if the tool call fails.
    pub async fn call_tool(
        &self,
        exposed_tool_name: &str,
        arguments: Value,
    ) -> Result<Value, McpAdapterError> {
        // Resolve routing and clone the transport Arc, then drop the lock
        // before the async call to avoid holding the guard across await points.
        let (server_name, original_name, transport) = {
            let servers = self.servers.read().await;

            let routing = servers.iter().find_map(|(server_name, entry)| {
                for descriptor in entry.session.cached_tools() {
                    let exposed = entry.tool_name_prefix.as_ref().map_or_else(
                        || descriptor.name.clone(),
                        |prefix| format!("{prefix}/{}", descriptor.name),
                    );
                    if exposed == exposed_tool_name {
                        return Some((server_name.clone(), descriptor.name.clone()));
                    }
                }
                None
            });

            let (server_name, original_name) =
                routing.ok_or_else(|| McpAdapterError::ToolNotFound {
                    name: exposed_tool_name.to_owned(),
                })?;

            let transport = servers
                .get(&server_name)
                .ok_or_else(|| McpAdapterError::ServerNotFound {
                    name: server_name.clone(),
                })?
                .session
                .transport()
                .clone();
            drop(servers);

            (server_name, original_name, transport)
        };

        transport
            .call_tool(&original_name, arguments)
            .await
            .map_err(|e| McpAdapterError::Transport {
                message: format!("Tool '{original_name}' on server '{server_name}' failed: {e}"),
            })
    }

    /// Returns a reference to the callbacks bundle.
    #[must_use]
    pub fn callbacks(&self) -> &McpCallbacks {
        &self.callbacks
    }
}

// ---------------------------------------------------------------------------
// AggregatedToolDescriptor
// ---------------------------------------------------------------------------

/// A tool descriptor with routing metadata for [`MultiServerMcpClient`].
#[derive(Debug, Clone)]
pub struct AggregatedToolDescriptor {
    /// The tool name as exposed by this client (may include server prefix).
    pub exposed_name: String,
    /// The name of the server that provides this tool.
    pub server_name: String,
    /// The tool's original name on the server.
    pub original_name: String,
    /// Human-readable description.
    pub description: String,
    /// JSON Schema for the tool's input parameters.
    pub input_schema: Value,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::pagination::PaginationCursor;

    #[test]
    fn connection_enum_variants_exist() {
        let _stdio = Connection::Stdio {
            command: "mcp-server".into(),
            args: vec![],
            env: HashMap::new(),
        };
        let _ws = Connection::WebSocket {
            url: "ws://localhost:3000".into(),
            auth_token: None,
        };
        let _sse = Connection::Sse {
            url: "http://localhost:3000/sse".into(),
            auth_token: None,
            timeout_secs: None,
        };
        let _http = Connection::StreamableHttp {
            url: "http://localhost:3000".into(),
            auth_token: None,
            timeout_secs: None,
        };
    }

    #[test]
    fn config_builder() {
        let config = MultiServerMcpClientConfig::new()
            .with_server(
                "s1",
                Connection::WebSocket {
                    url: "ws://localhost:3000".into(),
                    auth_token: None,
                },
            )
            .with_server_prefix("s1", "srv1")
            .with_global_prefix("global");

        assert!(config.servers.contains_key("s1"));
        assert_eq!(config.server_prefixes.get("s1"), Some(&"srv1".to_owned()));
        assert_eq!(config.global_tool_prefix, Some("global".to_owned()));
    }

    #[test]
    fn pagination_used_in_client_context() {
        // Verify PaginationCursor is usable from client module context.
        let mut cursor = PaginationCursor::new();
        assert!(cursor.advance(Some("token1".into())));
        assert!(!cursor.advance(None));
    }
}