@@ -119,7 +119,7 @@ export async function runVersionedSSECloudServer() {
119
119
enrichedBody . params . _meta . apiKey = apiKey ;
120
120
}
121
121
122
- console . log ( `[V1] Message received for API key: ${ apiKey } ` ) ;
122
+ // session-aware logging will be emitted after sessionId resolution
123
123
124
124
// Prefer explicit sessionId from query, then common header names
125
125
const rawSessionId =
@@ -128,6 +128,10 @@ export async function runVersionedSSECloudServer() {
128
128
( req . headers [ 'x-mcp-session-id' ] as string ) ||
129
129
'' ;
130
130
131
+ console . log (
132
+ `[V1][sid:${ rawSessionId || 'unknown' } ] Message received for API key: ${ apiKey } `
133
+ ) ;
134
+
131
135
let compositeKey = `${ apiKey } -${ rawSessionId } ` ;
132
136
let versionedTransport = transports [ compositeKey ] ;
133
137
@@ -139,7 +143,7 @@ export async function runVersionedSSECloudServer() {
139
143
if ( candidates . length === 1 ) {
140
144
const [ fallbackKey , vt ] = candidates [ 0 ] ;
141
145
console . warn (
142
- `[V1] sessionId not provided or not found. Falling back to single active transport: ${ fallbackKey } `
146
+ `[V1][sid: ${ rawSessionId || 'unknown' } ] sessionId not provided or not found. Falling back to single active transport: ${ fallbackKey } `
143
147
) ;
144
148
compositeKey = fallbackKey ;
145
149
versionedTransport = vt ;
@@ -153,7 +157,9 @@ export async function runVersionedSSECloudServer() {
153
157
enrichedBody
154
158
) ;
155
159
} else {
156
- console . error ( `[V1] No transport found for sessionId: ${ compositeKey } ` ) ;
160
+ console . error (
161
+ `[V1][sid:${ rawSessionId || 'unknown' } ] No transport found for sessionId: ${ compositeKey } `
162
+ ) ;
157
163
res . status ( 400 ) . json ( {
158
164
error : 'No V1 transport found for sessionId' ,
159
165
} ) ;
@@ -184,9 +190,10 @@ export async function runVersionedSSECloudServer() {
184
190
enrichedBody . params . _meta . apiKey = apiKey ;
185
191
}
186
192
187
- console . log ( `[V2] Message received for API key: ${ apiKey } ` ) ;
188
-
189
193
const sessionId = req . query . sessionId as string ;
194
+ console . log (
195
+ `[V2][sid:${ sessionId || 'unknown' } ] Message received for API key: ${ apiKey } `
196
+ ) ;
190
197
const compositeKey = `${ apiKey } -${ sessionId } ` ;
191
198
const versionedTransport = transports [ compositeKey ] ;
192
199
@@ -197,7 +204,9 @@ export async function runVersionedSSECloudServer() {
197
204
enrichedBody
198
205
) ;
199
206
} else {
200
- console . error ( `[V2] No transport found for sessionId: ${ compositeKey } ` ) ;
207
+ console . error (
208
+ `[V2][sid:${ sessionId || 'unknown' } ] No transport found for sessionId: ${ compositeKey } `
209
+ ) ;
201
210
res . status ( 400 ) . json ( {
202
211
error : 'No V2 transport found for sessionId' ,
203
212
} ) ;
@@ -227,9 +236,11 @@ export async function runVersionedSSECloudServer() {
227
236
( req . headers [ 'x-mcp-session-id' ] as string ) ||
228
237
'' ;
229
238
239
+ const sidLog = existingSessionId || 'init' ;
240
+ console . log ( `[V1][HTTP][sid:${ sidLog } ] Route entered` ) ;
230
241
if ( existingSessionId ) {
231
242
console . log (
232
- `[V1][HTTP] Incoming ${ req . method } for session ${ existingSessionId } `
243
+ `[V1][HTTP][sid: ${ existingSessionId } ] Incoming ${ req . method } `
233
244
) ;
234
245
}
235
246
@@ -253,7 +264,7 @@ export async function runVersionedSSECloudServer() {
253
264
sessionIdGenerator : ( ) => randomUUID ( ) ,
254
265
onsessioninitialized : ( sid : string ) => {
255
266
httpTransports [ sid ] = { transport, version : 'v1' , apiKey } ;
256
- console . log ( `[V1][HTTP] Initialized session ${ sid } ` ) ;
267
+ console . log ( `[V1][HTTP][sid: ${ sid } ] Initialized session ` ) ;
257
268
} ,
258
269
} ) ;
259
270
@@ -262,19 +273,39 @@ export async function runVersionedSSECloudServer() {
262
273
if ( sid && httpTransports [ sid ] ) delete httpTransports [ sid ] ;
263
274
} ;
264
275
276
+ console . log ( '[V1][HTTP][sid:init] Connecting transport to server' ) ;
265
277
await v1Server . connect ( transport ) ;
278
+ const t1 = Date . now ( ) ;
279
+ console . log (
280
+ '[V1][HTTP][sid:init] Calling handleRequest for initialize'
281
+ ) ;
266
282
await transport . handleRequest ( req , res , body ) ;
283
+ console . log (
284
+ `[V1][HTTP][sid:${ transport . sessionId || 'unknown' } ] handleRequest (initialize) completed in ${ Date . now ( ) - t1 } ms`
285
+ ) ;
267
286
return ;
268
287
}
269
288
270
289
// No session found and not initialize
290
+ console . error (
291
+ `[V1][HTTP][sid:${ existingSessionId || 'unknown' } ] Invalid or missing session ID`
292
+ ) ;
271
293
res . status ( 400 ) . json ( {
272
294
jsonrpc : '2.0' ,
273
295
error : { code : - 32000 , message : 'Invalid or missing session ID' } ,
274
296
id : body ?. id ?? null ,
275
297
} ) ;
276
298
} catch ( error ) {
277
299
if ( ! res . headersSent ) {
300
+ const sidForErr =
301
+ ( req . query . sessionId as string ) ||
302
+ ( req . headers [ 'mcp-session-id' ] as string ) ||
303
+ ( req . headers [ 'x-mcp-session-id' ] as string ) ||
304
+ 'unknown' ;
305
+ console . error (
306
+ `[V1][HTTP][sid:${ sidForErr } ] Internal server error` ,
307
+ error
308
+ ) ;
278
309
res . status ( 500 ) . json ( {
279
310
jsonrpc : '2.0' ,
280
311
error : { code : - 32603 , message : 'Internal server error' } ,
@@ -306,10 +337,11 @@ export async function runVersionedSSECloudServer() {
306
337
( req . headers [ 'mcp-session-id' ] as string ) ||
307
338
( req . headers [ 'x-mcp-session-id' ] as string ) ||
308
339
'' ;
309
-
340
+ const sidLogV2 = existingSessionId || 'init' ;
341
+ console . log ( `[V2][HTTP][sid:${ sidLogV2 } ] Route entered` ) ;
310
342
if ( existingSessionId ) {
311
343
console . log (
312
- `[V2][HTTP] Incoming ${ req . method } for session ${ existingSessionId } `
344
+ `[V2][HTTP][sid: ${ existingSessionId } ] Incoming ${ req . method } `
313
345
) ;
314
346
}
315
347
@@ -319,21 +351,29 @@ export async function runVersionedSSECloudServer() {
319
351
httpTransports [ existingSessionId ] . version === 'v2' &&
320
352
httpTransports [ existingSessionId ] . apiKey === apiKey
321
353
) {
354
+ console . log (
355
+ `[V2][HTTP][sid:${ existingSessionId } ] Delegating to existing transport`
356
+ ) ;
357
+ const t0 = Date . now ( ) ;
322
358
await httpTransports [ existingSessionId ] . transport . handleRequest (
323
359
req ,
324
360
res ,
325
361
body
326
362
) ;
363
+ console . log (
364
+ `[V2][HTTP][sid:${ existingSessionId } ] handleRequest (existing) completed in ${ Date . now ( ) - t0 } ms`
365
+ ) ;
327
366
return ;
328
367
}
329
368
330
369
// Create new streamable transport on initialize
331
370
if ( body && body . method === 'initialize' ) {
371
+ console . log ( '[V2][HTTP][sid:init] Initializing new session' ) ;
332
372
const transport = new StreamableHTTPServerTransport ( {
333
373
sessionIdGenerator : ( ) => randomUUID ( ) ,
334
374
onsessioninitialized : ( sid : string ) => {
335
375
httpTransports [ sid ] = { transport, version : 'v2' , apiKey } ;
336
- console . log ( `[V2][HTTP] Initialized session ${ sid } ` ) ;
376
+ console . log ( `[V2][HTTP][sid: ${ sid } ] Initialized session ` ) ;
337
377
} ,
338
378
} ) ;
339
379
@@ -342,19 +382,34 @@ export async function runVersionedSSECloudServer() {
342
382
if ( sid && httpTransports [ sid ] ) delete httpTransports [ sid ] ;
343
383
} ;
344
384
385
+ console . log ( '[V2][HTTP][sid:init] Connecting transport to server' ) ;
345
386
await v2Server . connect ( transport ) ;
387
+ const t1 = Date . now ( ) ;
388
+ console . log (
389
+ '[V2][HTTP][sid:init] Calling handleRequest for initialize'
390
+ ) ;
346
391
await transport . handleRequest ( req , res , body ) ;
392
+ console . log (
393
+ `[V2][HTTP][sid:${ transport . sessionId || 'unknown' } ] handleRequest (initialize) completed in ${ Date . now ( ) - t1 } ms`
394
+ ) ;
347
395
return ;
348
396
}
349
397
350
398
// No session found and not initialize
399
+ console . error (
400
+ `[V2][HTTP][sid:${ ( req . headers [ 'mcp-session-id' ] as string ) || ( req . headers [ 'x-mcp-session-id' ] as string ) || 'unknown' } ] Invalid or missing session ID`
401
+ ) ;
351
402
res . status ( 400 ) . json ( {
352
403
jsonrpc : '2.0' ,
353
404
error : { code : - 32000 , message : 'Invalid or missing session ID' } ,
354
405
id : body ?. id ?? null ,
355
406
} ) ;
356
407
} catch ( error ) {
357
408
if ( ! res . headersSent ) {
409
+ console . error (
410
+ `[V2][HTTP][sid:${ ( req . headers [ 'mcp-session-id' ] as string ) || ( req . headers [ 'x-mcp-session-id' ] as string ) || 'unknown' } ] Internal server error` ,
411
+ error
412
+ ) ;
358
413
res . status ( 500 ) . json ( {
359
414
jsonrpc : '2.0' ,
360
415
error : { code : - 32603 , message : 'Internal server error' } ,
0 commit comments