Skip to content

Commit 4444027

Browse files
committed
fix: prevent MCP connection error when toggling Always Allow
- Add connection state check before fetching tools list - Handle disconnected servers gracefully when updating config - Catch and log errors without disrupting UI flow Fixes #8598
1 parent 3a47c55 commit 4444027

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/services/mcp/McpHub.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,17 @@ export class McpHub {
16891689
await fs.writeFile(normalizedPath, JSON.stringify(config, null, 2))
16901690

16911691
if (connection) {
1692-
connection.server.tools = await this.fetchToolsList(serverName, source)
1692+
// Check if the connection is still active before fetching tools
1693+
if (connection.type === "connected" && connection.server.status === "connected") {
1694+
try {
1695+
connection.server.tools = await this.fetchToolsList(serverName, source)
1696+
} catch (error) {
1697+
// If fetching tools fails, log the error but don't throw
1698+
// This prevents the "Connection closed" error from disrupting the UI
1699+
console.error(`Failed to fetch tools after updating ${listName} for ${serverName}:`, error)
1700+
// The tools list will be refreshed on the next successful connection
1701+
}
1702+
}
16931703
await this.notifyWebviewOfServerChanges()
16941704
}
16951705
}
@@ -1701,6 +1711,18 @@ export class McpHub {
17011711
shouldAllow: boolean,
17021712
): Promise<void> {
17031713
try {
1714+
// Check if the connection exists and is active before proceeding
1715+
const connection = this.findConnection(serverName, source)
1716+
if (!connection) {
1717+
throw new Error(`Server ${serverName} with source ${source} not found`)
1718+
}
1719+
1720+
// If the server is disconnected, we can still update the config
1721+
// but we should handle it gracefully
1722+
if (connection.type === "disconnected" || connection.server.status === "disconnected") {
1723+
console.warn(`Server ${serverName} is disconnected. Updating configuration only.`)
1724+
}
1725+
17041726
await this.updateServerToolList(serverName, source, toolName, "alwaysAllow", shouldAllow)
17051727
} catch (error) {
17061728
this.showErrorMessage(

0 commit comments

Comments
 (0)