Skip to content

Commit 094574f

Browse files
committed
test(mcp-server): Add tests for span creation with various notification types
1 parent c2f3e82 commit 094574f

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

packages/core/test/lib/mcp-server.test.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,132 @@ describe('wrapMcpServerWithSentry', () => {
376376
);
377377
});
378378

379+
it('should create spans with logging attributes for notifications/message', async () => {
380+
await wrappedMcpServer.connect(mockTransport);
381+
382+
const loggingNotification = {
383+
jsonrpc: '2.0',
384+
method: 'notifications/message',
385+
params: {
386+
level: 'info',
387+
logger: 'math-service',
388+
data: 'Addition completed: 2 + 5 = 7'
389+
}
390+
};
391+
392+
mockTransport.onmessage?.(loggingNotification, {});
393+
394+
expect(startSpanSpy).toHaveBeenCalledWith(
395+
{
396+
name: 'notifications/message logger:math-service',
397+
forceTransaction: true,
398+
attributes: {
399+
'mcp.method.name': 'notifications/message',
400+
'mcp.session.id': 'test-session-123',
401+
'mcp.notification.direction': 'client_to_server',
402+
'mcp.transport': 'http',
403+
'network.transport': 'tcp',
404+
'network.protocol.version': '2.0',
405+
'mcp.logging.level': 'info',
406+
'mcp.logging.logger': 'math-service',
407+
'mcp.logging.data_type': 'string',
408+
'mcp.logging.message': 'Addition completed: 2 + 5 = 7',
409+
'sentry.op': 'mcp.server',
410+
'sentry.origin': 'auto.mcp.notification',
411+
'sentry.source': 'route',
412+
},
413+
},
414+
expect.any(Function)
415+
);
416+
});
417+
418+
it('should create spans with attributes for other notification types', async () => {
419+
await wrappedMcpServer.connect(mockTransport);
420+
421+
// Test notifications/cancelled
422+
const cancelledNotification = {
423+
jsonrpc: '2.0',
424+
method: 'notifications/cancelled',
425+
params: {
426+
requestId: 'req-123',
427+
reason: 'user_requested'
428+
}
429+
};
430+
431+
mockTransport.onmessage?.(cancelledNotification, {});
432+
433+
expect(startSpanSpy).toHaveBeenCalledWith(
434+
expect.objectContaining({
435+
name: 'notifications/cancelled request:req-123',
436+
attributes: expect.objectContaining({
437+
'mcp.method.name': 'notifications/cancelled',
438+
'mcp.cancelled.request_id': 'req-123',
439+
'mcp.cancelled.reason': 'user_requested',
440+
'mcp.notification.direction': 'client_to_server',
441+
}),
442+
}),
443+
expect.any(Function)
444+
);
445+
446+
vi.clearAllMocks();
447+
448+
// Test notifications/progress
449+
const progressNotification = {
450+
jsonrpc: '2.0',
451+
method: 'notifications/progress',
452+
params: {
453+
progressToken: 'token-456',
454+
progress: 75,
455+
total: 100,
456+
message: 'Processing files...'
457+
}
458+
};
459+
460+
mockTransport.onmessage?.(progressNotification, {});
461+
462+
expect(startSpanSpy).toHaveBeenCalledWith(
463+
expect.objectContaining({
464+
name: 'notifications/progress token:token-456',
465+
attributes: expect.objectContaining({
466+
'mcp.method.name': 'notifications/progress',
467+
'mcp.progress.token': 'token-456',
468+
'mcp.progress.current': 75,
469+
'mcp.progress.total': 100,
470+
'mcp.progress.percentage': 75,
471+
'mcp.progress.message': 'Processing files...',
472+
'mcp.notification.direction': 'client_to_server',
473+
}),
474+
}),
475+
expect.any(Function)
476+
);
477+
478+
vi.clearAllMocks();
479+
480+
// Test notifications/resources/updated
481+
const resourceUpdatedNotification = {
482+
jsonrpc: '2.0',
483+
method: 'notifications/resources/updated',
484+
params: {
485+
uri: 'file:///tmp/data.json'
486+
}
487+
};
488+
489+
mockTransport.onmessage?.(resourceUpdatedNotification, {});
490+
491+
expect(startSpanSpy).toHaveBeenCalledWith(
492+
expect.objectContaining({
493+
name: 'notifications/resources/updated file:///tmp/data.json',
494+
attributes: expect.objectContaining({
495+
'mcp.method.name': 'notifications/resources/updated',
496+
'mcp.resource.uri': 'file:///tmp/data.json',
497+
'mcp.resource.protocol': 'file:',
498+
'mcp.notification.direction': 'client_to_server',
499+
}),
500+
}),
501+
expect.any(Function)
502+
);
503+
});
504+
379505

380506
});
381507
});

0 commit comments

Comments
 (0)