Skip to content

Conversation

@cliffhall
Copy link
Member

@cliffhall cliffhall commented Sep 17, 2025

Description

  • In server/index.ts

    • import nodeFetch and Headers (as NodeHeaders) from node-fetch
    • create fetch constant set to nodeFetch (overrides globalThis.fetch)
    • create Headers constant set to NodeHeaders (overridesglobalThis.Headers)
    • 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 expects web-compatible stream
  • In package-lock.json and package.json

    • add node-fetch as a dependency

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 Headers interface, which may not be present in some Node environments.

The fix was to use NodeHeaders from node-fetch and also for SSE streams to convert Node streams to webstyle ReadableStreams.

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

  - 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 olaservo merged commit d2a715c into modelcontextprotocol:main Sep 17, 2025
4 checks passed
@cliffhall cliffhall deleted the fix-new-headers-issue branch September 17, 2025 21:58
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.

Unable to connect to SSE ednpoint when running Node 23

2 participants