Skip to main content

solo_api/
mcp_session.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! v0.11.0 P1 — MCP `Mcp-Session-Id` session store + middleware.
4//!
5//! v0.10.2 shipped `/mcp` as a one-shot request/response surface — every
6//! POST opened a fresh dispatcher with no cross-request state. v0.11.0
7//! lifts that to the full MCP Streamable HTTP spec, and this module is
8//! the foundation: a `DashMap`-backed [`SessionStore`] of
9//! [`SessionState`] entries keyed by [`SessionId`], plus an
10//! [`mcp_session_middleware`] Axum middleware that validates the
11//! `Mcp-Session-Id` request header against the store.
12//!
13//! ## Locked design (plan §3)
14//!
15//! - **Decision A — In-memory storage.** A
16//!   `DashMap<SessionId, SessionState>` gives lock-free per-session
17//!   reads on the dispatch hot path. Solo runs as a single-process
18//!   daemon today; cross-process session persistence is deferred to a
19//!   future release (clients re-`initialize` on daemon restart).
20//! - **Decision D — TTL.** 30 min inactivity + 4 hr absolute cap.
21//!   Background cleanup task runs every 60 s and removes expired
22//!   sessions; a lazy expiry check on every `get` is the safety net
23//!   for the window between sweeps.
24//! - **Expired session → 404.** The middleware returns 404 Not Found
25//!   with a body that includes a `re-initialize` instruction so
26//!   clients can distinguish "session expired" from "server down".
27//!
28//! ## Dispatcher integration (Option B — session-agnostic)
29//!
30//! The brief leaves "does the dispatcher learn about sessions?" open.
31//! P1 picks **Option B**: [`crate::mcp_dispatch::McpDispatcher`] stays
32//! session-agnostic. Sessions are purely an HTTP-transport concern;
33//! the dispatcher receives the resolved tenant + audit principal and
34//! has no knowledge of `SessionId`. The stdio path (which has no
35//! sessions) keeps working unchanged. v0.11.0 P3 will route per-tool
36//! progress events through the session's notification channel by
37//! reading the session out of the request extension before building
38//! the per-request `ProgressEmitter` — without baking sessions into
39//! the dispatcher itself.
40//!
41//! ## v0.11.0 P2 — event buffer + publish API
42//!
43//! P2 grows [`SessionState`] with the two fields the resumable GET
44//! stream rides on top of:
45//!
46//!   - `event_tx: broadcast::Sender<McpStreamEvent>` (capacity
47//!     [`MCP_SESSION_EVENT_BUFFER_CAPACITY`] = 256 per Decision E).
48//!   - `next_event_id: AtomicU64` (monotonic per-session event id).
49//!
50//! A new [`SessionState::publish_event`] helper allocates the next id,
51//! constructs an [`McpStreamEvent`], and fans it out to every live
52//! subscriber. P3 (per-tool progress) and P4 (notifications/message
53//! bridge) call this method on the same session record the HTTP POST
54//! handler resolved; the GET handler in `http.rs` consumes via
55//! [`SessionState::subscribe_events`].
56//!
57//! ## What this module does NOT do
58//!
59//! - **No tenant/principal binding check.** Cross-request tenant
60//!   mismatch (`409 Conflict`) and cross-principal access
61//!   (`401 Unauthorized`) still TBD — P2 wires the broadcast channel
62//!   but leaves the auth-binding policy decisions to a follow-up
63//!   priority (Plan §9 Q4).
64//! - **No audit emission on session open/close.** Plan §9 Q3 still
65//!   open — P2 keeps the store as pure in-memory plumbing.
66
67use std::collections::VecDeque;
68use std::sync::Arc;
69use std::sync::atomic::AtomicU64;
70use std::time::Duration;
71
72use axum::extract::{Request, State};
73use axum::http::{HeaderMap, HeaderName, HeaderValue, StatusCode};
74use axum::middleware::Next;
75use axum::response::{IntoResponse, Response};
76use dashmap::DashMap;
77use serde::{Deserialize, Serialize};
78use solo_core::TenantId;
79use tokio::sync::broadcast;
80use uuid::Uuid;
81
82use crate::auth::AuthenticatedPrincipal;
83
84/// HTTP header name carrying the `Mcp-Session-Id` per the MCP
85/// Streamable HTTP transport spec. Lowercase because HTTP headers are
86/// case-insensitive on the wire and axum stores them lowercased.
87pub const MCP_SESSION_ID_HEADER: &str = "mcp-session-id";
88
89/// Inactivity TTL (milliseconds). A session whose `last_accessed_at_ms`
90/// is more than this old is considered expired (Decision D).
91pub const MCP_SESSION_INACTIVITY_TTL_MS: u64 = 30 * 60 * 1000;
92
93/// Absolute TTL (milliseconds). A session is unconditionally expired
94/// this long after its `created_at_ms`, regardless of activity
95/// (Decision D — bounds worst-case memory growth for orphaned
96/// sessions).
97pub const MCP_SESSION_ABSOLUTE_TTL_MS: u64 = 4 * 60 * 60 * 1000;
98
99/// Cadence (seconds) for the background sweep task that removes
100/// expired sessions. Lazy expiry on every `get` is the primary safety
101/// net; the sweep keeps total memory bounded between accesses for
102/// idle sessions.
103pub const MCP_SESSION_SWEEP_INTERVAL_SECS: u64 = 60;
104
105/// HTTP header name carrying the `Last-Event-ID` per the SSE
106/// specification. v0.11.0 P2 reads this on `GET /mcp` to resume an
107/// interrupted stream from a known event id (Decision E).
108pub const MCP_LAST_EVENT_ID_HEADER: &str = "last-event-id";
109
110/// Capacity of the per-session `tokio::sync::broadcast` channel that
111/// carries server-initiated SSE events (init, message, progress,
112/// heartbeat, lagged). Per plan §3 Decision E. A subscriber that
113/// drifts further behind than this sees a `RecvError::Lagged(n)` on
114/// its next `recv`, at which point the GET handler emits one
115/// `event: lagged` and resumes from the current cursor.
116pub const MCP_SESSION_EVENT_BUFFER_CAPACITY: usize = 256;
117
118/// Opaque session id assigned by the server. v7 UUID — time-ordered
119/// for sortability per Solo's `memory_id` discipline; printed as a
120/// regular hyphenated UUID string on the wire.
121///
122/// Server-assigned (not client-proposed) per Decision A — keeps
123/// correctness on the server.
124#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
125pub struct SessionId(String);
126
127impl SessionId {
128    /// Generate a fresh server-assigned id.
129    pub fn new() -> Self {
130        Self(Uuid::now_v7().to_string())
131    }
132
133    /// Parse a wire-format id. Returns `None` for empty / non-UUID
134    /// strings; the middleware treats `None` as "unknown session" →
135    /// 404 (rather than 400) so clients see a single re-init code
136    /// path regardless of header malformation.
137    pub fn parse(raw: &str) -> Option<Self> {
138        // Reject empty / whitespace strings. We don't reject
139        // arbitrary UUID strings here because the `DashMap` lookup
140        // does the real validation: an id we never assigned simply
141        // isn't in the store.
142        let s = raw.trim();
143        if s.is_empty() {
144            return None;
145        }
146        Some(Self(s.to_string()))
147    }
148
149    /// String representation suitable for the `Mcp-Session-Id` response
150    /// header value.
151    pub fn as_str(&self) -> &str {
152        &self.0
153    }
154}
155
156impl Default for SessionId {
157    fn default() -> Self {
158        Self::new()
159    }
160}
161
162impl std::fmt::Display for SessionId {
163    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
164        f.write_str(&self.0)
165    }
166}
167
168/// Discriminator for the per-session SSE event stream. The wire `event:`
169/// field is rendered from the kebab-case spelling of each variant — kept
170/// in lock-step with the constants below so handlers + clients agree
171/// on the literal string.
172///
173/// Variants:
174///
175///   - [`Init`] — emitted once when a subscriber connects (`event: init`).
176///     The payload includes the session id + tenant + connect ts.
177///   - [`Message`] — JSON-RPC `notifications/message` from the P4 bridge
178///     (`event: message`).
179///   - [`Progress`] — JSON-RPC `notifications/progress` from P3 long-running
180///     tool handlers (`event: progress`).
181///   - [`Lagged`] — synthetic event emitted by the GET handler when a
182///     subscriber falls past the broadcast buffer's capacity, OR when
183///     a `Last-Event-ID` is older than the buffer's oldest retained
184///     event (`event: lagged`). Carries `{dropped: <count>}` so clients
185///     know whether to resync state.
186///   - [`Heartbeat`] — synthetic event emitted by the heartbeat tick to
187///     keep proxies + clients aware the stream is alive
188///     (`event: heartbeat`).
189///
190/// [`Init`]: McpEventKind::Init
191/// [`Message`]: McpEventKind::Message
192/// [`Progress`]: McpEventKind::Progress
193/// [`Lagged`]: McpEventKind::Lagged
194/// [`Heartbeat`]: McpEventKind::Heartbeat
195#[derive(Debug, Clone, Copy, PartialEq, Eq)]
196pub enum McpEventKind {
197    /// `event: init` — subscriber-connect handshake.
198    Init,
199    /// `event: message` — JSON-RPC `notifications/message`.
200    Message,
201    /// `event: progress` — JSON-RPC `notifications/progress`.
202    Progress,
203    /// `event: lagged` — subscriber drifted past the buffer.
204    Lagged,
205    /// `event: heartbeat` — periodic liveness ping.
206    Heartbeat,
207}
208
209/// SSE event name emitted on session-connect (`event: init`).
210pub const MCP_STREAM_EVENT_INIT_NAME: &str = "init";
211/// SSE event name carrying a JSON-RPC `notifications/message` payload.
212pub const MCP_STREAM_EVENT_MESSAGE_NAME: &str = "message";
213/// SSE event name carrying a JSON-RPC `notifications/progress` payload.
214pub const MCP_STREAM_EVENT_PROGRESS_NAME: &str = "progress";
215/// SSE event name emitted when a subscriber lags past the buffer.
216pub const MCP_STREAM_EVENT_LAGGED_NAME: &str = "lagged";
217/// SSE event name emitted on the periodic heartbeat tick.
218pub const MCP_STREAM_EVENT_HEARTBEAT_NAME: &str = "heartbeat";
219
220impl McpEventKind {
221    /// Wire-format `event:` string. Kept in lock-step with the
222    /// constants above so the GET handler + clients agree on the
223    /// literal.
224    pub fn as_str(&self) -> &'static str {
225        match self {
226            McpEventKind::Init => MCP_STREAM_EVENT_INIT_NAME,
227            McpEventKind::Message => MCP_STREAM_EVENT_MESSAGE_NAME,
228            McpEventKind::Progress => MCP_STREAM_EVENT_PROGRESS_NAME,
229            McpEventKind::Lagged => MCP_STREAM_EVENT_LAGGED_NAME,
230            McpEventKind::Heartbeat => MCP_STREAM_EVENT_HEARTBEAT_NAME,
231        }
232    }
233}
234
235/// One event on a session's SSE stream. Cloneable so the broadcast
236/// channel can fan out one event to N concurrent subscribers.
237///
238/// The `id` is monotonic per session — clients carry the last seen id
239/// in a `Last-Event-ID` header on reconnect to request a replay of
240/// missed events. Heartbeats CARRY ids too (no second id space) so a
241/// reconnecting client never sees a gap.
242#[derive(Debug, Clone)]
243pub struct McpStreamEvent {
244    /// Monotonic per-session event id. First event is `1` (the init
245    /// event); `0` is reserved as the sentinel "I have never seen
246    /// anything" value clients send on first connect.
247    pub id: u64,
248    /// Wire-event discriminator (`init` / `message` / `progress` /
249    /// `lagged` / `heartbeat`).
250    pub event: McpEventKind,
251    /// JSON payload. For `message` / `progress` this is the full
252    /// JSON-RPC envelope minus the transport `id`. For `init` /
253    /// `lagged` / `heartbeat` it's an event-specific Solo-shaped
254    /// object documented at each call site.
255    pub data: serde_json::Value,
256}
257
258/// One session's state. v0.11.0 P2 grows this from P1's minimal
259/// "tenant + timestamps" record by adding the broadcast event channel
260/// + monotonic event-id counter the resumable GET stream rides on.
261///
262/// **Not `Clone`** — atomics + `broadcast::Sender` make `Clone` a
263/// surprising contract. The store hands out `Arc<SessionState>` so
264/// concurrent requests observe each other's `touch()` calls + share
265/// one event channel. Callers that want a snapshot of the timestamps
266/// can read them via the public fields directly.
267#[derive(Debug)]
268pub struct SessionState {
269    /// Tenant the session is bound to. Set on session create from the
270    /// extractor-resolved tenant; a future priority will refuse to
271    /// reuse a session under a different tenant.
272    pub tenant_id: TenantId,
273    /// Authenticated principal at session create time. `None` for
274    /// unauthenticated loopback deployments (the daemon default).
275    /// A future cross-principal access check uses this to refuse a
276    /// session presented with a different bearer / OIDC subject.
277    pub principal: Option<AuthenticatedPrincipal>,
278    /// Wall-clock millis at session create. Compared against
279    /// `MCP_SESSION_ABSOLUTE_TTL_MS`.
280    pub created_at_ms: i64,
281    /// Wall-clock millis updated on every successful `SessionStore::get`.
282    /// Compared against `MCP_SESSION_INACTIVITY_TTL_MS`. Stored as
283    /// `AtomicI64` so reads via `Arc<SessionState>` can refresh without
284    /// re-inserting into the DashMap shard.
285    pub last_accessed_at_ms: std::sync::atomic::AtomicI64,
286    /// v0.11.0 P2: broadcast channel fed by `publish_event`. The GET
287    /// handler subscribes to this on connect; P3 (progress) and P4
288    /// (notifications/message) publish into it. Capacity bounded by
289    /// [`MCP_SESSION_EVENT_BUFFER_CAPACITY`] per Decision E.
290    ///
291    /// Note: `broadcast::channel` does NOT backfill freshly-subscribed
292    /// receivers with previously-sent events. To support the
293    /// `Last-Event-ID` resume contract we also keep a ring buffer
294    /// (`event_replay_buffer`) which the GET handler reads on connect
295    /// before tailing this channel for live events.
296    pub event_tx: broadcast::Sender<McpStreamEvent>,
297    /// v0.11.0 P2: monotonic per-session event id counter. Allocated
298    /// via `fetch_add(1, SeqCst)` from `publish_event`; first event
299    /// has id `1` (id `0` is the "never seen" sentinel clients send on
300    /// the first `Last-Event-ID` header).
301    pub next_event_id: AtomicU64,
302    /// v0.11.0 P2: bounded ring buffer of recent events for
303    /// `Last-Event-ID` replay. Capacity matches the broadcast channel
304    /// ([`MCP_SESSION_EVENT_BUFFER_CAPACITY`]); oldest entry evicted
305    /// on insert past the cap. `std::sync::Mutex` rather than
306    /// `tokio::sync::Mutex` because the critical sections are tiny
307    /// (push one event / clone a Vec out) — no `await` inside the
308    /// lock. Wrapping in `Arc<Mutex<...>>` keeps `SessionState` cheap
309    /// to share across the broadcast subscribers + the publisher.
310    pub event_replay_buffer: Arc<std::sync::Mutex<VecDeque<McpStreamEvent>>>,
311}
312
313impl SessionState {
314    /// Build a fresh session-state record. Used by [`SessionStore::insert`]
315    /// and the session-extractor path. Allocates a fresh broadcast
316    /// channel (capacity [`MCP_SESSION_EVENT_BUFFER_CAPACITY`]) for the
317    /// session's SSE stream and a matching-capacity replay ring buffer.
318    pub fn new(tenant_id: TenantId, principal: Option<AuthenticatedPrincipal>) -> Self {
319        let now_ms = now_ms();
320        let (event_tx, _) = broadcast::channel(MCP_SESSION_EVENT_BUFFER_CAPACITY);
321        let event_replay_buffer = Arc::new(std::sync::Mutex::new(VecDeque::with_capacity(
322            MCP_SESSION_EVENT_BUFFER_CAPACITY,
323        )));
324        Self {
325            tenant_id,
326            principal,
327            created_at_ms: now_ms,
328            last_accessed_at_ms: std::sync::atomic::AtomicI64::new(now_ms),
329            event_tx,
330            // Start at 1 so the first allocated id is `1` — `0` is
331            // reserved for "client has never seen an event" on the
332            // `Last-Event-ID` header.
333            next_event_id: AtomicU64::new(1),
334            event_replay_buffer,
335        }
336    }
337
338    /// True iff this session is past either TTL.
339    fn is_expired(&self, now_ms: i64) -> bool {
340        let absolute_deadline = self.created_at_ms.saturating_add(MCP_SESSION_ABSOLUTE_TTL_MS as i64);
341        if now_ms >= absolute_deadline {
342            return true;
343        }
344        let last = self.last_accessed_at_ms.load(std::sync::atomic::Ordering::Relaxed);
345        let inactivity_deadline = last.saturating_add(MCP_SESSION_INACTIVITY_TTL_MS as i64);
346        now_ms >= inactivity_deadline
347    }
348
349    /// Bump `last_accessed_at_ms` to "now". Called on every successful
350    /// `SessionStore::get`.
351    fn touch(&self) {
352        self.last_accessed_at_ms
353            .store(now_ms(), std::sync::atomic::Ordering::Relaxed);
354    }
355
356    /// Allocate the next event id, construct an [`McpStreamEvent`],
357    /// and (a) push it onto the replay ring buffer + (b) broadcast it
358    /// to every live subscriber. Returns the assigned id so callers
359    /// (P3/P4) can correlate their write with the resulting stream
360    /// entry.
361    ///
362    /// Lossy on the broadcast side by design: if there are no live
363    /// receivers (or every receiver has been dropped)
364    /// `broadcast::Sender::send` returns `Err(SendError)` — the event
365    /// is silently dropped from the live channel but STILL appended
366    /// to the replay buffer so a future subscriber's `Last-Event-ID`
367    /// replay observes it.
368    ///
369    /// Replay buffer is bounded at [`MCP_SESSION_EVENT_BUFFER_CAPACITY`]
370    /// entries; pushing past the cap evicts the oldest entry. This
371    /// matches the broadcast channel's capacity so the two stay in
372    /// lock-step — a subscriber that subscribed before any events
373    /// were published and then lags past 256 events sees the same
374    /// "buffer overrun" semantics whether it observes them via the
375    /// broadcast lagged-error path or via a `Last-Event-ID` resume.
376    pub fn publish_event(&self, kind: McpEventKind, data: serde_json::Value) -> u64 {
377        let id = self
378            .next_event_id
379            .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
380        let event = McpStreamEvent {
381            id,
382            event: kind,
383            data,
384        };
385        // Push to the replay buffer first — guarantees a subscriber
386        // that races a publish-then-subscribe sequence either sees
387        // the event via the buffer snapshot or via the broadcast
388        // channel (never neither).
389        if let Ok(mut buf) = self.event_replay_buffer.lock() {
390            if buf.len() >= MCP_SESSION_EVENT_BUFFER_CAPACITY {
391                buf.pop_front();
392            }
393            buf.push_back(event.clone());
394        }
395        // Ignore the result: `SendError` means "no live receivers",
396        // which is fine — sessions can exist without an open GET
397        // stream, and the replay buffer above carries forward.
398        let _ = self.event_tx.send(event);
399        id
400    }
401
402    /// Subscribe to the session's event stream. Returns a fresh
403    /// `broadcast::Receiver` that observes every event published from
404    /// this call forward. Combined with [`SessionState::snapshot_replay_buffer`]
405    /// + `Last-Event-ID` replay logic in the GET handler, this gives
406    /// the spec's resume-from-missed-event semantics.
407    pub fn subscribe_events(&self) -> broadcast::Receiver<McpStreamEvent> {
408        self.event_tx.subscribe()
409    }
410
411    /// Snapshot the current replay buffer. Returns a `Vec<McpStreamEvent>`
412    /// in monotonically increasing id order. The GET handler calls
413    /// this once on connect AFTER calling [`Self::subscribe_events`]
414    /// (so any event published during the snapshot lands in the live
415    /// receiver — the handler dedupes the overlap by id).
416    pub fn snapshot_replay_buffer(&self) -> Vec<McpStreamEvent> {
417        match self.event_replay_buffer.lock() {
418            Ok(buf) => buf.iter().cloned().collect(),
419            // Poisoned lock: return empty rather than panicking. The
420            // GET handler treats this as "no buffered events" which
421            // is harmless — the subscriber falls through to the live
422            // broadcast receiver.
423            Err(poisoned) => poisoned.into_inner().iter().cloned().collect(),
424        }
425    }
426}
427
428/// In-memory, lock-free session store keyed by [`SessionId`]. The
429/// `Arc<SessionState>` value lets the middleware hand a cheap clone to
430/// each request without holding a `DashMap` shard lock for the whole
431/// dispatch.
432///
433/// Cloning a `SessionStore` is cheap — internally it's an
434/// `Arc<Inner>`. Pass a clone to the per-process `SoloHttpState`; the
435/// background sweep task holds another clone via `Arc::downgrade`.
436#[derive(Clone)]
437pub struct SessionStore {
438    inner: Arc<SessionStoreInner>,
439}
440
441struct SessionStoreInner {
442    sessions: DashMap<SessionId, Arc<SessionState>>,
443    /// Sweep task handle. Held so it can be aborted when the last
444    /// store reference drops. Wrapped in `Mutex` so `new` and `Drop`
445    /// can both touch it; never contended on the hot path.
446    sweep_task: std::sync::Mutex<Option<tokio::task::JoinHandle<()>>>,
447}
448
449impl SessionStore {
450    /// Build a fresh store and spawn the background sweep task on the
451    /// current tokio runtime. Panics if called outside a tokio runtime
452    /// context — callers should construct this from inside an `async`
453    /// context (e.g. the daemon's startup, or
454    /// `tokio::runtime::Handle::current()`).
455    pub fn new() -> Self {
456        let inner = Arc::new(SessionStoreInner {
457            sessions: DashMap::new(),
458            sweep_task: std::sync::Mutex::new(None),
459        });
460        let weak = Arc::downgrade(&inner);
461        let sweep = tokio::spawn(async move {
462            let mut tick = tokio::time::interval(Duration::from_secs(
463                MCP_SESSION_SWEEP_INTERVAL_SECS,
464            ));
465            // Skip the immediate first tick — interval::tick fires once
466            // immediately by default which would sweep a freshly-empty
467            // store on startup (harmless but noisy in tests).
468            tick.tick().await;
469            loop {
470                tick.tick().await;
471                let Some(inner) = weak.upgrade() else {
472                    // Store dropped; exit the loop. The `Drop` impl
473                    // aborts us anyway; this is the cooperative path
474                    // for graceful shutdown.
475                    return;
476                };
477                sweep_once(&inner.sessions);
478            }
479        });
480        *inner.sweep_task.lock().expect("sweep_task mutex poisoned") = Some(sweep);
481        Self { inner }
482    }
483
484    /// Build a store WITHOUT a background sweep task. Used by tests that
485    /// run outside a tokio runtime context or want to drive sweep
486    /// manually via [`Self::sweep_now`].
487    #[cfg(test)]
488    pub(crate) fn new_for_tests_no_sweep() -> Self {
489        let inner = Arc::new(SessionStoreInner {
490            sessions: DashMap::new(),
491            sweep_task: std::sync::Mutex::new(None),
492        });
493        Self { inner }
494    }
495
496    /// Insert a new session and return its assigned id. Each call
497    /// produces a fresh server-assigned [`SessionId`] (UUID v7).
498    pub fn insert(&self, state: SessionState) -> SessionId {
499        let id = SessionId::new();
500        self.inner
501            .sessions
502            .insert(id.clone(), Arc::new(state));
503        id
504    }
505
506    /// Look up a session by id. Returns `None` if absent OR expired —
507    /// expired entries are removed lazily here so the store doesn't
508    /// hand out stale state between sweeps. The returned
509    /// `Arc<SessionState>`'s `last_accessed_at_ms` is bumped to "now"
510    /// on a successful hit.
511    pub fn get(&self, id: &SessionId) -> Option<Arc<SessionState>> {
512        let now = now_ms();
513        // Fast path: clone the Arc out of the shard, then check expiry
514        // outside the shard lock. If expired, remove and return None.
515        let cloned = self.inner.sessions.get(id).map(|r| r.clone());
516        let state = cloned?;
517        if state.is_expired(now) {
518            self.inner.sessions.remove(id);
519            return None;
520        }
521        state.touch();
522        Some(state)
523    }
524
525    /// Drop a session by id. Returns `true` if it was present.
526    pub fn delete(&self, id: &SessionId) -> bool {
527        self.inner.sessions.remove(id).is_some()
528    }
529
530    /// Current count of stored sessions. Used by tests + future
531    /// /v1/health-style readiness probes.
532    pub fn len(&self) -> usize {
533        self.inner.sessions.len()
534    }
535
536    /// True if the store has no sessions.
537    pub fn is_empty(&self) -> bool {
538        self.inner.sessions.is_empty()
539    }
540
541    /// Force one immediate sweep. Used by the background task and by
542    /// tests that want deterministic sweep behaviour without waiting
543    /// 60s for the next tick.
544    pub fn sweep_now(&self) {
545        sweep_once(&self.inner.sessions);
546    }
547}
548
549impl Default for SessionStore {
550    fn default() -> Self {
551        Self::new()
552    }
553}
554
555impl Drop for SessionStoreInner {
556    fn drop(&mut self) {
557        if let Ok(mut guard) = self.sweep_task.lock()
558            && let Some(handle) = guard.take()
559        {
560            handle.abort();
561        }
562    }
563}
564
565/// Walk the map once and remove every expired entry. Held outside
566/// `SessionStore::sweep_now` so the background task can call it
567/// against `Arc<SessionStoreInner>` directly without re-borrowing
568/// the cloneable `SessionStore` wrapper.
569fn sweep_once(sessions: &DashMap<SessionId, Arc<SessionState>>) {
570    let now = now_ms();
571    // Collect first to avoid holding shard guards while issuing
572    // removes (DashMap supports `retain` but `retain` blocks readers
573    // shard-by-shard; collect-then-remove keeps the hot path
574    // contention-free).
575    let expired: Vec<SessionId> = sessions
576        .iter()
577        .filter(|entry| entry.value().is_expired(now))
578        .map(|entry| entry.key().clone())
579        .collect();
580    for id in expired {
581        sessions.remove(&id);
582    }
583}
584
585/// Current wall-clock time in milliseconds. Centralised so tests can
586/// hook in later (none of the v0.11.0 P1 tests need to). Returns
587/// `chrono::Utc::now().timestamp_millis()` to match the rest of the
588/// crate's timestamps.
589fn now_ms() -> i64 {
590    chrono::Utc::now().timestamp_millis()
591}
592
593/// Body of the 404 response the middleware returns when a request
594/// presents an unknown / expired `Mcp-Session-Id`. The
595/// `re-initialize` field is the contract for client retry logic:
596/// drop the stale id, POST `/mcp` without the header, capture the
597/// `Mcp-Session-Id` from the response.
598pub const MCP_SESSION_EXPIRED_ERROR: &str = "session_expired";
599
600/// Axum middleware that enforces the `Mcp-Session-Id` contract.
601///
602/// Behaviour:
603///   - **No `Mcp-Session-Id` header** → pass through. The downstream
604///     POST handler treats this as a session-init request and emits
605///     the assigned id in the response header.
606///   - **`Mcp-Session-Id` header present + session in store + not
607///     expired** → attach `Arc<SessionState>` + `SessionId` to the
608///     request extensions and pass through.
609///   - **`Mcp-Session-Id` header present + session unknown OR
610///     expired** → 404 with body `{"error": "session_expired", ...,
611///     "retry": "re-initialize"}`.
612pub async fn mcp_session_middleware(
613    State(store): State<SessionStore>,
614    mut req: Request,
615    next: Next,
616) -> Response {
617    let header_value = req
618        .headers()
619        .get(MCP_SESSION_ID_HEADER)
620        .and_then(|h| h.to_str().ok())
621        .map(|s| s.to_string());
622
623    if let Some(raw) = header_value {
624        let id = match SessionId::parse(&raw) {
625            Some(id) => id,
626            None => return session_expired_response(&raw),
627        };
628        match store.get(&id) {
629            Some(state) => {
630                req.extensions_mut().insert(id);
631                req.extensions_mut().insert(state);
632            }
633            None => return session_expired_response(&raw),
634        }
635    }
636    next.run(req).await
637}
638
639/// 404 + structured body. Browser MCP clients (Anthropic AI SDK's
640/// `experimental_createMCPClient`) read the `error` discriminator to
641/// route into the re-initialize path automatically.
642fn session_expired_response(presented_id: &str) -> Response {
643    let body = axum::Json(serde_json::json!({
644        "error": MCP_SESSION_EXPIRED_ERROR,
645        "status": 404,
646        "message": format!(
647            "Mcp-Session-Id `{presented_id}` is unknown or expired; \
648             re-initialize via POST /mcp without Mcp-Session-Id"
649        ),
650        "retry": "re-initialize",
651    }));
652    (StatusCode::NOT_FOUND, body).into_response()
653}
654
655/// Insert a `Mcp-Session-Id` response header so the client can echo
656/// it back on subsequent requests. Used by the POST handler when it
657/// freshly creates a session on a request that arrived without the
658/// header.
659pub fn set_session_id_header(headers: &mut HeaderMap, id: &SessionId) {
660    // SessionId::new produces a UUID-string which is always ASCII;
661    // `HeaderValue::from_str` is safe. The `expect` documents the
662    // invariant for future maintainers — we'd rather panic in CI than
663    // silently drop the header.
664    let value = HeaderValue::from_str(id.as_str())
665        .expect("SessionId is ASCII-safe (UUID) for HeaderValue");
666    headers.insert(
667        HeaderName::from_static(MCP_SESSION_ID_HEADER),
668        value,
669    );
670}
671
672#[cfg(test)]
673mod tests {
674    use super::*;
675    use std::sync::atomic::Ordering;
676
677    fn fake_tenant() -> TenantId {
678        TenantId::default_tenant()
679    }
680
681    fn fresh_state() -> SessionState {
682        SessionState::new(fake_tenant(), None)
683    }
684
685    #[test]
686    fn session_store_insert_returns_unique_id() {
687        let store = SessionStore::new_for_tests_no_sweep();
688        let id_a = store.insert(fresh_state());
689        let id_b = store.insert(fresh_state());
690        assert_ne!(id_a, id_b, "two inserts must produce distinct ids");
691        assert_eq!(store.len(), 2);
692    }
693
694    #[test]
695    fn session_store_get_returns_state_when_present() {
696        let store = SessionStore::new_for_tests_no_sweep();
697        let id = store.insert(fresh_state());
698        let got = store.get(&id);
699        assert!(got.is_some(), "get must return Some for a just-inserted id");
700        assert_eq!(got.unwrap().tenant_id, fake_tenant());
701    }
702
703    /// Build a state whose `created_at_ms` + `last_accessed_at_ms` are
704    /// both shifted backwards by `delta_ms`. Used by the TTL tests to
705    /// simulate an inactive / aged session without driving the wall
706    /// clock. Mirrors `SessionState::new` for the v0.11.0 P2 broadcast
707    /// channel + replay buffer + event id counter — those fields are
708    /// independent of the timestamp shift.
709    fn aged_state(tenant_id: TenantId, principal: Option<AuthenticatedPrincipal>, delta_ms: i64) -> SessionState {
710        let now = now_ms();
711        let shifted = now.saturating_sub(delta_ms);
712        let mut state = SessionState::new(tenant_id, principal);
713        state.created_at_ms = shifted;
714        state.last_accessed_at_ms.store(shifted, Ordering::Relaxed);
715        state
716    }
717
718    #[test]
719    fn session_store_get_returns_none_when_expired_by_inactivity() {
720        let store = SessionStore::new_for_tests_no_sweep();
721        // Hand-build a state whose `last_accessed_at_ms` is older than
722        // the inactivity TTL.
723        let stale_delta = MCP_SESSION_INACTIVITY_TTL_MS as i64 + 1;
724        let stale = Arc::new(aged_state(fake_tenant(), None, stale_delta));
725        let id = SessionId::new();
726        store.inner.sessions.insert(id.clone(), stale);
727        assert!(
728            store.get(&id).is_none(),
729            "session inactive past TTL must read as expired"
730        );
731        // Lazy expiry also evicts the entry.
732        assert!(
733            store.inner.sessions.get(&id).is_none(),
734            "expired entry must be removed from the underlying map"
735        );
736    }
737
738    #[test]
739    fn session_store_get_returns_none_when_expired_by_absolute_ttl() {
740        let store = SessionStore::new_for_tests_no_sweep();
741        // Created past the absolute TTL but recently touched —
742        // absolute-TTL still wins.
743        let absolute_delta = MCP_SESSION_ABSOLUTE_TTL_MS as i64 + 1;
744        let state = aged_state(fake_tenant(), None, absolute_delta);
745        // Touch back to "now" so only the absolute deadline trips.
746        state.last_accessed_at_ms.store(now_ms(), Ordering::Relaxed);
747        let aged = Arc::new(state);
748        let id = SessionId::new();
749        store.inner.sessions.insert(id.clone(), aged);
750        assert!(
751            store.get(&id).is_none(),
752            "session past absolute TTL must read as expired even when recently touched"
753        );
754    }
755
756    #[test]
757    fn session_store_get_refreshes_last_accessed_on_hit() {
758        let store = SessionStore::new_for_tests_no_sweep();
759        let id = store.insert(fresh_state());
760        let before = store
761            .inner
762            .sessions
763            .get(&id)
764            .unwrap()
765            .last_accessed_at_ms
766            .load(Ordering::Relaxed);
767        // Yield long enough that the millis clock advances.
768        std::thread::sleep(std::time::Duration::from_millis(5));
769        let _ = store.get(&id).expect("session must still be present");
770        let after = store
771            .inner
772            .sessions
773            .get(&id)
774            .unwrap()
775            .last_accessed_at_ms
776            .load(Ordering::Relaxed);
777        assert!(
778            after > before,
779            "get must bump last_accessed_at_ms (before={before}, after={after})"
780        );
781    }
782
783    #[test]
784    fn session_store_delete_returns_true_when_present() {
785        let store = SessionStore::new_for_tests_no_sweep();
786        let id = store.insert(fresh_state());
787        assert!(store.delete(&id));
788        assert!(store.get(&id).is_none(), "deleted session must not read");
789    }
790
791    #[test]
792    fn session_store_delete_returns_false_when_absent() {
793        let store = SessionStore::new_for_tests_no_sweep();
794        assert!(!store.delete(&SessionId::new()));
795    }
796
797    #[test]
798    fn session_store_sweep_now_removes_expired() {
799        let store = SessionStore::new_for_tests_no_sweep();
800        // One healthy, one stale.
801        let healthy_id = store.insert(fresh_state());
802        let stale_delta = MCP_SESSION_INACTIVITY_TTL_MS as i64 + 1;
803        let stale = Arc::new(aged_state(fake_tenant(), None, stale_delta));
804        let stale_id = SessionId::new();
805        store.inner.sessions.insert(stale_id.clone(), stale);
806        assert_eq!(store.len(), 2);
807        store.sweep_now();
808        assert_eq!(store.len(), 1, "sweep must drop the expired session");
809        assert!(
810            store.get(&healthy_id).is_some(),
811            "sweep must preserve the healthy session"
812        );
813        assert!(
814            store.inner.sessions.get(&stale_id).is_none(),
815            "stale id must be gone from the map after sweep"
816        );
817    }
818
819    #[tokio::test]
820    async fn session_store_background_sweep_removes_expired() {
821        // Spawn a store with a real sweep task on the current rt.
822        let store = SessionStore::new();
823        // Seed a stale entry directly into the inner map.
824        let stale_delta = MCP_SESSION_INACTIVITY_TTL_MS as i64 + 1;
825        let stale = Arc::new(aged_state(fake_tenant(), None, stale_delta));
826        let stale_id = SessionId::new();
827        store.inner.sessions.insert(stale_id.clone(), stale);
828        // Don't wait 60s; just call sweep_now to prove the same code
829        // path the background task drives works. The 60s-cadence
830        // background task itself is exercised by Drop semantics +
831        // the explicit `sweep_once` unit test above.
832        store.sweep_now();
833        assert!(store.inner.sessions.get(&stale_id).is_none());
834    }
835
836    #[test]
837    fn session_id_round_trips_through_string() {
838        let id = SessionId::new();
839        let s = id.as_str().to_string();
840        let parsed = SessionId::parse(&s).expect("ASCII round-trip");
841        assert_eq!(id, parsed);
842    }
843
844    #[test]
845    fn session_id_parse_rejects_empty_string() {
846        assert!(SessionId::parse("").is_none());
847        assert!(SessionId::parse("   ").is_none());
848    }
849
850    // ----------------------------------------------------------------
851    // v0.11.0 P2 — event buffer + publish_event unit tests
852    // ----------------------------------------------------------------
853
854    /// First three `publish_event` calls allocate ids 1, 2, 3 in order.
855    /// Pins the "start-at-1, monotonic increment" contract; clients
856    /// rely on a 0-sentinel for `Last-Event-ID` ("never seen anything")
857    /// so the first allocated id MUST be ≥ 1.
858    #[test]
859    fn session_state_publish_event_returns_monotonic_ids() {
860        let state = fresh_state();
861        let id1 = state.publish_event(McpEventKind::Init, serde_json::json!({"connected": true}));
862        let id2 = state.publish_event(McpEventKind::Message, serde_json::json!({"hello": 1}));
863        let id3 = state.publish_event(McpEventKind::Progress, serde_json::json!({"progress": 5}));
864        assert_eq!(id1, 1, "first event must allocate id 1 (id 0 reserved for client sentinel)");
865        assert_eq!(id2, 2);
866        assert_eq!(id3, 3);
867    }
868
869    /// A subscriber that called `subscribe_events()` BEFORE the publish
870    /// observes the published event on its receiver. Pins the
871    /// broadcast wiring end-to-end.
872    #[tokio::test]
873    async fn session_state_publish_event_broadcasts_to_subscribers() {
874        let state = fresh_state();
875        let mut rx = state.subscribe_events();
876        let id = state.publish_event(
877            McpEventKind::Message,
878            serde_json::json!({"jsonrpc": "2.0", "method": "notifications/message"}),
879        );
880        let received = rx.recv().await.expect("subscriber must observe the broadcast event");
881        assert_eq!(received.id, id);
882        assert_eq!(received.event, McpEventKind::Message);
883        assert_eq!(received.data["method"], "notifications/message");
884    }
885
886    /// Publishing past the broadcast channel's capacity (256) and the
887    /// matching ring buffer's capacity:
888    ///
889    ///   - The replay buffer retains only the last 256 events (oldest
890    ///     evicted on every push past the cap).
891    ///   - A receiver that subscribed before the publishes observes
892    ///     either every event or a `RecvError::Lagged` followed by the
893    ///     tail of the events. We assert the buffer-only side here
894    ///     (deterministic) — the receiver behaviour is covered by the
895    ///     GET-handler integration test.
896    #[test]
897    fn session_state_event_buffer_capacity_256() {
898        let state = fresh_state();
899        let total = (MCP_SESSION_EVENT_BUFFER_CAPACITY + 50) as u64; // 306
900        for _ in 0..total {
901            state.publish_event(McpEventKind::Message, serde_json::json!({}));
902        }
903        let snapshot = state.snapshot_replay_buffer();
904        assert_eq!(
905            snapshot.len(),
906            MCP_SESSION_EVENT_BUFFER_CAPACITY,
907            "replay buffer must retain exactly {} entries after overflow",
908            MCP_SESSION_EVENT_BUFFER_CAPACITY,
909        );
910        // Oldest retained id = total - capacity + 1 (since ids start at 1
911        // and we published `total` events).
912        let expected_first_id = total - MCP_SESSION_EVENT_BUFFER_CAPACITY as u64 + 1;
913        let expected_last_id = total; // last allocated id
914        assert_eq!(
915            snapshot.first().unwrap().id,
916            expected_first_id,
917            "oldest retained event id must be {expected_first_id}",
918        );
919        assert_eq!(
920            snapshot.last().unwrap().id,
921            expected_last_id,
922            "newest retained event id must be {expected_last_id}",
923        );
924        // Buffer is contiguous (each id is previous + 1).
925        for win in snapshot.windows(2) {
926            assert_eq!(
927                win[1].id,
928                win[0].id + 1,
929                "replay buffer must be contiguous (no gaps)",
930            );
931        }
932    }
933
934    /// `publish_event` with no live receivers does NOT error — the
935    /// event still lands in the replay buffer so a future subscriber
936    /// observes it via the `Last-Event-ID` replay path.
937    #[test]
938    fn session_state_publish_event_no_subscribers_is_lossless_to_buffer() {
939        let state = fresh_state();
940        let id = state.publish_event(McpEventKind::Init, serde_json::json!({"hi": true}));
941        let snapshot = state.snapshot_replay_buffer();
942        assert_eq!(snapshot.len(), 1);
943        assert_eq!(snapshot[0].id, id);
944    }
945}