mcpkit-core 0.6.0

Core types and traits for the Model Context Protocol (MCP)
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
//! Typestate pattern for connection lifecycle management.
//!
//! This module implements the typestate pattern to enforce correct
//! connection state transitions at compile time. This prevents runtime
//! errors from calling methods on connections in invalid states.
//!
//! # Connection States
//!
//! ```text
//! Disconnected -> Connected -> Initializing -> Ready -> Closing -> Disconnected
//! ```
//!
//! # Example
//!
//! ```rust
//! use mcpkit_core::state::{Connection, Disconnected, Connected};
//!
//! // Connection starts in Disconnected state
//! let conn: Connection<Disconnected> = Connection::new();
//!
//! // Each state has appropriate methods
//! let id = conn.id();
//! assert!(!id.is_empty());
//!
//! // Connect to transition to Connected state
//! let connected: Connection<Connected> = conn.connect();
//! assert!(connected.connected_at().is_some());
//! ```

use std::marker::PhantomData;
use std::time::{Duration, Instant};

use crate::capability::{
    ClientCapabilities, ClientInfo, InitializeRequest, InitializeResult, ServerCapabilities,
    ServerInfo,
};
use crate::error::McpError;
use crate::protocol::RequestId;

/// Marker type for disconnected state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Disconnected;

/// Marker type for connected state (transport established).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Connected;

/// Marker type for initializing state (handshake in progress).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Initializing;

/// Marker type for ready state (fully operational).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Ready;

/// Marker type for closing state (shutdown in progress).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Closing;

/// Internal connection data shared across states.
#[derive(Debug)]
pub struct ConnectionInner {
    /// Unique connection identifier.
    pub id: String,
    /// When the connection was established.
    pub connected_at: Option<Instant>,
    /// Last activity timestamp.
    pub last_activity: Option<Instant>,
    /// Request counter for generating IDs.
    pub request_counter: u64,
    /// Client info (available after initialization).
    pub client_info: Option<ClientInfo>,
    /// Server info (available after initialization).
    pub server_info: Option<ServerInfo>,
    /// Client capabilities (available after initialization).
    pub client_capabilities: Option<ClientCapabilities>,
    /// Server capabilities (available after initialization).
    pub server_capabilities: Option<ServerCapabilities>,
}

impl ConnectionInner {
    /// Create new connection inner data.
    fn new() -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            connected_at: None,
            last_activity: None,
            request_counter: 0,
            client_info: None,
            server_info: None,
            client_capabilities: None,
            server_capabilities: None,
        }
    }

    /// Generate the next request ID.
    fn next_request_id(&mut self) -> RequestId {
        self.request_counter += 1;
        RequestId::Number(self.request_counter)
    }

    /// Update last activity timestamp.
    fn touch(&mut self) {
        self.last_activity = Some(Instant::now());
    }
}

impl Default for ConnectionInner {
    fn default() -> Self {
        Self::new()
    }
}

/// A connection in a specific state.
///
/// The type parameter `S` represents the current state of the connection.
/// Different methods are available depending on the state.
#[derive(Debug)]
pub struct Connection<S> {
    inner: ConnectionInner,
    _state: PhantomData<S>,
}

impl Connection<Disconnected> {
    /// Create a new disconnected connection.
    #[must_use]
    pub fn new() -> Self {
        Self {
            inner: ConnectionInner::new(),
            _state: PhantomData,
        }
    }

    /// Get the connection ID.
    #[must_use]
    pub fn id(&self) -> &str {
        &self.inner.id
    }

    /// Establish the connection (transition to Connected state).
    ///
    /// In a real implementation, this would take a transport and
    /// establish the connection. Here we just transition the state.
    #[must_use]
    pub fn connect(mut self) -> Connection<Connected> {
        self.inner.connected_at = Some(Instant::now());
        self.inner.touch();
        Connection {
            inner: self.inner,
            _state: PhantomData,
        }
    }
}

impl Default for Connection<Disconnected> {
    fn default() -> Self {
        Self::new()
    }
}

impl Connection<Connected> {
    /// Get the connection ID.
    #[must_use]
    pub fn id(&self) -> &str {
        &self.inner.id
    }

    /// Get when the connection was established.
    #[must_use]
    pub const fn connected_at(&self) -> Option<Instant> {
        self.inner.connected_at
    }

