Skip to content

Commit 1f9c47d

Browse files
committed
fix: creating multiple users on atlas connect [MCP-280]
1 parent 38518ab commit 1f9c47d

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

src/tools/atlas/connect/connectCluster.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,39 @@ export class ConnectClusterTool extends AtlasToolBase {
218218
const ipAccessListUpdated = await ensureCurrentIpInAccessList(this.session.apiClient, projectId);
219219
let createdUser = false;
220220

221+
const state = this.queryConnection(projectId, clusterName);
222+
switch (state) {
223+
case "connected-to-other-cluster":
224+
case "disconnected": {
225+
await this.session.disconnect();
226+
227+
const { connectionString, atlas } = await this.prepareClusterConnection(
228+
projectId,
229+
clusterName,
230+
connectionType
231+
);
232+
233+
createdUser = true;
234+
235+
// try to connect for about 5 minutes asynchronously
236+
void this.connectToCluster(connectionString, atlas).catch((err: unknown) => {
237+
const error = err instanceof Error ? err : new Error(String(err));
238+
this.session.logger.error({
239+
id: LogId.atlasConnectFailure,
240+
context: "atlas-connect-cluster",
241+
message: `error connecting to cluster: ${error.message}`,
242+
});
243+
});
244+
break;
245+
}
246+
case "connecting":
247+
case "connected":
248+
case "unknown":
249+
default: {
250+
break;
251+
}
252+
}
253+
221254
for (let i = 0; i < 60; i++) {
222255
const state = this.queryConnection(projectId, clusterName);
223256
switch (state) {
@@ -246,34 +279,15 @@ export class ConnectClusterTool extends AtlasToolBase {
246279
return { content };
247280
}
248281
case "connecting":
249-
case "unknown": {
250-
break;
251-
}
282+
case "unknown":
252283
case "connected-to-other-cluster":
253284
case "disconnected":
254285
default: {
255-
await this.session.disconnect();
256-
const { connectionString, atlas } = await this.prepareClusterConnection(
257-
projectId,
258-
clusterName,
259-
connectionType
260-
);
261-
262-
createdUser = true;
263-
// try to connect for about 5 minutes asynchronously
264-
void this.connectToCluster(connectionString, atlas).catch((err: unknown) => {
265-
const error = err instanceof Error ? err : new Error(String(err));
266-
this.session.logger.error({
267-
id: LogId.atlasConnectFailure,
268-
context: "atlas-connect-cluster",
269-
message: `error connecting to cluster: ${error.message}`,
270-
});
271-
});
272286
break;
273287
}
274288
}
275289

276-
await sleep(500);
290+
await sleep(500); // wait 500ms before checking the connection state again
277291
}
278292

279293
const content: CallToolResult["content"] = [

0 commit comments

Comments
 (0)