@@ -46,73 +46,53 @@ describe('wrapMcpServerWithSentry', () => {
46
46
} ) ;
47
47
48
48
describe ( 'Transport-level instrumentation' , ( ) => {
49
+ let mockMcpServer : ReturnType < typeof createMockMcpServer > ;
50
+ let wrappedMcpServer : ReturnType < typeof createMockMcpServer > ;
51
+ let mockTransport : ReturnType < typeof createMockTransport > ;
52
+
53
+ beforeEach ( async ( ) => {
54
+ mockMcpServer = createMockMcpServer ( ) ;
55
+ wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
56
+ mockTransport = createMockTransport ( ) ;
57
+
58
+ // Connect the server to transport - this is common to most tests
59
+ await wrappedMcpServer . connect ( mockTransport ) ;
60
+ } ) ;
61
+
49
62
it ( 'should proxy the connect method' , ( ) => {
50
- const mockMcpServer = createMockMcpServer ( ) ;
51
- const originalConnect = mockMcpServer . connect ;
63
+ // We need to test this before connection, so create fresh instances
64
+ const freshMockMcpServer = createMockMcpServer ( ) ;
65
+ const originalConnect = freshMockMcpServer . connect ;
52
66
53
- const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
67
+ const freshWrappedMcpServer = wrapMcpServerWithSentry ( freshMockMcpServer ) ;
54
68
55
- expect ( wrappedMcpServer . connect ) . not . toBe ( originalConnect ) ;
69
+ expect ( freshWrappedMcpServer . connect ) . not . toBe ( originalConnect ) ;
56
70
} ) ;
57
71
58
- it ( 'should intercept transport onmessage handler' , async ( ) => {
59
- const mockMcpServer = createMockMcpServer ( ) ;
60
- const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
61
-
62
- const mockTransport = createMockTransport ( ) ;
72
+ it ( 'should intercept transport onmessage handler' , ( ) => {
63
73
const originalOnMessage = mockTransport . onmessage ;
64
-
65
- await wrappedMcpServer . connect ( mockTransport ) ;
66
-
67
- // onmessage should be wrapped
74
+ // onmessage should be wrapped after connection
68
75
expect ( mockTransport . onmessage ) . not . toBe ( originalOnMessage ) ;
69
76
} ) ;
70
77
71
- it ( 'should intercept transport send handler' , async ( ) => {
72
- const mockMcpServer = createMockMcpServer ( ) ;
73
- const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
74
-
75
- const mockTransport = createMockTransport ( ) ;
78
+ it ( 'should intercept transport send handler' , ( ) => {
76
79
const originalSend = mockTransport . send ;
77
-
78
- await wrappedMcpServer . connect ( mockTransport ) ;
79
-
80
- // send should be wrapped
80
+ // send should be wrapped after connection
81
81
expect ( mockTransport . send ) . not . toBe ( originalSend ) ;
82
82
} ) ;
83
83
84
- it ( 'should intercept transport onclose handler' , async ( ) => {
85
- const mockMcpServer = createMockMcpServer ( ) ;
86
- const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
87
-
88
- const mockTransport = createMockTransport ( ) ;
84
+ it ( 'should intercept transport onclose handler' , ( ) => {
89
85
const originalOnClose = mockTransport . onclose ;
90
-
91
- await wrappedMcpServer . connect ( mockTransport ) ;
92
-
93
- // onclose should be wrapped
86
+ // onclose should be wrapped after connection
94
87
expect ( mockTransport . onclose ) . not . toBe ( originalOnClose ) ;
95
88
} ) ;
96
89
97
- it ( 'should call original connect and preserve functionality' , async ( ) => {
98
- const mockMcpServer = createMockMcpServer ( ) ;
99
- const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
100
-
101
- const mockTransport = createMockTransport ( ) ;
102
-
103
- await wrappedMcpServer . connect ( mockTransport ) ;
104
-
105
- // Original connect should have been called
90
+ it ( 'should call original connect and preserve functionality' , ( ) => {
91
+ // Original connect should have been called during beforeEach
106
92
expect ( mockMcpServer . connect ) . toHaveBeenCalledWith ( mockTransport ) ;
107
93
} ) ;
108
94
109
- it ( 'should create spans for incoming JSON-RPC requests' , async ( ) => {
110
- const mockMcpServer = createMockMcpServer ( ) ;
111
- const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
112
-
113
- const mockTransport = createMockTransport ( ) ;
114
- await wrappedMcpServer . connect ( mockTransport ) ;
115
-
95
+ it ( 'should create spans for incoming JSON-RPC requests' , ( ) => {
116
96
const jsonRpcRequest = {
117
97
jsonrpc : '2.0' ,
118
98
method : 'tools/call' ,
@@ -133,13 +113,7 @@ describe('wrapMcpServerWithSentry', () => {
133
113
) ;
134
114
} ) ;
135
115
136
- it ( 'should create spans for incoming JSON-RPC notifications' , async ( ) => {
137
- const mockMcpServer = createMockMcpServer ( ) ;
138
- const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
139
-
140
- const mockTransport = createMockTransport ( ) ;
141
- await wrappedMcpServer . connect ( mockTransport ) ;
142
-
116
+ it ( 'should create spans for incoming JSON-RPC notifications' , ( ) => {
143
117
const jsonRpcNotification = {
144
118
jsonrpc : '2.0' ,
145
119
method : 'notifications/initialized' ,
@@ -160,12 +134,6 @@ describe('wrapMcpServerWithSentry', () => {
160
134
} ) ;
161
135
162
136
it ( 'should create spans for outgoing notifications' , async ( ) => {
163
- const mockMcpServer = createMockMcpServer ( ) ;
164
- const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
165
-
166
- const mockTransport = createMockTransport ( ) ;
167
- await wrappedMcpServer . connect ( mockTransport ) ;
168
-
169
137
const outgoingNotification = {
170
138
jsonrpc : '2.0' ,
171
139
method : 'notifications/tools/list_changed' ,
@@ -185,29 +153,17 @@ describe('wrapMcpServerWithSentry', () => {
185
153
) ;
186
154
} ) ;
187
155
188
- it ( 'should not create spans for non-JSON-RPC messages' , async ( ) => {
189
- const mockMcpServer = createMockMcpServer ( ) ;
190
- const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
191
-
192
- const mockTransport = createMockTransport ( ) ;
193
- await wrappedMcpServer . connect ( mockTransport ) ;
194
-
156
+ it ( 'should not create spans for non-JSON-RPC messages' , ( ) => {
195
157
// Simulate non-JSON-RPC message
196
158
expect ( mockTransport . onmessage ) . toBeDefined ( ) ;
197
159
mockTransport . onmessage ?.( { some : 'data' } , { } ) ;
198
160
199
161
expect ( tracingModule . startSpan ) . not . toHaveBeenCalled ( ) ;
200
162
} ) ;
201
163
202
- it ( 'should handle transport onclose events' , async ( ) => {
203
- const mockMcpServer = createMockMcpServer ( ) ;
204
- const wrappedMcpServer = wrapMcpServerWithSentry ( mockMcpServer ) ;
205
-
206
- const mockTransport = createMockTransport ( ) ;
164
+ it ( 'should handle transport onclose events' , ( ) => {
207
165
mockTransport . sessionId = 'test-session-123' ;
208
166
209
- await wrappedMcpServer . connect ( mockTransport ) ;
210
-
211
167
// Trigger onclose - should not throw
212
168
expect ( mockTransport . onclose ) . toBeDefined ( ) ;
213
169
expect ( ( ) => mockTransport . onclose ?.( ) ) . not . toThrow ( ) ;
0 commit comments