    /// Get how long the connection has been active.
    #[must_use]
    pub fn uptime(&self) -> Duration {
        self.inner
            .connected_at
            .map(|t| t.elapsed())
            .unwrap_or_default()
    }

    /// Begin initialization (transition to Initializing state).
    ///
    /// For clients: Send initialize request with client info and capabilities.
    /// For servers: This is called when receiving an initialize request.
    #[must_use]
    pub fn initialize(
        mut self,
        client_info: ClientInfo,
        client_capabilities: ClientCapabilities,
    ) -> (Connection<Initializing>, InitializeRequest) {
        self.inner.client_info = Some(client_info.clone());
        self.inner.client_capabilities = Some(client_capabilities.clone());
        self.inner.touch();

        let request = InitializeRequest::new(client_info, client_capabilities);

        (
            Connection {
                inner: self.inner,
                _state: PhantomData,
            },
            request,
        )
    }

    /// Disconnect (transition back to Disconnected state).
    #[must_use]
    pub fn disconnect(self) -> Connection<Disconnected> {
        Connection {
            inner: ConnectionInner::new(),
            _state: PhantomData,
        }
    }
}

impl Connection<Initializing> {
    /// Get the connection ID.
    #[must_use]
    pub fn id(&self) -> &str {
        &self.inner.id
    }

    /// Get the client info.
    #[must_use]
    pub const fn client_info(&self) -> Option<&ClientInfo> {
        self.inner.client_info.as_ref()
    }

    /// Get the client capabilities.
    #[must_use]
    pub const fn client_capabilities(&self) -> Option<&ClientCapabilities> {
        self.inner.client_capabilities.as_ref()
    }

    /// Complete initialization (transition to Ready state).
    ///
    /// This is called after the initialize response is received (client)
    /// or sent (server).
    #[must_use]
    pub fn complete(
        mut self,
        server_info: ServerInfo,
        server_capabilities: ServerCapabilities,
    ) -> Connection<Ready> {
        self.inner.server_info = Some(server_info);
        self.inner.server_capabilities = Some(server_capabilities);
        self.inner.touch();

        Connection {
            inner: self.inner,
            _state: PhantomData,
        }
    }

    /// Abort initialization (transition back to Disconnected).
    #[must_use]
    pub fn abort(self) -> Connection<Disconnected> {
        Connection {
            inner: ConnectionInner::new(),
            _state: PhantomData,
        }
    }
}

impl Connection<Ready> {
    /// Get the connection ID.
    #[must_use]
    pub fn id(&self) -> &str {
        &self.inner.id
    }

    /// Get when the connection was established.
    #[must_use]
    pub const fn connected_at(&self) -> Option<Instant> {
        self.inner.connected_at
    }

    /// Get how long the connection has been active.
    #[must_use]
    pub fn uptime(&self) -> Duration {
        self.inner
            .connected_at
            .map(|t| t.elapsed())
            .unwrap_or_default()
    }

    /// Get the last activity timestamp.
    #[must_use]
    pub const fn last_activity(&self) -> Option<Instant> {
        self.inner.last_activity
    }

    /// Get the client info.
    ///
    /// # Panics
    ///
    /// This should never panic if the connection was properly initialized,
    /// as the typestate pattern ensures this is only callable in Ready state.
    /// Use `try_client_info()` for a fallible version.
    #[must_use]
    pub fn client_info(&self) -> &ClientInfo {
        self.inner
            .client_info
            .as_ref()
            .expect("client_info should be set in Ready state")
    }

    /// Try to get the client info.
    ///
    /// Returns `None` if the client info was not set (should not happen in normal use).
    #[must_use]
    pub const fn try_client_info(&self) -> Option<&ClientInfo> {
        self.inner.client_info.as_ref()
    }

    /// Get the server info.
    ///
    /// # Panics
    ///
    /// This should never panic if the connection was properly initialized,
    /// as the typestate pattern ensures this is only callable in Ready state.
    /// Use `try_server_info()` for a fallible version.
    #[must_use]
    pub fn server_info(&self) -> &ServerInfo {
        self.inner
            .server_info
            .as_ref()
            .expect("server_info should be set in Ready state")
    }

