pub struct SessionState { /* private fields */ }Expand description
Thread-safe session state container for per-session key-value storage.
This allows handlers to store and retrieve state that persists across requests within a single MCP session. The state is typed as JSON values to support flexible data storage.
§Thread Safety
SessionState is designed for concurrent access from multiple handlers. Operations are synchronized via an internal mutex.
§Example
// In a tool handler:
ctx.set_state("counter", 42);
let count: Option<i32> = ctx.get_state("counter");Implementations§
Source§impl SessionState
impl SessionState
Sourcepub fn with_local_overrides(&self) -> Self
pub fn with_local_overrides(&self) -> Self
Returns a view with request-local overrides layered on top of the shared session state.
Cloning the returned value preserves the same local override map, while ordinary writes continue to target the shared session state.
Sourcepub fn snapshot(&self) -> Self
pub fn snapshot(&self) -> Self
Creates an isolated snapshot of the current session state.
Unlike Clone, which shares the same underlying storage, this copies
the current key-value map into a fresh container so later writes do not
bleed across requests.
Sourcepub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
Gets a value from session state by key.
Returns None if the key doesn’t exist or if deserialization fails.
§Type Parameters
T- The expected type of the value (must implement Deserialize)
Sourcepub fn get_raw(&self, key: &str) -> Option<Value>
pub fn get_raw(&self, key: &str) -> Option<Value>
Gets a raw JSON value from session state by key.
Returns None if the key doesn’t exist.
Sourcepub fn set<T: Serialize>(&self, key: impl Into<String>, value: T) -> bool
pub fn set<T: Serialize>(&self, key: impl Into<String>, value: T) -> bool
Sets a value in session state.
The value is serialized to JSON for storage. Returns true if
the value was successfully stored.
§Type Parameters
T- The type of the value (must implement Serialize)
Sourcepub fn set_raw(&self, key: impl Into<String>, value: Value) -> bool
pub fn set_raw(&self, key: impl Into<String>, value: Value) -> bool
Sets a raw JSON value in session state.
Returns true if the value was successfully stored.
Sourcepub fn set_local_raw(&self, key: impl Into<String>, value: Value) -> bool
pub fn set_local_raw(&self, key: impl Into<String>, value: Value) -> bool
Sets a request-local raw JSON value layered over the shared session state.
Returns false if this state does not have local overrides enabled.
Sourcepub fn set_local<T: Serialize>(&self, key: impl Into<String>, value: T) -> bool
pub fn set_local<T: Serialize>(&self, key: impl Into<String>, value: T) -> bool
Sets a request-local value layered over the shared session state.
Returns false if serialization fails or local overrides are unavailable.
Source§impl SessionState
impl SessionState
Sourcepub fn is_tool_enabled(&self, name: &str) -> bool
pub fn is_tool_enabled(&self, name: &str) -> bool
Returns whether a tool is enabled (not disabled) for this session.
Tools are enabled by default unless explicitly disabled.
Sourcepub fn is_resource_enabled(&self, uri: &str) -> bool
pub fn is_resource_enabled(&self, uri: &str) -> bool
Returns whether a resource is enabled (not disabled) for this session.
Resources are enabled by default unless explicitly disabled.
Sourcepub fn is_prompt_enabled(&self, name: &str) -> bool
pub fn is_prompt_enabled(&self, name: &str) -> bool
Returns whether a prompt is enabled (not disabled) for this session.
Prompts are enabled by default unless explicitly disabled.
Sourcepub fn disabled_tools(&self) -> HashSet<String>
pub fn disabled_tools(&self) -> HashSet<String>
Returns the set of disabled tools.
Sourcepub fn disabled_resources(&self) -> HashSet<String>
pub fn disabled_resources(&self) -> HashSet<String>
Returns the set of disabled resources.
Sourcepub fn disabled_prompts(&self) -> HashSet<String>
pub fn disabled_prompts(&self) -> HashSet<String>
Returns the set of disabled prompts.
Trait Implementations§
Source§impl Clone for SessionState
impl Clone for SessionState
Source§fn clone(&self) -> SessionState
fn clone(&self) -> SessionState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SessionState
impl Debug for SessionState
Source§impl Default for SessionState
impl Default for SessionState
Source§fn default() -> SessionState
fn default() -> SessionState
Auto Trait Implementations§
impl Freeze for SessionState
impl RefUnwindSafe for SessionState
impl Send for SessionState
impl Sync for SessionState
impl Unpin for SessionState
impl UnsafeUnpin for SessionState
impl UnwindSafe for SessionState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more