feat(session): propagate callback exceptions to the awaiter#2674
Open
Ar-maan05 wants to merge 12 commits into
Open
feat(session): propagate callback exceptions to the awaiter#2674Ar-maan05 wants to merge 12 commits into
Ar-maan05 wants to merge 12 commits into
Conversation
- Guard stream.aclose() in propagate handler with try/except to handle already-closed streams (mirrors existing connection-close cleanup pattern) - Include exception class name and message in the INTERNAL_ERROR response sent to the peer for better debuggability - Add explanatory comment on why the receive loop exits after propagation
…r error msg" This reverts commit ed5721c.
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.
Allow exceptions raised inside user-defined client callbacks (elicitation, sampling, list roots) to propagate back to the awaiter of the outgoing request (e.g.,
session.call_tool), instead of being silently swallowed by the receive loop and converted into a genericINVALID_PARAMSJSON-RPC error.Usage Example
Users opt-in to propagation by adding a
__mcp_propagate__marker attribute to their custom exceptions:Solution
• Added self._propagate_errors: dict[RequestId, BaseException] = {} to BaseSession to stash exceptions marked with mcp_propagate = True .
• In _receive_loop , if a callback exception has mcp_propagate = True :
• Send an INTERNAL_ERROR response back to the server so it doesn't hang.
• Populate _propagate_errors for active request IDs.
• Close active outgoing request streams and exit the receive loop.
• In send_request , catch anyio.EndOfStream and raise the stashed exception on the caller's task.
• Added test_callback_exception_propagation in tests/shared/test_session.py to prevent regressions.
Closes #2673