    /// Try to get the server info.
    ///
    /// Returns `None` if the server info was not set (should not happen in normal use).
    #[must_use]
    pub const fn try_server_info(&self) -> Option<&ServerInfo> {
        self.inner.server_info.as_ref()
    }

    /// Get the client capabilities.
    ///
    /// # Panics
    ///
    /// This should never panic if the connection was properly initialized,
    /// as the typestate pattern ensures this is only callable in Ready state.
    /// Use `try_client_capabilities()` for a fallible version.
    #[must_use]
    pub fn client_capabilities(&self) -> &ClientCapabilities {
        self.inner
            .client_capabilities
            .as_ref()
            .expect("client_capabilities should be set in Ready state")
    }

    /// Try to get the client capabilities.
    ///
    /// Returns `None` if the client capabilities were not set (should not happen in normal use).
    #[must_use]
    pub const fn try_client_capabilities(&self) -> Option<&ClientCapabilities> {
        self.inner.client_capabilities.as_ref()
    }

    /// Get the server capabilities.
    ///
    /// # Panics
    ///
    /// This should never panic if the connection was properly initialized,
    /// as the typestate pattern ensures this is only callable in Ready state.
    /// Use `try_server_capabilities()` for a fallible version.
    #[must_use]
    pub fn server_capabilities(&self) -> &ServerCapabilities {
        self.inner
            .server_capabilities
            .as_ref()
            .expect("server_capabilities should be set in Ready state")
    }

    /// Try to get the server capabilities.
    ///
    /// Returns `None` if the server capabilities were not set (should not happen in normal use).
    #[must_use]
    pub const fn try_server_capabilities(&self) -> Option<&ServerCapabilities> {
        self.inner.server_capabilities.as_ref()
    }

    /// Generate the next request ID.
    pub fn next_request_id(&mut self) -> RequestId {
        self.inner.next_request_id()
    }

    /// Update the last activity timestamp.
    pub fn touch(&mut self) {
        self.inner.touch();
    }

    /// Check if the connection has been idle for longer than the given duration.
    #[must_use]
    pub fn is_idle(&self, timeout: Duration) -> bool {
        self.inner
            .last_activity
            .is_some_and(|t| t.elapsed() > timeout)
    }

    /// Begin shutdown (transition to Closing state).
    #[must_use]
    pub fn shutdown(self) -> Connection<Closing> {
        Connection {
            inner: self.inner,
            _state: PhantomData,
        }
    }
}

impl Connection<Closing> {
    /// Get the connection ID.
    #[must_use]
    pub fn id(&self) -> &str {
        &self.inner.id
    }

    /// Complete the shutdown (transition to Disconnected state).
    #[must_use]
    pub fn close(self) -> Connection<Disconnected> {
        Connection {
            inner: ConnectionInner::new(),
            _state: PhantomData,
        }
    }
}

/// Builder for creating initialize results (used by servers).
pub struct InitializeResultBuilder {
    server_info: ServerInfo,
    capabilities: ServerCapabilities,
    instructions: Option<String>,
}

impl InitializeResultBuilder {
    /// Create a new builder with server info.
    #[must_use]
    pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
        Self {
            server_info: ServerInfo::new(name, version),
            capabilities: ServerCapabilities::new(),
            instructions: None,
        }
    }

    /// Set the capabilities.
    #[must_use]
    pub fn capabilities(mut self, caps: ServerCapabilities) -> Self {
        self.capabilities = caps;
        self
    }

    /// Enable tool support.
    #[must_use]
    pub fn with_tools(mut self) -> Self {
        self.capabilities = self.capabilities.with_tools();
        self
    }

    /// Enable resource support.
    #[must_use]
    pub fn with_resources(mut self) -> Self {
        self.capabilities = self.capabilities.with_resources();
        self
    }

    /// Enable prompt support.
    #[must_use]
    pub fn with_prompts(mut self) -> Self {
        self.capabilities = self.capabilities.with_prompts();
        self
    }

    /// Enable task support.
    #[must_use]
    pub fn with_tasks(mut self) -> Self {
        self.capabilities = self.capabilities.with_tasks();
        self
    }

    /// Set instructions.
    #[must_use]
    pub fn instructions(mut self, instructions: impl Into<String>) -> Self {
        self.instructions = Some(instructions.into());
        self
    }

    /// Build the initialize result.
    #[must_use]
    pub fn build(self) -> InitializeResult {
        let mut result = InitializeResult::new(self.server_info, self.capabilities);
        if let Some(instructions) = self.instructions {
            result = result.instructions(instructions);
        }
        result
    }
}

