mcp-protocol-sdk 0.5.1

Production-ready Rust SDK for the Model Context Protocol (MCP) with multiple transport support
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
//! Client session management
//!
//! This module provides session management for MCP clients, including connection
//! state tracking, notification handling, and automatic reconnection capabilities.

use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{Mutex, RwLock, broadcast, mpsc, watch};
use tokio::time::{sleep, timeout};

use crate::client::mcp_client::McpClient;
use crate::core::error::{McpError, McpResult};
use crate::protocol::{messages::*, methods, types::*};
use crate::transport::traits::Transport;

/// Session state
#[derive(Debug, Clone, PartialEq)]
pub enum SessionState {
    /// Session is idle (ready but not actively processing)
    Idle,
    /// Session is disconnected
    Disconnected,
    /// Session is connecting
    Connecting,
    /// Session is connected and active
    Connected,
    /// Session is reconnecting after a failure
    Reconnecting,
    /// Session has failed and cannot reconnect
    Failed(String),
}

/// Notification handler trait
pub trait NotificationHandler: Send + Sync {
    /// Handle a notification from the server
    fn handle_notification(&self, notification: JsonRpcNotification);
}

/// Session configuration
#[derive(Debug, Clone)]
pub struct SessionConfig {
    /// Whether to enable automatic reconnection
    pub auto_reconnect: bool,
    /// Maximum number of reconnection attempts
    pub max_reconnect_attempts: u32,
    /// Initial reconnection delay in milliseconds
    pub reconnect_delay_ms: u64,
    /// Maximum reconnection delay in milliseconds
    pub max_reconnect_delay_ms: u64,
    /// Reconnection backoff multiplier
    pub reconnect_backoff: f64,
    /// Connection timeout in milliseconds
    pub connection_timeout_ms: u64,
    /// Heartbeat interval in milliseconds (0 to disable)
    pub heartbeat_interval_ms: u64,
    /// Heartbeat timeout in milliseconds
    pub heartbeat_timeout_ms: u64,

    // Additional fields for test compatibility
    /// Session timeout duration
    pub session_timeout: Duration,
    /// Request timeout duration
    pub request_timeout: Duration,
    /// Maximum concurrent requests
    pub max_concurrent_requests: u32,
    /// Enable compression
    pub enable_compression: bool,
    /// Buffer size for operations
    pub buffer_size: usize,
}

impl Default for SessionConfig {
    fn default() -> Self {
        Self {
            auto_reconnect: true,
            max_reconnect_attempts: 5,
            reconnect_delay_ms: 1000,
            max_reconnect_delay_ms: 30000,
            reconnect_backoff: 2.0,
            connection_timeout_ms: 10000,
            heartbeat_interval_ms: 30000,
            heartbeat_timeout_ms: 5000,
            session_timeout: Duration::from_secs(300),
            request_timeout: Duration::from_secs(30),
            max_concurrent_requests: 10,
            enable_compression: false,
            buffer_size: 8192,
        }
    }
}

/// Client session that manages connection lifecycle and notifications
pub struct ClientSession {
    /// The underlying MCP client
    client: Arc<Mutex<McpClient>>,
    /// Session configuration
    config: SessionConfig,
    /// Current session state
    state: Arc<RwLock<SessionState>>,
    /// State change broadcaster
    state_tx: watch::Sender<SessionState>,
    /// State change receiver
    state_rx: watch::Receiver<SessionState>,
    /// Notification handlers
    notification_handlers: Arc<RwLock<Vec<Box<dyn NotificationHandler>>>>,
    /// Connection timestamp
    connected_at: Arc<RwLock<Option<Instant>>>,
    /// Reconnection attempts counter
    reconnect_attempts: Arc<Mutex<u32>>,
    /// Shutdown signal
    shutdown_tx: Arc<Mutex<Option<mpsc::Sender<()>>>>,
}

