rust_mcp_sdk/hyper_servers/
session_store.rs

1mod in_memory;
2use std::sync::Arc;
3
4use async_trait::async_trait;
5pub use in_memory::*;
6use rust_mcp_transport::SessionId;
7use tokio::sync::Mutex;
8use uuid::Uuid;
9
10use crate::mcp_server::ServerRuntime;
11
12// Type alias for the server-side duplex stream used in sessions
13pub type TxServer = Arc<ServerRuntime>;
14
15/// Trait defining the interface for session storage operations
16///
17/// This trait provides asynchronous methods for managing session data,
18/// Implementors must be Send and Sync to support concurrent access.
19#[async_trait]
20pub trait SessionStore: Send + Sync {
21    /// Retrieves a session by its identifier
22    ///
23    /// # Arguments
24    /// * `key` - The session identifier to look up
25    ///
26    /// # Returns
27    /// * `Option<Arc<Mutex<TxServer>>>` - The session stream wrapped in `Arc<Mutex>` if found, None otherwise
28    async fn get(&self, key: &SessionId) -> Option<Arc<Mutex<TxServer>>>;
29    /// Stores a new session with the given identifier
30    ///
31    /// # Arguments
32    /// * `key` - The session identifier
33    /// * `value` - The duplex stream to store
34    async fn set(&self, key: SessionId, value: TxServer);
35    /// Deletes a session by its identifier
36    ///
37    /// # Arguments
38    /// * `key` - The session identifier to delete
39    async fn delete(&self, key: &SessionId);
40    /// Clears all sessions from the store
41    async fn clear(&self);
42
43    async fn keys(&self) -> Vec<SessionId>;
44
45    async fn values(&self) -> Vec<Arc<Mutex<TxServer>>>;
46
47    async fn has(&self, session: &SessionId) -> bool;
48}
49
50/// Trait for generating session identifiers
51///
52/// Implementors must be Send and Sync to support concurrent access.
53pub trait IdGenerator: Send + Sync {
54    fn generate(&self) -> SessionId;
55}
56
57/// Struct implementing the IdGenerator trait using UUID v4
58///
59/// This is a simple wrapper around the uuid crate's Uuid::new_v4 function
60/// to generate unique session identifiers.
61pub struct UuidGenerator {}
62
63impl IdGenerator for UuidGenerator {
64    /// Generates a new UUID v4-based session identifier
65    ///
66    /// # Returns
67    /// * `SessionId` - A new UUID-based session identifier as a String
68    fn generate(&self) -> SessionId {
69        Uuid::new_v4().to_string()
70    }
71}