Skip to content

Commit f8a275a

Browse files
committed
feat: add logs
1 parent 6817c87 commit f8a275a

File tree

1 file changed

+66
-11
lines changed

1 file changed

+66
-11
lines changed

src/versioned-server.ts

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export async function runVersionedSSECloudServer() {
119119
enrichedBody.params._meta.apiKey = apiKey;
120120
}
121121

122-
console.log(`[V1] Message received for API key: ${apiKey}`);
122+
// session-aware logging will be emitted after sessionId resolution
123123

124124
// Prefer explicit sessionId from query, then common header names
125125
const rawSessionId =
@@ -128,6 +128,10 @@ export async function runVersionedSSECloudServer() {
128128
(req.headers['x-mcp-session-id'] as string) ||
129129
'';
130130

131+
console.log(
132+
`[V1][sid:${rawSessionId || 'unknown'}] Message received for API key: ${apiKey}`
133+
);
134+
131135
let compositeKey = `${apiKey}-${rawSessionId}`;
132136
let versionedTransport = transports[compositeKey];
133137

@@ -139,7 +143,7 @@ export async function runVersionedSSECloudServer() {
139143
if (candidates.length === 1) {
140144
const [fallbackKey, vt] = candidates[0];
141145
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}`
143147
);
144148
compositeKey = fallbackKey;
145149
versionedTransport = vt;
@@ -153,7 +157,9 @@ export async function runVersionedSSECloudServer() {
153157
enrichedBody
154158
);
155159
} 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+
);
157163
res.status(400).json({
158164
error: 'No V1 transport found for sessionId',
159165
});
@@ -184,9 +190,10 @@ export async function runVersionedSSECloudServer() {
184190
enrichedBody.params._meta.apiKey = apiKey;
185191
}
186192

187-
console.log(`[V2] Message received for API key: ${apiKey}`);
188-
189193
const sessionId = req.query.sessionId as string;
194+
console.log(
195+
`[V2][sid:${sessionId || 'unknown'}] Message received for API key: ${apiKey}`
196+
);
190197
const compositeKey = `${apiKey}-${sessionId}`;
191198
const versionedTransport = transports[compositeKey];
192199

@@ -197,7 +204,9 @@ export async function runVersionedSSECloudServer() {
197204
enrichedBody
198205
);
199206
} 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+
);
201210
res.status(400).json({
202211
error: 'No V2 transport found for sessionId',
203212
});
@@ -227,9 +236,11 @@ export async function runVersionedSSECloudServer() {
227236
(req.headers['x-mcp-session-id'] as string) ||
228237
'';
229238

239+
const sidLog = existingSessionId || 'init';
240+
console.log(`[V1][HTTP][sid:${sidLog}] Route entered`);
230241
if (existingSessionId) {
231242
console.log(
232-
`[V1][HTTP] Incoming ${req.method} for session ${existingSessionId}`
243+
`[V1][HTTP][sid:${existingSessionId}] Incoming ${req.method}`
233244
);
234245
}
235246

@@ -253,7 +264,7 @@ export async function runVersionedSSECloudServer() {
253264
sessionIdGenerator: () => randomUUID(),
254265
onsessioninitialized: (sid: string) => {
255266
httpTransports[sid] = { transport, version: 'v1', apiKey };
256-
console.log(`[V1][HTTP] Initialized session ${sid}`);
267+
console.log(`[V1][HTTP][sid:${sid}] Initialized session`);
257268
},
258269
});
259270

@@ -262,19 +273,39 @@ export async function runVersionedSSECloudServer() {
262273
if (sid && httpTransports[sid]) delete httpTransports[sid];
263274
};
264275

276+
console.log('[V1][HTTP][sid:init] Connecting transport to server');
265277
await v1Server.connect(transport);
278+
const t1 = Date.now();
279+
console.log(
280+
'[V1][HTTP][sid:init] Calling handleRequest for initialize'
281+
);
266282
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+
);
267286
return;
268287
}
269288

270289
// No session found and not initialize
290+
console.error(
291+
`[V1][HTTP][sid:${existingSessionId || 'unknown'}] Invalid or missing session ID`
292+
);
271293
res.status(400).json({
272294
jsonrpc: '2.0',
273295
error: { code: -32000, message: 'Invalid or missing session ID' },
274296
id: body?.id ?? null,
275297
});
276298
} catch (error) {
277299
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+
);
278309
res.status(500).json({
279310
jsonrpc: '2.0',
280311
error: { code: -32603, message: 'Internal server error' },
@@ -306,10 +337,11 @@ export async function runVersionedSSECloudServer() {
306337
(req.headers['mcp-session-id'] as string) ||
307338
(req.headers['x-mcp-session-id'] as string) ||
308339
'';
309-
340+
const sidLogV2 = existingSessionId || 'init';
341+
console.log(`[V2][HTTP][sid:${sidLogV2}] Route entered`);
310342
if (existingSessionId) {
311343
console.log(
312-
`[V2][HTTP] Incoming ${req.method} for session ${existingSessionId}`
344+
`[V2][HTTP][sid:${existingSessionId}] Incoming ${req.method}`
313345
);
314346
}
315347

@@ -319,21 +351,29 @@ export async function runVersionedSSECloudServer() {
319351
httpTransports[existingSessionId].version === 'v2' &&
320352
httpTransports[existingSessionId].apiKey === apiKey
321353
) {
354+
console.log(
355+
`[V2][HTTP][sid:${existingSessionId}] Delegating to existing transport`
356+
);
357+
const t0 = Date.now();
322358
await httpTransports[existingSessionId].transport.handleRequest(
323359
req,
324360
res,
325361
body
326362
);
363+
console.log(
364+
`[V2][HTTP][sid:${existingSessionId}] handleRequest (existing) completed in ${Date.now() - t0}ms`
365+
);
327366
return;
328367
}
329368

330369
// Create new streamable transport on initialize
331370
if (body && body.method === 'initialize') {
371+
console.log('[V2][HTTP][sid:init] Initializing new session');
332372
const transport = new StreamableHTTPServerTransport({
333373
sessionIdGenerator: () => randomUUID(),
334374
onsessioninitialized: (sid: string) => {
335375
httpTransports[sid] = { transport, version: 'v2', apiKey };
336-
console.log(`[V2][HTTP] Initialized session ${sid}`);
376+
console.log(`[V2][HTTP][sid:${sid}] Initialized session`);
337377
},
338378
});
339379

@@ -342,19 +382,34 @@ export async function runVersionedSSECloudServer() {
342382
if (sid && httpTransports[sid]) delete httpTransports[sid];
343383
};
344384

385+
console.log('[V2][HTTP][sid:init] Connecting transport to server');
345386
await v2Server.connect(transport);
387+
const t1 = Date.now();
388+
console.log(
389+
'[V2][HTTP][sid:init] Calling handleRequest for initialize'
390+
);
346391
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+
);
347395
return;
348396
}
349397

350398
// 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+
);
351402
res.status(400).json({
352403
jsonrpc: '2.0',
353404
error: { code: -32000, message: 'Invalid or missing session ID' },
354405
id: body?.id ?? null,
355406
});
356407
} catch (error) {
357408
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+
);
358413
res.status(500).json({
359414
jsonrpc: '2.0',
360415
error: { code: -32603, message: 'Internal server error' },

0 commit comments

Comments
 (0)