turbomcp-client 3.0.8

MCP client with full protocol support, bidirectional communication, and plugin middleware
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Multi-Server Session Manager for MCP Clients
//!
//! This module provides a `SessionManager` for coordinating multiple MCP server sessions.
//! Unlike traditional HTTP connection pooling, MCP uses long-lived, stateful sessions
//! where each connection maintains negotiated capabilities and subscription state.
//!
//! # Key Concepts
//!
//! - **Session**: A long-lived, initialized MCP connection to a server
//! - **Multi-Server**: Manage connections to different MCP servers (GitHub, filesystem, etc.)
//! - **Health Monitoring**: Automatic ping-based health checks per session
//! - **Lifecycle Management**: Proper initialize → operate → shutdown for each session
//!
//! # Features
//!
//! - Multiple server sessions with independent state
//! - Automatic health checking with configurable intervals
//! - Per-session state tracking (healthy, degraded, unhealthy)
//! - Session lifecycle management
//! - Metrics and monitoring per session
//!
//! # When to Use
//!
//! Use `SessionManager` when your application needs to coordinate **multiple different
//! MCP servers** (e.g., IDE with GitHub server + filesystem server + database server).
//!
//! For **single server** scenarios:
//! - `Client<T>` is cheaply cloneable via Arc - share one session across multiple async tasks
//! - `TurboTransport` - Add retry/circuit breaker to one session

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tokio::time::Instant;
use turbomcp_protocol::{Error, Result};
use turbomcp_transport::Transport;

use super::core::Client;

/// Connection state for a managed client
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConnectionState {
    /// Connection is healthy and ready
    Healthy,
    /// Connection is degraded but functional
    Degraded,
    /// Connection is unhealthy and should be avoided
    Unhealthy,
    /// Connection is being established
    Connecting,
    /// Connection is disconnected
    Disconnected,
}

/// Information about a managed connection
#[derive(Debug, Clone)]
pub struct ConnectionInfo {
    /// Unique identifier for this connection
    pub id: String,
    /// Current state of the connection
    pub state: ConnectionState,
    /// When the connection was established
    pub established_at: Instant,
    /// Last successful health check
    pub last_health_check: Option<Instant>,
    /// Number of failed health checks
    pub failed_health_checks: usize,
    /// Number of successful requests
    pub successful_requests: usize,
    /// Number of failed requests
    pub failed_requests: usize,
}

/// Configuration for the connection manager
#[derive(Debug, Clone)]
pub struct ManagerConfig {
    /// Maximum number of concurrent connections
    pub max_connections: usize,
    /// Health check interval
    pub health_check_interval: Duration,
    /// Number of consecutive failures before marking unhealthy
    pub health_check_threshold: usize,
    /// Timeout for health checks
    pub health_check_timeout: Duration,
    /// Enable automatic reconnection
    pub auto_reconnect: bool,
    /// Initial reconnection delay
    pub reconnect_delay: Duration,
    /// Maximum reconnection delay (for exponential backoff)
    pub max_reconnect_delay: Duration,
    /// Reconnection backoff multiplier
    pub reconnect_backoff_multiplier: f64,
}

impl Default for ManagerConfig {
    fn default() -> Self {
        Self {
            max_connections: 10,
            health_check_interval: Duration::from_secs(30),
            health_check_threshold: 3,
            health_check_timeout: Duration::from_secs(5),
            auto_reconnect: true,
            reconnect_delay: Duration::from_secs(1),
            max_reconnect_delay: Duration::from_secs(60),
            reconnect_backoff_multiplier: 2.0,
        }
    }
}

impl ManagerConfig {
    /// Create a new manager configuration with default values
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }
}

/// Managed connection wrapper
struct ManagedConnection<T: Transport + 'static> {
    client: Client<T>,
    info: ConnectionInfo,
}

