Skip to content

Commit 7485066

Browse files
committed
feat: delegate MCP enable/disable logic to McpHub and streamline connection management
1 parent 90cf89d commit 7485066

File tree

2 files changed

+61
-50
lines changed

2 files changed

+61
-50
lines changed

src/core/webview/webviewMessageHandler.ts

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -881,57 +881,10 @@ export const webviewMessageHandler = async (
881881
const mcpEnabled = message.bool ?? true
882882
await updateGlobalState("mcpEnabled", mcpEnabled)
883883

884-
// If MCP is being disabled, disconnect all servers
884+
// Delegate MCP enable/disable logic to McpHub
885885
const mcpHubInstance = provider.getMcpHub()
886-
if (!mcpEnabled && mcpHubInstance) {
887-
// Disconnect all existing connections with error handling
888-
const existingConnections = [...mcpHubInstance.connections]
889-
const disconnectionErrors: Array<{ serverName: string; error: string }> = []
890-
891-
for (const conn of existingConnections) {
892-
try {
893-
await mcpHubInstance.deleteConnection(conn.server.name, conn.server.source)
894-
} catch (error) {
895-
const errorMessage = error instanceof Error ? error.message : String(error)
896-
disconnectionErrors.push({
897-
serverName: conn.server.name,
898-
error: errorMessage,
899-
})
900-
provider.log(`Failed to disconnect MCP server ${conn.server.name}: ${errorMessage}`)
901-
}
902-
}
903-
904-
// If there were errors, notify the user
905-
if (disconnectionErrors.length > 0) {
906-
const errorSummary = disconnectionErrors.map((e) => `${e.serverName}: ${e.error}`).join("\n")
907-
vscode.window.showWarningMessage(
908-
t("mcp:errors.disconnect_servers_partial", {
909-
count: disconnectionErrors.length,
910-
errors: errorSummary,
911-
}) ||
912-
`Failed to disconnect ${disconnectionErrors.length} MCP server(s). Check the output for details.`,
913-
)
914-
}
915-
916-
// Re-initialize servers to track them in disconnected state
917-
try {
918-
await mcpHubInstance.refreshAllConnections()
919-
} catch (error) {
920-
provider.log(`Failed to refresh MCP connections after disabling: ${error}`)
921-
vscode.window.showErrorMessage(
922-
t("mcp:errors.refresh_after_disable") || "Failed to refresh MCP connections after disabling",
923-
)
924-
}
925-
} else if (mcpEnabled && mcpHubInstance) {
926-
// If MCP is being enabled, reconnect all servers
927-
try {
928-
await mcpHubInstance.refreshAllConnections()
929-
} catch (error) {
930-
provider.log(`Failed to refresh MCP connections after enabling: ${error}`)
931-
vscode.window.showErrorMessage(
932-
t("mcp:errors.refresh_after_enable") || "Failed to refresh MCP connections after enabling",
933-
)
934-
}
886+
if (mcpHubInstance) {
887+
await mcpHubInstance.handleMcpEnabledChange(mcpEnabled)
935888
}
936889

937890
await provider.postStateToWebview()

src/services/mcp/McpHub.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,64 @@ export class McpHub {
17441744
}
17451745
}
17461746

1747+
/**
1748+
* Handles enabling/disabling MCP globally
1749+
* @param enabled Whether MCP should be enabled or disabled
1750+
* @returns Promise<void>
1751+
*/
1752+
async handleMcpEnabledChange(enabled: boolean): Promise<void> {
1753+
if (!enabled) {
1754+
// If MCP is being disabled, disconnect all servers with error handling
1755+
const existingConnections = [...this.connections]
1756+
const disconnectionErrors: Array<{ serverName: string; error: string }> = []
1757+
1758+
for (const conn of existingConnections) {
1759+
try {
1760+
await this.deleteConnection(conn.server.name, conn.server.source)
1761+
} catch (error) {
1762+
const errorMessage = error instanceof Error ? error.message : String(error)
1763+
disconnectionErrors.push({
1764+
serverName: conn.server.name,
1765+
error: errorMessage,
1766+
})
1767+
console.error(`Failed to disconnect MCP server ${conn.server.name}: ${errorMessage}`)
1768+
}
1769+
}
1770+
1771+
// If there were errors, notify the user
1772+
if (disconnectionErrors.length > 0) {
1773+
const errorSummary = disconnectionErrors.map((e) => `${e.serverName}: ${e.error}`).join("\n")
1774+
vscode.window.showWarningMessage(
1775+
t("mcp:errors.disconnect_servers_partial", {
1776+
count: disconnectionErrors.length,
1777+
errors: errorSummary,
1778+
}) ||
1779+
`Failed to disconnect ${disconnectionErrors.length} MCP server(s). Check the output for details.`,
1780+
)
1781+
}
1782+
1783+
// Re-initialize servers to track them in disconnected state
1784+
try {
1785+
await this.refreshAllConnections()
1786+
} catch (error) {
1787+
console.error(`Failed to refresh MCP connections after disabling: ${error}`)
1788+
vscode.window.showErrorMessage(
1789+
t("mcp:errors.refresh_after_disable") || "Failed to refresh MCP connections after disabling",
1790+
)
1791+
}
1792+
} else {
1793+
// If MCP is being enabled, reconnect all servers
1794+
try {
1795+
await this.refreshAllConnections()
1796+
} catch (error) {
1797+
console.error(`Failed to refresh MCP connections after enabling: ${error}`)
1798+
vscode.window.showErrorMessage(
1799+
t("mcp:errors.refresh_after_enable") || "Failed to refresh MCP connections after enabling",
1800+
)
1801+
}
1802+
}
1803+
}
1804+
17471805
async dispose(): Promise<void> {
17481806
// Prevent multiple disposals
17491807
if (this.isDisposed) {

0 commit comments

Comments
 (0)