Skip to content

Commit 28797a4

Browse files
committed
feat: suggest using the config connection string on connection failure
1 parent c692587 commit 28797a4

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
@@ -57,6 +57,9 @@ describe("Connect tool", () => {
5757
});
5858
const content = getResponseContent(response.content);
5959
expect(content).toContain("Error running connect");
60+
61+
// Should not suggest using the config connection string (because we don't have one)
62+
expect(content).not.toContain("Your config lists a different connection string");
6063
});
6164
});
6265
});
@@ -83,5 +86,34 @@ describe("Connect tool", () => {
8386
expect(content).toContain("Successfully connected");
8487
expect(content).toContain(newConnectionString);
8588
});
89+
90+
describe("when the arugment connection string is invalid", () => {
91+
it("suggests the config connection string if set", async () => {
92+
const response = await client().callTool({
93+
name: "connect",
94+
arguments: { connectionStringOrClusterName: "mongodb://localhost:12345" },
95+
});
96+
const content = getResponseContent(response.content);
97+
expect(content).toContain("Failed to connect to MongoDB at 'mongodb://localhost:12345'");
98+
expect(content).toContain(
99+
`Your config lists a different connection string: '${config.connectionString}' - do you want to try connecting to it instead?`
100+
);
101+
});
102+
103+
it("returns error message if the config connection string matches the argument", async () => {
104+
config.connectionString = "mongodb://localhost:12345";
105+
const response = await client().callTool({
106+
name: "connect",
107+
arguments: { connectionStringOrClusterName: "mongodb://localhost:12345" },
108+
});
109+
110+
const content = getResponseContent(response.content);
111+
112+
// Should be handled by default error handler and not suggest the config connection string
113+
// because it matches the argument connection string
114+
expect(content).toContain("Error running connect");
115+
expect(content).not.toContain("Your config lists a different connection string");
116+
});
117+
});
86118
});
87119
});

0 commit comments

Comments
 (0)