X Tutup
Skip to content

fix: reject JSON-RPC requests with null id instead of misclassifying as notifications#2251

Open
shivama205 wants to merge 2 commits intomodelcontextprotocol:mainfrom
shivama205:fix/reject-null-jsonrpc-id
Open

fix: reject JSON-RPC requests with null id instead of misclassifying as notifications#2251
shivama205 wants to merge 2 commits intomodelcontextprotocol:mainfrom
shivama205:fix/reject-null-jsonrpc-id

Conversation

@shivama205
Copy link
Contributor

Closes #2057

Problem

When a JSON-RPC request arrives with "id": null, the SDK silently reclassifies it as a JSONRPCNotification and returns 202 Accepted with no response body. The caller gets no error and no response — a silent failure that is hard to debug.

This happens because of Pydantic's union fallthrough: JSONRPCRequest correctly rejects the invalid id, but JSONRPCNotification absorbs the extra "id": null field since it doesn't define an id field and Pydantic's default behavior drops unknown fields.

Fix

Add a model_validator on JSONRPCNotification that rejects any input containing an id field. Per JSON-RPC 2.0, notifications MUST NOT have an id member, so this is both correct and targeted — it prevents the union fallthrough without affecting any valid messages.

This also catches other invalid id types (float, bool, list, dict) that would similarly be misclassified today.

Changes

  • Modified: src/mcp/types/jsonrpc.py — added reject_id_field model validator to JSONRPCNotification
  • Modified: tests/test_types.py — added regression tests for null/invalid id rejection, plus tests confirming valid requests and notifications still work

Test plan

  • All 1147 tests pass (0 failures)
  • ruff check passes
  • ruff format passes

…as notifications

When a JSON-RPC request arrives with "id": null, Pydantic's union
validation rejects it from JSONRPCRequest (since RequestId only allows
int | str) but then silently falls through to JSONRPCNotification, which
absorbs the extra "id" field. This causes the server to return 202
Accepted with no response body — a silent failure that is hard to debug.

Add a model_validator on JSONRPCNotification that rejects any input
containing an "id" field, since per JSON-RPC 2.0 notifications must not
have one. This ensures messages with invalid id values (null, float,
bool, etc.) are properly rejected with a validation error instead of
being silently reclassified.

Closes modelcontextprotocol#2057

Github-Issue: modelcontextprotocol#2057
Copy link

@Bortlesboat Bortlesboat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean fix, the model_validator approach is the right call here — catches the union fallthrough without touching any valid message paths. Tests look solid too.

@shivama205
Copy link
Contributor Author

@Bortlesboat thanks for the comment. Could you please help to review and approve the fix.

@Bortlesboat
Copy link

Happy to vouch for it, but I don't have write access to this repo so my review doesn't count as a formal approval. Hopefully a maintainer picks this up soon — it's a clean fix for a real bug.

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.

Requests with "id": null silently misclassified as notifications

2 participants

X Tutup