Skip to content

fix(client): let OAuth-derived Authorization override caller-supplied header#2475

Open
sanjibani wants to merge 2 commits into
modelcontextprotocol:mainfrom
sanjibani:feat/fix-fallback-auth-header
Open

fix(client): let OAuth-derived Authorization override caller-supplied header#2475
sanjibani wants to merge 2 commits into
modelcontextprotocol:mainfrom
sanjibani:feat/fix-fallback-auth-header

Conversation

@sanjibani

Copy link
Copy Markdown

Closes #2208.

StreamableHTTPClientTransport and SSEClientTransport both build the outgoing request's headers as:

return new Headers({
    ...headers,        // common: Authorization (OAuth), mcp-session-id, mcp-protocol-version
    ...extraHeaders    // caller-supplied via requestInit.headers
});

This meant a caller-supplied Authorization placeholder (e.g. an env-var API key passed through the MCP config) was placed AFTER the SDK-derived common headers, which silently overrode OAuth-computed tokens and broke the auth-refresh flow once the placeholder went stale. Several MCP servers (Atlassian Rovo and others) let both API tokens and OAuth share the same Authorization header and rely on it being valid.

Swap the spread order to ...extraHeaders, ...headers so SDK-computed common headers (including the freshest OAuth token) take precedence, matching the merge order used elsewhere in the SDK. Caller-supplied non-auth headers still pass through unchanged.

Test: a regression test in streamableHttp.test.ts seeds a stale Authorization placeholder in requestInit.headers alongside a working authProvider and asserts the request goes out with the OAuth-derived token. 722 client tests pass; eslint + prettier clean.

… header

Closes modelcontextprotocol#2208.

StreamableHTTPClientTransport and SSEClientTransport both build the
outgoing request's headers as:

  return new Headers({
      ...headers,        // common: Authorization (OAuth), mcp-session-id, mcp-protocol-version
      ...extraHeaders    // caller-supplied via requestInit.headers
  });

This meant a caller-supplied Authorization placeholder (e.g. an env-var
API key passed through the MCP config) was placed AFTER the SDK-derived
common headers, which silently overrode OAuth-computed tokens and broke
the auth-refresh flow once the placeholder went stale. Several MCP servers
(Atlassian Rovo and others) let both API tokens and OAuth share the same
Authorization header and rely on it being valid.

Swap the spread order to  so SDK-computed
common headers (including the freshest OAuth token) take precedence,
matching the merge order used elsewhere in the SDK. Caller-supplied
non-auth headers still pass through unchanged.

Test: a regression test in streamableHttp.test.ts seeds a stale
Authorization placeholder in requestInit.headers alongside a working
authProvider and asserts the request goes out with the OAuth-derived
token. 722 client tests pass; lint + format clean.
@sanjibani sanjibani requested a review from a team as a code owner July 10, 2026 05:31
@changeset-bot

changeset-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0003f27

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2475

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@2475

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@2475

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2475

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@2475

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2475

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2475

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2475

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2475

commit: 0003f27

…ntextprotocol#2475)

Signed-off-by: sanjibani <18418553+sanjibani@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

One line change to enable fallback authentication

1 participant