pub trait SessionView: Send + Sync {
// Required methods
fn session_id(&self) -> &str;
fn get_state<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<Value>, String>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
fn set_state<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: Value,
) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
fn get_metadata<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<Value>, String>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
fn set_metadata<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: Value,
) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
}Expand description
SessionView trait for middleware - re-exported from turul-mcp-session-storage Minimal session interface for middleware access
This trait provides a read-write view of session state for middleware without exposing the full SessionContext implementation details.
§Silent Write Failures
IMPORTANT: set_state and set_metadata currently always return Ok(()).
The underlying storage may log errors but does not propagate them to middleware.
This design prevents middleware from blocking request processing due to storage issues.
§Example
use turul_mcp_session_storage::SessionView;
use serde_json::json;
async fn middleware_example(session: &dyn SessionView) {
// Read session state
if let Ok(Some(counter)) = session.get_state("request_count").await {
println!("Session {}: {} requests", session.session_id(), counter);
}
// Write session state
let _ = session.set_state("last_access", json!("2025-10-05")).await;
}Required Methods§
Sourcefn session_id(&self) -> &str
fn session_id(&self) -> &str
Get the unique session identifier
§Returns
The session ID as a string reference. Typically a UUID v7 for temporal ordering.
Sourcefn get_state<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<Value>, String>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn get_state<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<Value>, String>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Sourcefn set_state<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: Value,
) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn set_state<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: Value,
) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Set a state value in the session
§Parameters
key: The state key to setvalue: The JSON value to store
§Returns
Ok(()): State was successfully set (or error was logged silently)Err(error): Storage error occurred
Note: Current implementation always returns Ok(()) even if underlying
storage fails. Errors are logged but not propagated.
Sourcefn get_metadata<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<Value>, String>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn get_metadata<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<Value>, String>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Get a metadata value from the session
Metadata is typically set during session initialization and provides context about the session (client version, capabilities, etc.).
§Parameters
key: The metadata key to retrieve
§Returns
Ok(Some(value)): Metadata value existsOk(None): Metadata key not foundErr(error): Storage error occurred
Sourcefn set_metadata<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: Value,
) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn set_metadata<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: Value,
) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Set a metadata value in the session
§Parameters
key: The metadata key to setvalue: The JSON value to store
§Returns
Ok(()): Metadata was successfully set (or error was logged silently)Err(error): Storage error occurred
Note: Current implementation always returns Ok(()) even if underlying
storage fails. Errors are logged but not propagated.
Implementors§
impl SessionView for StorageBackedSessionView
impl SessionView for SessionContext
Implement SessionView trait for SessionContext (trait is defined in turul-mcp-session-storage)
This allows SessionContext to be used with middleware. Metadata is stored using a special prefix (“meta:”) to distinguish it from regular state.