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