@@ -108,7 +108,7 @@ export class ConnectClusterTool extends AtlasToolBase {
108108 return cn . toString ( ) ;
109109 }
110110
111- private async connectToCluster ( connectionString : string ) : Promise < void > {
111+ private async connectToCluster ( connectionString : string , tryCount : number ) : Promise < void > {
112112 let lastError : Error | undefined = undefined ;
113113
114114 logger . debug (
@@ -117,11 +117,15 @@ export class ConnectClusterTool extends AtlasToolBase {
117117 `attempting to connect to cluster: ${ this . session . connectedAtlasCluster ?. clusterName } `
118118 ) ;
119119
120- for ( let i = 0 ; i < 600 ; i ++ ) {
121- // try for 5 minutes
120+ for ( let i = 0 ; i < tryCount ; i ++ ) {
122121 try {
123- await this . session . connectToMongoDB ( connectionString , this . config . connectOptions ) ;
124122 lastError = undefined ;
123+
124+ if ( ! this . session . connectedAtlasCluster ) {
125+ throw new Error ( "Cluster connection aborted" ) ;
126+ }
127+
128+ await this . session . connectToMongoDB ( connectionString , this . config . connectOptions ) ;
125129 break ;
126130 } catch ( err : unknown ) {
127131 const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
@@ -171,6 +175,19 @@ export class ConnectClusterTool extends AtlasToolBase {
171175 }
172176
173177 protected async execute ( { projectId, clusterName } : ToolArgs < typeof this . argsShape > ) : Promise < CallToolResult > {
178+ const connectingResult = {
179+ content : [
180+ {
181+ type : "text" as const ,
182+ text : `Attempting to connect to cluster "${ clusterName } "...` ,
183+ } ,
184+ {
185+ type : "text" as const ,
186+ text : `Warning: Provisioning a user and connecting to the cluster may take more time, please check again in a few seconds.` ,
187+ } ,
188+ ] ,
189+ } ;
190+
174191 try {
175192 const state = await this . queryConnection ( projectId , clusterName ) ;
176193 switch ( state ) {
@@ -184,14 +201,7 @@ export class ConnectClusterTool extends AtlasToolBase {
184201 ] ,
185202 } ;
186203 case "connecting" :
187- return {
188- content : [
189- {
190- type : "text" ,
191- text : "Cluster is connecting..." ,
192- } ,
193- ] ,
194- } ;
204+ return connectingResult ;
195205 case "connected-to-other-cluster" :
196206 case "disconnected" :
197207 default :
@@ -210,30 +220,40 @@ export class ConnectClusterTool extends AtlasToolBase {
210220
211221 await this . session . disconnect ( ) ;
212222 const connectionString = await this . prepareClusterConnection ( projectId , clusterName ) ;
213- process . nextTick ( async ( ) => {
214- try {
215- await this . connectToCluster ( connectionString ) ;
216- } catch ( err : unknown ) {
217- const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
218- logger . debug (
219- LogId . atlasConnectFailure ,
220- "atlas-connect-cluster" ,
221- `error connecting to cluster: ${ error . message } `
222- ) ;
223- }
224- } ) ;
225223
226- return {
227- content : [
228- {
229- type : "text" ,
230- text : `Attempting to connect to cluster "${ clusterName } "...` ,
231- } ,
232- {
233- type : "text" ,
234- text : `Warning: Check again in a few seconds.` ,
235- } ,
236- ] ,
237- } ;
224+ try {
225+ await this . connectToCluster ( connectionString , 60 ) ;
226+
227+ return {
228+ content : [
229+ {
230+ type : "text" ,
231+ text : `Connected to cluster "${ clusterName } ".` ,
232+ } ,
233+ ] ,
234+ } ;
235+ } catch ( err : unknown ) {
236+ const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
237+ logger . debug (
238+ LogId . atlasConnectFailure ,
239+ "atlas-connect-cluster" ,
240+ `error connecting to cluster: ${ error . message } `
241+ ) ;
242+
243+ process . nextTick ( async ( ) => {
244+ try {
245+ await this . connectToCluster ( connectionString , 600 ) ;
246+ } catch ( err : unknown ) {
247+ const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
248+ logger . debug (
249+ LogId . atlasConnectFailure ,
250+ "atlas-connect-cluster" ,
251+ `error connecting to cluster: ${ error . message } `
252+ ) ;
253+ }
254+ } ) ;
255+
256+ return connectingResult ;
257+ }
238258 }
239259}
0 commit comments