Skip to content

Commit 52478f7

Browse files
committed
fix(core): skip cancellation notification for initialize requests
Per the MCP spec, clients must not cancel the `initialize` request. When the caller aborts or the request times out during `connect()`, the promise still rejects locally, but `notifications/cancelled` is no longer emitted on the wire. Fixes modelcontextprotocol#998
1 parent b8886e7 commit 52478f7

3 files changed

Lines changed: 79 additions & 13 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modelcontextprotocol/core': patch
3+
---
4+
5+
Do not send `notifications/cancelled` for `initialize` requests. Per the MCP specification, clients must not cancel the `initialize` request; when the caller aborts or times out during `connect()`, the promise still rejects locally but the wire notification is no longer emitted.

‎packages/core/src/shared/protocol.ts‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -846,19 +846,23 @@ export abstract class Protocol<ContextT extends BaseContext> {
846846
const cancel = (reason: unknown) => {
847847
this._progressHandlers.delete(messageId);
848848

849-
this._transport
850-
?.send(
851-
{
852-
jsonrpc: '2.0',
853-
method: 'notifications/cancelled',
854-
params: {
855-
requestId: messageId,
856-
reason: String(reason)
857-
}
858-
},
859-
{ relatedRequestId, resumptionToken, onresumptiontoken }
860-
)
861-
.catch(error => this._onerror(new Error(`Failed to send cancellation: ${error}`)));
849+
// Per the MCP spec, the `initialize` request MUST NOT be cancelled by clients.
850+
// Abort/timeout still rejects the promise locally; we just skip the wire notification.
851+
if (request.method !== 'initialize') {
852+
this._transport
853+
?.send(
854+
{
855+
jsonrpc: '2.0',
856+
method: 'notifications/cancelled',
857+
params: {
858+
requestId: messageId,
859+
reason: String(reason)
860+
}
861+
},
862+
{ relatedRequestId, resumptionToken, onresumptiontoken }
863+
)
864+
.catch(error => this._onerror(new Error(`Failed to send cancellation: ${error}`)));
865+
}
862866

863867
// Wrap the reason in an SdkError if it isn't already
864868
const error = reason instanceof SdkError ? reason : new SdkError(SdkErrorCode.RequestTimeout, String(reason));

‎packages/core/test/shared/protocol.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,63 @@ describe('protocol tests', () => {
305305
expect(removeSpy).toHaveBeenCalledWith('abort', expect.any(Function));
306306
});
307307

308+
describe('initialize request cancellation', () => {
309+
test('should not send notifications/cancelled when an initialize request is aborted', async () => {
310+
await protocol.connect(transport);
311+
312+
const controller = new AbortController();
313+
const mockSchema = z.object({ result: z.string() });
314+
const reqPromise = testRequest(protocol, { method: 'initialize', params: {} }, mockSchema, {
315+
signal: controller.signal
316+
});
317+
318+
controller.abort('User cancelled');
319+
await expect(reqPromise).rejects.toThrow();
320+
321+
const cancelledSends = sendSpy.mock.calls.filter(([message]) => {
322+
const m = message as Partial<JSONRPCNotification>;
323+
return m?.method === 'notifications/cancelled';
324+
});
325+
expect(cancelledSends).toHaveLength(0);
326+
});
327+
328+
test('should not send notifications/cancelled when an initialize request times out', async () => {
329+
await protocol.connect(transport);
330+
331+
const mockSchema = z.object({ result: z.string() });
332+
await expect(
333+
testRequest(protocol, { method: 'initialize', params: {} }, mockSchema, {
334+
timeout: 0
335+
})
336+
).rejects.toThrow();
337+
338+
const cancelledSends = sendSpy.mock.calls.filter(([message]) => {
339+
const m = message as Partial<JSONRPCNotification>;
340+
return m?.method === 'notifications/cancelled';
341+
});
342+
expect(cancelledSends).toHaveLength(0);
343+
});
344+
345+
test('should still send notifications/cancelled for non-initialize requests', async () => {
346+
await protocol.connect(transport);
347+
348+
const controller = new AbortController();
349+
const mockSchema = z.object({ result: z.string() });
350+
const reqPromise = testRequest(protocol, { method: 'example', params: {} }, mockSchema, {
351+
signal: controller.signal
352+
});
353+
354+
controller.abort('User cancelled');
355+
await expect(reqPromise).rejects.toThrow();
356+
357+
const cancelledSends = sendSpy.mock.calls.filter(([message]) => {
358+
const m = message as Partial<JSONRPCNotification>;
359+
return m?.method === 'notifications/cancelled';
360+
});
361+
expect(cancelledSends).toHaveLength(1);
362+
});
363+
});
364+
308365
test('should not overwrite existing hooks when connecting transports', async () => {
309366
const oncloseMock = vi.fn();
310367
const onerrorMock = vi.fn();

0 commit comments

Comments
 (0)