@@ -19,8 +19,10 @@ export interface MCPConnectParams {
1919}
2020
2121export class MCPConnectionManager extends ConnectionManager {
22- private activeConnectionId : string | null = null ;
23- private activeConnectionProvider : ServiceProvider | null = null ;
22+ private activeConnection : {
23+ id : string ;
24+ provider : ServiceProvider ;
25+ } | null = null ;
2426
2527 constructor ( private readonly logger : LoggerBase ) {
2628 super ( ) ;
@@ -46,17 +48,19 @@ export class MCPConnectionManager extends ConnectionManager {
4648 connectParams . connectOptions ,
4749 ) ;
4850 await serviceProvider . runCommand ( 'admin' , { hello : 1 } ) ;
49- this . activeConnectionId = connectParams . connectionId ;
50- this . activeConnectionProvider = serviceProvider ;
51+ this . activeConnection = {
52+ id : connectParams . connectionId ,
53+ provider : serviceProvider ,
54+ } ;
5155 return this . changeState ( 'connection-success' , {
5256 tag : 'connected' ,
5357 serviceProvider,
5458 } ) ;
5559 } catch ( error ) {
5660 this . logger . error ( {
5761 id : MCPLogIds . ConnectError ,
58- context : 'VSCodeMCPConnectionManager.connect ' ,
59- message : error instanceof Error ? error . message : String ( error ) ,
62+ context : 'vscode-mcp-connection-manager ' ,
63+ message : `Error connecting to VSCode connection - ${ error instanceof Error ? error . message : String ( error ) } ` ,
6064 } ) ;
6165 return this . changeState ( 'connection-error' , {
6266 tag : 'errored' ,
@@ -67,72 +71,48 @@ export class MCPConnectionManager extends ConnectionManager {
6771
6872 async disconnect ( ) : Promise < ConnectionStateDisconnected > {
6973 try {
70- await this . activeConnectionProvider ?. close ( true ) ;
74+ await this . activeConnection ?. provider ?. close ( true ) ;
7175 } catch ( error ) {
7276 this . logger . error ( {
7377 id : MCPLogIds . DisconnectError ,
74- context : 'VSCodeMCPConnectionManager.disconnect ' ,
75- message : error instanceof Error ? error . message : String ( error ) ,
78+ context : 'vscode-mcp-connection-manager ' ,
79+ message : `Error disconnecting from VSCode connection - ${ error instanceof Error ? error . message : String ( error ) } ` ,
7680 } ) ;
7781 }
7882
79- this . activeConnectionId = null ;
80- this . activeConnectionProvider = null ;
83+ this . activeConnection = null ;
8184 return this . changeState ( 'connection-close' , {
8285 tag : 'disconnected' ,
8386 } ) ;
8487 }
8588
86- async updateConnection ( {
87- connectionId,
88- connectionString,
89- connectOptions,
90- } : {
91- connectionId : string | undefined ;
92- connectionString : string | undefined ;
93- connectOptions : DevtoolsConnectOptions | undefined ;
94- } ) : Promise < void > {
95- try {
96- if ( this . activeConnectionId && this . activeConnectionId !== connectionId ) {
97- await this . disconnect ( ) ;
98- }
99-
100- const connectionWasDisconnected =
101- ! connectionId || ! connectionString || ! connectOptions ;
89+ async updateConnection (
90+ connectParams : MCPConnectParams | undefined ,
91+ ) : Promise < void > {
92+ if ( connectParams ?. connectionId === this . activeConnection ?. id ) {
93+ return ;
94+ }
10295
103- if (
104- this . activeConnectionId === connectionId ||
105- connectionWasDisconnected
106- ) {
107- return ;
108- }
96+ await this . disconnect ( ) ;
10997
110- if ( isAtlasStream ( connectionString ) ) {
111- this . logger . warning ( {
112- id : MCPLogIds . UpdateConnectionError ,
113- context : 'VSCodeMCPConnectionManager.updateConnection' ,
114- message : 'updateConnection called for an AtlasStreams connection' ,
115- } ) ;
116- this . changeState ( 'connection-error' , {
117- tag : 'errored' ,
118- errorReason :
119- 'MongoDB MCP server do not support connecting to Atlas Streams' ,
120- } ) ;
121- return ;
122- }
98+ if ( ! connectParams ) {
99+ return ;
100+ }
123101
124- await this . connectToVSCodeConnection ( {
125- connectionString,
126- connectOptions,
127- connectionId,
128- } ) ;
129- } catch ( error ) {
130- this . logger . error ( {
102+ if ( isAtlasStream ( connectParams . connectionString ) ) {
103+ this . logger . warning ( {
131104 id : MCPLogIds . UpdateConnectionError ,
132- context : 'VSCodeMCPConnectionManager.updateConnection ' ,
133- message : error instanceof Error ? error . message : String ( error ) ,
105+ context : 'vscode-mcp-connection-manager ' ,
106+ message : 'Attempting a connection to an AtlasStreams.' ,
134107 } ) ;
135- throw error ;
108+ this . changeState ( 'connection-error' , {
109+ tag : 'errored' ,
110+ errorReason :
111+ 'MongoDB MCP server does not support connecting to Atlas Streams' ,
112+ } ) ;
113+ return ;
136114 }
115+
116+ await this . connectToVSCodeConnection ( connectParams ) ;
137117 }
138118}
0 commit comments