/// Server group configuration for failover support
#[derive(Debug, Clone)]
pub struct ServerGroup {
    /// Primary server ID
    pub primary: String,
    /// Backup server IDs in priority order
    pub backups: Vec<String>,
    /// Minimum health check failures before failover
    pub failover_threshold: usize,
}

impl ServerGroup {
    /// Create a new server group with primary and backups
    pub fn new(primary: impl Into<String>, backups: Vec<String>) -> Self {
        Self {
            primary: primary.into(),
            backups,
            failover_threshold: 3,
        }
    }

    /// Set the failover threshold
    #[must_use]
    pub fn with_failover_threshold(mut self, threshold: usize) -> Self {
        self.failover_threshold = threshold;
        self
    }

    /// Get all server IDs in priority order (primary first, then backups)
    #[must_use]
    pub fn all_servers(&self) -> Vec<&str> {
        std::iter::once(self.primary.as_str())
            .chain(self.backups.iter().map(|s| s.as_str()))
            .collect()
    }

    /// Get the next available server after the current one
    #[must_use]
    pub fn next_server(&self, current: &str) -> Option<&str> {
        let servers = self.all_servers();
        let current_idx = servers.iter().position(|&s| s == current)?;
        servers.get(current_idx + 1).copied()
    }
}

/// Multi-Server Session Manager for MCP Clients
///
/// The `SessionManager` coordinates multiple MCP server sessions with automatic
/// health monitoring and lifecycle management. Each session represents a long-lived,
/// initialized connection to a different MCP server.
///
/// # Use Cases
///
/// - **Multi-Server Applications**: IDE with multiple tool servers
/// - **Service Coordination**: Orchestrate operations across multiple MCP servers
/// - **Health Monitoring**: Track health of all connected servers
/// - **Failover**: Switch between primary/backup servers
///
/// # Examples
///
/// ```rust,no_run
/// use turbomcp_client::SessionManager;
/// use turbomcp_transport::stdio::StdioTransport;
///
/// # async fn example() -> turbomcp_protocol::Result<()> {
/// let mut manager = SessionManager::with_defaults();
///
/// // Add sessions to different servers
/// let github_transport = StdioTransport::new();
/// let fs_transport = StdioTransport::new();
/// manager.add_server("github", github_transport).await?;
/// manager.add_server("filesystem", fs_transport).await?;
///
/// // Start health monitoring
/// manager.start_health_monitoring().await;
///
/// // Get stats
/// let stats = manager.session_stats().await;
/// println!("Managing {} sessions", stats.len());
/// # Ok(())
/// # }
/// ```
pub struct SessionManager<T: Transport + 'static> {
    config: ManagerConfig,
    connections: Arc<RwLock<HashMap<String, ManagedConnection<T>>>>,
    health_check_task: Option<tokio::task::JoinHandle<()>>,
}

