Skip to content

Commit 25d23f1

Browse files
committed
Complete protocol session foundation
- Fix session validation to handle all session/sessionless combinations - Update capturedTransport pattern to capture session state - Include sessionId in all response messages using captured state - Update RequestHandlerExtra sessionId type to support SessionId - Remove unused imports from tests Phase 1 complete: Protocol class has full session support
1 parent 02275ef commit 25d23f1

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/shared/protocol-session.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it, expect, jest, beforeEach } from '@jest/globals';
22
import { Protocol, SessionState } from './protocol.js';
3-
import { ErrorCode, JSONRPCRequest, JSONRPCNotification, JSONRPCMessage, Request, Notification, Result, MessageExtraInfo } from '../types.js';
3+
import { ErrorCode, JSONRPCRequest, JSONRPCMessage, Request, Notification, Result, MessageExtraInfo } from '../types.js';
44
import { Transport } from './transport.js';
55

66
// Mock transport for testing

src/shared/protocol.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ export type RequestHandlerExtra<SendRequestT extends Request,
147147
authInfo?: AuthInfo;
148148

149149
/**
150-
* The session ID from the transport, if available.
150+
* The session ID from the protocol session, if available.
151151
*/
152-
sessionId?: string;
152+
sessionId?: SessionId;
153153

154154
/**
155155
* Metadata from the original request.
@@ -480,8 +480,9 @@ export abstract class Protocol<
480480
const handler =
481481
this._requestHandlers.get(request.method) ?? this.fallbackRequestHandler;
482482

483-
// Capture the current transport at request time to ensure responses go to the correct client
483+
// Capture the current transport and session state at request time to ensure responses go to the correct client
484484
const capturedTransport = this._transport;
485+
const capturedSessionState = this._sessionState ? { ...this._sessionState } : undefined;
485486

486487
if (handler === undefined) {
487488
capturedTransport
@@ -492,6 +493,7 @@ export abstract class Protocol<
492493
code: ErrorCode.MethodNotFound,
493494
message: "Method not found",
494495
},
496+
...(capturedSessionState && { sessionId: capturedSessionState.sessionId }),
495497
})
496498
.catch((error) =>
497499
this._onerror(
@@ -506,7 +508,7 @@ export abstract class Protocol<
506508

507509
const fullExtra: RequestHandlerExtra<SendRequestT, SendNotificationT> = {
508510
signal: abortController.signal,
509-
sessionId: capturedTransport?.sessionId,
511+
sessionId: capturedSessionState?.sessionId,
510512
_meta: request.params?._meta,
511513
sendNotification:
512514
(notification) =>
@@ -531,6 +533,7 @@ export abstract class Protocol<
531533
result,
532534
jsonrpc: "2.0",
533535
id: request.id,
536+
...(capturedSessionState && { sessionId: capturedSessionState.sessionId }),
534537
});
535538
},
536539
(error) => {
@@ -547,6 +550,7 @@ export abstract class Protocol<
547550
: ErrorCode.InternalError,
548551
message: error.message ?? "Internal error",
549552
},
553+
...(capturedSessionState && { sessionId: capturedSessionState.sessionId }),
550554
});
551555
},
552556
)

0 commit comments

Comments
 (0)