impl ClientSession {
    /// Create a new client session
    pub fn new(client: McpClient) -> Self {
        let (state_tx, state_rx) = watch::channel(SessionState::Disconnected);

        Self {
            client: Arc::new(Mutex::new(client)),
            config: SessionConfig::default(),
            state: Arc::new(RwLock::new(SessionState::Disconnected)),
            state_tx,
            state_rx,
            notification_handlers: Arc::new(RwLock::new(Vec::new())),
            connected_at: Arc::new(RwLock::new(None)),
            reconnect_attempts: Arc::new(Mutex::new(0)),
            shutdown_tx: Arc::new(Mutex::new(None)),
        }
    }

    /// Create a new client session with custom configuration
    pub fn with_config(client: McpClient, config: SessionConfig) -> Self {
        let mut session = Self::new(client);
        session.config = config;
        session
    }

    /// Get the current session state
    pub async fn state(&self) -> SessionState {
        let state = self.state.read().await;
        state.clone()
    }

    /// Subscribe to state changes
    pub fn subscribe_state_changes(&self) -> watch::Receiver<SessionState> {
        self.state_rx.clone()
    }

    /// Check if the session is connected
    pub async fn is_connected(&self) -> bool {
        let state = self.state.read().await;
        matches!(*state, SessionState::Connected)
    }

    /// Get connection uptime
    pub async fn uptime(&self) -> Option<Duration> {
        let connected_at = self.connected_at.read().await;
        connected_at.map(|time| time.elapsed())
    }

    /// Add a notification handler
    pub async fn add_notification_handler<H>(&self, handler: H)
    where
        H: NotificationHandler + 'static,
    {
        let mut handlers = self.notification_handlers.write().await;
        handlers.push(Box::new(handler));
    }

    /// Connect to the server with the provided transport
    pub async fn connect<T>(&self, transport: T) -> McpResult<InitializeResult>
    where
        T: Transport + 'static,
    {
        self.transition_state(SessionState::Connecting).await?;

        let connect_future = async {
            let mut client = self.client.lock().await;
            client.connect(transport).await
        };

        let result = timeout(
            Duration::from_millis(self.config.connection_timeout_ms),
            connect_future,
        )
        .await;

        match result {
            Ok(Ok(init_result)) => {
                self.transition_state(SessionState::Connected).await?;

                // Record connection time
                {
                    let mut connected_at = self.connected_at.write().await;
                    *connected_at = Some(Instant::now());
                }

                // Reset reconnection attempts
                {
                    let mut attempts = self.reconnect_attempts.lock().await;
                    *attempts = 0;
                }

                // Start background tasks
                self.start_background_tasks().await?;

                Ok(init_result)
            }
            Ok(Err(error)) => {
                self.transition_state(SessionState::Failed(error.to_string()))
                    .await?;
                Err(error)
            }
            Err(_) => {
                let error = McpError::Connection("Connection timeout".to_string());
                self.transition_state(SessionState::Failed(error.to_string()))
                    .await?;
                Err(error)
            }
        }
    }

    /// Disconnect from the server
    pub async fn disconnect(&self) -> McpResult<()> {
        // Stop background tasks
        self.stop_background_tasks().await;

        // Disconnect the client
        {
            let client = self.client.lock().await;
            client.disconnect().await?;
        }

        // Update state
        self.transition_state(SessionState::Disconnected).await?;

        // Clear connection time
        {
            let mut connected_at = self.connected_at.write().await;
            *connected_at = None;
        }

        Ok(())
    }

    /// Reconnect to the server
    pub async fn reconnect<T>(
        &self,
        transport_factory: impl Fn() -> T,
    ) -> McpResult<InitializeResult>
    where
        T: Transport + 'static,
    {
        if !self.config.auto_reconnect {
            return Err(McpError::Connection(
                "Auto-reconnect is disabled".to_string(),
            ));
        }

        let mut attempts = self.reconnect_attempts.lock().await;
        if *attempts >= self.config.max_reconnect_attempts {
            let error = McpError::Connection("Max reconnection attempts exceeded".to_string());
            self.transition_state(SessionState::Failed(error.to_string()))
                .await?;
            return Err(error);
        }

        *attempts += 1;
        let current_attempts = *attempts;
        drop(attempts);

        self.transition_state(SessionState::Reconnecting).await?;

        // Calculate reconnection delay with exponential backoff
        let delay = std::cmp::min(
            (self.config.reconnect_delay_ms as f64
                * self
                    .config
                    .reconnect_backoff
                    .powi(current_attempts as i32 - 1)) as u64,
            self.config.max_reconnect_delay_ms,
        );

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

        // Attempt to reconnect
        self.connect(transport_factory()).await
    }

