Skip to content

Commit c201baa

Browse files
author
aheizi
committed
fix mcp connection
1 parent 792a822 commit c201baa

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

src/services/mcp/McpHub.ts

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,20 @@ export class McpHub {
338338
console.error(`Server "${name}" stderr:`, errorOutput)
339339
const connection = this.connections.find((conn) => conn.server.name === name)
340340
if (connection) {
341-
// NOTE: we do not set server status to "disconnected" because stderr logs do not necessarily mean the server crashed or disconnected, it could just be informational. In fact when the server first starts up, it immediately logs "<name> server running on stdio" to stderr.
342-
this.appendErrorMessage(connection, errorOutput)
343-
// Only need to update webview right away if it's already disconnected
344-
if (connection.server.status === "disconnected") {
345-
await this.notifyWebviewOfServerChanges()
341+
// Filter out normal startup messages that appear in stderr
342+
const isStartupMessage =
343+
errorOutput.includes("server running") ||
344+
errorOutput.includes("MCP server running") ||
345+
errorOutput.includes('mode: "stdio"')
346+
347+
// Only append to error message if it's not a startup message
348+
if (!isStartupMessage) {
349+
// NOTE: we do not set server status to "disconnected" because stderr logs do not necessarily mean the server crashed or disconnected, it could just be informational.
350+
this.appendErrorMessage(connection, errorOutput)
351+
// Only need to update webview right away if it's already disconnected
352+
if (connection.server.status === "disconnected") {
353+
await this.notifyWebviewOfServerChanges()
354+
}
346355
}
347356
}
348357
})
@@ -526,13 +535,15 @@ export class McpHub {
526535
if (config) {
527536
vscode.window.showInformationMessage(`Restarting ${serverName} MCP server...`)
528537
connection.server.status = "connecting"
529-
connection.server.error = ""
538+
connection.server.error = "" // Clear any previous error messages
530539
await this.notifyWebviewOfServerChanges()
531540
await delay(500) // artificial delay to show user that server is restarting
532541
try {
542+
// Save the original source before deleting the connection
543+
const source = connection.server.source || "global"
533544
await this.deleteConnection(serverName)
534-
// Try to connect again using existing config
535-
await this.connectToServer(serverName, JSON.parse(config))
545+
// Try to connect again using existing config and preserve the original source
546+
await this.connectToServer(serverName, JSON.parse(config), source)
536547
vscode.window.showInformationMessage(`${serverName} MCP server connected`)
537548
} catch (error) {
538549
console.error(`Failed to restart connection for ${serverName}:`, error)
@@ -692,16 +703,30 @@ export class McpHub {
692703

693704
public async deleteServer(serverName: string): Promise<void> {
694705
try {
695-
const settingsPath = await this.getMcpSettingsFilePath()
706+
// Find the connection to determine if it's a global or project server
707+
const connection = this.connections.find((conn) => conn.server.name === serverName)
708+
const isProjectServer = connection?.server.source === "project"
709+
710+
// Determine which config file to modify
711+
let configPath: string
712+
if (isProjectServer) {
713+
const projectMcpPath = await this.getProjectMcpPath()
714+
if (!projectMcpPath) {
715+
throw new Error("Project MCP configuration file not found")
716+
}
717+
configPath = projectMcpPath
718+
} else {
719+
configPath = await this.getMcpSettingsFilePath()
720+
}
696721

697-
// Ensure the settings file exists and is accessible
722+
// Ensure the config file exists and is accessible
698723
try {
699-
await fs.access(settingsPath)
724+
await fs.access(configPath)
700725
} catch (error) {
701-
throw new Error("Settings file not accessible")
726+
throw new Error(`Configuration file not accessible: ${configPath}`)
702727
}
703728

704-
const content = await fs.readFile(settingsPath, "utf-8")
729+
const content = await fs.readFile(configPath, "utf-8")
705730
const config = JSON.parse(content)
706731

707732
// Validate the config structure
@@ -722,10 +747,17 @@ export class McpHub {
722747
mcpServers: config.mcpServers,
723748
}
724749

725-
await fs.writeFile(settingsPath, JSON.stringify(updatedConfig, null, 2))
750+
await fs.writeFile(configPath, JSON.stringify(updatedConfig, null, 2))
726751

727-
// Update server connections
728-
await this.updateServerConnections(config.mcpServers)
752+
// Delete the connection
753+
await this.deleteConnection(serverName)
754+
755+
// If it's a project server, update project servers, otherwise update global servers
756+
if (isProjectServer) {
757+
await this.updateProjectMcpServers()
758+
} else {
759+
await this.updateServerConnections(config.mcpServers)
760+
}
729761

730762
vscode.window.showInformationMessage(`Deleted MCP server: ${serverName}`)
731763
} else {

0 commit comments

Comments
 (0)