Skip to content

HttpClientStreamHttpTransport: add authorization error handler#861

Open
Kehrlann wants to merge 1 commit intomainfrom
dgarnier/http-client-error-handler
Open

HttpClientStreamHttpTransport: add authorization error handler#861
Kehrlann wants to merge 1 commit intomainfrom
dgarnier/http-client-error-handler

Conversation

@Kehrlann
Copy link
Contributor

@Kehrlann Kehrlann commented Mar 10, 2026

Motivation and Context

In the context of authorization management, we need to handle authorization errors (HTTP 401 and 403) in the client transports. This PR introduces a hook to react to these specific errors. See gh-240.

The hook exposes a "retry" mechanism, so that handlers can decide the request must be replayed, for example, after performing a dynamic client registration, or after updating the scopes in the "step up" scenario.

This PR does not target the SSE transport as we don't intend to evolve it.

Only the sendMessage is targeted. DELETE doesn't require authentication AFAIK.

How Has This Been Tested?

Test with spring's mcp-security project.

Breaking Changes

n/a

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

@Kehrlann Kehrlann added area/client P1 Significant bug affecting many users, highly requested feature P2 Moderate issues affecting some users, edge cases, potentially valuable feature enhancement New feature or request and removed P2 Moderate issues affecting some users, edge cases, potentially valuable feature labels Mar 10, 2026
@Kehrlann Kehrlann added this to the 1.1.0 milestone Mar 10, 2026
@Kehrlann Kehrlann force-pushed the dgarnier/http-client-error-handler branch 2 times, most recently from 1ccce75 to f8c77f9 Compare March 10, 2026 16:21
@Kehrlann Kehrlann marked this pull request as ready for review March 10, 2026 16:21
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
@Kehrlann Kehrlann force-pushed the dgarnier/http-client-error-handler branch from f8c77f9 to e0c1225 Compare March 10, 2026 16:23
Copy link
Member

@chemicL chemicL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the overall design. I have a couple of suggestions regarding method names and also the blocking avoidance in the sync implementation. Feel free to merge once these are addressed. Thanks!

* @return {@link Publisher} emitting true if the original request should be replayed,
* false otherwise.
*/
Publisher<Boolean> handle(HttpResponse.ResponseInfo responseInfo, McpTransportContext context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method seems like it's aimed at the end user. I think the name is fine and semantics are clear. What I'd suggest documenting is what happens when there is an error from the returned Publisher.

* @param retryHandler the handler to use to retry the HTTP request.
* @return a {@link Publisher} to signal either an error or a retry
*/
default Publisher<Void> handle(HttpResponse.ResponseInfo responseInfo, McpTransportContext context,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method is rather aimed at transport implementors, not end users. It has different semantics, it returns Publisher<Void> instead of Publisher<Boolean> from the other method. I'd suggest calling it onAuthError and documenting what is the intention for a return Publisher that errors.

Comment on lines +64 to +65
var shouldRetry = handler.handle(info, context);
return Mono.just(shouldRetry).subscribeOn(Schedulers.boundedElastic());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subscribeOn use is ineffective. You should wrap the handler.handle inside a Mono.fromCallable and on top of that chain add the subscribeOn operator to avoid blocking a non-blocking thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/client enhancement New feature or request P1 Significant bug affecting many users, highly requested feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants