Skip to content

Commit 03077f8

Browse files
committed
test(mcp-server): Update tests to control transport connection in individual cases
1 parent 5a97d69 commit 03077f8

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

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

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ describe('wrapMcpServerWithSentry', () => {
5050
let wrappedMcpServer: ReturnType<typeof createMockMcpServer>;
5151
let mockTransport: ReturnType<typeof createMockTransport>;
5252

53-
beforeEach(async () => {
53+
beforeEach(() => {
5454
mockMcpServer = createMockMcpServer();
5555
wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
5656
mockTransport = createMockTransport();
57-
58-
// Connect the server to transport - this is common to most tests
59-
await wrappedMcpServer.connect(mockTransport);
57+
// Don't connect transport here. let individual tests control when connection happens
6058
});
6159

6260
it('should proxy the connect method', () => {
@@ -69,30 +67,43 @@ describe('wrapMcpServerWithSentry', () => {
6967
expect(freshWrappedMcpServer.connect).not.toBe(originalConnect);
7068
});
7169

72-
it('should intercept transport onmessage handler', () => {
70+
it('should intercept transport onmessage handler', async () => {
7371
const originalOnMessage = mockTransport.onmessage;
72+
73+
await wrappedMcpServer.connect(mockTransport);
74+
7475
// onmessage should be wrapped after connection
7576
expect(mockTransport.onmessage).not.toBe(originalOnMessage);
7677
});
7778

78-
it('should intercept transport send handler', () => {
79+
it('should intercept transport send handler', async () => {
7980
const originalSend = mockTransport.send;
81+
82+
await wrappedMcpServer.connect(mockTransport);
83+
8084
// send should be wrapped after connection
8185
expect(mockTransport.send).not.toBe(originalSend);
8286
});
8387

84-
it('should intercept transport onclose handler', () => {
88+
it('should intercept transport onclose handler', async () => {
8589
const originalOnClose = mockTransport.onclose;
90+
91+
await wrappedMcpServer.connect(mockTransport);
92+
8693
// onclose should be wrapped after connection
8794
expect(mockTransport.onclose).not.toBe(originalOnClose);
8895
});
8996

90-
it('should call original connect and preserve functionality', () => {
91-
// Original connect should have been called during beforeEach
97+
it('should call original connect and preserve functionality', async () => {
98+
await wrappedMcpServer.connect(mockTransport);
99+
100+
// Original connect should have been called
92101
expect(mockMcpServer.connect).toHaveBeenCalledWith(mockTransport);
93102
});
94103

95-
it('should create spans for incoming JSON-RPC requests', () => {
104+
it('should create spans for incoming JSON-RPC requests', async () => {
105+
await wrappedMcpServer.connect(mockTransport);
106+
96107
const jsonRpcRequest = {
97108
jsonrpc: '2.0',
98109
method: 'tools/call',
@@ -101,7 +112,6 @@ describe('wrapMcpServerWithSentry', () => {
101112
};
102113

103114
// Simulate incoming message
104-
expect(mockTransport.onmessage).toBeDefined();
105115
mockTransport.onmessage?.(jsonRpcRequest, {});
106116

107117
expect(tracingModule.startSpan).toHaveBeenCalledWith(
@@ -113,15 +123,16 @@ describe('wrapMcpServerWithSentry', () => {
113123
);
114124
});
115125

116-
it('should create spans for incoming JSON-RPC notifications', () => {
126+
it('should create spans for incoming JSON-RPC notifications', async () => {
127+
await wrappedMcpServer.connect(mockTransport);
128+
117129
const jsonRpcNotification = {
118130
jsonrpc: '2.0',
119131
method: 'notifications/initialized',
120132
// No 'id' field - this makes it a notification
121133
};
122134

123135
// Simulate incoming notification
124-
expect(mockTransport.onmessage).toBeDefined();
125136
mockTransport.onmessage?.(jsonRpcNotification, {});
126137

127138
expect(tracingModule.startSpan).toHaveBeenCalledWith(
@@ -134,14 +145,15 @@ describe('wrapMcpServerWithSentry', () => {
134145
});
135146

136147
it('should create spans for outgoing notifications', async () => {
148+
await wrappedMcpServer.connect(mockTransport);
149+
137150
const outgoingNotification = {
138151
jsonrpc: '2.0',
139152
method: 'notifications/tools/list_changed',
140153
// No 'id' field
141154
};
142155

143156
// Simulate outgoing notification
144-
expect(mockTransport.send).toBeDefined();
145157
await mockTransport.send?.(outgoingNotification);
146158

147159
expect(tracingModule.startSpan).toHaveBeenCalledWith(
@@ -153,19 +165,20 @@ describe('wrapMcpServerWithSentry', () => {
153165
);
154166
});
155167

156-
it('should not create spans for non-JSON-RPC messages', () => {
168+
it('should not create spans for non-JSON-RPC messages', async () => {
169+
await wrappedMcpServer.connect(mockTransport);
170+
157171
// Simulate non-JSON-RPC message
158-
expect(mockTransport.onmessage).toBeDefined();
159172
mockTransport.onmessage?.({ some: 'data' }, {});
160173

161174
expect(tracingModule.startSpan).not.toHaveBeenCalled();
162175
});
163176

164-
it('should handle transport onclose events', () => {
177+
it('should handle transport onclose events', async () => {
178+
await wrappedMcpServer.connect(mockTransport);
165179
mockTransport.sessionId = 'test-session-123';
166180

167181
// Trigger onclose - should not throw
168-
expect(mockTransport.onclose).toBeDefined();
169182
expect(() => mockTransport.onclose?.()).not.toThrow();
170183
});
171184
});
@@ -175,16 +188,17 @@ describe('wrapMcpServerWithSentry', () => {
175188
let wrappedMcpServer: ReturnType<typeof createMockMcpServer>;
176189
let mockTransport: ReturnType<typeof createMockTransport>;
177190

178-
beforeEach(async () => {
191+
beforeEach(() => {
179192
mockMcpServer = createMockMcpServer();
180193
wrappedMcpServer = wrapMcpServerWithSentry(mockMcpServer);
181194
mockTransport = createMockTransport();
182195
mockTransport.sessionId = 'test-session-123';
183-
184-
await wrappedMcpServer.connect(mockTransport);
196+
// Don't connect here - let individual tests control when connection happens
185197
});
186198

187-
it('should create spans with correct MCP server semantic attributes for tool operations', () => {
199+
it('should create spans with correct MCP server semantic attributes for tool operations', async () => {
200+
await wrappedMcpServer.connect(mockTransport);
201+
188202
const jsonRpcRequest = {
189203
jsonrpc: '2.0',
190204
method: 'tools/call',
@@ -237,7 +251,9 @@ describe('wrapMcpServerWithSentry', () => {
237251
);
238252
});
239253

240-
it('should create spans with correct attributes for resource operations', () => {
254+
it('should create spans with correct attributes for resource operations', async () => {
255+
await wrappedMcpServer.connect(mockTransport);
256+
241257
const jsonRpcRequest = {
242258
jsonrpc: '2.0',
243259
method: 'resources/read',
@@ -265,7 +281,9 @@ describe('wrapMcpServerWithSentry', () => {
265281
);
266282
});
267283

268-
it('should create spans with correct attributes for prompt operations', () => {
284+
it('should create spans with correct attributes for prompt operations', async () => {
285+
await wrappedMcpServer.connect(mockTransport);
286+
269287
const jsonRpcRequest = {
270288
jsonrpc: '2.0',
271289
method: 'prompts/get',
@@ -293,7 +311,9 @@ describe('wrapMcpServerWithSentry', () => {
293311
);
294312
});
295313

296-
it('should create spans with correct attributes for notifications (no request id)', () => {
314+
it('should create spans with correct attributes for notifications (no request id)', async () => {
315+
await wrappedMcpServer.connect(mockTransport);
316+
297317
const jsonRpcNotification = {
298318
jsonrpc: '2.0',
299319
method: 'notifications/tools/list_changed',
@@ -327,7 +347,9 @@ describe('wrapMcpServerWithSentry', () => {
327347
expect(attributes).not.toHaveProperty('mcp.request.id');
328348
});
329349

330-
it('should create spans for list operations without target in name', () => {
350+
it('should create spans for list operations without target in name', async () => {
351+
await wrappedMcpServer.connect(mockTransport);
352+
331353
const jsonRpcRequest = {
332354
jsonrpc: '2.0',
333355
method: 'tools/list',

0 commit comments

Comments
 (0)