Skip to main content

BoundedSessionManager

Struct BoundedSessionManager 

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

Wraps LocalSessionManager and limits the number of concurrent sessions.

When the limit is reached, the oldest session (by creation order) is closed before the new one is created. This prevents unbounded memory growth when many clients connect without explicitly closing their sessions.

Optionally, a rate limit can be applied to session creation via BoundedSessionManager::with_rate_limit.

§Concurrency note

Under concurrent session creation, the live count may transiently exceed max_sessions by at most the number of concurrent callers. The limit is best-effort under contention; use a semaphore if exact enforcement is required.

Implementations§

Source§

impl BoundedSessionManager

Source

pub fn new(session_config: SessionConfig, max_sessions: usize) -> Self

Create a new BoundedSessionManager.

  • session_config — passed through to the inner LocalSessionManager.
  • max_sessions — maximum number of concurrent sessions. When this limit is reached, the oldest session is evicted before creating a new one. Must be at least 1.
§Panics

Panics if max_sessions is 0.

Source

pub fn with_rate_limit(self, max_creates: usize, window: Duration) -> Self

Configure a rate limit on session creation.

At most max_creates sessions may be created within any rolling window duration. If exceeded, BoundedSessionError::RateLimited is returned and no eviction is performed.

§Panics

Panics if max_creates is 0. Pass no rate limit instead of 0 — a limit of zero would silently block all session creation.

Trait Implementations§

Source§

impl SessionManager for BoundedSessionManager

Source§

type Error = BoundedSessionError

Source§

type Transport = WorkerTransport<LocalSessionWorker>

Source§

async fn create_session( &self, ) -> Result<(SessionId, Self::Transport), Self::Error>

Create a new session and return its ID together with the transport that will be used to exchange MCP messages within this session.
Source§

async fn close_session(&self, id: &SessionId) -> Result<(), Self::Error>

Close and remove the session. Corresponds to an HTTP DELETE request with Mcp-Session-Id.
Source§

async fn initialize_session( &self, id: &SessionId, message: ClientJsonRpcMessage, ) -> Result<ServerJsonRpcMessage, Self::Error>

Forward the first message (the initialize request) to the session.
Source§

async fn has_session(&self, id: &SessionId) -> Result<bool, Self::Error>

Return true if a session with the given ID exists and is active.
Source§

async fn create_stream( &self, id: &SessionId, message: ClientJsonRpcMessage, ) -> Result<impl Stream<Item = ServerSseMessage> + Send + Sync + 'static, Self::Error>

Route a client request into the session and return an SSE stream carrying the server’s response(s).
Source§

async fn accept_message( &self, id: &SessionId, message: ClientJsonRpcMessage, ) -> Result<(), Self::Error>

Accept a notification, response, or error message from the client without producing a response stream.
Source§

async fn create_standalone_stream( &self, id: &SessionId, ) -> Result<impl Stream<Item = ServerSseMessage> + Send + Sync + 'static, Self::Error>

Create an SSE stream not tied to a specific client request (HTTP GET).
Source§

async fn resume( &self, id: &SessionId, last_event_id: String, ) -> Result<impl Stream<Item = ServerSseMessage> + Send + Sync + 'static, Self::Error>

Resume an SSE stream from the given Last-Event-ID, replaying any events the client missed.

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<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