Skip to content

Commit 4a54404

Browse files
authored
chore: warn about the usage of deprecated cli arguments MCP-107 (#493)
1 parent d1e0ee3 commit 4a54404

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/common/config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ function parseCliConfig(args: string[]): CliOptions {
253253
};
254254

255255
const positionalArguments = parsed._ ?? [];
256+
257+
// we use console.warn here because we still don't have our logging system configured
258+
// so we don't have a logger. For stdio, the warning will be received as a string in
259+
// the client and IDEs like VSCode do show the message in the log window. For HTTP,
260+
// it will be in the stdout of the process.
261+
warnAboutDeprecatedCliArgs({ ...parsed, _: positionalArguments }, console.warn);
262+
256263
// if we have a positional argument that matches a connection string
257264
// store it as the connection specifier and remove it from the argument
258265
// list, so it doesn't get misunderstood by the mongosh args-parser
@@ -264,6 +271,28 @@ function parseCliConfig(args: string[]): CliOptions {
264271
return parsed;
265272
}
266273

274+
export function warnAboutDeprecatedCliArgs(
275+
args: CliOptions &
276+
UserConfig & {
277+
_?: string[];
278+
},
279+
warn: (msg: string) => void
280+
): void {
281+
let usedDeprecatedArgument = false;
282+
// the first position argument should be used
283+
// instead of --connectionString, as it's how the mongosh works.
284+
if (args.connectionString) {
285+
usedDeprecatedArgument = true;
286+
warn(
287+
"The --connectionString argument is deprecated. Prefer using the first positional argument for the connection string or the MDB_MCP_CONNECTION_STRING environment variable."
288+
);
289+
}
290+
291+
if (usedDeprecatedArgument) {
292+
warn("Refer to https://www.mongodb.com/docs/mcp-server/get-started/ for setting up the MCP Server.");
293+
}
294+
}
295+
267296
function commaSeparatedToArray<T extends string[]>(str: string | string[] | undefined): T {
268297
if (str === undefined) {
269298
return [] as unknown as T;

tests/unit/common/config.test.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { describe, it, expect } from "vitest";
1+
import { describe, it, expect, vi, beforeEach } from "vitest";
22
import type { UserConfig } from "../../../src/common/config.js";
3-
import { setupUserConfig, defaultUserConfig } from "../../../src/common/config.js";
3+
import { setupUserConfig, defaultUserConfig, warnAboutDeprecatedCliArgs } from "../../../src/common/config.js";
4+
import type { CliOptions } from "@mongosh/arg-parser";
45

56
describe("config", () => {
67
describe("env var parsing", () => {
@@ -605,3 +606,39 @@ describe("config", () => {
605606
});
606607
});
607608
});
609+
610+
describe("Deprecated CLI arguments", () => {
611+
const referDocMessage =
612+
"Refer to https://www.mongodb.com/docs/mcp-server/get-started/ for setting up the MCP Server.";
613+
614+
type TestCase = { readonly cliArg: keyof (CliOptions & UserConfig); readonly warning: string };
615+
const testCases = [
616+
{
617+
cliArg: "connectionString",
618+
warning:
619+
"The --connectionString argument is deprecated. Prefer using the first positional argument for the connection string or the MDB_MCP_CONNECTION_STRING environment variable.",
620+
},
621+
] as TestCase[];
622+
623+
for (const { cliArg, warning } of testCases) {
624+
describe(`deprecation behaviour of ${cliArg}`, () => {
625+
let cliArgs: CliOptions & UserConfig & { _?: string[] };
626+
let warn: (msg: string) => void;
627+
628+
beforeEach(() => {
629+
cliArgs = { [cliArg]: "RandomString" } as unknown as CliOptions & UserConfig & { _?: string[] };
630+
warn = vi.fn();
631+
632+
warnAboutDeprecatedCliArgs(cliArgs, warn);
633+
});
634+
635+
it(`warns the usage of ${cliArg} as it is deprecated`, () => {
636+
expect(warn).toHaveBeenCalledWith(warning);
637+
});
638+
639+
it(`shows the reference message when ${cliArg} was passed`, () => {
640+
expect(warn).toHaveBeenCalledWith(referDocMessage);
641+
});
642+
});
643+
}
644+
});

0 commit comments

Comments
 (0)