model-context-protocol 0.2.2

A Rust implementation of the Model Context Protocol (MCP) for AI tool integration
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
//! McpServerHub - A hub that aggregates multiple MCP servers into a single server.
//!
//! The McpServerHub connects to multiple external MCP servers and exposes their
//! tools as a unified MCP server that can be wrapped by McpStdioServer or McpHttpServer.
//!
//! # Example
//!
//! ```rust,ignore
//! use mcp::{McpServerHub, McpServerConnectionConfig};
//! use mcp::server::stdio::McpStdioServer;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Create a hub that aggregates multiple servers
//!     let hub = McpServerHub::new("aggregator");
//!
//!     // Connect to external servers
//!     let calc_config = McpServerConnectionConfig::stdio(
//!         "calculator",
//!         "node",
//!         vec!["calc-server.js".into()],
//!     );
//!     hub.connect(calc_config).await?;
//!
//!     let files_config = McpServerConnectionConfig::stdio(
//!         "files",
//!         "python",
//!         vec!["files-server.py".into()],
//!     );
//!     hub.connect(files_config).await?;
//!
//!     // Run as a stdio server - all connected tools are now exposed
//!     McpStdioServer::run(hub.into_config()).await?;
//!     Ok(())
//! }
//! ```

use serde_json::Value;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;

use crate::hub_common::HubConnections;
use crate::protocol::McpToolDefinition;
use crate::server::McpServerConfig;
use crate::tool::{BoxFuture, DynTool, McpTool, ToolCallResult, ToolProvider};
use crate::transport::{McpServerConnectionConfig, McpTransportError};

/// A hub that aggregates multiple MCP servers into a single MCP server.
///
/// This allows you to:
/// - Connect to multiple external MCP servers
/// - Expose all their tools through a single unified server
/// - Wrap the hub with McpStdioServer or McpHttpServer
/// - Automatically restart servers on failure
/// - Parallel tool discovery for performance
///
/// Tools from connected servers are automatically discovered and made available
/// through the hub's server interface.
pub struct McpServerHub {
    /// Hub server name
    name: String,
    /// Shared connection infrastructure
    connections: HubConnections,
    /// Default timeout for operations
    timeout: Duration,
}

