@@ -4,19 +4,52 @@ import type { Secret } from "../keychain.js";
44import { matchingConfigKey } from "./configUtils.js" ;
55import { UserConfigSchema , type UserConfig } from "./userConfig.js" ;
66import {
7- defaultParserOptions ,
7+ defaultParserOptions as defaultArgParserOptions ,
88 parseArgsWithCliOptions ,
99 CliOptionsSchema ,
1010 UnknownArgumentError ,
1111} from "@mongosh/arg-parser/arg-parser" ;
12- import type { z as z4 } from "zod/v4" ;
13-
14- export function createUserConfig ( { args } : { args : string [ ] } ) : {
12+ import { z as z4 } from "zod/v4" ;
13+ import type yargsParser from "yargs-parser" ;
14+
15+ export type ParserOptions = Partial < yargsParser . Options > ;
16+
17+ export const defaultParserOptions = {
18+ // This is the name of key that yargs-parser will look up in CLI
19+ // arguments (--config) and ENV variables (MDB_MCP_CONFIG) to load an
20+ // initial configuration from.
21+ config : "config" ,
22+ // This helps parse the relevant environment variables.
23+ envPrefix : "MDB_MCP_" ,
24+ configuration : {
25+ ...defaultArgParserOptions . configuration ,
26+ // To avoid populating `_` with end-of-flag arguments we explicitly
27+ // populate `--` variable and altogether ignore them later.
28+ "populate--" : true ,
29+ } ,
30+ } satisfies ParserOptions ;
31+
32+ export function createUserConfig ( {
33+ args,
34+ overrides,
35+ parserOptions = defaultParserOptions ,
36+ } : {
37+ args : string [ ] ;
38+ overrides ?: z4 . ZodRawShape ;
39+ parserOptions ?: ParserOptions ;
40+ } ) : {
1541 warnings : string [ ] ;
1642 parsed : UserConfig | undefined ;
1743 error : string | undefined ;
1844} {
19- const { error : parseError , warnings, parsed } = parseUserConfigSources ( args ) ;
45+ const schema = overrides
46+ ? z4 . object ( {
47+ ...UserConfigSchema . shape ,
48+ ...overrides ,
49+ } )
50+ : UserConfigSchema ;
51+
52+ const { error : parseError , warnings, parsed } = parseUserConfigSources ( { args, schema, parserOptions } ) ;
2053
2154 if ( parseError ) {
2255 return { error : parseError , warnings, parsed : undefined } ;
@@ -40,7 +73,7 @@ export function createUserConfig({ args }: { args: string[] }): {
4073 parsed . connectionString = connectionInfo . connectionString ;
4174 }
4275
43- const configParseResult = UserConfigSchema . safeParse ( parsed ) ;
76+ const configParseResult = schema . safeParse ( parsed ) ;
4477 const mongoshArguments = CliOptionsSchema . safeParse ( parsed ) ;
4578 const error = configParseResult . error || mongoshArguments . error ;
4679 if ( error ) {
@@ -62,34 +95,29 @@ export function createUserConfig({ args }: { args: string[] }): {
6295 } ;
6396}
6497
65- function parseUserConfigSources ( cliArguments : string [ ] ) : {
98+ function parseUserConfigSources < T extends typeof UserConfigSchema > ( {
99+ args,
100+ schema = UserConfigSchema as T ,
101+ parserOptions,
102+ } : {
103+ args : string [ ] ;
104+ schema : T ;
105+ parserOptions : ParserOptions ;
106+ } ) : {
66107 error : string | undefined ;
67108 warnings : string [ ] ;
68- parsed : Partial < CliOptions & z4 . infer < typeof UserConfigSchema > > ;
109+ parsed : Partial < CliOptions & z4 . infer < T > > ;
69110} {
70- let parsed : Partial < CliOptions & z4 . infer < typeof UserConfigSchema > > ;
71- let deprecated : Record < string , keyof UserConfig > ;
111+ let parsed : Partial < CliOptions & z4 . infer < T > > ;
112+ let deprecated : Record < string , string > ;
72113 try {
73114 const { parsed : parsedResult , deprecated : deprecatedResult } = parseArgsWithCliOptions ( {
74- args : cliArguments ,
75- schema : UserConfigSchema ,
76- parserOptions : {
77- // This is the name of key that yargs-parser will look up in CLI
78- // arguments (--config) and ENV variables (MDB_MCP_CONFIG) to load an
79- // initial configuration from.
80- config : "config" ,
81- // This helps parse the relevant environment variables.
82- envPrefix : "MDB_MCP_" ,
83- configuration : {
84- ...defaultParserOptions . configuration ,
85- // To avoid populating `_` with end-of-flag arguments we explicitly
86- // populate `--` variable and altogether ignore them later.
87- "populate--" : true ,
88- } ,
89- } ,
115+ args,
116+ schema,
117+ parserOptions,
90118 } ) ;
91119 parsed = parsedResult ;
92- deprecated = deprecatedResult ;
120+ deprecated = deprecatedResult as Record < string , string > ;
93121
94122 // Delete fileNames - this is a field populated by mongosh but not used by us.
95123 delete parsed . fileNames ;
@@ -112,7 +140,7 @@ function parseUserConfigSources(cliArguments: string[]): {
112140 }
113141
114142 const deprecationWarnings = [
115- ...getWarnings ( parsed , cliArguments ) ,
143+ ...getWarnings ( parsed , args ) ,
116144 ...Object . entries ( deprecated ) . map ( ( [ deprecated , replacement ] ) => {
117145 return `Warning: The --${ deprecated } argument is deprecated. Use --${ replacement } instead.` ;
118146 } ) ,
0 commit comments