From 9d5481dc9c9671cc9de22c178b912fdd6b024bc5 Mon Sep 17 00:00:00 2001 From: Yufeng He <40085740+he-yufeng@users.noreply.github.com> Date: Tue, 19 May 2026 02:11:29 +0800 Subject: [PATCH] fix: keep related request id zero from debouncing --- .changeset/related-zero-debounce.md | 5 +++++ packages/core/src/shared/protocol.ts | 3 ++- packages/core/test/shared/protocol.test.ts | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 .changeset/related-zero-debounce.md diff --git a/.changeset/related-zero-debounce.md b/.changeset/related-zero-debounce.md new file mode 100644 index 0000000000..b155a6863a --- /dev/null +++ b/.changeset/related-zero-debounce.md @@ -0,0 +1,5 @@ +--- +'@modelcontextprotocol/core': patch +--- + +Treat numeric related request ID `0` as present when deciding whether a notification can be debounced. diff --git a/packages/core/src/shared/protocol.ts b/packages/core/src/shared/protocol.ts index ed78cc68d0..04f0608f2d 100644 --- a/packages/core/src/shared/protocol.ts +++ b/packages/core/src/shared/protocol.ts @@ -832,7 +832,8 @@ export abstract class Protocol { const debouncedMethods = this._options?.debouncedNotificationMethods ?? []; // A notification can only be debounced if it's in the list AND it's "simple" // (i.e., has no parameters and no related request ID that could be lost). - const canDebounce = debouncedMethods.includes(notification.method) && !notification.params && !options?.relatedRequestId; + const hasRelatedRequestId = options?.relatedRequestId !== undefined; + const canDebounce = debouncedMethods.includes(notification.method) && !notification.params && !hasRelatedRequestId; if (canDebounce) { // If a notification of this type is already scheduled, do nothing. diff --git a/packages/core/test/shared/protocol.test.ts b/packages/core/test/shared/protocol.test.ts index 6e77430d61..7bdf4ab308 100644 --- a/packages/core/test/shared/protocol.test.ts +++ b/packages/core/test/shared/protocol.test.ts @@ -654,6 +654,20 @@ describe('protocol tests', () => { expect(sendSpy).toHaveBeenCalledWith(expect.any(Object), { relatedRequestId: 'req-2' }); }); + it('should NOT debounce a notification that has relatedRequestId 0', async () => { + // ARRANGE + protocol = new TestProtocolImpl({ debouncedNotificationMethods: ['test/debounced_with_zero_id'] }); + await protocol.connect(transport); + + // ACT + await protocol.notification({ method: 'test/debounced_with_zero_id' }, { relatedRequestId: 0 }); + await protocol.notification({ method: 'test/debounced_with_zero_id' }, { relatedRequestId: 0 }); + + // ASSERT + expect(sendSpy).toHaveBeenCalledTimes(2); + expect(sendSpy).toHaveBeenCalledWith(expect.any(Object), { relatedRequestId: 0 }); + }); + it('should clear pending debounced notifications on connection close', async () => { // ARRANGE protocol = new TestProtocolImpl({ debouncedNotificationMethods: ['test/debounced'] });