Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const OPTIONS = {
"greedy-arrays": true,
"short-option-groups": false,
},
};
} as const;

function isConnectionSpecifier(arg: string | undefined): boolean {
return (
Expand Down Expand Up @@ -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<string, unknown>): Partial<UserConfig> {
const CONFIG_WITH_URLS: Set<string> = new Set<(typeof OPTIONS)["string"][number]>(["connectionString"]);

function setValue(obj: Record<string, unknown>, 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;
Expand Down Expand Up @@ -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[];
};
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/common/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down Expand Up @@ -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 = [
{
Expand Down
Loading