feat(mcp): add headersProvider for dynamic per-request authentication - #1383
Open
mekkiamiri wants to merge 1 commit into
Open
feat(mcp): add headersProvider for dynamic per-request authentication#1383mekkiamiri wants to merge 1 commit into
mekkiamiri wants to merge 1 commit into
Conversation
Support a Supplier<Map> headersProvider on SseServerParameters and StreamableHttpServerParameters, evaluated on every HTTP request instead of frozen at construction time. When set, headersProvider takes precedence over the static headers map. This enables multi-tenant scenarios where each user's JWT must be forwarded to MCP servers requiring authentication.
Contributor
|
Hi @mekkiamiri, thank you for your contribution! We appreciate you taking the time to submit this pull request. Currently this PR is under review by our team, we will keep you posted if any additional information is required. thank you. |
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
headersProvider(Supplier<Map>) toSseServerParametersandStreamableHttpServerParameters, evaluated per-request instead of frozen at construction timeheadersProvidertakes precedence over the staticheadersmapprovider.get()returning null and wraps exceptions inMono.error()(Streamable HTTP) /RuntimeException(SSE)@JsonIgnoreon getter and builder setter to prevent Jackson serialization failuresMotivation
In multi-tenant applications (e.g. Spring Boot serving multiple authenticated users), each user has their own JWT token that must be forwarded to MCP servers requiring authentication. Currently, headers are frozen at
McpToolsetconstruction time, making per-user auth impossible without creating a new toolset per request.Related: #1382
Changed files
SseServerParameters.java— newheadersProvider()abstract method +@JsonIgnoreStreamableHttpServerParameters.java— newheadersProviderfield + builder methodDefaultMcpTransportBuilder.java— resolve effective headers from provider or static map, with null/exception guardsTest plan
SseServerParametersTest— builder accepts/stores Supplier, defaults to nullStreamableHttpServerParametersTest— builder accepts/stores Supplier, defaults to null, static headers unaffectedDefaultMcpTransportBuilderTest— provider called per-request (2x invocation count), provider takes precedence over static headers, null-returning provider does not throw, throwing provider returns Mono.error()