Skip to content

fix(core): prevent debouncing notifications when relatedRequestId is 0#2498

Open
harshitj183 wants to merge 2 commits into
modelcontextprotocol:mainfrom
harshitj183:fix/debounce-related-request-id-0
Open

fix(core): prevent debouncing notifications when relatedRequestId is 0#2498
harshitj183 wants to merge 2 commits into
modelcontextprotocol:mainfrom
harshitj183:fix/debounce-related-request-id-0

Conversation

@harshitj183

Copy link
Copy Markdown

Description

This PR fixes a bug in the protocol notification debounce guard where a relatedRequestId of 0 was incorrectly treated as falsy/absent.

In JSON-RPC/MCP, 0 is a valid request/notification identifier. It is also the default starting ID for Protocol instances in the TypeScript SDK.

Currently, the debounce guard logic uses a truthiness check:

const canDebounce = debouncedMethods.includes(notification.method) && !notification.params && !options?.relatedRequestId;

Because 0 is falsy, !options?.relatedRequestId evaluates to true when relatedRequestId is 0, causing the notification to be incorrectly debounced and coalesced.

Fix

We replace the truthiness check with an explicit undefined check:

const canDebounce =
    debouncedMethods.includes(notification.method) &&
    !notification.params &&
    options?.relatedRequestId === undefined;

This aligns the behavior of relatedRequestId: 0 with all other present IDs (such as strings or positive integers), ensuring they bypass the global debounce logic and are sent immediately.

Testing

  1. Added a test case in packages/core-internal/test/shared/protocol.test.ts (should NOT debounce a notification that has a relatedRequestId: 0) that calls protocol.notification twice synchronously with relatedRequestId: 0 to verify that they are both sent immediately and not coalesced.
  2. Ran pnpm test:all to verify that all workspace packages compile and all tests pass.

Resolves #2117

@harshitj183 harshitj183 requested a review from a team as a code owner July 14, 2026 17:38
@changeset-bot

changeset-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: fd2c01a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 14, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2498

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@2498

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@2498

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2498

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@2498

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2498

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2498

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2498

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2498

commit: fd2c01a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

relatedRequestId 0 is treated as absent by notification debounce guard

1 participant