11import { Client } from "@modelcontextprotocol/sdk/client/index.js"
22import { StdioClientTransport , StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js"
33import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js"
4- import { EventSource } from "eventsource"
4+ import ReconnectingEventSource from "reconnecting- eventsource"
55import {
66 CallToolResultSchema ,
77 ListResourcesResultSchema ,
@@ -243,8 +243,16 @@ export class McpHub {
243243 headers : config . headers ,
244244 } ,
245245 }
246- global . EventSource = EventSource
247- transport = new SSEClientTransport ( new URL ( config . url ) , sseOptions )
246+ // Configure ReconnectingEventSource options
247+ const reconnectingEventSourceOptions = {
248+ max_retry_time : 5000 , // Maximum retry time in milliseconds
249+ withCredentials : config . headers ?. [ "Authorization" ] ? true : false , // Enable credentials if Authorization header exists
250+ }
251+ global . EventSource = ReconnectingEventSource
252+ transport = new SSEClientTransport ( new URL ( config . url ) , {
253+ ...sseOptions ,
254+ eventSourceInit : reconnectingEventSourceOptions ,
255+ } )
248256
249257 // Set up SSE specific error handling
250258 transport . onerror = async ( error ) => {
@@ -255,11 +263,6 @@ export class McpHub {
255263 this . appendErrorMessage ( connection , error . message )
256264 }
257265 await this . notifyWebviewOfServerChanges ( )
258-
259- // Attempt reconnection for SSE
260- if ( ! this . isDisposed ) {
261- await this . handleSSEReconnection ( name , config )
262- }
263266 }
264267 }
265268
@@ -295,26 +298,6 @@ export class McpHub {
295298 }
296299 }
297300
298- private async handleSSEReconnection ( name : string , config : z . infer < typeof ServerConfigSchema > ) : Promise < void > {
299- const maxRetries = 3
300- let retryCount = 0
301-
302- while ( retryCount < maxRetries ) {
303- try {
304- await delay ( Math . pow ( 2 , retryCount ) * 1000 ) // Exponential backoff
305- await this . connectToServer ( name , config )
306- return
307- } catch ( error ) {
308- retryCount ++
309- console . error ( `Retry ${ retryCount } failed for ${ name } :` , error )
310- }
311- }
312-
313- vscode . window . showErrorMessage (
314- `Failed to reconnect to ${ name } after ${ maxRetries } attempts. Please check your connection.` ,
315- )
316- }
317-
318301 private appendErrorMessage ( connection : McpConnection , error : string ) {
319302 const newError = connection . server . error ? `${ connection . server . error } \n${ error } ` : error
320303 connection . server . error = newError //.slice(0, 800)
0 commit comments