@@ -2,8 +2,12 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
2
import {
3
3
SSEClientTransport ,
4
4
SseError ,
5
+ SSEClientTransportOptions ,
5
6
} from "@modelcontextprotocol/sdk/client/sse.js" ;
6
- import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js" ;
7
+ import {
8
+ StreamableHTTPClientTransport ,
9
+ StreamableHTTPClientTransportOptions ,
10
+ } from "@modelcontextprotocol/sdk/client/streamableHttp.js" ;
7
11
import {
8
12
ClientNotification ,
9
13
ClientRequest ,
@@ -279,29 +283,6 @@ export function useConnection({
279
283
setConnectionStatus ( "error-connecting-to-proxy" ) ;
280
284
return ;
281
285
}
282
- let mcpProxyServerUrl ;
283
- switch ( transportType ) {
284
- case "stdio" :
285
- mcpProxyServerUrl = new URL ( `${ getMCPProxyAddress ( config ) } /stdio` ) ;
286
- mcpProxyServerUrl . searchParams . append ( "command" , command ) ;
287
- mcpProxyServerUrl . searchParams . append ( "args" , args ) ;
288
- mcpProxyServerUrl . searchParams . append ( "env" , JSON . stringify ( env ) ) ;
289
- break ;
290
-
291
- case "sse" :
292
- mcpProxyServerUrl = new URL ( `${ getMCPProxyAddress ( config ) } /sse` ) ;
293
- mcpProxyServerUrl . searchParams . append ( "url" , sseUrl ) ;
294
- break ;
295
-
296
- case "streamable-http" :
297
- mcpProxyServerUrl = new URL ( `${ getMCPProxyAddress ( config ) } /mcp` ) ;
298
- mcpProxyServerUrl . searchParams . append ( "url" , sseUrl ) ;
299
- break ;
300
- }
301
- ( mcpProxyServerUrl as URL ) . searchParams . append (
302
- "transportType" ,
303
- transportType ,
304
- ) ;
305
286
306
287
try {
307
288
// Inject auth manually instead of using SSEClientTransport, because we're
@@ -320,21 +301,82 @@ export function useConnection({
320
301
}
321
302
322
303
// Create appropriate transport
323
- const transportOptions = {
324
- eventSourceInit : {
325
- fetch : (
326
- url : string | URL | globalThis . Request ,
327
- init : RequestInit | undefined ,
328
- ) => fetch ( url , { ...init , headers } ) ,
329
- } ,
330
- requestInit : {
331
- headers,
332
- } ,
333
- } ;
304
+ let transportOptions :
305
+ | StreamableHTTPClientTransportOptions
306
+ | SSEClientTransportOptions ;
307
+
308
+ let mcpProxyServerUrl ;
309
+ switch ( transportType ) {
310
+ case "stdio" :
311
+ mcpProxyServerUrl = new URL ( `${ getMCPProxyAddress ( config ) } /stdio` ) ;
312
+ mcpProxyServerUrl . searchParams . append ( "command" , command ) ;
313
+ mcpProxyServerUrl . searchParams . append ( "args" , args ) ;
314
+ mcpProxyServerUrl . searchParams . append ( "env" , JSON . stringify ( env ) ) ;
315
+ transportOptions = {
316
+ authProvider : serverAuthProvider ,
317
+ eventSourceInit : {
318
+ fetch : (
319
+ url : string | URL | globalThis . Request ,
320
+ init : RequestInit | undefined ,
321
+ ) => fetch ( url , { ...init , headers } ) ,
322
+ } ,
323
+ requestInit : {
324
+ headers,
325
+ } ,
326
+ } ;
327
+ break ;
328
+
329
+ case "sse" :
330
+ mcpProxyServerUrl = new URL ( `${ getMCPProxyAddress ( config ) } /sse` ) ;
331
+ mcpProxyServerUrl . searchParams . append ( "url" , sseUrl ) ;
332
+ transportOptions = {
333
+ authProvider : serverAuthProvider ,
334
+ eventSourceInit : {
335
+ fetch : (
336
+ url : string | URL | globalThis . Request ,
337
+ init : RequestInit | undefined ,
338
+ ) => fetch ( url , { ...init , headers } ) ,
339
+ } ,
340
+ requestInit : {
341
+ headers,
342
+ } ,
343
+ } ;
344
+ break ;
345
+
346
+ case "streamable-http" :
347
+ mcpProxyServerUrl = new URL ( `${ getMCPProxyAddress ( config ) } /mcp` ) ;
348
+ mcpProxyServerUrl . searchParams . append ( "url" , sseUrl ) ;
349
+ transportOptions = {
350
+ authProvider : serverAuthProvider ,
351
+ eventSourceInit : {
352
+ fetch : (
353
+ url : string | URL | globalThis . Request ,
354
+ init : RequestInit | undefined ,
355
+ ) => fetch ( url , { ...init , headers } ) ,
356
+ } ,
357
+ requestInit : {
358
+ headers,
359
+ } ,
360
+ // TODO these should be configurable...
361
+ reconnectionOptions : {
362
+ maxReconnectionDelay : 30000 ,
363
+ initialReconnectionDelay : 1000 ,
364
+ reconnectionDelayGrowFactor : 1.5 ,
365
+ maxRetries : 2 ,
366
+ } ,
367
+ } ;
368
+ break ;
369
+ }
370
+ ( mcpProxyServerUrl as URL ) . searchParams . append (
371
+ "transportType" ,
372
+ transportType ,
373
+ ) ;
374
+
334
375
const clientTransport =
335
376
transportType === "streamable-http"
336
377
? new StreamableHTTPClientTransport ( mcpProxyServerUrl as URL , {
337
378
sessionId : undefined ,
379
+ ...transportOptions ,
338
380
} )
339
381
: new SSEClientTransport ( mcpProxyServerUrl as URL , transportOptions ) ;
340
382
0 commit comments