Skip to content

Commit 3975c8d

Browse files
author
aheizi
committed
replace eventsource with reconnecting-eventsource to optimize the code.
(cherry picked from commit 505f86d)
1 parent 6f5ce14 commit 3975c8d

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

package-lock.json

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@
333333
"delay": "^6.0.0",
334334
"diff": "^5.2.0",
335335
"diff-match-patch": "^1.0.5",
336-
"eventsource": "^3.0.5",
337336
"fast-deep-equal": "^3.1.3",
338337
"fastest-levenshtein": "^1.0.16",
339338
"get-folder-size": "^5.0.0",
@@ -351,6 +350,7 @@
351350
"pretty-bytes": "^6.1.1",
352351
"puppeteer-chromium-resolver": "^23.0.0",
353352
"puppeteer-core": "^23.4.0",
353+
"reconnecting-eventsource": "^1.6.4",
354354
"serialize-error": "^11.0.3",
355355
"simple-git": "^3.27.0",
356356
"sound-play": "^1.1.0",

src/services/mcp/McpHub.ts

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
22
import { StdioClientTransport, StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js"
33
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js"
4-
import { EventSource } from "eventsource"
4+
import ReconnectingEventSource from "reconnecting-eventsource"
55
import {
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

Comments
 (0)