    /// Get the underlying client (for direct operations)
    pub fn client(&self) -> Arc<Mutex<McpClient>> {
        self.client.clone()
    }

    /// Get session configuration
    pub fn config(&self) -> &SessionConfig {
        &self.config
    }

    // ========================================================================
    // Background Tasks
    // ========================================================================

    /// Start background tasks (notification handling, heartbeat)
    async fn start_background_tasks(&self) -> McpResult<()> {
        let (_shutdown_tx, shutdown_rx): (broadcast::Sender<()>, broadcast::Receiver<()>) =
            broadcast::channel(16);
        {
            let mut shutdown_guard = self.shutdown_tx.lock().await;
            *shutdown_guard = Some(mpsc::channel(1).0); // Store a dummy for interface compatibility
        }

        // Start notification handler task
        {
            let client = self.client.clone();
            let handlers = self.notification_handlers.clone();
            let mut shutdown_rx_clone = shutdown_rx.resubscribe();

            tokio::spawn(async move {
                loop {
                    tokio::select! {
                        _ = shutdown_rx_clone.recv() => break,
                        notification_result = async {
                            let client_guard = client.lock().await;
                            client_guard.receive_notification().await
                        } => {
                            match notification_result {
                                Ok(Some(notification)) => {
                                    let handlers_guard = handlers.read().await;
                                    for handler in handlers_guard.iter() {
                                        handler.handle_notification(notification.clone());
                                    }
                                }
                                Ok(None) => {
                                    // No notification available, continue
                                }
                                Err(_) => {
                                    // Error receiving notification, might be disconnected
                                    break;
                                }
                            }
                        }
                    }
                }
            });
        }

        // Start heartbeat task if enabled
        if self.config.heartbeat_interval_ms > 0 {
            let client = self.client.clone();
            let heartbeat_interval = Duration::from_millis(self.config.heartbeat_interval_ms);
            let heartbeat_timeout = Duration::from_millis(self.config.heartbeat_timeout_ms);
            let state = self.state.clone();
            let state_tx = self.state_tx.clone();
            let mut shutdown_rx_clone = shutdown_rx.resubscribe();

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

                loop {
                    tokio::select! {
                        _ = shutdown_rx_clone.recv() => break,
                        _ = interval.tick() => {
                            // Check if we're still connected
                            {
                                let current_state = state.read().await;
                                if !matches!(*current_state, SessionState::Connected) {
                                    break;
                                }
                            }

                            // Send ping
                            let ping_result = timeout(heartbeat_timeout, async {
                                let client_guard = client.lock().await;
                                client_guard.ping().await
                            }).await;

                            if ping_result.is_err() {
                                // Heartbeat failed, mark as disconnected
                                let _ = state_tx.send(SessionState::Disconnected);
                                break;
                            }
                        }
                    }
                }
            });
        }

        Ok(())
    }

    /// Stop background tasks
    async fn stop_background_tasks(&self) {
        let shutdown_tx = {
            let mut shutdown_guard = self.shutdown_tx.lock().await;
            shutdown_guard.take()
        };

        if let Some(tx) = shutdown_tx {
            let _ = tx.send(()).await; // Ignore error if receiver is dropped
        }
    }

    /// Transition to a new state
    async fn transition_state(&self, new_state: SessionState) -> McpResult<()> {
        {
            let mut state = self.state.write().await;
            *state = new_state.clone();
        }

        // Broadcast the state change
        if self.state_tx.send(new_state).is_err() {
            // Receiver may have been dropped, which is okay
        }

        Ok(())
    }
}

/// Default notification handler that logs notifications
pub struct LoggingNotificationHandler;

