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