Skip to main content

StateManager

Struct StateManager 

Source
pub struct StateManager { /* private fields */ }
Expand description

State manager for pending generation sessions.

Uses an in-memory HashMap protected by RwLock for thread-safe access. Sessions expire after 30 minutes and are cleaned up lazily.

§Examples

use mcp_execution_server::state::StateManager;
use mcp_execution_server::types::PendingGeneration;
use mcp_execution_core::{ServerId, ServerConfig};
use mcp_execution_introspector::ServerInfo;
use std::path::PathBuf;

let state = StateManager::new();

let pending = PendingGeneration::new(
    ServerId::new("github"),
    server_info,
    ServerConfig::builder().command("npx".to_string()).build(),
    PathBuf::from("/tmp/output"),
);

// Store and get session ID
let session_id = state.store(pending).await;

// Retrieve session data
let retrieved = state.take(session_id).await;
assert!(retrieved.is_some());

Implementations§

Source§

impl StateManager

Source

pub fn new() -> Self

Creates a new state manager.

Source

pub async fn store(&self, generation: PendingGeneration) -> Uuid

Stores a pending generation and returns a session ID.

This operation also performs lazy cleanup of expired sessions.

§Examples
use mcp_execution_server::state::StateManager;

let state = StateManager::new();
let session_id = state.store(pending).await;
Source

pub async fn take(&self, session_id: Uuid) -> Option<PendingGeneration>

Retrieves and removes a pending generation.

Returns None if the session is not found or has expired. This operation also performs lazy cleanup of expired sessions.

§Examples
use mcp_execution_server::state::StateManager;

let state = StateManager::new();
let session_id = state.store(pending).await;

let retrieved = state.take(session_id).await;
assert!(retrieved.is_some());

// Second take returns None (already removed)
let second = state.take(session_id).await;
assert!(second.is_none());
Source

pub async fn get(&self, session_id: Uuid) -> Option<PendingGeneration>

Gets a pending generation without removing it.

Returns None if the session is not found or has expired.

§Examples
use mcp_execution_server::state::StateManager;

let state = StateManager::new();
let session_id = state.store(pending).await;

// Get without removing
let peeked = state.get(session_id).await;
assert!(peeked.is_some());

// Still available
let peeked_again = state.get(session_id).await;
assert!(peeked_again.is_some());
Source

pub async fn pending_count(&self) -> usize

Returns the current pending session count (excluding expired).

§Examples
use mcp_execution_server::state::StateManager;

let state = StateManager::new();
assert_eq!(state.pending_count().await, 0);
Source

pub async fn cleanup_expired(&self) -> usize

Cleans up all expired sessions.

Returns the number of sessions that were removed.

§Examples
use mcp_execution_server::state::StateManager;

let state = StateManager::new();
let removed = state.cleanup_expired().await;
assert_eq!(removed, 0);

Trait Implementations§

Source§

impl Debug for StateManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for StateManager

Source§

fn default() -> StateManager

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more