Skip to content

Commit b26cd46

Browse files
authored
fix(connection): support for multiple hosts urls #327 (#514)
1 parent 526fa3b commit b26cd46

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/common/config.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const OPTIONS = {
8989
"greedy-arrays": true,
9090
"short-option-groups": false,
9191
},
92-
};
92+
} as const;
9393

9494
function isConnectionSpecifier(arg: string | undefined): boolean {
9595
return (
@@ -187,12 +187,19 @@ function getExportsPath(): string {
187187
// are prefixed with `MDB_MCP_` and the keys match the UserConfig keys, but are converted
188188
// to SNAKE_UPPER_CASE.
189189
function parseEnvConfig(env: Record<string, unknown>): Partial<UserConfig> {
190+
const CONFIG_WITH_URLS: Set<string> = new Set<(typeof OPTIONS)["string"][number]>(["connectionString"]);
191+
190192
function setValue(obj: Record<string, unknown>, path: string[], value: string): void {
191193
const currentField = path.shift();
192194
if (!currentField) {
193195
return;
194196
}
195197
if (path.length === 0) {
198+
if (CONFIG_WITH_URLS.has(currentField)) {
199+
obj[currentField] = value;
200+
return;
201+
}
202+
196203
const numberValue = Number(value);
197204
if (!isNaN(numberValue)) {
198205
obj[currentField] = numberValue;
@@ -249,7 +256,7 @@ function SNAKE_CASE_toCamelCase(str: string): string {
249256
// whatever is in mongosh.
250257
function parseCliConfig(args: string[]): CliOptions {
251258
const programArgs = args.slice(2);
252-
const parsed = argv(programArgs, OPTIONS) as unknown as CliOptions &
259+
const parsed = argv(programArgs, OPTIONS as unknown as argv.Options) as unknown as CliOptions &
253260
UserConfig & {
254261
_?: string[];
255262
};

tests/unit/common/config.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ import type { Secret } from "../../../src/common/keychain.js";
1212

1313
describe("config", () => {
1414
describe("env var parsing", () => {
15+
describe("mongodb urls", () => {
16+
it("should not try to parse a multiple-host urls", () => {
17+
const actual = setupUserConfig({
18+
env: {
19+
MDB_MCP_CONNECTION_STRING: "mongodb://user:password@host1,host2,host3/",
20+
},
21+
cli: [],
22+
defaults: defaultUserConfig,
23+
});
24+
25+
expect(actual.connectionString).toEqual("mongodb://user:password@host1,host2,host3/");
26+
});
27+
});
28+
1529
describe("string cases", () => {
1630
const testCases = [
1731
{ envVar: "MDB_MCP_API_BASE_URL", property: "apiBaseUrl", value: "http://test.com" },
@@ -67,6 +81,16 @@ describe("config", () => {
6781
});
6882

6983
describe("cli parsing", () => {
84+
it("should not try to parse a multiple-host urls", () => {
85+
const actual = setupUserConfig({
86+
cli: ["myself", "--", "--connectionString", "mongodb://user:password@host1,host2,host3/"],
87+
env: {},
88+
defaults: defaultUserConfig,
89+
});
90+
91+
expect(actual.connectionString).toEqual("mongodb://user:password@host1,host2,host3/");
92+
});
93+
7094
describe("string use cases", () => {
7195
const testCases = [
7296
{

0 commit comments

Comments
 (0)