@@ -118,13 +118,14 @@ export class ConnectClusterTool extends AtlasToolBase {
118118 ) ;
119119
120120 for ( let i = 0 ; i < tryCount ; i ++ ) {
121+ if ( ! this . session . connectedAtlasCluster ) {
122+ lastError = new Error ( "Cluster connection aborted" ) ;
123+ break ;
124+ }
125+
121126 try {
122127 lastError = undefined ;
123128
124- if ( ! this . session . connectedAtlasCluster ) {
125- throw new Error ( "Cluster connection aborted" ) ;
126- }
127-
128129 await this . session . connectToMongoDB ( connectionString , this . config . connectOptions ) ;
129130 break ;
130131 } catch ( err : unknown ) {
@@ -222,6 +223,8 @@ export class ConnectClusterTool extends AtlasToolBase {
222223 const connectionString = await this . prepareClusterConnection ( projectId , clusterName ) ;
223224
224225 try {
226+ // First, try to connect to the cluster within the current tool call.
227+ // We give it 60 attempts with 500 ms delay between each, so ~30 seconds
225228 await this . connectToCluster ( connectionString , 60 ) ;
226229
227230 return {
@@ -240,17 +243,19 @@ export class ConnectClusterTool extends AtlasToolBase {
240243 `error connecting to cluster: ${ error . message } `
241244 ) ;
242245
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- }
246+ // We couldn't connect in ~30 seconds, likely because user creation is taking longer
247+ // Retry the connection with longer timeout (~5 minutes), while also returning a response
248+ // to the client. Many clients will have a 1 minute timeout for tool calls, so we want to
249+ // return well before that.
250+ //
251+ // Once we add support for streamable http, we'd want to use progress notifications here.
252+ void this . connectToCluster ( connectionString , 600 ) . catch ( ( err ) => {
253+ const error = err instanceof Error ? err : new Error ( String ( err ) ) ;
254+ logger . debug (
255+ LogId . atlasConnectFailure ,
256+ "atlas-connect-cluster" ,
257+ `error connecting to cluster: ${ error . message } `
258+ ) ;
254259 } ) ;
255260
256261 return connectingResult ;
0 commit comments