@@ -571,7 +571,7 @@ describe('MCP Server Transport Instrumentation', () => {
571571
572572 it ( 'excludes sessionId when undefined' , ( ) => {
573573 const transport = createMockTransport ( ) ;
574- transport . sessionId = undefined ;
574+ transport . sessionId = '' ;
575575 const attributes = buildTransportAttributes ( transport ) ;
576576
577577 expect ( attributes [ 'mcp.session.id' ] ) . toBeUndefined ( ) ;
@@ -584,4 +584,75 @@ describe('MCP Server Transport Instrumentation', () => {
584584 expect ( attributes [ 'mcp.session.id' ] ) . toBeUndefined ( ) ;
585585 } ) ;
586586 } ) ;
587+
588+ describe ( 'Initialize Span Attributes' , ( ) => {
589+ it ( 'should add client info to initialize span on request' , async ( ) => {
590+ const mockMcpServer = createMockMcpServer ( ) ;
591+ const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
592+ const transport = createMockTransport ( ) ;
593+ transport . sessionId = '' ;
594+
595+ await wrappedMcpServer . connect ( transport ) ;
596+
597+ const mockSpan = { setAttributes : vi . fn ( ) , end : vi . fn ( ) } ;
598+ startInactiveSpanSpy . mockReturnValue ( mockSpan ) ;
599+
600+ transport . onmessage ?.(
601+ {
602+ jsonrpc : '2.0' ,
603+ method : 'initialize' ,
604+ id : 'init-1' ,
605+ params : { protocolVersion : '2025-06-18' , clientInfo : { name : 'test-client' , version : '1.0.0' } } ,
606+ } ,
607+ { } ,
608+ ) ;
609+
610+ expect ( mockSpan . setAttributes ) . toHaveBeenCalledWith (
611+ expect . objectContaining ( {
612+ 'mcp.client.name' : 'test-client' ,
613+ 'mcp.client.version' : '1.0.0' ,
614+ 'mcp.protocol.version' : '2025-06-18' ,
615+ } ) ,
616+ ) ;
617+ } ) ;
618+
619+ it ( 'should add server info to initialize span on response' , async ( ) => {
620+ const mockMcpServer = createMockMcpServer ( ) ;
621+ const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
622+ const transport = createMockTransport ( ) ;
623+
624+ await wrappedMcpServer . connect ( transport ) ;
625+
626+ const mockSpan = { setAttributes : vi . fn ( ) , end : vi . fn ( ) } ;
627+ startInactiveSpanSpy . mockReturnValue ( mockSpan as any ) ;
628+
629+ transport . onmessage ?.(
630+ {
631+ jsonrpc : '2.0' ,
632+ method : 'initialize' ,
633+ id : 'init-1' ,
634+ params : { protocolVersion : '2025-06-18' , clientInfo : { name : 'test-client' , version : '1.0.0' } } ,
635+ } ,
636+ { } ,
637+ ) ;
638+
639+ await transport . send ?.( {
640+ jsonrpc : '2.0' ,
641+ id : 'init-1' ,
642+ result : {
643+ protocolVersion : '2025-06-18' ,
644+ serverInfo : { name : 'test-server' , version : '2.0.0' } ,
645+ capabilities : { } ,
646+ } ,
647+ } ) ;
648+
649+ expect ( mockSpan . setAttributes ) . toHaveBeenCalledWith (
650+ expect . objectContaining ( {
651+ 'mcp.server.name' : 'test-server' ,
652+ 'mcp.server.version' : '2.0.0' ,
653+ } ) ,
654+ ) ;
655+ expect ( mockSpan . end ) . toHaveBeenCalled ( ) ;
656+ } ) ;
657+ } ) ;
587658} ) ;
0 commit comments