@@ -12,69 +12,18 @@ import type { CompositeLogger } from "./logger.js";
12
12
import { LogId } from "./logger.js" ;
13
13
import type { ConnectionInfo } from "@mongosh/arg-parser" ;
14
14
import { 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 > {
76
26
private deviceId : DeviceId ;
77
- private clientName : string ;
78
27
private bus : EventEmitter ;
79
28
80
29
constructor (
@@ -85,23 +34,15 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
85
34
bus ?: EventEmitter
86
35
) {
87
36
super ( ) ;
88
-
89
37
this . bus = bus ?? new EventEmitter ( ) ;
90
- this . state = { tag : "disconnected" } ;
91
-
92
38
this . bus . on ( "mongodb-oidc-plugin:auth-failed" , this . onOidcAuthFailed . bind ( this ) ) ;
93
39
this . bus . on ( "mongodb-oidc-plugin:auth-succeeded" , this . onOidcAuthSucceeded . bind ( this ) ) ;
94
-
95
40
this . deviceId = deviceId ;
96
41
this . clientName = "unknown" ;
97
42
}
98
43
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 ) ;
105
46
106
47
if ( this . state . tag === "connected" || this . state . tag === "connecting" ) {
107
48
await this . disconnect ( ) ;
@@ -111,22 +52,22 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
111
52
let connectionInfo : ConnectionInfo ;
112
53
113
54
try {
114
- settings = { ...settings } ;
55
+ connectParams = { ...connectParams } ;
115
56
const appNameComponents : AppNameComponents = {
116
57
appName : `${ packageInfo . mcpServerName } ${ packageInfo . version } ` ,
117
58
deviceId : this . deviceId . get ( ) ,
118
59
clientName : this . clientName ,
119
60
} ;
120
61
121
- settings . connectionString = await setAppNameParamIfMissing ( {
122
- connectionString : settings . connectionString ,
62
+ connectParams . connectionString = await setAppNameParamIfMissing ( {
63
+ connectionString : connectParams . connectionString ,
123
64
components : appNameComponents ,
124
65
} ) ;
125
66
126
67
connectionInfo = generateConnectionInfoFromCliArgs ( {
127
68
...this . userConfig ,
128
69
...this . driverOptions ,
129
- connectionSpecifier : settings . connectionString ,
70
+ connectionSpecifier : connectParams . connectionString ,
130
71
} ) ;
131
72
132
73
if ( connectionInfo . driverOptions . oidc ) {
@@ -152,7 +93,7 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
152
93
this . changeState ( "connection-errored" , {
153
94
tag : "errored" ,
154
95
errorReason,
155
- connectedAtlasCluster : settings . atlas ,
96
+ connectedAtlasCluster : connectParams . atlas ,
156
97
} ) ;
157
98
throw new MongoDBError ( ErrorCodes . MisconfiguredConnectionString , errorReason ) ;
158
99
}
@@ -167,7 +108,7 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
167
108
168
109
return this . changeState ( "connection-requested" , {
169
110
tag : "connecting" ,
170
- connectedAtlasCluster : settings . atlas ,
111
+ connectedAtlasCluster : connectParams . atlas ,
171
112
serviceProvider,
172
113
connectionStringAuthType : connectionType ,
173
114
oidcConnectionType : connectionType as OIDCConnectionAuthType ,
@@ -178,7 +119,7 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
178
119
179
120
return this . changeState ( "connection-succeeded" , {
180
121
tag : "connected" ,
181
- connectedAtlasCluster : settings . atlas ,
122
+ connectedAtlasCluster : connectParams . atlas ,
182
123
serviceProvider,
183
124
connectionStringAuthType : connectionType ,
184
125
} ) ;
@@ -187,7 +128,7 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
187
128
this . changeState ( "connection-errored" , {
188
129
tag : "errored" ,
189
130
errorReason,
190
- connectedAtlasCluster : settings . atlas ,
131
+ connectedAtlasCluster : connectParams . atlas ,
191
132
} ) ;
192
133
throw new MongoDBError ( ErrorCodes . NotConnectedToMongoDB , errorReason ) ;
193
134
}
@@ -211,21 +152,6 @@ export class MCPConnectionManager extends EventEmitter<MCPConnectionManagerEvent
211
152
return { tag : "disconnected" } ;
212
153
}
213
154
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
-
229
155
private onOidcAuthFailed ( error : unknown ) : void {
230
156
if ( this . state . tag === "connecting" && this . state . connectionStringAuthType ?. startsWith ( "oidc" ) ) {
231
157
void this . disconnectOnOidcError ( error ) ;
0 commit comments