fix: propagate reactor context up till transport#154
fix: propagate reactor context up till transport#154chemicL merged 1 commit intomodelcontextprotocol:mainfrom
Conversation
518b6fb to
4f5ae36
Compare
4f5ae36 to
84ed798
Compare
|
Hi @tzolov, would like to know your thoughts on this, is there any hope of getting this PR merged in? Could let me know as well if you think there is a better solution to this issue, thanks in advance! |
chemicL
left a comment
There was a problem hiding this comment.
Thank you for the contribution! I agree with the change 100%. As a follow-up it would be nice to add a test to validate the context in the transport sendMessage method but it can be challenging. However, please feel free to open a PR for it.
|
Hi @chemicL, regarding this change it actually only solves the problem of context passing for the async client, what do you think about supporting this for the sync client as well? The change however will probably be a breaking change as it requires the signature of the sync client's functions to change as they need to accept the context as an argument and pass it into the underlying mono returned by the underlying async client. Any thoughts on this? |
|
@FH-30 I don't know your use case that much, but for transitioning between a synchronous world and a reactive one we can rely on Context Propagation. Typically, when it comes to security, as I understand, for instance with Spring Security, the security context can be bound to a |
Motivation and Context
When implementing my own custom transport class which has the requirement of passing in a unique JWT Token per incoming request to call tool, I came to realize that context propagation up till the transport's sendMessage function is not working for regular requests but only working for notifications.
Upon further inspection this is because of how sendRequest and sendNotification are implemented in the McpClientSession class.
The sendRequest method creates a response mono and the transport's sendMessage creates a separate mono, the context from the client is only propagated into the response mono but isn't passed into the mono returned by sendMessage. This differs from sendNotification that returns the mono created by the transport's sendMessage back to the client so the context attached by the client gets propagated into the mono in the transport's sendMessage for notifications.

This MR modifies the McpClientSession's sendRequest to explicitly propagate the context into the mono created by transport's sendMessage allowing the transport's sendMessage to access the JWT Token from this context and perform the required logic.
How Has This Been Tested?
yes, ran the existing tests.
Breaking Changes
None
Types of changes
Checklist
Additional context