impl McpServerHub {
    /// Create a new hub with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            connections: HubConnections::new(),
            timeout: Duration::from_secs(30),
        }
    }

    /// Create a hub with a custom timeout.
    pub fn with_timeout(name: impl Into<String>, timeout: Duration) -> Self {
        Self {
            name: name.into(),
            connections: HubConnections::new(),
            timeout,
        }
    }

    /// Connect to an external MCP server.
    ///
    /// This method:
    /// 1. Creates the appropriate transport based on config
    /// 2. Initializes the connection
    /// 3. Discovers tools and creates proxy tools for them
    /// 4. Starts a restart monitor if restart policy is enabled
    pub async fn connect(
        self: &Arc<Self>,
        config: McpServerConnectionConfig,
    ) -> Result<(), McpTransportError> {
        let server_name = config.name.clone();
        let restart_enabled = config.restart_policy.enabled;

        // Connect using shared infrastructure
        let connection = self.connections.connect(config).await?;

        // Start restart monitor if enabled
        if restart_enabled {
            let hub = Arc::clone(self);
            let conn = Arc::clone(&connection);
            let name = server_name.clone();

            tokio::spawn(async move {
                hub.restart_monitor(name, conn).await;
            });
        }

        Ok(())
    }

    /// Monitor a connection and restart on failure.
    async fn restart_monitor(&self, name: String, conn: Arc<crate::hub_common::ManagedConnection>) {
        let policy = &conn.config.restart_policy;

        loop {
            // Wait for health check interval or restart notification
            tokio::select! {
                _ = conn.restart_notify.notified() => {}
                _ = tokio::time::sleep(Duration::from_secs(5)) => {
                    if conn.is_alive().await {
                        continue;
                    }
                }
            }

            // Check if shutdown was requested
            if conn.shutdown_requested.load(Ordering::SeqCst) {
                break;
            }

            // Check if transport is still alive (double-check)
            if conn.is_alive().await {
                continue;
            }

            // Server is dead - notify all pending requests to fail immediately
            conn.notify_failure();

            // Get current attempt count
            let attempt = conn.restart_count.fetch_add(1, Ordering::SeqCst);

            // Check if we've exceeded max attempts
            if let Some(max) = policy.max_attempts {
                if attempt >= max {
                    eprintln!(
                        "[McpServerHub] Server '{}' exceeded max restart attempts ({})",
                        name, max
                    );
                    break;
                }
            }

            // Calculate delay with exponential backoff
            let delay = policy.delay_for_attempt(attempt);

            eprintln!(
                "[McpServerHub] Server '{}' disconnected. Restarting in {}ms (attempt {}/{})",
                name,
                delay,
                attempt + 1,
                policy
                    .max_attempts
                    .map(|m| m.to_string())
                    .unwrap_or_else(|| "∞".into())
            );

            tokio::time::sleep(Duration::from_millis(delay)).await;

            // Check again if shutdown was requested during sleep
            if conn.shutdown_requested.load(Ordering::SeqCst) {
                break;
            }

            // Attempt reconnection
            match self.connections.establish_connection(&conn).await {
                Ok(_) => {
                    eprintln!("[McpServerHub] Server '{}' reconnected successfully", name);
                    conn.restart_count.store(0, Ordering::SeqCst);
                }
                Err(e) => {
                    eprintln!(
                        "[McpServerHub] Server '{}' failed to reconnect: {}",
                        name, e
                    );
                }
            }
        }
    }

    /// Trigger an immediate restart for a specific server.
    pub fn trigger_restart(&self, server_name: &str) {
        if let Some(conn) = self.connections.get(server_name) {
            conn.restart_notify.notify_one();
        }
    }

    /// Call a tool by name, routing to the correct server.
    ///
    /// If the server restarts while a request is pending, the request will
    /// immediately fail with a `ServerRestarting` error rather than timing out.
    /// Uses circuit breaker to prevent cascading failures.
    pub async fn call_tool(&self, name: &str, args: Value) -> Result<Value, McpTransportError> {
        self.connections.call_tool(name, args).await
    }

    /// List all tools from all connected servers.
    pub async fn list_tools(&self) -> Result<Vec<(String, McpToolDefinition)>, McpTransportError> {
        Ok(self.connections.list_tools())
    }

    /// List all tool definitions.
    pub async fn list_all_tools(&self) -> Result<Vec<McpToolDefinition>, McpTransportError> {
        Ok(self.connections.list_tool_definitions())
    }

    /// Discover tools from all servers in parallel.
    pub async fn discover_tools_parallel(
        &self,
    ) -> Result<Vec<(String, McpToolDefinition)>, McpTransportError> {
        self.connections.discover_tools_parallel(self.timeout).await
    }

    /// Refresh tool cache by re-querying all servers (parallel).
    pub async fn refresh_tools(&self) -> Result<(), McpTransportError> {
        self.connections.refresh_tools_parallel(self.timeout).await
    }

    /// Get list of connected server names.
    pub fn list_servers(&self) -> Vec<String> {
        self.connections.list_servers()
    }

    /// Check if a server is connected.
    pub fn is_connected(&self, server_name: &str) -> bool {
        self.connections.is_connected(server_name)
    }

    /// Check if a server is connected and alive.
    pub async fn is_alive(&self, server_name: &str) -> bool {
        if let Some(conn) = self.connections.get(server_name) {
            conn.is_alive().await
        } else {
            false
        }
    }

    /// Get health status of all servers.
    pub async fn health_check(&self) -> Vec<(String, bool)> {
        self.connections.health_check().await
    }

    /// Get the server name that provides a specific tool.
    pub fn server_for_tool(&self, tool_name: &str) -> Option<String> {
        self.connections.server_for_tool(tool_name)
    }

    /// Disconnect a specific server (stops restart monitor).
    pub async fn disconnect(&self, server_name: &str) -> Result<(), McpTransportError> {
        let connection = self
            .connections
            .remove(server_name)
            .ok_or_else(|| McpTransportError::ServerNotFound(server_name.to_string()))?;

        // Signal shutdown to restart monitor
        connection.shutdown_requested.store(true, Ordering::SeqCst);
        connection.restart_notify.notify_one();

        // Clear tools for this server
        self.connections.clear_tools_for_server(server_name);

        // Shutdown transport
        if let Some(transport) = connection.get_transport().await {
            transport.shutdown().await?;
        }

        Ok(())
    }

    /// Shutdown all connected servers.
    pub async fn shutdown_all(&self) -> Result<(), McpTransportError> {
        let names: Vec<String> = self.list_servers();
        let mut errors = Vec::new();

        for name in names {
            if let Err(e) = self.disconnect(&name).await {
                errors.push(format!("{}: {}", name, e));
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(McpTransportError::TransportError(errors.join("; ")))
        }
    }

    /// Convert this hub into an McpServerConfig that can be used with
    /// McpStdioServer or McpHttpServer.
    ///
    /// This creates proxy tools that route calls to the connected external servers.
    pub fn into_config(self, version: &str) -> McpServerConfig {
        let hub = Arc::new(self);
        let provider = HubToolProvider {
            hub: Arc::clone(&hub),
        };

        McpServerConfig::builder()
            .name(&hub.name)
            .version(version)
            .with_tools_from(provider)
            .build()
    }

    /// Create an McpServerConfig from this hub (keeps hub accessible).
    ///
    /// Use this when you need to keep a reference to the hub for direct access.
    pub fn to_config(self: &Arc<Self>, version: &str) -> McpServerConfig {
        let provider = HubToolProvider {
            hub: Arc::clone(self),
        };

        McpServerConfig::builder()
            .name(&self.name)
            .version(version)
            .with_tools_from(provider)
            .build()
    }

    /// Get proxy tools for all connected servers.
    ///
    /// Use this when you want to combine hub tools with local tools:
    /// ```rust,ignore
    /// let config = McpServerConfig::builder()
    ///     .name("my-server")
    ///     .version("1.0.0")
    ///     .register_tools_in_group("local")  // Local tools
    ///     .with_tools(hub.proxy_tools())     // Proxied tools
    ///     .build();
    /// ```
    pub fn proxy_tools(self: &Arc<Self>) -> Vec<DynTool> {
        let provider = HubToolProvider {
            hub: Arc::clone(self),
        };
        provider.tools()
    }

    /// Get circuit breaker statistics for a server.
    pub fn circuit_breaker_stats(
        &self,
        server_name: &str,
    ) -> Option<crate::circuit_breaker::CircuitBreakerStats> {
        self.connections.circuit_breaker_stats(server_name)
    }

    /// Reset circuit breaker for a server.
    pub fn reset_circuit_breaker(&self, server_name: &str) {
        self.connections.reset_circuit_breaker(server_name);
    }
}

