Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
30 changes: 30 additions & 0 deletions src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ function parseCliConfig(args: string[]): CliOptions {
};

const positionalArguments = parsed._ ?? [];

// we use console.warn here because we still don't have our logging system configured
// so we don't have a logger. For stdio, the warning will be received as a string in
// the client and IDEs like VSCode do show the message in the log window. For HTTP,
// it will be in the stdout of the process.
warnAboutDeprecatedCliArgs({ ...parsed, _: positionalArguments }, console.warn);

// if we have a positional argument that matches a connection string
// store it as the connection specifier and remove it from the argument
// list, so it doesn't get misunderstood by the mongosh args-parser
Expand All @@ -264,6 +271,29 @@ function parseCliConfig(args: string[]): CliOptions {
return parsed;
}

export function warnAboutDeprecatedCliArgs(
args: CliOptions &
UserConfig & {
_?: string[];
},
warn: (msg: string) => void
): void {
console.log(args);
let usedDeprecatedArgument = false;
// the first position argument should be used
// instead of --connectionString, as it's how the mongosh works.
if (args.connectionString) {
usedDeprecatedArgument = true;
warn(
"The --connectionString argument is deprecated. Prefer using the first positional argument for the connection string or the MDB_MCP_CONNECTION_STRING environment variable."
);
}

if (usedDeprecatedArgument) {
warn("Refer to https://www.mongodb.com/docs/mcp-server/get-started/ for setting up the MCP Server.");
}
}

function commaSeparatedToArray<T extends string[]>(str: string | string[] | undefined): T {
if (str === undefined) {
return [] as unknown as T;
Expand Down
41 changes: 39 additions & 2 deletions tests/unit/common/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi, beforeEach } from "vitest";
import type { UserConfig } from "../../../src/common/config.js";
import { setupUserConfig, defaultUserConfig } from "../../../src/common/config.js";
import { setupUserConfig, defaultUserConfig, warnAboutDeprecatedCliArgs } from "../../../src/common/config.js";
import type { CliOptions } from "@mongosh/arg-parser";

describe("config", () => {
describe("env var parsing", () => {
Expand Down Expand Up @@ -605,3 +606,39 @@ describe("config", () => {
});
});
});

describe("Deprecated CLI arguments", () => {
const referDocMessage =
"Refer to https://www.mongodb.com/docs/mcp-server/get-started/ for setting up the MCP Server.";

type TestCase = { readonly cliArg: keyof (CliOptions & UserConfig); readonly warning: string };
const testCases = [
{
cliArg: "connectionString",
warning:
"The --connectionString argument is deprecated. Prefer using the first positional argument for the connection string or the MDB_MCP_CONNECTION_STRING environment variable.",
},
] as TestCase[];

for (const { cliArg, warning } of testCases) {
describe(`deprecation behaviour of ${cliArg}`, () => {
let cliArgs: CliOptions & UserConfig & { _?: string[] };
let warn: (msg: string) => void;

beforeEach(() => {
cliArgs = { [cliArg]: "RandomString" } as unknown as CliOptions & UserConfig & { _?: string[] };
warn = vi.fn();

warnAboutDeprecatedCliArgs(cliArgs, warn);
});

it(`warns the usage of ${cliArg} as it is deprecated`, () => {
expect(warn).toHaveBeenCalledWith(warning);
});

it(`shows the reference message when ${cliArg} was passed`, () => {
expect(warn).toHaveBeenCalledWith(referDocMessage);
});
});
}
});
Loading