Skip to content

Commit 7fda570

Browse files
committed
chore: get default user config from schema
1 parent 3f6de0f commit 7fda570

File tree

3 files changed

+62
-50
lines changed

3 files changed

+62
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ The MongoDB MCP Server can be configured using multiple methods, with the follow
354354
| `atlasTemporaryDatabaseUserLifetimeMs` | `MDB_MCP_ATLAS_TEMPORARY_DATABASE_USER_LIFETIME_MS` | `14400000` | Time in milliseconds that temporary database users created when connecting to MongoDB Atlas clusters will remain active before being automatically deleted. |
355355
| `confirmationRequiredTools` | `MDB_MCP_CONFIRMATION_REQUIRED_TOOLS` | `"atlas-create-access-list,atlas-create-db-user,drop-database,drop-collection,delete-many,drop-index"` | Comma separated values of tool names that require user confirmation before execution. Requires the client to support elicitation. |
356356
| `connectionString` | `MDB_MCP_CONNECTION_STRING` | `<not set>` | MongoDB connection string for direct database connections. Optional, if not set, you'll need to call the connect tool before interacting with MongoDB data. |
357-
| `disableEmbeddingsValidation` | `MDB_MCP_DISABLE_EMBEDDINGS_VALIDATION` | `false` | When set to true, disables validation of embeddings dimensions. |
357+
| `disableEmbeddingsValidation` | `MDB_MCP_DISABLE_EMBEDDINGS_VALIDATION` | `<not set>` | When set to true, disables validation of embeddings dimensions. |
358358
| `disabledTools` | `MDB_MCP_DISABLED_TOOLS` | `""` | Comma separated values of tool names, operation types, and/or categories of tools that will be disabled. |
359359
| `exportCleanupIntervalMs` | `MDB_MCP_EXPORT_CLEANUP_INTERVAL_MS` | `120000` | Time in milliseconds between export cleanup cycles that remove expired export files. |
360360
| `exportTimeoutMs` | `MDB_MCP_EXPORT_TIMEOUT_MS` | `300000` | Time in milliseconds after which an export is considered expired and eligible for cleanup. |

