Throw error and exit on MCP failure in headless mode (#8559)

In headless/CLI mode, when an MCP server fails to load, the process now
throws an error and exits rather than silently continuing. This provides
clear feedback to users about configuration issues.

Changes:
- Track headless mode flag in MCPService
- Throw errors on MCP connection failures in headless mode
- Throw errors when MCP tools/prompts fail to load in headless mode
- Aggregate and report all MCP failures after connection attempts

Fixes CON-4568


Generated with [Continue](https://continue.dev)

Co-authored-by: continue[bot] <continue[bot]@users.noreply.github.com>
Co-authored-by: Continue <noreply@continue.dev>
This commit is contained in:
continue[bot] 2025-11-03 14:46:03 -08:00 • committed by GitHub
parent 6aad84013c
commit 794de1b197
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -50,6 +50,7 @@ export class MCPService
private connections: Map<string, ServerConnection> = new Map();
private assistant: AssistantConfig | null = null;
private isShuttingDown = false;
private isHeadless = false;
getDependencies(): string[] {
return [SERVICE_NAMES.CONFIG];
@ -77,6 +78,9 @@ export class MCPService
serverCount: assistant.mcpServers?.length || 0,
});
// Store headless mode flag
this.isHeadless = waitForConnections;
await this.shutdownConnections();
this.assistant = assistant;
@ -103,6 +107,19 @@ export class MCPService
);
if (waitForConnections) {
await connectionInit;
// In headless mode, throw error if any MCP server failed to connect
const failedConnections = Array.from(this.connections.values()).filter(
(c) => c.status === "error",
);
if (failedConnections.length > 0) {
const errorMessages = failedConnections.map(
(c) => `${c.config?.name}: ${c.error}`,
);
throw new Error(
`MCP server(s) failed to load in headless mode:\n${errorMessages.join("\n")}`,
);
}
} else {
this.updateState();
}
@ -259,6 +276,13 @@ export class MCPService
name: serverName,
error: errorMessage,
});
// In headless mode, throw error on capability failures
if (this.isHeadless) {
throw new Error(
`Failed to load prompts from MCP server ${serverName}: ${errorMessage}`,
);
}
}
}
@ -276,18 +300,30 @@ export class MCPService
name: serverName,
error: errorMessage,
});
// In headless mode, throw error on capability failures
if (this.isHeadless) {
throw new Error(
`Failed to load tools from MCP server ${serverName}: ${errorMessage}`,
);
}
}
}
logger.debug("MCP server restarted successfully", { name: serverName });
logger.debug("MCP server connected successfully", { name: serverName });
} catch (error) {
const errorMessage = getErrorString(error);
connection.status = "error";
connection.error = errorMessage;
logger.error("Failed to restart MCP server", {
logger.error("Failed to connect to MCP server", {
name: serverName,
error: errorMessage,
});
// In headless mode, re-throw the error to fail fast
if (this.isHeadless) {
throw error;
}
}
this.updateState();