/// Tool provider that creates proxy tools for all tools in the hub.
struct HubToolProvider {
    hub: Arc<McpServerHub>,
}

impl ToolProvider for HubToolProvider {
    fn tools(&self) -> Vec<DynTool> {
        self.hub
            .connections
            .list_tools()
            .into_iter()
            .map(|(_, def)| {
                let tool: DynTool = Arc::new(ProxyTool {
                    name: def.name.clone(),
                    definition: def,
                    hub: Arc::clone(&self.hub),
                });
                tool
            })
            .collect()
    }
}

/// A proxy tool that forwards calls to an external MCP server via the hub.
struct ProxyTool {
    name: String,
    definition: McpToolDefinition,
    hub: Arc<McpServerHub>,
}

impl McpTool for ProxyTool {
    fn definition(&self) -> McpToolDefinition {
        self.definition.clone()
    }

    fn call<'a>(&'a self, args: Value) -> BoxFuture<'a, ToolCallResult> {
        let name = self.name.clone();
        let hub = Arc::clone(&self.hub);

        Box::pin(async move {
            match hub.call_tool(&name, args).await {
                Ok(value) => {
                    // Convert the Value to ToolContent
                    if let Some(s) = value.as_str() {
                        Ok(vec![crate::protocol::ToolContent::text(s)])
                    } else {
                        Ok(vec![crate::protocol::ToolContent::text(value.to_string())])
                    }
                }
                Err(e) => Err(e.to_string()),
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_hub_creation() {
        let hub = McpServerHub::new("test-hub");
        assert_eq!(hub.name, "test-hub");
        assert!(hub.list_servers().is_empty());
    }

    #[tokio::test]
    async fn test_hub_into_config() {
        let hub = McpServerHub::new("test-hub");
        let config = hub.into_config("1.0.0");
        assert_eq!(config.name(), "test-hub");
        assert_eq!(config.version(), "1.0.0");
    }

    #[tokio::test]
    async fn test_hub_unknown_tool() {
        let hub = McpServerHub::new("test");
        let result = hub.call_tool("nonexistent", serde_json::json!({})).await;
        assert!(matches!(result, Err(McpTransportError::UnknownTool(_))));
    }
}