|
| 1 | +import { |
| 2 | + ConnectionManager, |
| 3 | + type AnyConnectionState, |
| 4 | + type CompositeLogger, |
| 5 | + type MCPConnectParams, |
| 6 | + type ConnectionStateDisconnected, |
| 7 | +} from 'mongodb-mcp-server'; |
| 8 | +import { |
| 9 | + NodeDriverServiceProvider, |
| 10 | + type DevtoolsConnectOptions, |
| 11 | +} from '@mongosh/service-provider-node-driver'; |
| 12 | +import type { ServiceProvider } from '@mongosh/service-provider-core'; |
| 13 | +import { isAtlasStream } from 'mongodb-build-info'; |
| 14 | +import { MCPLogIds } from './mcpLogIds'; |
| 15 | + |
| 16 | +export interface VSCodeMCPConnectParams extends MCPConnectParams { |
| 17 | + connectionId: string; |
| 18 | + connectOptions: DevtoolsConnectOptions; |
| 19 | +} |
| 20 | + |
| 21 | +export class VSCodeMCPConnectionManager extends ConnectionManager<VSCodeMCPConnectParams> { |
| 22 | + private activeConnectionId: string | null = null; |
| 23 | + private activeConnectionProvider: ServiceProvider | null = null; |
| 24 | + |
| 25 | + constructor(private readonly logger: CompositeLogger) { |
| 26 | + super(); |
| 27 | + } |
| 28 | + |
| 29 | + async connect( |
| 30 | + connectParams: VSCodeMCPConnectParams, |
| 31 | + ): Promise<AnyConnectionState> { |
| 32 | + try { |
| 33 | + const serviceProvider = (this.activeConnectionProvider = |
| 34 | + await NodeDriverServiceProvider.connect( |
| 35 | + connectParams.connectionString, |
| 36 | + connectParams.connectOptions, |
| 37 | + )); |
| 38 | + this.activeConnectionId = connectParams.connectionId; |
| 39 | + return this.changeState('connection-succeeded', { |
| 40 | + tag: 'connected', |
| 41 | + serviceProvider, |
| 42 | + }); |
| 43 | + } catch (error) { |
| 44 | + this.logger.error({ |
| 45 | + id: MCPLogIds.ConnectError, |
| 46 | + context: 'VSCodeMCPConnectionManager.connect', |
| 47 | + message: error instanceof Error ? error.message : String(error), |
| 48 | + }); |
| 49 | + return this.changeState('connection-errored', { |
| 50 | + tag: 'errored', |
| 51 | + errorReason: error instanceof Error ? error.message : String(error), |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + async disconnect(): Promise<ConnectionStateDisconnected> { |
| 57 | + try { |
| 58 | + await this.activeConnectionProvider?.close(true); |
| 59 | + } catch (error) { |
| 60 | + this.logger.error({ |
| 61 | + id: MCPLogIds.DisconnectError, |
| 62 | + context: 'VSCodeMCPConnectionManager.disconnect', |
| 63 | + message: error instanceof Error ? error.message : String(error), |
| 64 | + }); |
| 65 | + throw error; |
| 66 | + } finally { |
| 67 | + this.activeConnectionId = null; |
| 68 | + this.activeConnectionProvider = null; |
| 69 | + } |
| 70 | + return Promise.resolve({ tag: 'disconnected' }); |
| 71 | + } |
| 72 | + |
| 73 | + async updateConnection({ |
| 74 | + connectionId, |
| 75 | + connectionString, |
| 76 | + connectOptions, |
| 77 | + }: { |
| 78 | + connectionId: string | null; |
| 79 | + connectionString: string | undefined; |
| 80 | + connectOptions: DevtoolsConnectOptions | undefined; |
| 81 | + }): Promise<void> { |
| 82 | + try { |
| 83 | + if (this.activeConnectionId && this.activeConnectionId !== connectionId) { |
| 84 | + await this.disconnect(); |
| 85 | + } |
| 86 | + |
| 87 | + if (this.activeConnectionId === connectionId) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + if (!connectionString || !connectOptions || !connectionId) { |
| 92 | + this.changeState('connection-errored', { |
| 93 | + tag: 'errored', |
| 94 | + errorReason: |
| 95 | + 'MongoDB MCP server cannot establish connection without the required connection string and MongoClientOptions', |
| 96 | + }); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + if (isAtlasStream(connectionString)) { |
| 101 | + this.changeState('connection-errored', { |
| 102 | + tag: 'errored', |
| 103 | + errorReason: |
| 104 | + 'MongoDB MCP server do not support connecting to Atlas Streams', |
| 105 | + }); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + await this.connect({ |
| 110 | + connectionString, |
| 111 | + connectOptions, |
| 112 | + connectionId, |
| 113 | + }); |
| 114 | + } catch (error) { |
| 115 | + this.logger.error({ |
| 116 | + id: MCPLogIds.UpdateConnectionError, |
| 117 | + context: 'VSCodeMCPConnectionManager.updateConnection', |
| 118 | + message: error instanceof Error ? error.message : String(error), |
| 119 | + }); |
| 120 | + throw error; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments