fix(client): preserve custom Accept headers in StreamableHTTPClientTransport#1655
Conversation
…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
🦋 Changeset detectedLatest commit: acb3d5c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
felixweinberger
left a comment
There was a problem hiding this comment.
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>
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@claude review |
Summary
StreamableHTTPClientTransportunconditionally overwrites customAcceptheaders provided viarequestInit.headerswith SDK defaults. This change only sets the defaultAcceptheader when the user has not already provided one.Issue
Fixes #1646
Changes
send()(POST requests): only setaccept: application/json, text/event-streamif no Accept header exists_startOrAuthSse()(GET SSE requests): only setaccept: text/event-streamif no Accept header existsTesting