diff --git a/src/common/config.ts b/src/common/config.ts index a4ff9c11..9fa78ec0 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -89,7 +89,7 @@ const OPTIONS = { "greedy-arrays": true, "short-option-groups": false, }, -}; +} as const; function isConnectionSpecifier(arg: string | undefined): boolean { return ( @@ -187,12 +187,19 @@ function getExportsPath(): string { // are prefixed with `MDB_MCP_` and the keys match the UserConfig keys, but are converted // to SNAKE_UPPER_CASE. function parseEnvConfig(env: Record): Partial { + const CONFIG_WITH_URLS: Set = new Set<(typeof OPTIONS)["string"][number]>(["connectionString"]); + function setValue(obj: Record, path: string[], value: string): void { const currentField = path.shift(); if (!currentField) { return; } if (path.length === 0) { + if (CONFIG_WITH_URLS.has(currentField)) { + obj[currentField] = value; + return; + } + const numberValue = Number(value); if (!isNaN(numberValue)) { obj[currentField] = numberValue; @@ -249,7 +256,7 @@ function SNAKE_CASE_toCamelCase(str: string): string { // whatever is in mongosh. function parseCliConfig(args: string[]): CliOptions { const programArgs = args.slice(2); - const parsed = argv(programArgs, OPTIONS) as unknown as CliOptions & + const parsed = argv(programArgs, OPTIONS as unknown as argv.Options) as unknown as CliOptions & UserConfig & { _?: string[]; }; diff --git a/tests/unit/common/config.test.ts b/tests/unit/common/config.test.ts index a66c6e51..f325461d 100644 --- a/tests/unit/common/config.test.ts +++ b/tests/unit/common/config.test.ts @@ -12,6 +12,20 @@ import type { Secret } from "../../../src/common/keychain.js"; describe("config", () => { describe("env var parsing", () => { + describe("mongodb urls", () => { + it("should not try to parse a multiple-host urls", () => { + const actual = setupUserConfig({ + env: { + MDB_MCP_CONNECTION_STRING: "mongodb://user:password@host1,host2,host3/", + }, + cli: [], + defaults: defaultUserConfig, + }); + + expect(actual.connectionString).toEqual("mongodb://user:password@host1,host2,host3/"); + }); + }); + describe("string cases", () => { const testCases = [ { envVar: "MDB_MCP_API_BASE_URL", property: "apiBaseUrl", value: "http://test.com" }, @@ -67,6 +81,16 @@ describe("config", () => { }); describe("cli parsing", () => { + it("should not try to parse a multiple-host urls", () => { + const actual = setupUserConfig({ + cli: ["myself", "--", "--connectionString", "mongodb://user:password@host1,host2,host3/"], + env: {}, + defaults: defaultUserConfig, + }); + + expect(actual.connectionString).toEqual("mongodb://user:password@host1,host2,host3/"); + }); + describe("string use cases", () => { const testCases = [ {