1- import { Transport } from "../shared/transport.js" ;
1+ import { Transport , FetchLike } from "../shared/transport.js" ;
22import { isInitializedNotification , isJSONRPCRequest , isJSONRPCResponse , JSONRPCMessage , JSONRPCMessageSchema } from "../types.js" ;
33import { auth , AuthResult , extractResourceMetadataUrl , OAuthClientProvider , UnauthorizedError } from "./auth.js" ;
44import { EventSourceParserStream } from "eventsource-parser/stream" ;
@@ -23,7 +23,7 @@ export class StreamableHTTPError extends Error {
2323/**
2424 * Options for starting or authenticating an SSE connection
2525 */
26- interface StartSSEOptions {
26+ export interface StartSSEOptions {
2727 /**
2828 * The resumption token used to continue long-running requests that were interrupted.
2929 *
@@ -99,6 +99,11 @@ export type StreamableHTTPClientTransportOptions = {
9999 */
100100 requestInit ?: RequestInit ;
101101
102+ /**
103+ * Custom fetch implementation used for all network requests.
104+ */
105+ fetch ?: FetchLike ;
106+
102107 /**
103108 * Options to configure the reconnection behavior.
104109 */
@@ -122,6 +127,7 @@ export class StreamableHTTPClientTransport implements Transport {
122127 private _resourceMetadataUrl ?: URL ;
123128 private _requestInit ?: RequestInit ;
124129 private _authProvider ?: OAuthClientProvider ;
130+ private _fetch ?: FetchLike ;
125131 private _sessionId ?: string ;
126132 private _reconnectionOptions : StreamableHTTPReconnectionOptions ;
127133 private _protocolVersion ?: string ;
@@ -138,6 +144,7 @@ export class StreamableHTTPClientTransport implements Transport {
138144 this . _resourceMetadataUrl = undefined ;
139145 this . _requestInit = opts ?. requestInit ;
140146 this . _authProvider = opts ?. authProvider ;
147+ this . _fetch = opts ?. fetch ;
141148 this . _sessionId = opts ?. sessionId ;
142149 this . _reconnectionOptions = opts ?. reconnectionOptions ?? DEFAULT_STREAMABLE_HTTP_RECONNECTION_OPTIONS ;
143150 }
@@ -200,7 +207,7 @@ export class StreamableHTTPClientTransport implements Transport {
200207 headers . set ( "last-event-id" , resumptionToken ) ;
201208 }
202209
203- const response = await fetch ( this . _url , {
210+ const response = await ( this . _fetch ?? fetch ) ( this . _url , {
204211 method : "GET" ,
205212 headers,
206213 signal : this . _abortController ?. signal ,
@@ -251,15 +258,15 @@ export class StreamableHTTPClientTransport implements Transport {
251258
252259 private _normalizeHeaders ( headers : HeadersInit | undefined ) : Record < string , string > {
253260 if ( ! headers ) return { } ;
254-
261+
255262 if ( headers instanceof Headers ) {
256263 return Object . fromEntries ( headers . entries ( ) ) ;
257264 }
258-
265+
259266 if ( Array . isArray ( headers ) ) {
260267 return Object . fromEntries ( headers ) ;
261268 }
262-
269+
263270 return { ...headers as Record < string , string > } ;
264271 }
265272
@@ -414,7 +421,7 @@ export class StreamableHTTPClientTransport implements Transport {
414421 signal : this . _abortController ?. signal ,
415422 } ;
416423
417- const response = await fetch ( this . _url , init ) ;
424+ const response = await ( this . _fetch ?? fetch ) ( this . _url , init ) ;
418425
419426 // Handle session ID received during initialization
420427 const sessionId = response . headers . get ( "mcp-session-id" ) ;
@@ -520,7 +527,7 @@ export class StreamableHTTPClientTransport implements Transport {
520527 signal : this . _abortController ?. signal ,
521528 } ;
522529
523- const response = await fetch ( this . _url , init ) ;
530+ const response = await ( this . _fetch ?? fetch ) ( this . _url , init ) ;
524531
525532 // We specifically handle 405 as a valid response according to the spec,
526533 // meaning the server does not support explicit session termination
0 commit comments