@@ -118,13 +118,14 @@ export class ConnectClusterTool extends AtlasToolBase {
118
118
) ;
119
119
120
120
for ( let i = 0 ; i < tryCount ; i ++ ) {
121
+ if ( ! this . session . connectedAtlasCluster ) {
122
+ lastError = new Error ( "Cluster connection aborted" ) ;
123
+ break ;
124
+ }
125
+
121
126
try {
122
127
lastError = undefined ;
123
128
124
- if ( ! this . session . connectedAtlasCluster ) {
125
- throw new Error ( "Cluster connection aborted" ) ;
126
- }
127
-
128
129
await this . session . connectToMongoDB ( connectionString , this . config . connectOptions ) ;
129
130
break ;
130
131
} catch ( err : unknown ) {
@@ -222,6 +223,8 @@ export class ConnectClusterTool extends AtlasToolBase {
222
223
const connectionString = await this . prepareClusterConnection ( projectId , clusterName ) ;
223
224
224
225
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
225
228
await this . connectToCluster ( connectionString , 60 ) ;
226
229
227
230
return {
@@ -240,17 +243,19 @@ export class ConnectClusterTool extends AtlasToolBase {
240
243
`error connecting to cluster: ${ error . message } `
241
244
) ;
242
245
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
+ ) ;
254
259
} ) ;
255
260
256
261
return connectingResult ;
0 commit comments