impl<T: Transport + Send + 'static> SessionManager<T> {
    /// Create a new connection manager with the specified configuration
    #[must_use]
    pub fn new(config: ManagerConfig) -> Self {
        Self {
            config,
            connections: Arc::new(RwLock::new(HashMap::new())),
            health_check_task: None,
        }
    }

    /// Create a new connection manager with default configuration
    #[must_use]
    pub fn with_defaults() -> Self {
        Self::new(ManagerConfig::default())
    }

    /// Add a new server session
    ///
    /// Creates and initializes a session to the specified MCP server.
    ///
    /// # Arguments
    ///
    /// * `id` - Unique identifier for this server (e.g., "github", "filesystem")
    /// * `transport` - Transport implementation for connecting to the server
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Maximum sessions limit is reached
    /// - Server ID already exists
    /// - Client initialization fails
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use turbomcp_client::SessionManager;
    /// # use turbomcp_transport::stdio::StdioTransport;
    /// # async fn example() -> turbomcp_protocol::Result<()> {
    /// let mut manager = SessionManager::with_defaults();
    /// let github_transport = StdioTransport::new();
    /// let fs_transport = StdioTransport::new();
    /// manager.add_server("github", github_transport).await?;
    /// manager.add_server("filesystem", fs_transport).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn add_server(&mut self, id: impl Into<String>, transport: T) -> Result<()> {
        let id = id.into();
        let mut connections = self.connections.write().await;

        // Check connection limit
        if connections.len() >= self.config.max_connections {
            return Err(Error::invalid_request(format!(
                "Maximum connections limit ({}) reached",
                self.config.max_connections
            )));
        }

        // Check for duplicate ID
        if connections.contains_key(&id) {
            return Err(Error::invalid_request(format!(
                "Connection with ID '{}' already exists",
                id
            )));
        }

        // Create client and initialize
        let client = Client::new(transport);
        client.initialize().await?;

        let info = ConnectionInfo {
            id: id.clone(),
            state: ConnectionState::Healthy,
            established_at: Instant::now(),
            last_health_check: Some(Instant::now()),
            failed_health_checks: 0,
            successful_requests: 0,
            failed_requests: 0,
        };

        connections.insert(id, ManagedConnection { client, info });

        Ok(())
    }

    /// Remove a managed connection
    ///
    /// # Arguments
    ///
    /// * `id` - ID of the connection to remove
    ///
    /// # Returns
    ///
    /// Returns `true` if the connection was removed, `false` if not found
    pub async fn remove_server(&mut self, id: &str) -> bool {
        let mut connections = self.connections.write().await;
        connections.remove(id).is_some()
    }

    /// Get information about a specific connection
    pub async fn get_session_info(&self, id: &str) -> Option<ConnectionInfo> {
        let connections = self.connections.read().await;
        connections.get(id).map(|conn| conn.info.clone())
    }

    /// List all managed connections
    pub async fn list_sessions(&self) -> Vec<ConnectionInfo> {
        let connections = self.connections.read().await;
        connections.values().map(|conn| conn.info.clone()).collect()
    }

    /// Get a healthy connection, preferring the one with the fewest active requests
    ///
    /// # Returns
    ///
    /// Returns the ID of a healthy connection, or None if no healthy connections exist
    pub async fn get_healthy_connection(&self) -> Option<String> {
        let connections = self.connections.read().await;
        connections
            .iter()
            .filter(|(_, conn)| conn.info.state == ConnectionState::Healthy)
            .min_by_key(|(_, conn)| conn.info.successful_requests + conn.info.failed_requests)
            .map(|(id, _)| id.clone())
    }

    /// Get count of connections by state
    pub async fn session_stats(&self) -> HashMap<ConnectionState, usize> {
        let connections = self.connections.read().await;
        let mut stats = HashMap::new();

        for conn in connections.values() {
            *stats.entry(conn.info.state.clone()).or_insert(0) += 1;
        }

        stats
    }

    /// Start automatic health monitoring
    ///
    /// Spawns a background task that periodically checks the health of all connections
    pub async fn start_health_monitoring(&mut self) {
        if self.health_check_task.is_some() {
            return; // Already running
        }

        let connections = Arc::clone(&self.connections);
        let interval = self.config.health_check_interval;
        let threshold = self.config.health_check_threshold;
        let timeout = self.config.health_check_timeout;

        let task = tokio::spawn(async move {
            let mut interval_timer = tokio::time::interval(interval);

            loop {
                interval_timer.tick().await;

                let mut connections = connections.write().await;

                for (id, managed) in connections.iter_mut() {
                    // Perform health check (ping)
                    let health_result = tokio::time::timeout(timeout, managed.client.ping()).await;

                    match health_result {
                        Ok(Ok(_)) => {
                            // Health check successful
                            managed.info.last_health_check = Some(Instant::now());
                            managed.info.failed_health_checks = 0;

                            if managed.info.state != ConnectionState::Healthy {
                                tracing::info!(
                                    connection_id = %id,
                                    "Connection recovered and is now healthy"
                                );
                                managed.info.state = ConnectionState::Healthy;
                            }
                        }
                        Ok(Err(_)) | Err(_) => {
                            // Health check failed
                            managed.info.failed_health_checks += 1;

                            if managed.info.failed_health_checks >= threshold {
                                if managed.info.state != ConnectionState::Unhealthy {
                                    tracing::warn!(
                                        connection_id = %id,
                                        failed_checks = managed.info.failed_health_checks,
                                        "Connection marked as unhealthy"
                                    );
                                    managed.info.state = ConnectionState::Unhealthy;
                                }
                            } else if managed.info.state == ConnectionState::Healthy {
                                tracing::debug!(
                                    connection_id = %id,
                                    failed_checks = managed.info.failed_health_checks,
                                    "Connection degraded"
                                );
                                managed.info.state = ConnectionState::Degraded;
                            }
                        }
                    }
                }
            }
        });

        self.health_check_task = Some(task);
    }

    /// Stop automatic health monitoring
    pub fn stop_health_monitoring(&mut self) {
        if let Some(task) = self.health_check_task.take() {
            task.abort();
        }
    }

    /// Get total number of managed connections
    pub async fn session_count(&self) -> usize {
        let connections = self.connections.read().await;
        connections.len()
    }
}