impl NotificationHandler for LoggingNotificationHandler {
    fn handle_notification(&self, notification: JsonRpcNotification) {
        tracing::info!(
            "Received notification: {} {:?}",
            notification.method,
            notification.params
        );
    }
}

/// Resource update notification handler
pub struct ResourceUpdateHandler {
    callback: Box<dyn Fn(String) + Send + Sync>,
}

impl ResourceUpdateHandler {
    /// Create a new resource update handler
    pub fn new<F>(callback: F) -> Self
    where
        F: Fn(String) + Send + Sync + 'static,
    {
        Self {
            callback: Box::new(callback),
        }
    }
}

impl NotificationHandler for ResourceUpdateHandler {
    fn handle_notification(&self, notification: JsonRpcNotification) {
        if notification.method == methods::RESOURCES_UPDATED {
            if let Some(params) = notification.params {
                if let Ok(update_params) = serde_json::from_value::<ResourceUpdatedParams>(params) {
                    (self.callback)(update_params.uri);
                }
            }
        }
    }
}

/// Tool list changed notification handler
pub struct ToolListChangedHandler {
    callback: Box<dyn Fn() + Send + Sync>,
}

impl ToolListChangedHandler {
    /// Create a new tool list changed handler
    pub fn new<F>(callback: F) -> Self
    where
        F: Fn() + Send + Sync + 'static,
    {
        Self {
            callback: Box::new(callback),
        }
    }
}

impl NotificationHandler for ToolListChangedHandler {
    fn handle_notification(&self, notification: JsonRpcNotification) {
        if notification.method == methods::TOOLS_LIST_CHANGED {
            (self.callback)();
        }
    }
}

/// Progress notification handler
pub struct ProgressHandler {
    callback: Box<dyn Fn(String, f32, Option<u32>) + Send + Sync>,
}

impl ProgressHandler {
    /// Create a new progress handler
    pub fn new<F>(callback: F) -> Self
    where
        F: Fn(String, f32, Option<u32>) + Send + Sync + 'static,
    {
        Self {
            callback: Box::new(callback),
        }
    }
}

impl NotificationHandler for ProgressHandler {
    fn handle_notification(&self, notification: JsonRpcNotification) {
        if notification.method == methods::PROGRESS {
            if let Some(params) = notification.params {
                if let Ok(progress_params) = serde_json::from_value::<ProgressParams>(params) {
                    (self.callback)(
                        progress_params.progress_token.to_string(),
                        progress_params.progress,
                        progress_params.total.map(|t| t as u32),
                    );
                }
            }
        }
    }
}

/// Session statistics
#[derive(Debug, Clone)]
pub struct SessionStats {
    /// Current session state
    pub state: SessionState,
    /// Connection uptime
    pub uptime: Option<Duration>,
    /// Number of reconnection attempts
    pub reconnect_attempts: u32,
    /// Connection timestamp
    pub connected_at: Option<Instant>,
}