src/common/config.ts

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,21 @@ interface ConfigFieldMeta {
181181
*/
182182
export const configRegistry = z4.registry<ConfigFieldMeta>();
183183

184+
function getLocalDataPath(): string {
185+
return process.platform === "win32"
186+
? path.join(process.env.LOCALAPPDATA || process.env.APPDATA || os.homedir(), "mongodb")
187+
: path.join(os.homedir(), ".mongodb");
188+
}
189+
190+
export function getLogPath(): string {
191+
const logPath = path.join(getLocalDataPath(), "mongodb-mcp", ".app-logs");
192+
return logPath;
193+
}
194+
195+
export function getExportsPath(): string {
196+
return path.join(getLocalDataPath(), "mongodb-mcp", "exports");
197+
}
198+
184199
export const UserConfigSchema = z4.object({
185200
apiBaseUrl: z4.string().default("https://cloud.mongodb.com/"),
186201
apiClientId: z4
@@ -209,6 +224,7 @@ export const UserConfigSchema = z4.object({
209224
}),
210225
logPath: z4
211226
.string()
227+
.default(getLogPath())
212228
.describe("Folder to store logs.")
213229
.register(configRegistry, { defaultValueDescription: "see below*" }),
214230
disabledTools: z4
@@ -282,6 +298,7 @@ export const UserConfigSchema = z4.object({
282298
),
283299
exportsPath: z4
284300
.string()
301+
.default(getExportsPath())
285302
.describe("Folder to store exported data files.")
286303
.register(configRegistry, { defaultValueDescription: "see below*" }),
287304
exportTimeoutMs: z4
@@ -327,53 +344,14 @@ export const UserConfigSchema = z4.object({
327344
export type PreviewFeature = z4.infer<typeof UserConfigSchema>["previewFeatures"][number];
328345
export type UserConfig = z4.infer<typeof UserConfigSchema> & CliOptions;
329346

330-
export const defaultUserConfig: UserConfig = {
331-
apiBaseUrl: "https://cloud.mongodb.com/",
332-
logPath: getLogPath(),
333-
exportsPath: getExportsPath(),
334-
exportTimeoutMs: 5 * 60 * 1000, // 5 minutes
335-
exportCleanupIntervalMs: 2 * 60 * 1000, // 2 minutes
336-
disabledTools: [],
337-
telemetry: "enabled",
338-
readOnly: false,
339-
indexCheck: false,
340-
confirmationRequiredTools: [
341-
"atlas-create-access-list",
342-
"atlas-create-db-user",
343-
"drop-database",
344-
"drop-collection",
345-
"delete-many",
346-
"drop-index",
347-
],
348-
transport: "stdio",
349-
httpPort: 3000,
350-
httpHost: "127.0.0.1",
351-
loggers: ["disk", "mcp"],
352-
idleTimeoutMs: 10 * 60 * 1000, // 10 minutes
353-
notificationTimeoutMs: 9 * 60 * 1000, // 9 minutes
354-
httpHeaders: {},
355-
maxDocumentsPerQuery: 100, // By default, we only fetch a maximum 100 documents per query / aggregation
356-
maxBytesPerQuery: 16 * 1024 * 1024, // By default, we only return ~16 mb of data per query / aggregation
357-
atlasTemporaryDatabaseUserLifetimeMs: 4 * 60 * 60 * 1000, // 4 hours
358-
voyageApiKey: "",
359-
disableEmbeddingsValidation: false,
360-
vectorSearchDimensions: 1024,
361-
vectorSearchSimilarityFunction: "euclidean",
362-
previewFeatures: [],
363-
};
347+
export const defaultUserConfig: UserConfig = UserConfigSchema.parse({});
364348

365349
export const config = setupUserConfig({
366350
defaults: defaultUserConfig,
367351
cli: process.argv,
368352
env: process.env,
369353
});
370354

371-
function getLocalDataPath(): string {
372-
return process.platform === "win32"
373-
? path.join(process.env.LOCALAPPDATA || process.env.APPDATA || os.homedir(), "mongodb")
374-
: path.join(os.homedir(), ".mongodb");
375-
}
376-
377355
export type DriverOptions = ConnectionInfo["driverOptions"];
378356
export const defaultDriverOptions: DriverOptions = {
379357
readConcern: {
@@ -388,15 +366,6 @@ export const defaultDriverOptions: DriverOptions = {
388366
applyProxyToOIDC: true,
389367
};
390368

391-
function getLogPath(): string {
392-
const logPath = path.join(getLocalDataPath(), "mongodb-mcp", ".app-logs");
393-
return logPath;
394-
}
395-
396-
function getExportsPath(): string {
397-
return path.join(getLocalDataPath(), "mongodb-mcp", "exports");
398-
}
399-
400369
// Gets the config supplied by the user as environment variables. The variable names
401370
// are prefixed with `MDB_MCP_` and the keys match the UserConfig keys, but are converted
402371
// to SNAKE_UPPER_CASE.

tests/unit/common/config.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,55 @@ import {
55
defaultUserConfig,
66
registerKnownSecretsInRootKeychain,
77
warnAboutDeprecatedOrUnknownCliArgs,
8+
getLogPath,
9+
getExportsPath,
810
} from "../../../src/common/config.js";
911
import type { CliOptions } from "@mongosh/arg-parser";
1012
import { Keychain } from "../../../src/common/keychain.js";
1113
import type { Secret } from "../../../src/common/keychain.js";
1214

1315
describe("config", () => {
16+
it("should generate defaults from UserConfigSchema that match expected values", () => {
17+
// Expected hardcoded values (what we had before)
18+
const expectedDefaults = {
19+
apiBaseUrl: "https://cloud.mongodb.com/",
20+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
21+
logPath: getLogPath() as string,
22+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
23+
exportsPath: getExportsPath() as string,
24+
exportTimeoutMs: 5 * 60 * 1000, // 5 minutes
25+
exportCleanupIntervalMs: 2 * 60 * 1000, // 2 minutes
26+
disabledTools: [],
27+
telemetry: "enabled",
28+
readOnly: false,
29+
indexCheck: false,
30+
confirmationRequiredTools: [
31+
"atlas-create-access-list",
32+
"atlas-create-db-user",
33+
"drop-database",
34+
"drop-collection",
35+
"delete-many",
36+
"drop-index",
37+
],
38+
transport: "stdio",
39+
httpPort: 3000,
40+
httpHost: "127.0.0.1",
41+
loggers: ["disk", "mcp"],
42+
idleTimeoutMs: 10 * 60 * 1000, // 10 minutes
43+
notificationTimeoutMs: 9 * 60 * 1000, // 9 minutes
44+
httpHeaders: {},
45+
maxDocumentsPerQuery: 100,
46+
maxBytesPerQuery: 16 * 1024 * 1024, // ~16 mb
47+
atlasTemporaryDatabaseUserLifetimeMs: 4 * 60 * 60 * 1000, // 4 hours
48+
voyageApiKey: "",
49+
vectorSearchDimensions: 1024,
50+
vectorSearchSimilarityFunction: "euclidean",
51+
previewFeatures: [],
52+
};
53+
54+
expect(defaultUserConfig).toStrictEqual(expectedDefaults);
55+
});
56+
1457
describe("env var parsing", () => {
1558
describe("mongodb urls", () => {
1659
it("should not try to parse a multiple-host urls", () => {

0 commit comments

Comments
 (0)