impl<T: Transport + 'static> Drop for SessionManager<T> {
    fn drop(&mut self) {
        self.stop_health_monitoring();
    }
}

// ============================================================================
// Specialized Implementation for TurboTransport
// ============================================================================

impl SessionManager<turbomcp_transport::resilience::TurboTransport> {
    /// Add a server with automatic robustness (specialized for TurboTransport)
    ///
    /// This convenience method is only available when using `SessionManager<TurboTransport>`.
    /// It wraps any transport in TurboTransport with the specified configurations.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use turbomcp_client::SessionManager;
    /// # use turbomcp_transport::stdio::StdioTransport;
    /// # use turbomcp_transport::resilience::*;
    /// # async fn example() -> turbomcp_protocol::Result<()> {
    /// let mut manager: SessionManager<TurboTransport> = SessionManager::with_defaults();
    ///
    /// // Use explicit configuration for clarity
    /// use std::time::Duration;
    /// manager.add_resilient_server(
    ///     "github",
    ///     StdioTransport::new(),
    ///     RetryConfig {
    ///         max_attempts: 5,
    ///         base_delay: Duration::from_millis(200),
    ///         ..Default::default()
    ///     },
    ///     CircuitBreakerConfig {
    ///         failure_threshold: 3,
    ///         timeout: Duration::from_secs(30),
    ///         ..Default::default()
    ///     },
    ///     HealthCheckConfig {
    ///         interval: Duration::from_secs(15),
    ///         timeout: Duration::from_secs(5),
    ///         ..Default::default()
    ///     },
    /// ).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn add_resilient_server<BaseT>(
        &mut self,
        id: impl Into<String>,
        transport: BaseT,
        retry_config: turbomcp_transport::resilience::RetryConfig,
        circuit_config: turbomcp_transport::resilience::CircuitBreakerConfig,
        health_config: turbomcp_transport::resilience::HealthCheckConfig,
    ) -> Result<()>
    where
        BaseT: Transport + 'static,
    {
        use turbomcp_transport::resilience::TurboTransport;

        let robust = TurboTransport::new(
            Box::new(transport),
            retry_config,
            circuit_config,
            health_config,
        );

        self.add_server(id, robust).await
    }
}

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

    #[test]
    fn test_manager_config_defaults() {
        let config = ManagerConfig::default();
        assert_eq!(config.max_connections, 10);
        assert!(config.auto_reconnect);
    }

    #[test]
    fn test_connection_state_equality() {
        assert_eq!(ConnectionState::Healthy, ConnectionState::Healthy);
        assert_ne!(ConnectionState::Healthy, ConnectionState::Unhealthy);
    }
}