SessionView

Trait SessionView 

Source
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 Self: 'async_trait,
             'life0: 'async_trait,
             'life1: '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 Self: 'async_trait,
             'life0: 'async_trait,
             'life1: '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 Self: 'async_trait,
             'life0: 'async_trait,
             'life1: '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 Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

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§

Source

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.

Source

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 Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get a state value from the session

§Parameters
  • key: The state key to retrieve
§Returns
  • Ok(Some(value)): State value exists
  • Ok(None): State key not found
  • Err(error): Storage error occurred
Source

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 Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Set a state value in the session

§Parameters
  • key: The state key to set
  • value: 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.

Source

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 Self: 'async_trait, 'life0: 'async_trait, 'life1: '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 exists
  • Ok(None): Metadata key not found
  • Err(error): Storage error occurred
Source

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 Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Set a metadata value in the session

§Parameters
  • key: The metadata key to set
  • value: 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§