|
| 1 | +#!/usr/bin/env tsx |
| 2 | + |
| 3 | +/** |
| 4 | + * This script generates argument definitions and updates: |
| 5 | + * - server.json arrays |
| 6 | + * - TODO: README.md configuration table |
| 7 | + * |
| 8 | + * It uses the Zod schema and OPTIONS defined in src/common/config.ts |
| 9 | + */ |
| 10 | + |
| 11 | +import { readFileSync, writeFileSync } from "fs"; |
| 12 | +import { join, dirname } from "path"; |
| 13 | +import { fileURLToPath } from "url"; |
| 14 | +import { OPTIONS, UserConfigSchema } from "../src/common/config.js"; |
| 15 | +import type { ZodObject, ZodRawShape } from "zod"; |
| 16 | + |
| 17 | +const __filename = fileURLToPath(import.meta.url); |
| 18 | +const __dirname = dirname(__filename); |
| 19 | + |
| 20 | +function camelCaseToSnakeCase(str: string): string { |
| 21 | + return str.replace(/[A-Z]/g, (letter) => `_${letter}`).toUpperCase(); |
| 22 | +} |
| 23 | + |
| 24 | +// List of configuration keys that contain sensitive/secret information |
| 25 | +// These should be redacted in logs and marked as secret in environment variable definitions |
| 26 | +const SECRET_CONFIG_KEYS = new Set([ |
| 27 | + "connectionString", |
| 28 | + "username", |
| 29 | + "password", |
| 30 | + "apiClientId", |
| 31 | + "apiClientSecret", |
| 32 | + "tlsCAFile", |
| 33 | + "tlsCertificateKeyFile", |
| 34 | + "tlsCertificateKeyFilePassword", |
| 35 | + "tlsCRLFile", |
| 36 | + "sslCAFile", |
| 37 | + "sslPEMKeyFile", |
| 38 | + "sslPEMKeyPassword", |
| 39 | + "sslCRLFile", |
| 40 | + "voyageApiKey", |
| 41 | +]); |
| 42 | + |
| 43 | +interface EnvironmentVariable { |
| 44 | + name: string; |
| 45 | + description: string; |
| 46 | + isRequired: boolean; |
| 47 | + format: string; |
| 48 | + isSecret: boolean; |
| 49 | + configKey: string; |
| 50 | + defaultValue?: unknown; |
| 51 | +} |
| 52 | + |
| 53 | +interface ConfigMetadata { |
| 54 | + description: string; |
| 55 | + defaultValue?: unknown; |
| 56 | +} |
| 57 | + |
| 58 | +function extractZodDescriptions(): Record<string, ConfigMetadata> { |
| 59 | + const result: Record<string, ConfigMetadata> = {}; |
| 60 | + |
| 61 | + // Get the shape of the Zod schema |
| 62 | + const shape = (UserConfigSchema as ZodObject<ZodRawShape>).shape; |
| 63 | + |
| 64 | + for (const [key, fieldSchema] of Object.entries(shape)) { |
| 65 | + const schema = fieldSchema; |
| 66 | + // Extract description from Zod schema |
| 67 | + const description = schema.description || `Configuration option: ${key}`; |
| 68 | + |
| 69 | + // Extract default value if present |
| 70 | + let defaultValue: unknown = undefined; |
| 71 | + if (schema._def && "defaultValue" in schema._def) { |
| 72 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access |
| 73 | + defaultValue = schema._def.defaultValue() as unknown; |
| 74 | + } |
| 75 | + |
| 76 | + result[key] = { |
| 77 | + description, |
| 78 | + defaultValue, |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + return result; |
| 83 | +} |
| 84 | + |
| 85 | +function generateEnvironmentVariables( |
| 86 | + options: typeof OPTIONS, |
| 87 | + zodMetadata: Record<string, ConfigMetadata> |
| 88 | +): EnvironmentVariable[] { |
| 89 | + const envVars: EnvironmentVariable[] = []; |
| 90 | + const processedKeys = new Set<string>(); |
| 91 | + |
| 92 | + // Helper to add env var |
| 93 | + const addEnvVar = (key: string, type: "string" | "number" | "boolean" | "array"): void => { |
| 94 | + if (processedKeys.has(key)) return; |
| 95 | + processedKeys.add(key); |
| 96 | + |
| 97 | + const envVarName = `MDB_MCP_${camelCaseToSnakeCase(key)}`; |
| 98 | + |
| 99 | + // Get description and default value from Zod metadata |
| 100 | + const metadata = zodMetadata[key] || { |
| 101 | + description: `Configuration option: ${key}`, |
| 102 | + }; |
| 103 | + |
| 104 | + // Determine format based on type |
| 105 | + let format = type; |
| 106 | + if (type === "array") { |
| 107 | + format = "string"; // Arrays are passed as comma-separated strings |
| 108 | + } |
| 109 | + |
| 110 | + envVars.push({ |
| 111 | + name: envVarName, |
| 112 | + description: metadata.description, |
| 113 | + isRequired: false, |
| 114 | + format: format, |
| 115 | + isSecret: SECRET_CONFIG_KEYS.has(key), |
| 116 | + configKey: key, |
| 117 | + defaultValue: metadata.defaultValue, |
| 118 | + }); |
| 119 | + }; |
| 120 | + |
| 121 | + // Process all string options |
| 122 | + for (const key of options.string) { |
| 123 | + addEnvVar(key, "string"); |
| 124 | + } |
| 125 | + |
| 126 | + // Process all number options |
| 127 | + for (const key of options.number) { |
| 128 | + addEnvVar(key, "number"); |
| 129 | + } |
| 130 | + |
| 131 | + // Process all boolean options |
| 132 | + for (const key of options.boolean) { |
| 133 | + addEnvVar(key, "boolean"); |
| 134 | + } |
| 135 | + |
| 136 | + // Process all array options |
| 137 | + for (const key of options.array) { |
| 138 | + addEnvVar(key, "array"); |
| 139 | + } |
| 140 | + |
| 141 | + // Sort by name for consistent output |
| 142 | + return envVars.sort((a, b) => a.name.localeCompare(b.name)); |
| 143 | +} |
| 144 | + |
| 145 | +function generatePackageArguments(envVars: EnvironmentVariable[]): unknown[] { |
| 146 | + const packageArguments: unknown[] = []; |
| 147 | + |
| 148 | + // Generate positional arguments from the same config options (only documented ones) |
| 149 | + const documentedVars = envVars.filter((v) => !v.description.startsWith("Configuration option:")); |
| 150 | + |
| 151 | + // Generate named arguments from the same config options |
| 152 | + for (const argument of documentedVars) { |
| 153 | + const arg: Record<string, unknown> = { |
| 154 | + type: "named", |
| 155 | + name: "--" + argument.configKey, |
| 156 | + description: argument.description, |
| 157 | + isRequired: argument.isRequired, |
| 158 | + }; |
| 159 | + |
| 160 | + // Add format if it's not string (string is the default) |
| 161 | + if (argument.format !== "string") { |
| 162 | + arg.format = argument.format; |
| 163 | + } |
| 164 | + |
| 165 | + packageArguments.push(arg); |
| 166 | + } |
| 167 | + |
| 168 | + return packageArguments; |
| 169 | +} |
| 170 | + |
| 171 | +function updateServerJsonEnvVars(envVars: EnvironmentVariable[]): void { |
| 172 | + const serverJsonPath = join(__dirname, "..", "server.json"); |
| 173 | + const packageJsonPath = join(__dirname, "..", "package.json"); |
| 174 | + |
| 175 | + const content = readFileSync(serverJsonPath, "utf-8"); |
| 176 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")) as { version: string }; |
| 177 | + const serverJson = JSON.parse(content) as { |
| 178 | + version?: string; |
| 179 | + packages: { |
| 180 | + registryType?: string; |
| 181 | + identifier?: string; |
| 182 | + environmentVariables: EnvironmentVariable[]; |
| 183 | + packageArguments?: unknown[]; |
| 184 | + version?: string; |
| 185 | + }[]; |
| 186 | + }; |
| 187 | + |
| 188 | + // Get version from package.json |
| 189 | + const version = packageJson.version; |
| 190 | + |
| 191 | + // Generate environment variables array (only documented ones) |
| 192 | + const documentedVars = envVars.filter((v) => !v.description.startsWith("Configuration option:")); |
| 193 | + const envVarsArray = documentedVars.map((v) => ({ |
| 194 | + name: v.name, |
| 195 | + description: v.description, |
| 196 | + isRequired: v.isRequired, |
| 197 | + format: v.format, |
| 198 | + isSecret: v.isSecret, |
| 199 | + })); |
| 200 | + |
| 201 | + // Generate package arguments (named arguments in camelCase) |
| 202 | + const packageArguments = generatePackageArguments(envVars); |
| 203 | + |
| 204 | + // Update version at root level |
| 205 | + serverJson.version = process.env.VERSION || version; |
| 206 | + |
| 207 | + // Update environmentVariables, packageArguments, and version for all packages |
| 208 | + if (serverJson.packages && Array.isArray(serverJson.packages)) { |
| 209 | + for (const pkg of serverJson.packages) { |
| 210 | + pkg.environmentVariables = envVarsArray as EnvironmentVariable[]; |
| 211 | + pkg.packageArguments = packageArguments; |
| 212 | + pkg.version = version; |
| 213 | + |
| 214 | + // Update OCI identifier version tag if this is an OCI package |
| 215 | + if (pkg.registryType === "oci" && pkg.identifier) { |
| 216 | + // Replace the version tag in the OCI identifier (e.g., docker.io/mongodb/mongodb-mcp-server:1.0.0) |
| 217 | + pkg.identifier = pkg.identifier.replace(/:[^:]+$/, `:${version}`); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + writeFileSync(serverJsonPath, JSON.stringify(serverJson, null, 2) + "\n", "utf-8"); |
| 223 | + console.log(`✓ Updated server.json (version ${version})`); |
| 224 | +} |
| 225 | + |
| 226 | +function main(): void { |
| 227 | + const zodMetadata = extractZodDescriptions(); |
| 228 | + |
| 229 | + const envVars = generateEnvironmentVariables(OPTIONS, zodMetadata); |
| 230 | + updateServerJsonEnvVars(envVars); |
| 231 | +} |
| 232 | + |
| 233 | +main(); |
0 commit comments