Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,34 @@ import argv from "yargs-parser";

import packageJson from "../package.json" with { type: "json" };
import fs from "fs";
import { ReadConcernLevel, ReadPreferenceMode, W } from "mongodb";
const { localDataPath, configPath } = getLocalDataPath();

// If we decide to support non-string config options, we'll need to extend the mechanism for parsing
// env variables.
interface UserConfig extends Record<string, string | undefined> {
interface UserConfig extends Record<string, unknown> {
apiBaseUrl: string;
clientId: string;
stateFile: string;
connectionString?: string;
connectOptions: {
readConcern: ReadConcernLevel;
readPreference: ReadPreferenceMode;
writeConcern: W;
timeoutMS: number;
};
}

const defaults: UserConfig = {
apiBaseUrl: "https://cloud.mongodb.com/",
clientId: "0oabtxactgS3gHIR0297",
stateFile: path.join(localDataPath, "state.json"),
connectOptions: {
readConcern: "local",
readPreference: "secondaryPreferred",
writeConcern: "majority",
timeoutMS: 30_000,
},
};

const mergedUserConfig = {
Expand Down Expand Up @@ -66,21 +79,51 @@ function getLocalDataPath(): { localDataPath: string; configPath: string } {
// are prefixed with `MDB_MCP_` and the keys match the UserConfig keys, but are converted
// to SNAKE_UPPER_CASE.
function getEnvConfig(): Partial<UserConfig> {
const camelCaseToSNAKE_UPPER_CASE = (str: string): string => {
return str.replace(/([a-z])([A-Z])/g, "$1_$2").toUpperCase();
};
function setValue(obj: Record<string, unknown>, path: string[], value: string): void {
const currentField = path.shift()!;
if (path.length === 0) {
const numberValue = Number(value);
if (!isNaN(numberValue)) {
obj[currentField] = numberValue;
return;
}

const booleanValue = value.toLocaleLowerCase();
if (booleanValue === "true" || booleanValue === "false") {
obj[currentField] = booleanValue === "true";
return;
}

obj[currentField] = value;
return;
}

const result: Partial<UserConfig> = {};
for (const key of Object.keys(defaults)) {
const envVarName = `MDB_MCP_${camelCaseToSNAKE_UPPER_CASE(key)}`;
if (process.env[envVarName]) {
result[key] = process.env[envVarName];
if (!obj[currentField]) {
obj[currentField] = {};
}

setValue(obj[currentField] as Record<string, unknown>, path, value);
}

const result: Record<string, unknown> = {};
for (const [key, value] of Object.entries(process.env).filter(
([key, value]) => value !== undefined && key.startsWith("MDB_MCP_")
)) {
const fieldPath = key
.replace("MDB_MCP_", "")
.split(".")
.map((part) => SNAKE_CASE_toCamelCase(part));

setValue(result, fieldPath, value!);
}

return result;
}

function SNAKE_CASE_toCamelCase(str: string): string {
return str.toLowerCase().replace(/([-_][a-z])/g, (group) => group.toUpperCase().replace("_", ""));
}

// Gets the config supplied by the user as a JSON file. The file is expected to be located in the local data path
// and named `config.json`.
function getFileConfig(): Partial<UserConfig> {
Expand Down
6 changes: 6 additions & 0 deletions src/tools/mongodb/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export class ConnectTool extends MongoDBToolBase {
const provider = await NodeDriverServiceProvider.connect(connectionString, {
productDocsLink: "https://docs.mongodb.com/todo-mcp",
productName: "MongoDB MCP",
readConcern: config.connectOptions.readConcern,
readPreference: config.connectOptions.readPreference,
writeConcern: {
w: config.connectOptions.writeConcern,
},
timeoutMS: config.connectOptions.timeoutMS,
});

this.state.serviceProvider = provider;
Expand Down