Skip to content

fix: handle tools/resources/prompts list_changed notifications in client#2171

Open
giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
giulio-leone:fix/client-list-changed-notifications
Open

fix: handle tools/resources/prompts list_changed notifications in client#2171
giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
giulio-leone:fix/client-list-changed-notifications

Conversation

@giulio-leone
Copy link

Summary

Fixes #2107

The client silently drops notifications/tools/list_changed, notifications/resources/list_changed, and notifications/prompts/list_changed notifications from the server — they fall through to case _: pass in _received_notification().

Root Cause

ClientSession._received_notification() only handled LoggingMessageNotification and ElicitCompleteNotification. The three list_changed notification types (ToolListChangedNotification, ResourceListChangedNotification, PromptListChangedNotification) were caught by the catch-all case _: pass and discarded.

Fix

Add optional callback parameters following the established logging_callback pattern:

  • tool_list_changed_callback: ToolListChangedFnT | None
  • resource_list_changed_callback: ResourceListChangedFnT | None
  • prompt_list_changed_callback: PromptListChangedFnT | None

Each callback is a simple async def () -> None protocol. When set, it is invoked upon receiving the corresponding notification. When not set, a no-op default is used (preserving backward compatibility).

Both ClientSession and the high-level Client dataclass are updated.

Tests

5 new tests covering:

  • Each notification type invokes its callback
  • No crash when no callback is set (no-op default)
  • Multiple notifications each invoke the callback

187/187 client tests pass (2 consecutive clean runs). ruff clean.

Previously, the client silently dropped ToolListChangedNotification,
ResourceListChangedNotification, and PromptListChangedNotification
from the server. They fell through to the catch-all case in
_received_notification().

Add optional callbacks (tool_list_changed_callback,
resource_list_changed_callback, prompt_list_changed_callback)
following the same pattern as logging_callback. When set, the
callbacks are invoked when the corresponding notification arrives.
When not set, a no-op default is used (preserving backward compat).

Updated both ClientSession and the high-level Client dataclass.

Fixes modelcontextprotocol#2107

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.

Handle list_changed notifications instead of dropping them

1 participant