diff --git a/src/mcpDoctor.ts b/src/mcpDoctor.ts index 7e1f174001..fa30fa68d1 100644 --- a/src/mcpDoctor.ts +++ b/src/mcpDoctor.ts @@ -383,11 +383,12 @@ export const runProductionMcpDoctorCli = async ( if (!parsed.ok) { if (parsed.help) return { output: `${renderMcpDoctorHelp()}\n`, exitCode: 0 }; + const failure = parsed.fullOutput + ? { ok: false, error: parsed.error } + : parsed.error; return { output: renderCliDocument( - parsed.format === "jsonl" - ? jsonLinesErrorRecord({ code: parsed.code, message: parsed.message }) - : { code: parsed.code, message: parsed.message }, + parsed.format === "jsonl" ? jsonLinesErrorRecord(failure) : failure, parsed.format, input.stdoutIsTerminal, ), @@ -482,11 +483,11 @@ const parseIdentityToolResult = ( }; }; -/** - * Parsed `rea mcp doctor` arguments, including a help request. The `help` - * variant is the only success-independent early return; every other accepted - * flag is an output selector declared in `MCP_DOCTOR_OPTIONS`. - */ +interface McpDoctorArgumentError { + readonly code: "MISSING_OUTPUT_VALUE" | "VALIDATION_ERROR"; + readonly message: string; +} + type McpDoctorArguments = | { readonly ok: true; @@ -498,84 +499,58 @@ type McpDoctorArguments = readonly ok: false; readonly help: false; readonly format: OutputFormat; - readonly code: "MISSING_OUTPUT_VALUE" | "VALIDATION_ERROR"; - readonly message: string; + readonly fullOutput: boolean; + readonly error: McpDoctorArgumentError; }; const parseOutputArguments = ( arguments_: readonly string[], ): McpDoctorArguments => { - // Help wins over output selection and validation errors, but only when it - // appears in option position. A token consumed as `--format`'s value is - // not a flag, so `--format --help` reports an invalid format instead of - // masking the typo with usage. - if (hasHelpInOptionPosition(arguments_)) return { ok: false, help: true }; + // Keep the first error while collecting output choices. Help wins when + // encountered in option position, outside a consumed format value. let format: OutputFormat = "toon"; let fullOutput = false; + let error: McpDoctorArgumentError | undefined; for (let index = 0; index < arguments_.length; index += 1) { - const argument = arguments_[index]; + const argument = arguments_[index]!; if (argument === "--json") { format = "json"; continue; } - const inline = argument?.startsWith("--format=") + const inline = argument.startsWith("--format=") ? argument.slice("--format=".length) : undefined; if (argument === "--format" || inline !== undefined) { const candidate = inline ?? arguments_[index + 1]; if (inline === undefined) index += 1; if (candidate === undefined || candidate === "") - return { - ok: false, - help: false, - format, + error ??= { code: "MISSING_OUTPUT_VALUE", message: missingCliOutputValueMessage("--format"), }; - if (!isOutputFormat(candidate)) - return { - ok: false, - help: false, - format, + else if (isOutputFormat(candidate)) format = candidate; + else + error ??= { code: "VALIDATION_ERROR", message: `Invalid output format: ${candidate}`, }; - format = candidate; continue; } if (argument === "--full-output") { fullOutput = true; continue; } - if (HELP_FLAGS.has(argument ?? "")) return { ok: false, help: true }; - return { - ok: false, - help: false, - format, + if (HELP_FLAGS.has(argument)) return { ok: false, help: true }; + error ??= { code: "VALIDATION_ERROR", - message: `Unknown mcp doctor option: ${argument ?? "missing"}`, + message: `Unknown mcp doctor option: ${argument}`, }; } + if (error !== undefined) + return { ok: false, help: false, format, fullOutput, error }; return { ok: true, format, fullOutput }; }; -/** - * Report whether `--help`/`-h` appears in option position. Tokens consumed - * as `--format`'s value are skipped so `--format --help` stays an invalid - * format error instead of silently becoming a help request. - */ -const hasHelpInOptionPosition = (arguments_: readonly string[]): boolean => { - for (let index = 0; index < arguments_.length; index += 1) { - const argument = arguments_[index]; - if (argument === "--format") { - index += 1; - continue; - } - if (HELP_FLAGS.has(argument ?? "")) return true; - } - return false; -}; - const isOutputFormat = (value: string | undefined): value is OutputFormat => OUTPUT_FORMATS.some((candidate) => candidate === value); diff --git a/tests/boundary/cli/mcpDoctorHelpCli.test.ts b/tests/boundary/cli/mcpDoctorHelpCli.test.ts index c81da5a2af..b3255a46d9 100644 --- a/tests/boundary/cli/mcpDoctorHelpCli.test.ts +++ b/tests/boundary/cli/mcpDoctorHelpCli.test.ts @@ -42,6 +42,56 @@ describe("MCP doctor output routing boundary", () => { ); }); +workspaceCliTest( + "keeps output choices that appear after an MCP doctor argument error", + async ({ cli }) => { + const unknownOption = { + code: "VALIDATION_ERROR", + message: "Unknown mcp doctor option: --bogus", + }; + const missingFormat = { + code: "MISSING_OUTPUT_VALUE", + message: + "--format requires a value, as in --format or --format=.", + }; + const invalidFormat = { + code: "VALIDATION_ERROR", + message: "Invalid output format: --full-output", + }; + for (const { args, expected } of [ + { args: ["--bogus", "--json"], expected: unknownOption }, + { + args: ["--json", "--bogus", "--full-output", "--format="], + expected: { ok: false, error: unknownOption }, + }, + { + args: ["--format=", "--json", "--full-output"], + expected: { ok: false, error: missingFormat }, + }, + { + args: ["--format", "--full-output", "--json"], + expected: invalidFormat, + }, + { + args: ["--json", "--bogus", "--format=jsonl", "--full-output"], + expected: { + $rea: "jsonl/v1", + type: "error", + ok: false, + result: { ok: false, error: unknownOption }, + }, + }, + ]) { + const result = await cli.run({ arguments: ["mcp", "doctor", ...args] }); + expect(result, args.join(" ")).toMatchObject({ + exitCode: 1, + stderr: "", + }); + expect(result.json, args.join(" ")).toEqual(expected); + } + }, +); + describe("MCP doctor help boundary", () => { workspaceCliTest( "answers the advertised help option instead of validating it", @@ -74,6 +124,7 @@ describe("MCP doctor help boundary", () => { async ({ cli }) => { for (const arguments_ of [ ["mcp", "doctor", "--json", "--help"], + ["mcp", "doctor", "--bogus", "--help"], ["mcp", "doctor", "--format", "bogus", "--help"], ["mcp", "doctor", "--help", "--format", "json"], ]) {