Skip to content

fix(mcp): bind sessions to their principal and reclaim idle ones - #115

Merged
htcom-code merged 1 commit into
mainfrom
fix/mcp-session-binding
Nov 29, 2026
Merged

htcom-code merged 1 commit into
mainfrom
fix/mcp-session-binding

Conversation

@htcom-code

Copy link
Copy Markdown
Owner

Summary

MCP sessions on /platform/mcp used sequential ids, never expired, and were not bound to the principal that created them. This brings McpSessionStore to parity with Protean's McpSessionRegistry / McpHttpController.

  • Random ids — session ids are UUID v4 (uuid::Uuid::new_v4, CSPRNG-backed), as Java's UUID.randomUUID().
  • Principal binding — the initialize request's principal (or none) owns the session. POST/GET/DELETE from any other principal behave exactly as for an unknown session: POST 404 UNKNOWN_SESSION problem, GET bare 404, DELETE 204 with the session kept. An unauthenticated session is usable only by unauthenticated requests (Java Objects.equals(owner, principal)).
  • Idle reclaim — sessions with no owner lookup for 30 minutes (Java protean.mcp.session.timeout default) are removed and their GET stream closed. Reclaim runs on every store access and every 15 s on an open stream (Java's heartbeat cadence). Only an owner's successful lookup counts as activity. Embedders can change it with McpSessionStore::set_session_idle_timeout (Duration::ZERO disables, as <= 0 does in Java).
  • Order — the protocol and session checks now run before a request is registered for notifications/cancelled, as Java validates before dispatch. A blank Mcp-Session-Id is treated as no session, as in Java.
  • stdio — the stdio session is owned by stdio-local, so the HTTP listener sharing the store cannot reach it. The host now sends its EOF DELETE as stdio-local so it still ends the session.
  • No session cap — Protean has none; idle reclaim bounds lifetime (parity).

Java evidence

  • McpSessionRegistry.java:88-92 — create(owner) with UUID.randomUUID()
  • McpSessionRegistry.java:98-105 — get(id, principalName): another owner is indistinguishable from unknown, only a successful lookup touches
  • McpSessionRegistry.java:296-304 — idle sweep terminates sessions (closes the stream)
  • McpHttpController.java:107-113 (POST 404 problem), :165-168 (GET bare 404), :174-182 (DELETE only by owner, always 204)
  • McpConfiguration.java:112 — protean.mcp.session.timeout:30m
  • McpSessionOwnershipHttpTest.java — cross-principal POST/GET 404, DELETE 204 keeps the session, unauthenticated cannot use an owned session

Non-parity (recorded in docs/design/17-mcp-session-and-extension-contract.md §2.1)

The idle timeout is a Spring property in Java, not a ConfigRegistry key, so it is not added to the Protrus config registry; the standalone host uses the 30-minute default and embedders set it through the library API.

Tests

New: mcp_session_ids_are_random_uuids, mcp_another_principal_cannot_use_stream_or_end_the_session, mcp_sessions_are_reclaimed_after_the_idle_timeout. Existing MCP tests updated for the new open signature only.

cargo test --workspace --locked: 407 passed, 0 failed, 6 ignored (38 test binaries, including protrus-host mcp_scenarios).

Closes #113

MCP session ids came from a sequential counter, sessions were never
expired, and POST/GET/DELETE on /platform/mcp only checked that an id
existed. Any caller that could reach the endpoint could guess an id and
read another session's stream and replay buffer or end it, including
the stdio session through the shared HTTP listener.

Follow Protean's McpSessionRegistry and McpHttpController:
- issue random UUID v4 session ids
- bind each session to the principal that initialized it; another
  principal's POST/GET answers 404 as for an unknown session and its
  DELETE is a 204 no-op
- reclaim sessions idle for 30 minutes (only owner lookups count as
  activity), closing their streams; embedders can change the timeout
  with McpSessionStore::set_session_idle_timeout
- validate the session before registering the request for
  cancellation, as Java does before dispatch
- end the stdio session as the stdio-local principal at EOF

Design doc 17 records the timeout config key non-parity.

Closes #113

Tags: #mcp #security #session
Co-Authored-By: htjulia <htjulia1@gmail.com>
@htcom-code
htcom-code merged commit 40ff139 into main Nov 29, 2026
4 checks passed
@htcom-code
htcom-code deleted the fix/mcp-session-binding branch November 29, 2026 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(mcp): bind MCP sessions to their principal and reclaim idle sessions

1 participant