/// Validate that a connection can transition to the ready state.
pub const fn validate_initialization(
    _client_caps: &ClientCapabilities,
    _server_caps: &ServerCapabilities,
) -> Result<(), McpError> {
    // For now, just return Ok. In a real implementation, you might
    // check for required capability combinations.
    Ok(())
}

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

    #[test]
    fn test_connection_lifecycle() {
        // Start disconnected
        let conn: Connection<Disconnected> = Connection::new();
        assert!(!conn.id().is_empty());

        // Connect
        let conn: Connection<Connected> = conn.connect();
        assert!(conn.connected_at().is_some());

        // Initialize
        let client = ClientInfo::new("test", "1.0.0");
        let caps = ClientCapabilities::new();
        let (conn, _request): (Connection<Initializing>, _) = conn.initialize(client, caps);
        assert!(conn.client_info().is_some());

        // Complete
        let server = ServerInfo::new("server", "1.0.0");
        let server_caps = ServerCapabilities::new().with_tools();
        let mut conn: Connection<Ready> = conn.complete(server, server_caps);
        assert!(conn.server_capabilities().has_tools());

        // Generate request IDs
        let id1 = conn.next_request_id();
        let id2 = conn.next_request_id();
        assert_ne!(id1, id2);

        // Shutdown
        let conn: Connection<Closing> = conn.shutdown();
        let _conn: Connection<Disconnected> = conn.close();
    }

    #[test]
    fn test_uptime() {
        let conn = Connection::new().connect();
        std::thread::sleep(std::time::Duration::from_millis(10));
        assert!(conn.uptime() >= std::time::Duration::from_millis(10));
    }

    #[test]
    fn test_idle_detection() {
        let client = ClientInfo::new("test", "1.0.0");
        let server = ServerInfo::new("server", "1.0.0");

        let (conn, _) = Connection::new()
            .connect()
            .initialize(client, ClientCapabilities::new());

        let conn = conn.complete(server, ServerCapabilities::new());

        // Should not be idle immediately
        assert!(!conn.is_idle(Duration::from_secs(1)));
    }

    #[test]
    fn test_initialize_result_builder() {
        let result = InitializeResultBuilder::new("my-server", "1.0.0")
            .with_tools()
            .with_resources()
            .instructions("Use this server to access tools and resources")
            .build();

        assert_eq!(result.server_info.name, "my-server");
        assert!(result.capabilities.has_tools());
        assert!(result.capabilities.has_resources());
        assert!(result.instructions.is_some());
    }

    #[test]
    fn test_abort_initialization() {
        let client = ClientInfo::new("test", "1.0.0");
        let (conn, _) = Connection::new()
            .connect()
            .initialize(client, ClientCapabilities::new());

        // Abort should return to disconnected
        let _conn: Connection<Disconnected> = conn.abort();
    }

    #[test]
    fn test_disconnect_from_connected() {
        let conn = Connection::new().connect();
        let _conn: Connection<Disconnected> = conn.disconnect();
    }

    #[test]
    fn test_fallible_accessors() -> Result<(), Box<dyn std::error::Error>> {
        let client = ClientInfo::new("test-client", "1.0.0");
        let server = ServerInfo::new("test-server", "2.0.0");
        let client_caps = ClientCapabilities::new();
        let server_caps = ServerCapabilities::new().with_tools();

        let (conn, _) = Connection::new().connect().initialize(client, client_caps);

        let conn = conn.complete(server, server_caps);

        // Test fallible accessors return Some
        assert!(conn.try_client_info().is_some());
        assert!(conn.try_server_info().is_some());
        assert!(conn.try_client_capabilities().is_some());
        assert!(conn.try_server_capabilities().is_some());

        // Test values are correct
        assert_eq!(
            conn.try_client_info().ok_or("Expected client info")?.name,
            "test-client"
        );
        assert_eq!(
            conn.try_server_info().ok_or("Expected server info")?.name,
            "test-server"
        );
        assert!(
            conn.try_server_capabilities()
                .ok_or("Expected server capabilities")?
                .has_tools()
        );
        Ok(())
    }
}