Skip to content

Deprecate Builder.customizeRequest() in favor of httpRequestCustomizer()#791

Merged
Kehrlann merged 5 commits intomodelcontextprotocol:mainfrom
gyeo009:deprecate-customizeRequest
Apr 2, 2026
Merged

Deprecate Builder.customizeRequest() in favor of httpRequestCustomizer()#791
Kehrlann merged 5 commits intomodelcontextprotocol:mainfrom
gyeo009:deprecate-customizeRequest

Conversation

@gyeo009
Copy link
Copy Markdown
Contributor

@gyeo009 gyeo009 commented Feb 13, 2026

Motivation and Context

Builder.customizeRequest() in HttpClientSseClientTransport and HttpClientStreamableHttpTransport executes its consumer once at build time, mutating the shared requestBuilder. Any headers set here (e.g., Authorization) are frozen into the template and copied to every subsequent request via requestBuilder.copy().

This silently breaks OAuth token refresh scenarios: after refreshing an access token, the transport continues sending the old expired token because the header was baked in at construction time.

The method name customizeRequest strongly suggests per-request behavior, making it easy to use incorrectly. The correct alternatives — httpRequestCustomizer() and asyncHttpRequestCustomizer() — already exist and run on every request, but there is no deprecation notice or Javadoc warning guiding users toward them.

We encountered this in production where our MCP connector's OAuth token refresh was completely non-functional due to this behavior.

Changes

  • Add @Deprecated to customizeRequest() in both HttpClientSseClientTransport.Builder and HttpClientStreamableHttpTransport.Builder
  • Update Javadoc to clearly describe the build-time-only semantics
  • Point users toward httpRequestCustomizer() / asyncHttpRequestCustomizer() as the per-request alternatives

How Has This Been Tested?

This is a Javadoc + annotation-only change. Verified compilation passes with spring-javaformat:apply and mvn compile.

Breaking Changes

None. This only adds a deprecation warning — no behavioral changes.

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

Closes #788

@gyeo009 gyeo009 force-pushed the deprecate-customizeRequest branch from 096f8c3 to c5a3662 Compare February 13, 2026 13:03
@Kehrlann Kehrlann force-pushed the deprecate-customizeRequest branch from c5a3662 to 7449f2a Compare March 2, 2026 16:34
@Kehrlann Kehrlann self-assigned this Mar 2, 2026
@Kehrlann Kehrlann self-requested a review March 2, 2026 16:34
@Kehrlann
Copy link
Copy Markdown
Contributor

Kehrlann commented Mar 2, 2026

@gyeo009 thank you for your contribution. I agree that this should be deprecated.

@Kehrlann Kehrlann added waiting for user Waiting for user feedback or more details enhancement New feature or request labels Mar 2, 2026
Copy link
Copy Markdown
Contributor

@Kehrlann Kehrlann left a comment

Choose a reason for hiding this comment

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

Please update the Javadoc to point to HttpClientStreamableHttpTransport.Builder.requestBuilder(HttpRequest.Builder) in case users want to specify "stable" headers that are applied to every request. (and same for SSE)

@gyeo009 gyeo009 force-pushed the deprecate-customizeRequest branch 4 times, most recently from 4cddda2 to a3b0cfa Compare March 14, 2026 12:54
@gyeo009
Copy link
Copy Markdown
Contributor Author

gyeo009 commented Mar 14, 2026

@Kehrlann

  • Addressed in e35dacc — added requestBuilder(HttpRequest.Builder) as the recommended alternative for stable headers in the deprecated Javadoc (both transports).
  • Additionally, a3b0cfa trims the Javadoc body to focus on the method's behavior, as the migration guidance is already captured in the deprecated tag.

@Kehrlann Kehrlann changed the base branch from main to 1.1.x April 2, 2026 09:00
@Kehrlann Kehrlann changed the base branch from 1.1.x to main April 2, 2026 09:00
Kehrlann
Kehrlann previously approved these changes Apr 2, 2026
gyeo009 and others added 4 commits April 2, 2026 11:05
…and HttpClientStreamableHttpTransport

customizeRequest() executes its consumer once at build time, freezing
headers into the shared requestBuilder. This silently breaks OAuth
token refresh scenarios where the Authorization header needs to be
updated after the transport is built.

Add @deprecated and update Javadoc to clarify the build-time-only
semantics and guide users toward httpRequestCustomizer() or
asyncHttpRequestCustomizer() which run on every request.

Closes modelcontextprotocol#788
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
@Kehrlann Kehrlann force-pushed the deprecate-customizeRequest branch from 755404e to ec6393b Compare April 2, 2026 09:05
@Kehrlann Kehrlann added area/client and removed waiting for user Waiting for user feedback or more details labels Apr 2, 2026
@Kehrlann Kehrlann self-requested a review April 2, 2026 09:38
@Kehrlann Kehrlann merged commit 301dbe4 into modelcontextprotocol:main Apr 2, 2026
21 checks passed
Kehrlann pushed a commit that referenced this pull request Apr 2, 2026
…r() (#791)

Deprecate Builder.customizeRequest() in HttpClientSseClientTransport and HttpClientStreamableHttpTransport

customizeRequest() executes its consumer once at build time, freezing
headers into the shared requestBuilder. This silently breaks OAuth
token refresh scenarios where the Authorization header needs to be
updated after the transport is built.

Add @deprecated and update Javadoc to clarify the build-time-only
semantics and guide users toward httpRequestCustomizer() or
asyncHttpRequestCustomizer() which run on every request.

Closes #788

Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
@Kehrlann
Copy link
Copy Markdown
Contributor

Kehrlann commented Apr 2, 2026

Backported to 1.1.x through 7e4c179

@Kehrlann
Copy link
Copy Markdown
Contributor

Kehrlann commented Apr 2, 2026

Thank you for your contribution!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecate Builder.customizeRequest() in favor of httpRequestCustomizer()

2 participants