Skip to content

fix: handle cli metadata flags before startup#145

Open
Viodmian wants to merge 1 commit into
currents-dev:mainfrom
Viodmian:fix-cli-metadata-flags
Open

fix: handle cli metadata flags before startup#145
Viodmian wants to merge 1 commit into
currents-dev:mainfrom
Viodmian:fix-cli-metadata-flags

Conversation

@Viodmian

@Viodmian Viodmian commented Jun 8, 2026

Copy link
Copy Markdown

Summary

  • handle top-level --help / -h and --version / -v before starting the MCP server
  • return a concise error for unknown top-level options without requiring CURRENTS_API_KEY
  • add unit coverage plus packaged CLI smoke tests for metadata flags without CURRENTS_API_KEY
  • make the existing packaged npm/npx integration tests work on Windows by invoking npm shims through the shell

Reproduction before this change

With @currents/mcp@2.3.2 installed and no CURRENTS_API_KEY set:

mcp --help
mcp --version

Both commands exited 1 with CURRENTS_API_KEY is not set instead of showing CLI metadata.

Verification

npm ci --ignore-scripts --no-audit --no-fund
npm test -- --run src/cli.test.ts
npx tsc --noEmit
npx tsdown
npx vitest run
git diff --check
node dist\index.mjs --help
node dist\index.mjs --version
node dist\index.mjs --definitely-not-real
node dist\index.mjs

Results:

  • focused CLI tests: 6 passed
  • full Vitest suite: 13 files, 392 tests passed
  • typecheck passed
  • build passed
  • --help exits 0 and prints usage without CURRENTS_API_KEY
  • --version exits 0 and prints 2.3.2 without CURRENTS_API_KEY
  • unknown top-level option exits 1 with a concise message
  • normal no-arg startup still exits 1 with the missing API key guidance

Summary by CodeRabbit

Release Notes

  • New Features

    • Added support for --help and --version CLI flags with proper error handling for unknown options
    • Enhanced cross-platform compatibility with platform-aware command selection for Windows and Unix-like systems
  • Tests

    • Added comprehensive unit tests for CLI metadata flag handling
    • Updated integration tests with cross-platform command execution support

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds CLI metadata flag handling (--help, --version) to the MCP server entrypoint, complete with unit tests and cross-platform integration test updates for Windows compatibility. The implementation exports a CliResult type and handleCliMetadataFlags function that inspects argv and returns control flow information, integrated into server startup before MCP initialization.

Changes

CLI Metadata Flags and Cross-Platform Integration

Layer / File(s) Summary
CLI handler contract and unit tests
mcp-server/src/cli.ts, mcp-server/src/cli.test.ts
New CliResult type and handleCliMetadataFlags function parse argv for --help/-h, --version/-v, and unknown options, writing output to provided streams and returning a handled/exitCode structure. Unit tests via a lightweight capture() helper verify all flag branches, exact output formatting, and exit codes.
Entrypoint integration
mcp-server/src/index.ts
Server entrypoint imports the CLI handler, declares __VERSION__, calls handleCliMetadataFlags(process.argv, __VERSION__) early, and exits with the returned code if flags are handled before proceeding to server startup.
CLI integration tests with platform-aware commands
mcp-server/src/cli-bin.integration.test.ts
Integration tests now select npm/npx based on process.platform and pass shell where needed for Windows. Helper functions locate the installed mcp binary under node_modules/.bin (handling .cmd on Windows) and remove CURRENTS_API_KEY from test environments. A new parameterized test runs the installed binary with --help and --version, asserting exit code 0 and expected output while confirming the API key is not leaked.
ESM integration test cross-platform fixes
mcp-server/src/package-published-esm.integration.test.ts
packTarball and consumer project setup switched from hardcoded "npm" to platform-aware npmCommand with Windows-specific shell handling, ensuring tarball creation and installation work correctly across platforms.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: handle cli metadata flags before startup' directly and clearly summarizes the main change: adding CLI metadata flag handling before MCP server startup.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
mcp-server/src/package-published-esm.integration.test.ts (1)

40-40: ⚡ Quick win

Consider extracting shared test utilities.

Both npmCommand and the packTarball function are duplicated between this file and cli-bin.integration.test.ts (context snippet 1). Extracting them to a shared test utility module would eliminate duplication and make future changes easier.

♻️ Suggested approach

Create mcp-server/src/test/test-utils.ts:

import { execFileSync } from "node:child_process";
import { readdirSync } from "node:fs";
import path from "node:path";

export const npmCommand = process.platform === "win32" ? "npm.cmd" : "npm";
export const npxCommand = process.platform === "win32" ? "npx.cmd" : "npx";

export function packTarball(root: string, packDest: string): string {
  execFileSync(
    npmCommand,
    ["pack", "--ignore-scripts", "--pack-destination", packDest],
    {
      cwd: root,
      shell: process.platform === "win32",
      stdio: ["ignore", "pipe", "pipe"],
    },
  );
  const tgz = readdirSync(packDest).filter((f) => f.endsWith(".tgz"));
  if (tgz.length !== 1) {
    throw new Error(`expected one .tgz in ${packDest}, got: ${tgz.join(", ")}`);
  }
  return path.join(packDest, tgz[0]);
}

Then import in both test files:

-const npmCommand = process.platform === "win32" ? "npm.cmd" : "npm";
+import { npmCommand, packTarball } from "./test/test-utils.js";

Also applies to: 43-59

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@mcp-server/src/package-published-esm.integration.test.ts` at line 40, The
tests duplicate the npmCommand constant and packTarball function across test
files; extract them into a shared test utility module (e.g., test-utils.ts) and
export npmCommand and packTarball so both tests can import them; update the
tests to import the shared npmCommand constant and packTarball function instead
of redefining them, ensuring packTarball still uses execFileSync with the same
args and behavior (including platform-aware shell and the single-tgz validation)
and that npmCommand remains platform-aware (process.platform === "win32" ?
"npm.cmd" : "npm").
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@mcp-server/src/package-published-esm.integration.test.ts`:
- Line 40: The tests duplicate the npmCommand constant and packTarball function
across test files; extract them into a shared test utility module (e.g.,
test-utils.ts) and export npmCommand and packTarball so both tests can import
them; update the tests to import the shared npmCommand constant and packTarball
function instead of redefining them, ensuring packTarball still uses
execFileSync with the same args and behavior (including platform-aware shell and
the single-tgz validation) and that npmCommand remains platform-aware
(process.platform === "win32" ? "npm.cmd" : "npm").

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 43c86ae4-b416-4c1d-bdf3-ff3ad99be370

📥 Commits

Reviewing files that changed from the base of the PR and between f620c80 and 7d6e86d.

📒 Files selected for processing (5)
  • mcp-server/src/cli-bin.integration.test.ts
  • mcp-server/src/cli.test.ts
  • mcp-server/src/cli.ts
  • mcp-server/src/index.ts
  • mcp-server/src/package-published-esm.integration.test.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant