Skip to content

Commit b8542f2

Browse files
committed
chore: warn about the usage of deprecated cli arguments
1 parent 7845035 commit b8542f2

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/common/config.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ function parseCliConfig(args: string[]): CliOptions {
251251
};
252252

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

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