Skip to content

fix: use 'is not None' for related_request_id to handle id=0#2158

Open
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
giulio-leone:fix/falsy-request-id-notification
Open

fix: use 'is not None' for related_request_id to handle id=0#2158
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
giulio-leone:fix/falsy-request-id-notification

Conversation

@giulio-leone
Copy link

Problem

When a JSON-RPC request uses id=0, notifications sent during tool execution are not routed to the correct request stream. The client never receives them.

Root cause: In send_notification(), the condition:

metadata=ServerMessageMetadata(related_request_id=related_request_id) if related_request_id else None

evaluates to False when related_request_id is 0 (integer zero is falsy in Python). This means the ServerMessageMetadata is not attached, so the message router in streamable_http.py falls through to the GET_STREAM_KEY instead of the request-specific stream.

Fix

- metadata=ServerMessageMetadata(related_request_id=related_request_id) if related_request_id else None,
+ metadata=ServerMessageMetadata(related_request_id=related_request_id) if related_request_id is not None else None,

This is consistent with the identity checks already used in streamable_http.py:

  • Line 984: message.id is not None
  • Line 993: session_message.metadata.related_request_id is not None
  • Line 997: target_request_id is not None

Testing

  • 64 tests pass across test_session.py and test_streamable_http.py (2 consecutive clean runs)
  • The JSON-RPC spec explicitly allows id=0 as a valid request identifier

Fixes #1218

giulio-leone and others added 2 commits February 28, 2026 04:27
When a JSON-RPC request uses id=0, notifications sent during tool
execution were not routed to the correct request stream because the
condition 'if related_request_id' evaluates to False for integer 0.

This caused notifications to be sent to the GET stream instead of the
POST stream for the specific request, making them invisible to clients
that don't have a GET SSE connection open (e.g. stateless mode).

Fix: change 'if related_request_id' to 'if related_request_id is not
None' in send_notification(), consistent with similar checks elsewhere
in the codebase (e.g. streamable_http.py line 984, 993, 997).

Fixes modelcontextprotocol#1218

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

simple-streamablehttp-stateless example will not return data if id=0

1 participant