fix: Clean up transport on IDE connection failure (#4902)

This commit is contained in:
Shreya Keshive 2025-07-25 21:57:34 -04:00 committed by GitHub
parent ca5dd28ab6
commit 771cb229ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 3 deletions

View File

@ -10,8 +10,7 @@ import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/
const logger = { const logger = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
debug: (...args: any[]) => debug: (...args: any[]) => console.debug('[DEBUG] [IDEClient]', ...args),
console.debug('[DEBUG] [ImportProcessor]', ...args),
}; };
export type IDEConnectionState = { export type IDEConnectionState = {
@ -37,6 +36,7 @@ export class IdeClient {
logger.debug('Failed to initialize IdeClient:', err); logger.debug('Failed to initialize IdeClient:', err);
}); });
} }
getConnectionStatus(): { getConnectionStatus(): {
status: IDEConnectionStatus; status: IDEConnectionStatus;
details?: string; details?: string;
@ -64,16 +64,18 @@ export class IdeClient {
return; return;
} }
let transport: StreamableHTTPClientTransport | undefined;
try { try {
this.client = new Client({ this.client = new Client({
name: 'streamable-http-client', name: 'streamable-http-client',
// TODO(#3487): use the CLI version here. // TODO(#3487): use the CLI version here.
version: '1.0.0', version: '1.0.0',
}); });
const transport = new StreamableHTTPClientTransport( transport = new StreamableHTTPClientTransport(
new URL(`http://localhost:${idePort}/mcp`), new URL(`http://localhost:${idePort}/mcp`),
); );
await this.client.connect(transport); await this.client.connect(transport);
this.client.setNotificationHandler( this.client.setNotificationHandler(
OpenFilesNotificationSchema, OpenFilesNotificationSchema,
(notification) => { (notification) => {
@ -95,6 +97,13 @@ export class IdeClient {
} catch (error) { } catch (error) {
this.connectionStatus = IDEConnectionStatus.Disconnected; this.connectionStatus = IDEConnectionStatus.Disconnected;
logger.debug('Failed to connect to MCP server:', error); logger.debug('Failed to connect to MCP server:', error);
if (transport) {
try {
await transport.close();
} catch (closeError) {
logger.debug('Failed to close transport:', closeError);
}
}
} }
} }
} }