@@ -241,7 +241,25 @@ export class HttpStreamTransport extends AbstractTransport {
241241 throw this . httpError ( 400 , `Bad Request: ${ e . message } ` , - 32700 , undefined , firstRequestId ) ;
242242 }
243243
244- const isInitialize = parsedMessages . some ( msg => isRequest ( msg ) && msg . method === 'initialize' ) ;
244+ // Find initialize requests and validate batching
245+ const initializeRequests = parsedMessages . filter ( msg => isRequest ( msg ) && msg . method === 'initialize' ) ;
246+
247+ // Check if initialize request is present
248+ if ( initializeRequests . length > 0 ) {
249+ // If we have an initialize request, verify it's not batched with other requests
250+ if ( initializeRequests . length > 1 ) {
251+ logger . error ( "Multiple initialize requests in the same batch" ) ;
252+ throw this . httpError ( 400 , "Bad Request: Multiple initialize requests in the same batch" , - 32600 , undefined , firstRequestId ) ;
253+ }
254+
255+ // If initialize is present and the batch contains more than 1 message, reject it
256+ if ( parsedMessages . length > 1 ) {
257+ logger . error ( "Initialize request cannot be batched with other requests" ) ;
258+ throw this . httpError ( 400 , "Bad Request: Initialize request must not be part of a JSON-RPC batch" , - 32600 , undefined , firstRequestId ) ;
259+ }
260+ }
261+
262+ const isInitialize = initializeRequests . length > 0 ;
245263 const sessionIdHeader = getRequestHeader ( req . headers , this . _config . session . headerName ) ;
246264 let session : SessionData | undefined ;
247265
0 commit comments