Polyfill Headers in proxy with node-fetch#813
Merged
olaservo merged 1 commit intomodelcontextprotocol:mainfrom Sep 17, 2025
Merged
Polyfill Headers in proxy with node-fetch#813olaservo merged 1 commit intomodelcontextprotocol:mainfrom
olaservo merged 1 commit intomodelcontextprotocol:mainfrom
Conversation
- in createCustomFetch function
- convert finalHeaders to headers object for node-fetch compatibility
- for SSE streams, convert node stream to response body to web ReadableStream since EventSource polyfill expects web-compatible stream
* In package-lock.json and package.json
- add node-fetch as a dependency
This fixes modelcontextprotocol#600
olaservo
approved these changes
Sep 17, 2025
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.
Description
In server/index.ts
nodeFetchandHeaders(asNodeHeaders) fromnode-fetchfetchconstant set tonodeFetch(overridesglobalThis.fetch)Headersconstant set toNodeHeaders(overridesglobalThis.Headers)createCustomFetchfunctionfinalHeaderstoheadersobject fornode-fetchcompatibilityReadableStreamsinceEventSourceexpects web-compatible streamIn package-lock.json and package.json
Motivation and Context
Some users now get a "Headers is undefined" message when connecting to an SSE server. This is likely a result of the recent fix that injects headers that are dynamically introduced in server responses into the response to sent to the client on its transport. That code uses the
Headersinterface, which may not be present in some Node environments.The fix was to use
NodeHeadersfromnode-fetchand also for SSE streams to convert Node streams to webstyleReadableStreams.This fixes #600
How Has This Been Tested?
Tested against both StreamableHttp and SSE versions of the Everything reference server
Breaking Changes
Nope.
Types of changes
Checklist
Additional context