diff --git a/src/common/config.ts b/src/common/config.ts index 414e9158..4bc02efe 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -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 @@ -264,6 +271,28 @@ function parseCliConfig(args: string[]): CliOptions { return parsed; } +export function warnAboutDeprecatedCliArgs( + args: CliOptions & + UserConfig & { + _?: string[]; + }, + warn: (msg: string) => void +): void { + 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(str: string | string[] | undefined): T { if (str === undefined) { return [] as unknown as T; diff --git a/tests/unit/common/config.test.ts b/tests/unit/common/config.test.ts index 78257a3c..789febff 100644 --- a/tests/unit/common/config.test.ts +++ b/tests/unit/common/config.test.ts @@ -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", () => { @@ -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); + }); + }); + } +});