@@ -11,6 +11,8 @@ import { PING_SSE_MESSAGE } from "../utils/ping-message.js";
1111import { ProtectedResourceMetadata } from "../../auth/metadata/protected-resource.js" ;
1212import { handleAuthentication } from "../utils/auth-handler.js" ;
1313import { initializeOAuthMetadata } from "../utils/oauth-metadata.js" ;
14+ import { requestContext , RequestContextData } from "../../utils/requestContext.js" ;
15+ import { AuthResult } from "../../auth/types.js" ;
1416
1517interface ExtendedIncomingMessage extends IncomingMessage {
1618 body ?: ClientRequest ;
@@ -167,9 +169,12 @@ export class SSEServerTransport extends AbstractTransport {
167169 }
168170
169171 if ( req . method === "POST" && url . pathname === this . _config . messageEndpoint ) {
172+ let authData : RequestContextData = { } ;
173+
170174 if ( this . _config . auth ?. endpoints ?. messages !== false ) {
171- const isAuthenticated = await handleAuthentication ( req , res , this . _config . auth , "message" )
172- if ( ! isAuthenticated ) return
175+ const authResult = await handleAuthentication ( req , res , this . _config . auth , "message" )
176+ if ( ! authResult ) return
177+ authData = ( authResult as AuthResult ) . data as RequestContextData || { } ;
173178 }
174179
175180 // **Connection Validation (User Requested):**
@@ -183,7 +188,7 @@ export class SSEServerTransport extends AbstractTransport {
183188 return ;
184189 }
185190
186- await this . handlePostMessage ( req , res )
191+ await this . handlePostMessage ( req , res , authData )
187192 return
188193 }
189194
@@ -250,7 +255,7 @@ export class SSEServerTransport extends AbstractTransport {
250255 logger . info ( `SSE connection established successfully: ${ connectionId } ` ) ;
251256 }
252257
253- private async handlePostMessage ( req : IncomingMessage , res : ServerResponse ) : Promise < void > {
258+ private async handlePostMessage ( req : IncomingMessage , res : ServerResponse , authData : RequestContextData = { } ) : Promise < void > {
254259 // Check if *any* connection is active, not just the old single _sseResponse
255260 if ( this . _connections . size === 0 ) {
256261 logger . warn ( `Rejecting message: no active SSE connections for server session ${ this . _sessionId } ` ) ;
@@ -301,7 +306,9 @@ export class SSEServerTransport extends AbstractTransport {
301306 throw new Error ( "No message handler registered" )
302307 }
303308
304- await this . _onmessage ( rpcMessage )
309+ await requestContext . run ( authData , async ( ) => {
310+ await this . _onmessage ! ( rpcMessage )
311+ } )
305312
306313 res . writeHead ( 202 ) . end ( "Accepted" )
307314
0 commit comments