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;
8
9use crate::mcp_server::ServerRuntime;
10
11// Type alias for the server-side duplex stream used in sessions
12pub type TxServer = Arc<ServerRuntime>;
13
14/// Trait defining the interface for session storage operations
15///
16/// This trait provides asynchronous methods for managing session data,
17/// Implementors must be Send and Sync to support concurrent access.
18#[async_trait]
19pub trait SessionStore: Send + Sync {
20 /// Retrieves a session by its identifier
21 ///
22 /// # Arguments
23 /// * `key` - The session identifier to look up
24 ///
25 /// # Returns
26 /// * `Option<Arc<Mutex<TxServer>>>` - The session stream wrapped in `Arc<Mutex>` if found, None otherwise
27 async fn get(&self, key: &SessionId) -> Option<Arc<Mutex<TxServer>>>;
28 /// Stores a new session with the given identifier
29 ///
30 /// # Arguments
31 /// * `key` - The session identifier
32 /// * `value` - The duplex stream to store
33 async fn set(&self, key: SessionId, value: TxServer);
34 /// Deletes a session by its identifier
35 ///
36 /// # Arguments
37 /// * `key` - The session identifier to delete
38 async fn delete(&self, key: &SessionId);
39 /// Clears all sessions from the store
40 async fn clear(&self);
41
42 async fn keys(&self) -> Vec<SessionId>;
43
44 async fn values(&self) -> Vec<Arc<Mutex<TxServer>>>;
45
46 async fn has(&self, session: &SessionId) -> bool;
47}