feat: add support for reactive hook in MCP client initialization#866
Open
noxymon wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
feat: add support for reactive hook in MCP client initialization#866noxymon wants to merge 1 commit intomodelcontextprotocol:mainfrom
noxymon wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
- Add connectHook to McpClient Sync and Async builders to allow custom reactive transformations of the connection lifecycle. - Update McpClientSession to propagate connection errors to pending requests, preventing 'onErrorDropped' scenarios. - Add McpClientInitializationTests to verify error propagation and custom hook functionality. Closes modelcontextprotocol#712
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.
Summary
This PR introduces a connectHook to the McpClient builders and improves connection error propagation in McpClientSession. It ensures that failures during the transport connection phase (e.g., process startup failures in Stdio) are correctly reported
to callers instead of being dropped by Project Reactor.
Motivation and Context
This change addresses Closes #712 (#712). Previously, errors occurring during the asynchronous connection phase were often "dropped" by Reactor because the internal subscription lacked an error
handler. This led to onErrorDropped logs and caused client.initialize() to hang until a timeout occurred, rather than failing immediately with a descriptive error. The new connectHook also provides the requested extensibility for users to intercept
and transform the connection lifecycle.
How Has This Been Tested?
Tested via a new functional test suite McpClientInitializationTests.java:
Breaking Changes
No breaking changes. The connectHook is optional, and the updated internal constructors maintain backward compatibility via @deprecated overloads.
Types of changes
Checklist
Additional context
The fix in McpClientSession works by iterating over all pendingResponses when a connection error is detected and failing them immediately. This is a more robust approach than relying on timeouts, as it provides the actual cause of the connection
failure (e.g., IOException) to the user.