pub struct SessionManager<T: Transport + 'static> { /* private fields */ }Expand description
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
use turbomcp_client::SessionManager;
use turbomcp_transport::stdio::StdioTransport;
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());Implementations§
Source§impl<T: Transport + Send + 'static> SessionManager<T>
impl<T: Transport + Send + 'static> SessionManager<T>
Sourcepub fn new(config: ManagerConfig) -> Self
pub fn new(config: ManagerConfig) -> Self
Create a new connection manager with the specified configuration
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a new connection manager with default configuration
Sourcepub async fn add_server(
&mut self,
id: impl Into<String>,
transport: T,
) -> Result<()>
pub async fn add_server( &mut self, id: impl Into<String>, transport: T, ) -> Result<()>
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
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?;Sourcepub async fn add_server_with_reconnect<F>(
&mut self,
id: impl Into<String>,
transport_factory: F,
) -> Result<()>
pub async fn add_server_with_reconnect<F>( &mut self, id: impl Into<String>, transport_factory: F, ) -> Result<()>
Add a server session with automatic reconnection support
This method accepts a factory function that creates new transport instances, enabling automatic reconnection if the session fails.
§Arguments
id- Unique identifier for this servertransport_factory- Function that creates new transport instances
§Examples
let mut manager = SessionManager::with_defaults();
// Transport with reconnection factory
manager.add_server_with_reconnect("api", || {
StdioTransport::new()
}).await?;Sourcepub async fn remove_server(&mut self, id: &str) -> bool
pub async fn remove_server(&mut self, id: &str) -> bool
Sourcepub async fn get_session_info(&self, id: &str) -> Option<ConnectionInfo>
pub async fn get_session_info(&self, id: &str) -> Option<ConnectionInfo>
Get information about a specific connection
Sourcepub async fn list_sessions(&self) -> Vec<ConnectionInfo>
pub async fn list_sessions(&self) -> Vec<ConnectionInfo>
List all managed connections
Sourcepub async fn get_healthy_connection(&self) -> Option<String>
pub async fn get_healthy_connection(&self) -> Option<String>
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
Sourcepub async fn session_stats(&self) -> HashMap<ConnectionState, usize>
pub async fn session_stats(&self) -> HashMap<ConnectionState, usize>
Get count of connections by state
Sourcepub async fn start_health_monitoring(&mut self)
pub async fn start_health_monitoring(&mut self)
Start automatic health monitoring
Spawns a background task that periodically checks the health of all connections
Sourcepub fn stop_health_monitoring(&mut self)
pub fn stop_health_monitoring(&mut self)
Stop automatic health monitoring
Sourcepub async fn session_count(&self) -> usize
pub async fn session_count(&self) -> usize
Get total number of managed connections
Source§impl SessionManager<TurboTransport>
impl SessionManager<TurboTransport>
Sourcepub async fn add_resilient_server<BaseT>(
&mut self,
id: impl Into<String>,
transport: BaseT,
retry_config: RetryConfig,
circuit_config: CircuitBreakerConfig,
health_config: HealthCheckConfig,
) -> Result<()>where
BaseT: Transport + 'static,
pub async fn add_resilient_server<BaseT>(
&mut self,
id: impl Into<String>,
transport: BaseT,
retry_config: RetryConfig,
circuit_config: CircuitBreakerConfig,
health_config: HealthCheckConfig,
) -> Result<()>where
BaseT: Transport + 'static,
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
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?;