@@ -23,43 +23,40 @@ export class ConnectClusterTool extends AtlasToolBase {
23
23
clusterName : z . string ( ) . describe ( "Atlas cluster name" ) ,
24
24
} ;
25
25
26
- private async queryConnection (
26
+ private queryConnection (
27
27
projectId : string ,
28
28
clusterName : string
29
- ) : Promise < "connected" | "disconnected" | "connecting" | "connected-to-other-cluster" | "unknown" > {
29
+ ) : "connected" | "disconnected" | "connecting" | "connected-to-other-cluster" | "unknown" {
30
30
if ( ! this . session . connectedAtlasCluster ) {
31
31
if ( this . session . isConnectedToMongoDB ) {
32
32
return "connected-to-other-cluster" ;
33
33
}
34
34
return "disconnected" ;
35
35
}
36
36
37
- if (
38
- this . session . connectedAtlasCluster . projectId !== projectId ||
39
- this . session . connectedAtlasCluster . clusterName !== clusterName
40
- ) {
41
- return "connected-to-other-cluster" ;
42
- }
43
-
44
- if ( this . session . connectionManager . currentConnectionState . tag !== "connected" ) {
45
- return "connecting" ;
46
- }
37
+ const currentConectionState = this . session . connectionManager . currentConnectionState ;
47
38
48
- try {
49
- await this . session . serviceProvider . runCommand ( "admin" , {
50
- ping : 1 ,
51
- } ) ;
52
-
53
- return "connected" ;
54
- } catch ( err : unknown ) {
55
- const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
56
- logger . debug (
57
- LogId . atlasConnectFailure ,
58
- "atlas-connect-cluster" ,
59
- `error querying cluster: ${ error . message } `
60
- ) ;
61
- return "unknown" ;
39
+ switch ( currentConectionState . tag ) {
40
+ case "connected" :
41
+ if (
42
+ this . session . connectedAtlasCluster . projectId !== projectId ||
43
+ this . session . connectedAtlasCluster . clusterName !== clusterName
44
+ ) {
45
+ return "connected-to-other-cluster" ;
46
+ }
47
+ break ;
48
+ case "connecting" :
49
+ case "disconnected" : // we might still be calling Atlas APIs and not attempted yet to connect to MongoDB, but we are still "connecting"
50
+ return "connecting" ;
51
+ case "errored" :
52
+ logger . debug (
53
+ LogId . atlasConnectFailure ,
54
+ "atlas-connect-cluster" ,
55
+ `error querying cluster: ${ currentConectionState . errorReason } `
56
+ ) ;
57
+ return "unknown" ;
62
58
}
59
+ return "unknown" ;
63
60
}
64
61
65
62
private async prepareClusterConnection (
@@ -205,7 +202,7 @@ export class ConnectClusterTool extends AtlasToolBase {
205
202
protected async execute ( { projectId, clusterName } : ToolArgs < typeof this . argsShape > ) : Promise < CallToolResult > {
206
203
await ensureCurrentIpInAccessList ( this . session . apiClient , projectId ) ;
207
204
for ( let i = 0 ; i < 60 ; i ++ ) {
208
- const state = await this . queryConnection ( projectId , clusterName ) ;
205
+ const state = this . queryConnection ( projectId , clusterName ) ;
209
206
switch ( state ) {
210
207
case "connected" : {
211
208
return {
0 commit comments