@@ -4,19 +4,51 @@ 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+
14+ export type ParserOptions = typeof defaultArgParserOptions ;
15+
16+ export const defaultParserOptions = {
17+ // This is the name of key that yargs-parser will look up in CLI
18+ // arguments (--config) and ENV variables (MDB_MCP_CONFIG) to load an
19+ // initial configuration from.
20+ config : "config" ,
21+ // This helps parse the relevant environment variables.
22+ envPrefix : "MDB_MCP_" ,
23+ configuration : {
24+ ...defaultArgParserOptions . configuration ,
25+ // To avoid populating `_` with end-of-flag arguments we explicitly
26+ // populate `--` variable and altogether ignore them later.
27+ "populate--" : true ,
28+ } ,
29+ } satisfies ParserOptions ;
30+
31+ export function parseUserConfig ( {
32+ args,
33+ overrides,
34+ parserOptions = defaultParserOptions ,
35+ } : {
36+ args : string [ ] ;
37+ overrides ?: z4 . ZodRawShape ;
38+ parserOptions ?: ParserOptions ;
39+ } ) : {
1540 warnings : string [ ] ;
1641 parsed : UserConfig | undefined ;
1742 error : string | undefined ;
1843} {
19- const { error : parseError , warnings, parsed } = parseUserConfigSources ( args ) ;
44+ const schema = overrides
45+ ? z4 . object ( {
46+ ...UserConfigSchema . shape ,
47+ ...overrides ,
48+ } )
49+ : UserConfigSchema ;
50+
51+ const { error : parseError , warnings, parsed } = parseUserConfigSources ( { args, schema, parserOptions } ) ;
2052
2153 if ( parseError ) {
2254 return { error : parseError , warnings, parsed : undefined } ;
@@ -40,7 +72,7 @@ export function createUserConfig({ args }: { args: string[] }): {
4072 parsed . connectionString = connectionInfo . connectionString ;
4173 }
4274
43- const configParseResult = UserConfigSchema . safeParse ( parsed ) ;
75+ const configParseResult = schema . safeParse ( parsed ) ;
4476 const mongoshArguments = CliOptionsSchema . safeParse ( parsed ) ;
4577 const error = configParseResult . error || mongoshArguments . error ;
4678 if ( error ) {
@@ -62,34 +94,29 @@ export function createUserConfig({ args }: { args: string[] }): {
6294 } ;
6395}
6496
65- function parseUserConfigSources ( cliArguments : string [ ] ) : {
97+ function parseUserConfigSources < T extends typeof UserConfigSchema > ( {
98+ args,
99+ schema = UserConfigSchema as T ,
100+ parserOptions,
101+ } : {
102+ args : string [ ] ;
103+ schema : T ;
104+ parserOptions : ParserOptions ;
105+ } ) : {
66106 error : string | undefined ;
67107 warnings : string [ ] ;
68- parsed : Partial < CliOptions & z4 . infer < typeof UserConfigSchema > > ;
108+ parsed : Partial < CliOptions & z4 . infer < T > > ;
69109} {
70- let parsed : Partial < CliOptions & z4 . infer < typeof UserConfigSchema > > ;
71- let deprecated : Record < string , keyof UserConfig > ;
110+ let parsed : Partial < CliOptions & z4 . infer < T > > ;
111+ let deprecated : Record < string , string > ;
72112 try {
73113 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- } ,
114+ args,
115+ schema,
116+ parserOptions,
90117 } ) ;
91118 parsed = parsedResult ;
92- deprecated = deprecatedResult ;
119+ deprecated = deprecatedResult as Record < string , string > ;
93120
94121 // Delete fileNames - this is a field populated by mongosh but not used by us.
95122 delete parsed . fileNames ;
@@ -112,7 +139,7 @@ function parseUserConfigSources(cliArguments: string[]): {
112139 }
113140
114141 const deprecationWarnings = [
115- ...getWarnings ( parsed , cliArguments ) ,
142+ ...getWarnings ( parsed , args ) ,
116143 ...Object . entries ( deprecated ) . map ( ( [ deprecated , replacement ] ) => {
117144 return `Warning: The --${ deprecated } argument is deprecated. Use --${ replacement } instead.` ;
118145 } ) ,
0 commit comments