fix(core): treat relatedRequestId 0 as present in debounce guard (closes #2117)#2141
Open
adityachilka1 wants to merge 1 commit into
Open
Conversation
|
@modelcontextprotocol/client
@modelcontextprotocol/codemod
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
7e82177 to
8637c59
Compare
`0` is a valid JSON-RPC RequestId (and is also the first id a Protocol instance uses). The debounce guard in `Protocol.notification` used a truthy check `!options?.relatedRequestId` which treated 0 as absent, and incorrectly coalesced related notifications that should have bypassed debouncing. Compare the asymmetry: | relatedRequestId | debounced? (pre-fix) | | 0 | YES (bug) | | 1 | no | | "0" | no | Switch to `options?.relatedRequestId == null` - catches anull/undefined, preserves 0 as a valid id. `relatedTask` is a structured object and cannot be falsy, so its guard is unchanged. Closes modelcontextprotocol#2117.
8637c59 to
d6ef917
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2117.
The debounce guard in
Protocol.notification()treatedrelatedRequestId: 0as absent:
0is a valid JSON-RPC RequestId (and is also the first id aProtocolinstance uses for requests). The issue author included a standalone repro that
drives
notification()directly and shows the asymmetry:01"0"So two related notifications for request id 0 in the same tick got coalesced
even though related notifications are explicitly supposed to bypass debouncing.
The fix
One-line change: swap the truthy check for a null check (the same fix pattern
the issue author proposed):
== nullcatches bothnullandundefined, and is the idiom the codebasealready uses when preserving 0 as a valid id (e.g. in parallel patches for
#2115/#2116).relatedTaskis aRelatedTaskMetadataobject, so a falsy check is stillcorrect for it.
Test
Added a regression test
should NOT debounce a notification that has relatedRequestId 0immediately after the existing'req-1' / 'req-2'test.It pins that two synchronous calls with
0each reach the transport (nocoalescing). Without the fix, the test fails (only 1 call reaches the spy).
Verification
-> 151 / 151 tests passing (the new test is in the 151).
No behavioral regression
The only change is that
relatedRequestId: 0now correctly bypasses debouncing.All other values (
1,"req-1",undefined) hit the same code path as before.(This is a clean pick-up of the issue author's root-cause analysis and
proposed fix - all credit to @tylerklose for the diagnosis.)