Skip to content

fix(client): preserve custom Accept headers in StreamableHTTPClientTransport#1655

Open
nielskaspers wants to merge 4 commits intomodelcontextprotocol:mainfrom
nielskaspers:fix/issue-1646-preserve-custom-accept-header
Open

fix(client): preserve custom Accept headers in StreamableHTTPClientTransport#1655
nielskaspers wants to merge 4 commits intomodelcontextprotocol:mainfrom
nielskaspers:fix/issue-1646-preserve-custom-accept-header

Conversation

@nielskaspers
Copy link
Copy Markdown
Contributor

Summary

StreamableHTTPClientTransport unconditionally overwrites custom Accept headers provided via requestInit.headers with SDK defaults. This change only sets the default Accept header when the user has not already provided one.

Issue

Fixes #1646

Changes

  • In send() (POST requests): only set accept: application/json, text/event-stream if no Accept header exists
  • In _startOrAuthSse() (GET SSE requests): only set accept: text/event-stream if no Accept header exists

Testing

  • Added test: custom Accept header is preserved on POST requests
  • Added test: custom Accept header is preserved on GET SSE requests
  • Added test: default Accept header is set when none provided
  • All 260 existing tests continue to pass
  • Lint passes

…ansport

The SDK unconditionally overwrites custom Accept headers provided via
requestInit.headers with its own defaults. This prevents users from
configuring MCP servers that require specific Accept header values.

Only set default Accept headers when the user has not provided one.

Fixes modelcontextprotocol#1646
@nielskaspers nielskaspers requested a review from a team as a code owner March 10, 2026 07:16
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 10, 2026

🦋 Changeset detected

Latest commit: acb3d5c

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

Copy link
Copy Markdown
Contributor

@felixweinberger felixweinberger left a comment

Choose a reason for hiding this comment

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

The Accept header on MCP transport requests is spec-mandated. POST needs application/json, text/event-stream and GET SSE needs text/event-stream for the transport to work. The current fix lets users omit these entirely, which would break the connection (406, or unparseable responses).

If the goal is to add a media type (e.g. for a gateway that routes on application/vnd.example.v1+json), the right approach is to append rather than replace:

const userAccept = headers.get('accept');
headers.set('accept', userAccept
    ? `${userAccept}, application/json, text/event-stream`
    : 'application/json, text/event-stream');

This keeps the required types present while letting the user's value through. The C# SDK does something similar via Headers.Accept.Add().

Before reworking though, could you say more on #1646 about what's requiring the custom Accept? Want to make sure append actually solves it.

Addresses review feedback: custom Accept headers are now appended to
the spec-mandated types instead of replacing them, ensuring the
required media types are always present while allowing users to
include additional types for proxy/gateway routing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 27, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1655

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1655

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1655

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1655

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1655

commit: acb3d5c

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@felixweinberger
Copy link
Copy Markdown
Contributor

@claude review

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.

SDK overwrites custom Accept headers unconditionally

2 participants