pub struct SessionState { /* private fields */ }Expand description
Shared session state that can be cloned across requests.
Uses atomic operations for thread-safe state transitions. Includes a type-safe extensions map for storing session-scoped data like authentication claims.
§Example
use tower_mcp::SessionState;
#[derive(Debug, Clone)]
struct UserClaims {
user_id: String,
role: String,
}
let session = SessionState::new();
// Store auth claims in the session
session.insert(UserClaims {
user_id: "user123".to_string(),
role: "admin".to_string(),
});
// Retrieve claims later
if let Some(claims) = session.get::<UserClaims>() {
assert_eq!(claims.role, "admin");
}Implementations§
Source§impl SessionState
impl SessionState
Sourcepub fn insert<T: Send + Sync + Clone + 'static>(&self, val: T)
pub fn insert<T: Send + Sync + Clone + 'static>(&self, val: T)
Insert a value into the session extensions.
This is typically used by auth middleware to store claims that can be checked by capability filters.
§Example
use tower_mcp::SessionState;
let session = SessionState::new();
session.insert(42u32);
assert_eq!(session.get::<u32>(), Some(42));Sourcepub fn get<T: Send + Sync + Clone + 'static>(&self) -> Option<T>
pub fn get<T: Send + Sync + Clone + 'static>(&self) -> Option<T>
Get a cloned value from the session extensions.
Returns None if no value of the given type has been inserted or if
the lock cannot be acquired.
§Example
use tower_mcp::SessionState;
let session = SessionState::new();
session.insert("hello".to_string());
assert_eq!(session.get::<String>(), Some("hello".to_string()));
assert_eq!(session.get::<u32>(), None);Sourcepub fn phase(&self) -> SessionPhase
pub fn phase(&self) -> SessionPhase
Get the current session phase
Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Check if the session is initialized (operation phase)
Sourcepub fn mark_initializing(&self) -> bool
pub fn mark_initializing(&self) -> bool
Transition from Uninitialized to Initializing.
Called after responding to an initialize request.
Returns true if the transition was successful.
Sourcepub fn mark_initialized(&self) -> bool
pub fn mark_initialized(&self) -> bool
Transition from Initializing to Initialized.
Called when receiving an initialized notification.
Returns true if the transition was successful.
Sourcepub fn is_request_allowed(&self, method: &str) -> bool
pub fn is_request_allowed(&self, method: &str) -> bool
Check if a request method is allowed in the current phase. Per spec:
- Before initialization: only
initializeandpingare valid - During all phases:
pingis always valid
Trait Implementations§
Source§impl Clone for SessionState
impl Clone for SessionState
Source§fn clone(&self) -> SessionState
fn clone(&self) -> SessionState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more