@@ -12,69 +12,18 @@ import type { CompositeLogger } from "./logger.js";
1212import { LogId } from "./logger.js" ;
1313import type { ConnectionInfo } from "@mongosh/arg-parser" ;
1414import { generateConnectionInfoFromCliArgs } from "@mongosh/arg-parser" ;
15-
16- export interface AtlasClusterConnectionInfo {
17- username : string ;
18- projectId : string ;
19- clusterName : string ;
20- expiryDate : Date ;
21- }
22-
23- export interface ConnectionSettings {
24- connectionString : string ;
25- atlas ?: AtlasClusterConnectionInfo ;
26- }
27-
28- type ConnectionTag = "connected" | "connecting" | "disconnected" | "errored" ;
29- type OIDCConnectionAuthType = "oidc-auth-flow" | "oidc-device-flow" ;
30- export type ConnectionStringAuthType = "scram" | "ldap" | "kerberos" | OIDCConnectionAuthType | "x.509" ;
31-
32- export interface ConnectionState {
33- tag : ConnectionTag ;
34- connectionStringAuthType ?: ConnectionStringAuthType ;
35- connectedAtlasCluster ?: AtlasClusterConnectionInfo ;
36- }
37-
38- export interface ConnectionStateConnected extends ConnectionState {
39- tag : "connected" ;
40- serviceProvider : NodeDriverServiceProvider ;
41- }
42-
43- export interface ConnectionStateConnecting extends ConnectionState {
44- tag : "connecting" ;
45- serviceProvider : NodeDriverServiceProvider ;
46- oidcConnectionType : OIDCConnectionAuthType ;
47- oidcLoginUrl ?: string ;
48- oidcUserCode ?: string ;
49- }
50-
51- export interface ConnectionStateDisconnected extends ConnectionState {
52- tag : "disconnected" ;
53- }
54-
55- export interface ConnectionStateErrored extends ConnectionState {
56- tag : "errored" ;
57- errorReason : string ;
58- }
59-
60- export type AnyConnectionState =
61- | ConnectionStateConnected
62- | ConnectionStateConnecting
63- | ConnectionStateDisconnected
64- | ConnectionStateErrored ;
65-
66- export interface MCPConnectionManagerEvents {
67- "connection-requested" : [ AnyConnectionState ] ;
68- "connection-succeeded" : [ ConnectionStateConnected ] ;
69- "connection-timed-out" : [ ConnectionStateErrored ] ;
70- "connection-closed" : [ ConnectionStateDisconnected ] ;
71- "connection-errored" : [ ConnectionStateErrored ] ;
72- }
73-
74- export class MCPConnectionManager extends EventEmitter < MCPConnectionManagerEvents > {
75- private state : AnyConnectionState ;
15+ import {
16+ ConnectionManager ,
17+ type AnyConnectionState ,
18+ type ConnectionStringAuthType ,
19+ type OIDCConnectionAuthType ,
20+ type ConnectionStateDisconnected ,
21+ type ConnectionStateErrored ,
22+ type MCPConnectParams ,
23+ } from "./connectionManager.js" ;
24+
25+ export class MCPConnectionManager extends ConnectionManager < MCPConnectParams > {
7626 private deviceId : DeviceId ;
77- private clientName : string ;
7827 private bus : EventEmitter ;
7928
8029 constructor (
@@ -85,23 +34,15 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
8534 bus ?: EventEmitter
8635 ) {
8736 super ( ) ;
88-
8937 this . bus = bus ?? new EventEmitter ( ) ;
90- this . state = { tag : "disconnected" } ;
91-
9238 this . bus . on ( "mongodb-oidc-plugin:auth-failed" , this . onOidcAuthFailed . bind ( this ) ) ;
9339 this . bus . on ( "mongodb-oidc-plugin:auth-succeeded" , this . onOidcAuthSucceeded . bind ( this ) ) ;
94-
9540 this . deviceId = deviceId ;
9641 this . clientName = "unknown" ;
9742 }
9843
99- setClientName ( clientName : string ) : void {
100- this . clientName = clientName ;
101- }
102-
103- async connect ( settings : ConnectionSettings ) : Promise < AnyConnectionState > {
104- this . emit ( "connection-requested" , this . state ) ;
44+ async connect ( connectParams : MCPConnectParams ) : Promise < AnyConnectionState > {
45+ this . _events . emit ( "connection-requested" , this . state ) ;
10546
10647 if ( this . state . tag === "connected" || this . state . tag === "connecting" ) {
10748 await this . disconnect ( ) ;
@@ -111,22 +52,22 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
11152 let connectionInfo : ConnectionInfo ;
11253
11354 try {
114- settings = { ...settings } ;
55+ connectParams = { ...connectParams } ;
11556 const appNameComponents : AppNameComponents = {
11657 appName : `${ packageInfo . mcpServerName } ${ packageInfo . version } ` ,
11758 deviceId : this . deviceId . get ( ) ,
11859 clientName : this . clientName ,
11960 } ;
12061
121- settings . connectionString = await setAppNameParamIfMissing ( {
122- connectionString : settings . connectionString ,
62+ connectParams . connectionString = await setAppNameParamIfMissing ( {
63+ connectionString : connectParams . connectionString ,
12364 components : appNameComponents ,
12465 } ) ;
12566
12667 connectionInfo = generateConnectionInfoFromCliArgs ( {
12768 ...this . userConfig ,
12869 ...this . driverOptions ,
129- connectionSpecifier : settings . connectionString ,
70+ connectionSpecifier : connectParams . connectionString ,
13071 } ) ;
13172
13273 if ( connectionInfo . driverOptions . oidc ) {
@@ -152,7 +93,7 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
15293 this . changeState ( "connection-errored" , {
15394 tag : "errored" ,
15495 errorReason,
155- connectedAtlasCluster : settings . atlas ,
96+ connectedAtlasCluster : connectParams . atlas ,
15697 } ) ;
15798 throw new MongoDBError ( ErrorCodes . MisconfiguredConnectionString , errorReason ) ;
15899 }
@@ -167,7 +108,7 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
167108
168109 return this . changeState ( "connection-requested" , {
169110 tag : "connecting" ,
170- connectedAtlasCluster : settings . atlas ,
111+ connectedAtlasCluster : connectParams . atlas ,
171112 serviceProvider,
172113 connectionStringAuthType : connectionType ,
173114 oidcConnectionType : connectionType as OIDCConnectionAuthType ,
@@ -178,7 +119,7 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
178119
179120 return this . changeState ( "connection-succeeded" , {
180121 tag : "connected" ,
181- connectedAtlasCluster : settings . atlas ,
122+ connectedAtlasCluster : connectParams . atlas ,
182123 serviceProvider,
183124 connectionStringAuthType : connectionType ,
184125 } ) ;
@@ -187,7 +128,7 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
187128 this . changeState ( "connection-errored" , {
188129 tag : "errored" ,
189130 errorReason,
190- connectedAtlasCluster : settings . atlas ,
131+ connectedAtlasCluster : connectParams . atlas ,
191132 } ) ;
192133 throw new MongoDBError ( ErrorCodes . NotConnectedToMongoDB , errorReason ) ;
193134 }
@@ -211,21 +152,6 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
211152 return { tag : "disconnected" } ;
212153 }
213154
214- get currentConnectionState ( ) : AnyConnectionState {
215- return this . state ;
216- }
217-
218- changeState < Event extends keyof MCPConnectionManagerEvents , State extends MCPConnectionManagerEvents [ Event ] [ 0 ] > (
219- event : Event ,
220- newState : State
221- ) : State {
222- this . state = newState ;
223- // TypeScript doesn't seem to be happy with the spread operator and generics
224- // eslint-disable-next-line
225- this . emit ( event , ...( [ newState ] as any ) ) ;
226- return newState ;
227- }
228-
229155 private onOidcAuthFailed ( error : unknown ) : void {
230156 if ( this . state . tag === "connecting" && this . state . connectionStringAuthType ?. startsWith ( "oidc" ) ) {
231157 void this . disconnectOnOidcError ( error ) ;
0 commit comments