Allow custom fetch in SSEClientTransport and StreamableHTTPClientTransport #721
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.
Summary of Changes
This pull request significantly enhances the flexibility of the
SSEClientTransportandStreamableHTTPClientTransportclasses by enabling the injection of a customfetchimplementation. This change empowers users to exert fine-grained control over network requests, facilitating advanced use cases such as custom authentication flows or specialized request handling, thereby improving the adaptability of the transport layer.Highlights
fetchoption toSSEClientTransportandStreamableHTTPClientTransportconstructors, allowing consumers to provide their own customfetchimplementation for network requests.fetchimplementation is now utilized for all internal network operations within these transports, including initial SSE connections (GET), sending messages (POST), and session termination (DELETE).FetchLiketype definition insrc/shared/transport.tsto ensure consistent typing for customfetchfunctions across the codebase.fetchimplementation, specifically ensuring proper authentication header propagation for various request types.Changelog
SSEClientTransportcorrectly utilizes a customfetchimplementation for both initial connection and subsequent POST requests, ensuring proper header propagation.FetchLiketype fromsrc/shared/transport.ts.fetchoption toSSEClientTransportOptionsand a corresponding private_fetchproperty.fetchcalls within_startOrAuth()andsend()methods to use the provided customfetchor fall back to the globalfetch.??) forfetchfallback logic as per review suggestions.StartSSEOptionsinterface for improved type safety in tests.StreamableHTTPClientTransportuses the customfetchimplementation for all its network operations, including authentication.FetchLiketype fromsrc/shared/transport.ts.StartSSEOptionsinterface.fetchoption toStreamableHTTPClientTransportOptionsand a corresponding private_fetchproperty._startOrAuthSse(),send(), andterminate()methods to use the customfetchimplementation for their respective network requests.??) forfetchfallback logic as per review suggestions.FetchLiketype, standardizing the signature for customfetchfunctions across the codebase.Motivation and Context
Picks up on the work started in abandoned PR #296, refining the implementation, extending it to include StreamableHttp in addition to SSE, and adds tests for both.
The ultimate motivation is in the requirement by a pending PR in the Inspector project, which seeks to bridge the WWW-Authenticate header returned by an MCP server back to the Inspector client within the Inspector proxy. This makes use of the custom fetch function to capture the header.
How Has This Been Tested?
New unit tests.
Breaking Changes
Nope.
Types of changes
Checklist
Additional context
Fixes #476