You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(server): add replayInitialization for stateless serverless session adoption
In stateless serverless deployments (Vercel, Lambda, Workers), each HTTP
request creates a fresh Server + Transport. The initialize handshake sets
state (_clientCapabilities, _clientVersion, sessionId, _initialized) that
is lost between requests, forcing developers to use Reflect.set hacks.
Add a `replayInitialization` callback to transport options that restores
session state on non-initialize requests. The transport reads the session
ID from the request header, calls the callback to fetch cached state, and
flows it to the server via a new `oninitialized` callback on the Transport
interface.
- Transport: `replayInitialization` option, `_tryReplayInitialization`
helper called once in `handleRequest` before method dispatch
- Transport: fix `validateSession` to recognize replayed sessions
- Core: `oninitialized` callback on Transport interface (same pattern as
onmessage/onclose/onerror)
- Server: override `connect()` to hook oninitialized, seeding
_clientCapabilities and _clientVersion via ??=
- Node middleware: delegate oninitialized getter/setter to inner transport
- Callback returns undefined → 404 per spec (client re-initializes)
- Callback throws → 500 internal error
- Race condition guard via _replayInProgress flag
Closes#1658, #1882
Copy file name to clipboardExpand all lines: packages/server/src/server/streamableHttp.ts
+82-4Lines changed: 82 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,15 @@
7
7
* For Node.js Express/HTTP compatibility, use {@linkcode @modelcontextprotocol/node!NodeStreamableHTTPServerTransport | NodeStreamableHTTPServerTransport} which wraps this transport.
0 commit comments