@@ -26,6 +26,7 @@ describe('wrapMcpServerWithSentry', () => {
2626 resource : mockResource ,
2727 tool : mockTool ,
2828 prompt : mockPrompt ,
29+ connect : vi . fn ( ) ,
2930 } ;
3031
3132 // Wrap the MCP server
@@ -93,11 +94,19 @@ describe('wrapMcpServerWithSentry', () => {
9394 resource : vi . fn ( ) ,
9495 tool : vi . fn ( ) ,
9596 prompt : vi . fn ( ) ,
97+ connect : vi . fn ( ) ,
9698 } ;
9799
98100 const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
99101 wrappedMcpServer . resource ( resourceName , { } , mockResourceHandler ) ;
100102
103+ // The original registration should use a wrapped handler
104+ expect ( mockMcpServer . resource ) . toHaveBeenCalledWith ( resourceName , { } , expect . any ( Function ) ) ;
105+
106+ // Invoke the wrapped handler to trigger Sentry span
107+ const wrappedResourceHandler = ( mockMcpServer . resource as any ) . mock . calls [ 0 ] [ 2 ] ;
108+ wrappedResourceHandler ( 'test-uri' , { foo : 'bar' } ) ;
109+
101110 expect ( tracingModule . startSpan ) . toHaveBeenCalledTimes ( 1 ) ;
102111 expect ( tracingModule . startSpan ) . toHaveBeenCalledWith (
103112 {
@@ -113,15 +122,16 @@ describe('wrapMcpServerWithSentry', () => {
113122 expect . any ( Function ) ,
114123 ) ;
115124
116- // Verify the original method was called with all arguments
117- expect ( mockMcpServer . resource ) . toHaveBeenCalledWith ( resourceName , { } , mockResourceHandler ) ;
125+ // Verify the original handler was called within the span
126+ expect ( mockResourceHandler ) . toHaveBeenCalledWith ( 'test-uri' , { foo : 'bar' } ) ;
118127 } ) ;
119128
120129 it ( 'should call the original resource method directly if name or handler is not valid' , ( ) => {
121130 const mockMcpServer = {
122131 resource : vi . fn ( ) ,
123132 tool : vi . fn ( ) ,
124133 prompt : vi . fn ( ) ,
134+ connect : vi . fn ( ) ,
125135 } ;
126136
127137 const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
@@ -147,11 +157,19 @@ describe('wrapMcpServerWithSentry', () => {
147157 resource : vi . fn ( ) ,
148158 tool : vi . fn ( ) ,
149159 prompt : vi . fn ( ) ,
160+ connect : vi . fn ( ) ,
150161 } ;
151162
152163 const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
153164 wrappedMcpServer . tool ( toolName , { } , mockToolHandler ) ;
154165
166+ // The original registration should use a wrapped handler
167+ expect ( mockMcpServer . tool ) . toHaveBeenCalledWith ( toolName , { } , expect . any ( Function ) ) ;
168+
169+ // Invoke the wrapped handler to trigger Sentry span
170+ const wrappedToolHandler = ( mockMcpServer . tool as any ) . mock . calls [ 0 ] [ 2 ] ;
171+ wrappedToolHandler ( { arg : 'value' } , { foo : 'baz' } ) ;
172+
155173 expect ( tracingModule . startSpan ) . toHaveBeenCalledTimes ( 1 ) ;
156174 expect ( tracingModule . startSpan ) . toHaveBeenCalledWith (
157175 {
@@ -167,15 +185,16 @@ describe('wrapMcpServerWithSentry', () => {
167185 expect . any ( Function ) ,
168186 ) ;
169187
170- // Verify the original method was called with all arguments
171- expect ( mockMcpServer . tool ) . toHaveBeenCalledWith ( toolName , { } , mockToolHandler ) ;
188+ // Verify the original handler was called within the span
189+ expect ( mockToolHandler ) . toHaveBeenCalledWith ( { arg : 'value' } , { foo : 'baz' } ) ;
172190 } ) ;
173191
174192 it ( 'should call the original tool method directly if name or handler is not valid' , ( ) => {
175193 const mockMcpServer = {
176194 resource : vi . fn ( ) ,
177195 tool : vi . fn ( ) ,
178196 prompt : vi . fn ( ) ,
197+ connect : vi . fn ( ) ,
179198 } ;
180199
181200 const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
@@ -198,15 +217,23 @@ describe('wrapMcpServerWithSentry', () => {
198217 resource : vi . fn ( ) ,
199218 tool : vi . fn ( ) ,
200219 prompt : vi . fn ( ) ,
220+ connect : vi . fn ( ) ,
201221 } ;
202222
203223 const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
204224 wrappedMcpServer . prompt ( promptName , { } , mockPromptHandler ) ;
205225
226+ // The original registration should use a wrapped handler
227+ expect ( mockMcpServer . prompt ) . toHaveBeenCalledWith ( promptName , { } , expect . any ( Function ) ) ;
228+
229+ // Invoke the wrapped handler to trigger Sentry span
230+ const wrappedPromptHandler = ( mockMcpServer . prompt as any ) . mock . calls [ 0 ] [ 2 ] ;
231+ wrappedPromptHandler ( { msg : 'hello' } , { data : 123 } ) ;
232+
206233 expect ( tracingModule . startSpan ) . toHaveBeenCalledTimes ( 1 ) ;
207234 expect ( tracingModule . startSpan ) . toHaveBeenCalledWith (
208235 {
209- name : `mcp-server/resource :${ promptName } ` ,
236+ name : `mcp-server/prompt :${ promptName } ` ,
210237 forceTransaction : true ,
211238 attributes : {
212239 [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'auto.function.mcp-server' ,
@@ -218,15 +245,16 @@ describe('wrapMcpServerWithSentry', () => {
218245 expect . any ( Function ) ,
219246 ) ;
220247
221- // Verify the original method was called with all arguments
222- expect ( mockMcpServer . prompt ) . toHaveBeenCalledWith ( promptName , { } , mockPromptHandler ) ;
248+ // Verify the original handler was called within the span
249+ expect ( mockPromptHandler ) . toHaveBeenCalledWith ( { msg : 'hello' } , { data : 123 } ) ;
223250 } ) ;
224251
225252 it ( 'should call the original prompt method directly if name or handler is not valid' , ( ) => {
226253 const mockMcpServer = {
227254 resource : vi . fn ( ) ,
228255 tool : vi . fn ( ) ,
229256 prompt : vi . fn ( ) ,
257+ connect : vi . fn ( ) ,
230258 } ;
231259
232260 const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
0 commit comments