Skip to content

Conversation

@matthew-gries
Copy link

Fixes #1401.

  • Logs and raises exceptions in the default ClientSession message handler. Includes test that verifies exceptions passed to the default handler are properly raised.
  • Makes session more resilient to stale server responses by logging warnings (instead of raising exceptions) when responses with unknown request IDs are received. This handles the case where the client times out and cleans up a pending request, but the server responds late (see test_notification_validation_error()). Previously, a RuntimeError was raised for unknown IDs, but it was swallowed by the old default message handler. With the new exception-raising behavior, keeping the RuntimeError would crash the session on benign timeout scenarios.

Motivation and Context

It solves the issue described in #1401 where exceptions in the default message handler for the ClientSession were being swallowed, which leads to silent failures that are difficult to debug.

How Has This Been Tested?

Added unit test for this, also test_notification_validation_error() in test_88_random_error.py

Breaking Changes

Technically yes because those who use the default message handler and were not receiving silent errors will now start seeing them. Those who override the message handler are not affected.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

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.

ClientSession Error Handling

1 participant