impl ClientSession {
    /// Get session statistics
    pub async fn stats(&self) -> SessionStats {
        let state = self.state().await;
        let uptime = self.uptime().await;
        let reconnect_attempts = {
            let attempts = self.reconnect_attempts.lock().await;
            *attempts
        };
        let connected_at = {
            let connected_at = self.connected_at.read().await;
            *connected_at
        };

        SessionStats {
            state,
            uptime,
            reconnect_attempts,
            connected_at,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::mcp_client::McpClient;
    use async_trait::async_trait;

    // Mock transport for testing
    struct MockTransport;

    #[async_trait]
    impl Transport for MockTransport {
        async fn send_request(&mut self, _request: JsonRpcRequest) -> McpResult<JsonRpcResponse> {
            // Return a successful initialize response
            let init_result = InitializeResult::new(
                crate::protocol::LATEST_PROTOCOL_VERSION.to_string(),
                ServerCapabilities::default(),
                ServerInfo {
                    name: "test-server".to_string(),
                    version: "1.0.0".to_string(),
                    title: Some("Test Server".to_string()),
                },
            );
            JsonRpcResponse::success(serde_json::Value::from(1), init_result)
                .map_err(|e| McpError::Serialization(e.to_string()))
        }

        async fn send_notification(&mut self, _notification: JsonRpcNotification) -> McpResult<()> {
            Ok(())
        }

        async fn receive_notification(&mut self) -> McpResult<Option<JsonRpcNotification>> {
            Ok(None)
        }

        async fn close(&mut self) -> McpResult<()> {
            Ok(())
        }
    }

    #[tokio::test]
    async fn test_session_creation() {
        let client = McpClient::new("test-client".to_string(), "1.0.0".to_string());
        let session = ClientSession::new(client);

        assert_eq!(session.state().await, SessionState::Disconnected);
        assert!(!session.is_connected().await);
        assert!(session.uptime().await.is_none());
    }

    #[tokio::test]
    async fn test_session_connection() {
        let client = McpClient::new("test-client".to_string(), "1.0.0".to_string());
        let session = ClientSession::new(client);

        let transport = MockTransport;
        let result = session.connect(transport).await;

        assert!(result.is_ok());
        assert_eq!(session.state().await, SessionState::Connected);
        assert!(session.is_connected().await);
        assert!(session.uptime().await.is_some());
    }

    #[tokio::test]
    async fn test_session_disconnect() {
        let client = McpClient::new("test-client".to_string(), "1.0.0".to_string());
        let session = ClientSession::new(client);

        // Connect first
        let transport = MockTransport;
        session.connect(transport).await.unwrap();
        assert!(session.is_connected().await);

        // Then disconnect
        session.disconnect().await.unwrap();
        assert_eq!(session.state().await, SessionState::Disconnected);
        assert!(!session.is_connected().await);
        assert!(session.uptime().await.is_none());
    }

    #[tokio::test]
    async fn test_notification_handlers() {
        let client = McpClient::new("test-client".to_string(), "1.0.0".to_string());
        let session = ClientSession::new(client);

        // Add a logging notification handler
        session
            .add_notification_handler(LoggingNotificationHandler)
            .await;

        // Add a resource update handler
        session
            .add_notification_handler(ResourceUpdateHandler::new(|uri| {
                println!("Resource updated: {uri}");
            }))
            .await;

        // Add a tool list changed handler
        session
            .add_notification_handler(ToolListChangedHandler::new(|| {
                println!("Tool list changed");
            }))
            .await;

        // Add a progress handler
        session
            .add_notification_handler(ProgressHandler::new(|token, progress, total| {
                println!("Progress {token}: {progress} / {total:?}");
            }))
            .await;

        let handlers = session.notification_handlers.read().await;
        assert_eq!(handlers.len(), 4);
    }

    #[tokio::test]
    async fn test_session_stats() {
        let client = McpClient::new("test-client".to_string(), "1.0.0".to_string());
        let session = ClientSession::new(client);

        let stats = session.stats().await;
        assert_eq!(stats.state, SessionState::Disconnected);
        assert!(stats.uptime.is_none());
        assert_eq!(stats.reconnect_attempts, 0);
        assert!(stats.connected_at.is_none());
    }

    #[tokio::test]
    async fn test_session_config() {
        let client = McpClient::new("test-client".to_string(), "1.0.0".to_string());
        let config = SessionConfig {
            auto_reconnect: false,
            max_reconnect_attempts: 10,
            reconnect_delay_ms: 2000,
            ..Default::default()
        };
        let session = ClientSession::with_config(client, config.clone());

        assert!(!session.config().auto_reconnect);
        assert_eq!(session.config().max_reconnect_attempts, 10);
        assert_eq!(session.config().reconnect_delay_ms, 2000);
    }

    #[tokio::test]
    async fn test_state_subscription() {
        let client = McpClient::new("test-client".to_string(), "1.0.0".to_string());
        let session = ClientSession::new(client);

        let mut state_rx = session.subscribe_state_changes();

        // Initial state
        assert_eq!(*state_rx.borrow(), SessionState::Disconnected);

        // Change state
        session
            .transition_state(SessionState::Connecting)
            .await
            .unwrap();

        // Wait for change
        state_rx.changed().await.unwrap();
        assert_eq!(*state_rx.borrow(), SessionState::Connecting);
    }
}