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 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.
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 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more