1
- import { Transport } from "../shared/transport.js" ;
1
+ import { Transport , FetchLike } from "../shared/transport.js" ;
2
2
import { isInitializedNotification , isJSONRPCRequest , isJSONRPCResponse , JSONRPCMessage , JSONRPCMessageSchema } from "../types.js" ;
3
3
import { auth , AuthResult , extractResourceMetadataUrl , OAuthClientProvider , UnauthorizedError } from "./auth.js" ;
4
4
import { EventSourceParserStream } from "eventsource-parser/stream" ;
@@ -23,7 +23,7 @@ export class StreamableHTTPError extends Error {
23
23
/**
24
24
* Options for starting or authenticating an SSE connection
25
25
*/
26
- interface StartSSEOptions {
26
+ export interface StartSSEOptions {
27
27
/**
28
28
* The resumption token used to continue long-running requests that were interrupted.
29
29
*
@@ -99,6 +99,11 @@ export type StreamableHTTPClientTransportOptions = {
99
99
*/
100
100
requestInit ?: RequestInit ;
101
101
102
+ /**
103
+ * Custom fetch implementation used for all network requests.
104
+ */
105
+ fetch ?: FetchLike ;
106
+
102
107
/**
103
108
* Options to configure the reconnection behavior.
104
109
*/
@@ -122,6 +127,7 @@ export class StreamableHTTPClientTransport implements Transport {
122
127
private _resourceMetadataUrl ?: URL ;
123
128
private _requestInit ?: RequestInit ;
124
129
private _authProvider ?: OAuthClientProvider ;
130
+ private _fetch ?: FetchLike ;
125
131
private _sessionId ?: string ;
126
132
private _reconnectionOptions : StreamableHTTPReconnectionOptions ;
127
133
private _protocolVersion ?: string ;
@@ -138,6 +144,7 @@ export class StreamableHTTPClientTransport implements Transport {
138
144
this . _resourceMetadataUrl = undefined ;
139
145
this . _requestInit = opts ?. requestInit ;
140
146
this . _authProvider = opts ?. authProvider ;
147
+ this . _fetch = opts ?. fetch ;
141
148
this . _sessionId = opts ?. sessionId ;
142
149
this . _reconnectionOptions = opts ?. reconnectionOptions ?? DEFAULT_STREAMABLE_HTTP_RECONNECTION_OPTIONS ;
143
150
}
@@ -200,7 +207,7 @@ export class StreamableHTTPClientTransport implements Transport {
200
207
headers . set ( "last-event-id" , resumptionToken ) ;
201
208
}
202
209
203
- const response = await fetch ( this . _url , {
210
+ const response = await ( this . _fetch ?? fetch ) ( this . _url , {
204
211
method : "GET" ,
205
212
headers,
206
213
signal : this . _abortController ?. signal ,
@@ -251,15 +258,15 @@ export class StreamableHTTPClientTransport implements Transport {
251
258
252
259
private _normalizeHeaders ( headers : HeadersInit | undefined ) : Record < string , string > {
253
260
if ( ! headers ) return { } ;
254
-
261
+
255
262
if ( headers instanceof Headers ) {
256
263
return Object . fromEntries ( headers . entries ( ) ) ;
257
264
}
258
-
265
+
259
266
if ( Array . isArray ( headers ) ) {
260
267
return Object . fromEntries ( headers ) ;
261
268
}
262
-
269
+
263
270
return { ...headers as Record < string , string > } ;
264
271
}
265
272
@@ -414,7 +421,7 @@ export class StreamableHTTPClientTransport implements Transport {
414
421
signal : this . _abortController ?. signal ,
415
422
} ;
416
423
417
- const response = await fetch ( this . _url , init ) ;
424
+ const response = await ( this . _fetch ?? fetch ) ( this . _url , init ) ;
418
425
419
426
// Handle session ID received during initialization
420
427
const sessionId = response . headers . get ( "mcp-session-id" ) ;
@@ -520,7 +527,7 @@ export class StreamableHTTPClientTransport implements Transport {
520
527
signal : this . _abortController ?. signal ,
521
528
} ;
522
529
523
- const response = await fetch ( this . _url , init ) ;
530
+ const response = await ( this . _fetch ?? fetch ) ( this . _url , init ) ;
524
531
525
532
// We specifically handle 405 as a valid response according to the spec,
526
533
// meaning the server does not support explicit session termination
0 commit comments