Skip to content

Conversation

@gagik
Copy link
Contributor

@gagik gagik commented Nov 25, 2025

A couple things here (though these specific commits are quite outdated now)

  1. 8333354 - existing behavior actually seems broken... cases like --browser=true have not been parsed correctly until now (concerning?) and the only real options have been the string-based ones. So this adds a) tests relating to this (which fail without the coercion change) and b) conditional coercing to boolean value when it is true or false. This seems to be intended behavior but let me know if I'm overlooking it.
  2. 589f446 - This switches the logic to Zod while keeping the same logic; I added some backwards compatibility tests to make sure this is correct. I kept the changes rather minimal but it'd be great to move more logic towards Zod in the future in terms of validation.

@gagik gagik changed the title chore: use zod for arg-parser and move from cli-repl MCP-298 chore: use zod for arg-parser MCP-298 Nov 26, 2025
gagik added a commit that referenced this pull request Nov 27, 2025
This is preparation for its usage of MCP and further changes in #2589
@gagik gagik changed the base branch from main to gagik/move-arg-parser November 28, 2025 15:18
@gagik gagik marked this pull request as ready for review November 28, 2025 15:19
@gagik gagik requested a review from a team as a code owner November 28, 2025 15:19
Copy link
Collaborator

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

Nice work!

Base automatically changed from gagik/move-arg-parser to main November 28, 2025 16:56
Copilot AI review requested due to automatic review settings December 1, 2025 15:28
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the CLI argument parsing implementation to use Zod for schema validation and type safety. The changes introduce a more maintainable and type-safe approach to handling command-line arguments while preserving backward compatibility with deprecated arguments.

Key Changes:

  • Migrated from yargs-parser with manual validation to a Zod-based schema approach
  • Added proper type definitions and exports for the arg-parser package
  • Fixed previously broken boolean argument parsing (e.g., --browser=true)

Reviewed changes

Copilot reviewed 19 out of 20 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/arg-parser/src/arg-parser.ts New implementation of argument parsing using Zod schemas with yargs-parser integration
packages/arg-parser/src/cli-options.ts Refactored CLI options from interface to Zod schema with metadata for deprecation handling
packages/arg-parser/src/arg-metadata.ts New metadata system for argument aliases, deprecation, and validation rules
packages/cli-repl/src/parse-mongosh-args.ts New wrapper function that uses the refactored arg-parser with mongosh-specific error handling
packages/cli-repl/src/arg-parser.ts Removed legacy argument parser implementation
packages/snippet-manager/package.json Updated zod dependency version
packages/build/package.json Updated @mongodb-js/dl-center dependency
packages/browser-repl/src/components/editor.tsx Added type annotations to callback parameters
configs/tsconfig-mongosh/tsconfig.common.json Updated module resolution to nodenext

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gagik gagik added the no-title-validation Skips validation of PR titles (conventional commit adherence + JIRA ticket inclusion) label Dec 2, 2025
Comment on lines +245 to +248
const processed = processPositionalCliOptions({
parsed,
positional,
});
Copy link
Contributor

@himanshusinghs himanshusinghs Dec 2, 2025

Choose a reason for hiding this comment

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

This bit is problematic for MCP CLI because nodb used in processPositionalCliOptions is of no significance for MCP which is also why we ignore it in the current parsing. This bit was refactored and undone in our refactor PR - https://github.com/mongodb-js/mongodb-mcp-server/pull/729/files#diff-9d5ba1a74fa3c1ca2e89c9b7a2c356807802c8b5be7473377ffd4d9ab348aef1L298-L303

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can mark nodb as unsupported

}: {
schema: z.ZodObject;
configuration?: YargsOptions['configuration'];
}): YargsOptions {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It sucks that we can't easily do unwrapType() typesafely (right?), but the fact that we have to rely on casting to unknown and on instanceof here means we have very little guarantee that an incorrect input type would be properly handled.

Can we make an assertion here that schema instanceof z.ZodObject? Otherwise this would just silently fail if we get an object created from a different zod library, which I don't think we'd want.

Copy link
Contributor Author

@gagik gagik Dec 2, 2025

Choose a reason for hiding this comment

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

z.ZodType is the best we got as far as asserting it I think, but still better than unknown yeah.
Haven't found a good candidate beyond this unwrapping though.

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

Labels

no-title-validation Skips validation of PR titles (conventional commit adherence + JIRA ticket inclusion)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants