Skip to content

Commit 371b279

Browse files
committed
feat: suggest using the config connection string on connection failure
1 parent eaffdbe commit 371b279

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/tools/mongodb/metadata/connect.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DbOperationType, MongoDBToolBase } from "../mongodbTool.js";
44
import { ToolArgs } from "../../tool.js";
55
import { ErrorCodes, MongoDBError } from "../../../errors.js";
66
import config from "../../../config.js";
7+
import { MongoError as DriverError } from "mongodb";
78

89
export class ConnectTool extends MongoDBToolBase {
910
protected name = "connect";
@@ -57,10 +58,32 @@ export class ConnectTool extends MongoDBToolBase {
5758
throw new MongoDBError(ErrorCodes.InvalidParams, "Invalid connection options");
5859
}
5960

60-
await this.connectToMongoDB(connectionString);
61+
try {
62+
await this.connectToMongoDB(connectionString);
63+
return {
64+
content: [{ type: "text", text: `Successfully connected to ${connectionString}.` }],
65+
};
66+
} catch (error) {
67+
// Sometimes the model will supply an incorrect connection string. If the user has configured
68+
// a different one as environment variable or a cli argument, suggest using that one instead.
69+
if (
70+
config.connectionString &&
71+
error instanceof DriverError &&
72+
config.connectionString !== connectionString
73+
) {
74+
return {
75+
content: [
76+
{
77+
type: "text",
78+
text:
79+
`Failed to connect to MongoDB at '${connectionString}' due to error: '${error.message}.` +
80+
`Your config lists a different connection string: '${config.connectionString}' - do you want to try connecting to it instead?`,
81+
},
82+
],
83+
};
84+
}
6185

62-
return {
63-
content: [{ type: "text", text: `Successfully connected to ${connectionString}.` }],
64-
};
86+
throw error;
87+
}
6588
}
6689
}

tests/integration/tools/mongodb/metadata/connect.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ describe("Connect tool", () => {
7777
});
7878
const content = getResponseContent(response.content);
7979
expect(content).toContain("Error running connect");
80+
81+
// Should not suggest using the config connection string (because we don't have one)
82+
expect(content).not.toContain("Your config lists a different connection string");
8083
});
8184
});
8285
});
@@ -103,5 +106,34 @@ describe("Connect tool", () => {
103106
expect(content).toContain("Successfully connected");
104107
expect(content).toContain(newConnectionString);
105108
});
109+
110+
describe("when the arugment connection string is invalid", () => {
111+
it("suggests the config connection string if set", async () => {
112+
const response = await client.callTool({
113+
name: "connect",
114+
arguments: { connectionStringOrClusterName: "mongodb://localhost:12345" },
115+
});
116+
const content = getResponseContent(response.content);
117+
expect(content).toContain("Failed to connect to MongoDB at 'mongodb://localhost:12345'");
118+
expect(content).toContain(
119+
`Your config lists a different connection string: '${config.connectionString}' - do you want to try connecting to it instead?`
120+
);
121+
});
122+
123+
it("returns error message if the config connection string matches the argument", async () => {
124+
config.connectionString = "mongodb://localhost:12345";
125+
const response = await client.callTool({
126+
name: "connect",
127+
arguments: { connectionStringOrClusterName: "mongodb://localhost:12345" },
128+
});
129+
130+
const content = getResponseContent(response.content);
131+
132+
// Should be handled by default error handler and not suggest the config connection string
133+
// because it matches the argument connection string
134+
expect(content).toContain("Error running connect");
135+
expect(content).not.toContain("Your config lists a different connection string");
136+
});
137+
});
106138
});
107139
});

0 commit comments

Comments
 (0)