Skip to content

feat(mcp): add headersProvider for dynamic per-request authentication - #1383

Open
mekkiamiri wants to merge 1 commit into
google:mainfrom
mekkiamiri:feat/mcp-dynamic-headers-provider
Open

feat(mcp): add headersProvider for dynamic per-request authentication#1383
mekkiamiri wants to merge 1 commit into
google:mainfrom
mekkiamiri:feat/mcp-dynamic-headers-provider

Conversation

@mekkiamiri

@mekkiamiri mekkiamiri commented Jul 29, 2026

Copy link
Copy Markdown

Summary

  • Add headersProvider (Supplier<Map>) to SseServerParameters and StreamableHttpServerParameters, evaluated per-request instead of frozen at construction time
  • When set, headersProvider takes precedence over the static headers map
  • Null-safe: guards against provider.get() returning null and wraps exceptions in Mono.error() (Streamable HTTP) / RuntimeException (SSE)
  • @JsonIgnore on getter and builder setter to prevent Jackson serialization failures

Motivation

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 McpToolset construction time, making per-user auth impossible without creating a new toolset per request.

Related: #1382

Changed files

  • SseServerParameters.java — new headersProvider() abstract method + @JsonIgnore
  • StreamableHttpServerParameters.java — new headersProvider field + builder method
  • DefaultMcpTransportBuilder.java — resolve effective headers from provider or static map, with null/exception guards

Test plan

  • SseServerParametersTest — builder accepts/stores Supplier, defaults to null
  • StreamableHttpServerParametersTest — builder accepts/stores Supplier, defaults to null, static headers unaffected
  • DefaultMcpTransportBuilderTest — provider called per-request (2x invocation count), provider takes precedence over static headers, null-returning provider does not throw, throwing provider returns Mono.error()
  • All 64 existing MCP tests pass (backward compatible)

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.
@hemasekhar-p

Copy link
Copy Markdown
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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants