-
Notifications
You must be signed in to change notification settings - Fork 175
chore: support schema overrides and custom parserOptions MCP-338 #800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4b8fe45
5ad2fa4
d90ea39
b9961ab
93ef973
666ab2b
3b4a437
6faa450
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,19 +4,51 @@ import type { Secret } from "../keychain.js"; | |
| import { matchingConfigKey } from "./configUtils.js"; | ||
| import { UserConfigSchema, type UserConfig } from "./userConfig.js"; | ||
| import { | ||
| defaultParserOptions, | ||
| defaultParserOptions as defaultArgParserOptions, | ||
| parseArgsWithCliOptions, | ||
| CliOptionsSchema, | ||
| UnknownArgumentError, | ||
| } from "@mongosh/arg-parser/arg-parser"; | ||
| import type { z as z4 } from "zod/v4"; | ||
|
|
||
| export function createUserConfig({ args }: { args: string[] }): { | ||
| import { z as z4 } from "zod/v4"; | ||
|
|
||
| export type ParserOptions = typeof defaultArgParserOptions; | ||
|
|
||
| export const defaultParserOptions = { | ||
| // This is the name of key that yargs-parser will look up in CLI | ||
| // arguments (--config) and ENV variables (MDB_MCP_CONFIG) to load an | ||
| // initial configuration from. | ||
| config: "config", | ||
| // This helps parse the relevant environment variables. | ||
| envPrefix: "MDB_MCP_", | ||
| configuration: { | ||
| ...defaultArgParserOptions.configuration, | ||
| // To avoid populating `_` with end-of-flag arguments we explicitly | ||
| // populate `--` variable and altogether ignore them later. | ||
| "populate--": true, | ||
| }, | ||
| } satisfies ParserOptions; | ||
|
|
||
| export function parseUserConfig({ | ||
| args, | ||
| overrides, | ||
| parserOptions = defaultParserOptions, | ||
| }: { | ||
| args: string[]; | ||
| overrides?: z4.ZodRawShape; | ||
| parserOptions?: ParserOptions; | ||
| }): { | ||
| warnings: string[]; | ||
| parsed: UserConfig | undefined; | ||
| error: string | undefined; | ||
| } { | ||
| const { error: parseError, warnings, parsed } = parseUserConfigSources(args); | ||
| const schema = overrides | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking q: Which takes precedent: env vars or overrides?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. generally overrides are applied to the schema and in practice likely only going to be used to modify the default, i.e. {
readOnly: UserConfigSchema.shape.readOnly.default(true)
}so any value (CLI, ENV) will override the default. I will add some documentation to make it clearer |
||
| ? z4.object({ | ||
| ...UserConfigSchema.shape, | ||
| ...overrides, | ||
| }) | ||
| : UserConfigSchema; | ||
|
|
||
| const { error: parseError, warnings, parsed } = parseUserConfigSources({ args, schema, parserOptions }); | ||
|
|
||
| if (parseError) { | ||
| return { error: parseError, warnings, parsed: undefined }; | ||
|
|
@@ -40,7 +72,7 @@ export function createUserConfig({ args }: { args: string[] }): { | |
| parsed.connectionString = connectionInfo.connectionString; | ||
| } | ||
|
|
||
| const configParseResult = UserConfigSchema.safeParse(parsed); | ||
| const configParseResult = schema.safeParse(parsed); | ||
| const mongoshArguments = CliOptionsSchema.safeParse(parsed); | ||
| const error = configParseResult.error || mongoshArguments.error; | ||
| if (error) { | ||
|
|
@@ -62,34 +94,29 @@ export function createUserConfig({ args }: { args: string[] }): { | |
| }; | ||
| } | ||
|
|
||
| function parseUserConfigSources(cliArguments: string[]): { | ||
| function parseUserConfigSources<T extends typeof UserConfigSchema>({ | ||
| args, | ||
| schema = UserConfigSchema as T, | ||
| parserOptions, | ||
| }: { | ||
| args: string[]; | ||
| schema: T; | ||
| parserOptions: ParserOptions; | ||
| }): { | ||
| error: string | undefined; | ||
| warnings: string[]; | ||
| parsed: Partial<CliOptions & z4.infer<typeof UserConfigSchema>>; | ||
| parsed: Partial<CliOptions & z4.infer<T>>; | ||
| } { | ||
| let parsed: Partial<CliOptions & z4.infer<typeof UserConfigSchema>>; | ||
| let deprecated: Record<string, keyof UserConfig>; | ||
| let parsed: Partial<CliOptions & z4.infer<T>>; | ||
| let deprecated: Record<string, string>; | ||
| try { | ||
| const { parsed: parsedResult, deprecated: deprecatedResult } = parseArgsWithCliOptions({ | ||
| args: cliArguments, | ||
| schema: UserConfigSchema, | ||
| parserOptions: { | ||
| // This is the name of key that yargs-parser will look up in CLI | ||
| // arguments (--config) and ENV variables (MDB_MCP_CONFIG) to load an | ||
| // initial configuration from. | ||
| config: "config", | ||
| // This helps parse the relevant environment variables. | ||
| envPrefix: "MDB_MCP_", | ||
| configuration: { | ||
| ...defaultParserOptions.configuration, | ||
| // To avoid populating `_` with end-of-flag arguments we explicitly | ||
| // populate `--` variable and altogether ignore them later. | ||
| "populate--": true, | ||
| }, | ||
| }, | ||
| args, | ||
| schema, | ||
| parserOptions, | ||
| }); | ||
| parsed = parsedResult; | ||
| deprecated = deprecatedResult; | ||
| deprecated = deprecatedResult as Record<string, string>; | ||
|
|
||
| // Delete fileNames - this is a field populated by mongosh but not used by us. | ||
| delete parsed.fileNames; | ||
|
|
@@ -112,7 +139,7 @@ function parseUserConfigSources(cliArguments: string[]): { | |
| } | ||
|
|
||
| const deprecationWarnings = [ | ||
| ...getWarnings(parsed, cliArguments), | ||
| ...getWarnings(parsed, args), | ||
| ...Object.entries(deprecated).map(([deprecated, replacement]) => { | ||
| return `Warning: The --${deprecated} argument is deprecated. Use --${replacement} instead.`; | ||
| }), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,22 @@ | ||
| export { Server, type ServerOptions } from "./server.js"; | ||
| export { Session, type SessionOptions } from "./common/session.js"; | ||
| export { type UserConfig, UserConfigSchema } from "./common/config/userConfig.js"; | ||
| export { createUserConfig as parseCliArgumentsAsUserConfig } from "./common/config/createUserConfig.js"; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so I unfortunately missed that we're doing this; it's weird to have this discrepancy where our external export is named differently than our internal one. To me the idea that the name isn't straightforward was a code smell.
It is technically a library breaking change though.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then we anticipate the release with this work will bump major version, correct?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, yes, though to avoid a major version bump, let's re-export with the old name and mark as deprecated? |
||
| export { parseUserConfig, defaultParserOptions, type ParserOptions } from "./common/config/parseUserConfig.js"; | ||
|
|
||
| import { parseUserConfig } from "./common/config/parseUserConfig.js"; | ||
| import type { UserConfig } from "./common/config/userConfig.js"; | ||
|
|
||
| /** @deprecated Use `parseUserConfig` instead. */ | ||
| export function parseArgsWithCliOptions(cliArguments: string[]): { | ||
| warnings: string[]; | ||
| parsed: UserConfig | undefined; | ||
| error: string | undefined; | ||
| } { | ||
| return parseUserConfig({ | ||
| args: cliArguments, | ||
| }); | ||
| } | ||
|
|
||
| export { LoggerBase, type LogPayload, type LoggerType, type LogLevel } from "./common/logger.js"; | ||
| export { StreamableHttpRunner } from "./transports/streamableHttp.js"; | ||
| export { StdioRunner } from "./transports/stdio.js"; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the concept of CreateUserConfigHelpers was unused.