Skip to content

Commit 28372be

Browse files
committed
fix: mix sync and async
1 parent e267b45 commit 28372be

File tree

3 files changed

+85
-70
lines changed

3 files changed

+85
-70
lines changed

src/session.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,27 @@ export class Session extends EventEmitter<{
6767
}
6868
this.serviceProvider = undefined;
6969
}
70-
if (!this.connectedAtlasCluster) {
71-
this.emit("disconnect");
72-
return;
73-
}
74-
void this.apiClient
75-
.deleteDatabaseUser({
76-
params: {
77-
path: {
78-
groupId: this.connectedAtlasCluster.projectId,
79-
username: this.connectedAtlasCluster.username,
80-
databaseName: "admin",
70+
if (this.connectedAtlasCluster?.username && this.connectedAtlasCluster?.projectId) {
71+
void this.apiClient
72+
.deleteDatabaseUser({
73+
params: {
74+
path: {
75+
groupId: this.connectedAtlasCluster.projectId,
76+
username: this.connectedAtlasCluster.username,
77+
databaseName: "admin",
78+
},
8179
},
82-
},
83-
})
84-
.catch((err: unknown) => {
85-
const error = err instanceof Error ? err : new Error(String(err));
86-
logger.error(
87-
LogId.atlasDeleteDatabaseUserFailure,
88-
"atlas-connect-cluster",
89-
`Error deleting previous database user: ${error.message}`
90-
);
91-
});
92-
this.connectedAtlasCluster = undefined;
93-
80+
})
81+
.catch((err: unknown) => {
82+
const error = err instanceof Error ? err : new Error(String(err));
83+
logger.error(
84+
LogId.atlasDeleteDatabaseUserFailure,
85+
"atlas-connect-cluster",
86+
`Error deleting previous database user: ${error.message}`
87+
);
88+
});
89+
this.connectedAtlasCluster = undefined;
90+
}
9491
this.emit("disconnect");
9592
}
9693

src/tools/atlas/metadata/connectCluster.ts

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/integration/tools/atlas/clusters.test.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,24 @@ describeWithAtlas("clusters", (integration) => {
183183
it("connects to cluster", async () => {
184184
const projectId = getProjectId();
185185

186-
const response = (await integration.mcpClient().callTool({
187-
name: "atlas-connect-cluster",
188-
arguments: { projectId, clusterName },
189-
})) as CallToolResult;
190-
expect(response.content).toBeArray();
191-
expect(response.content).toHaveLength(2);
192-
expect(response.content[0]?.type).toEqual("text");
193-
expect(response.content[0]?.text).toContain(`Attempting to connect to cluster "${clusterName}"...`);
194-
195186
for (let i = 0; i < 600; i++) {
196187
const response = (await integration.mcpClient().callTool({
197188
name: "atlas-connect-cluster",
198189
arguments: { projectId, clusterName },
199190
})) as CallToolResult;
200191
expect(response.content).toBeArray();
201-
expect(response.content).toHaveLength(1);
192+
expect(response.content.length).toBeGreaterThanOrEqual(1);
202193
expect(response.content[0]?.type).toEqual("text");
203194
const c = response.content[0] as { text: string };
204-
if (c.text.includes("Cluster is already connected.")) {
195+
if (
196+
c.text.includes("Cluster is already connected.") ||
197+
c.text.includes(`Connected to cluster "${clusterName}"`)
198+
) {
205199
break; // success
200+
} else {
201+
expect(response.content[0]?.text).toContain(
202+
`Attempting to connect to cluster "${clusterName}"...`
203+
);
206204
}
207205
await sleep(500);
208206
}

0 commit comments

Comments
 (0)