• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mongodb-js / mongodb-mcp-server / 15848919576

24 Jun 2025 11:14AM UTC coverage: 73.971%. First build
15848919576

Pull #318

github

web-flow
Merge e134a2230 into 96c8f62be
Pull Request #318: chore: handle other signals

214 of 377 branches covered (56.76%)

Branch coverage included in aggregate %.

792 of 983 relevant lines covered (80.57%)

55.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.0
/src/config.ts
1
import path from "path";
2
import os from "os";
3
import argv from "yargs-parser";
4

5
import { ReadConcernLevel, ReadPreferenceMode, W } from "mongodb";
6

7
export interface ConnectOptions {
8
    readConcern: ReadConcernLevel;
9
    readPreference: ReadPreferenceMode;
10
    writeConcern: W;
11
    timeoutMS: number;
12
}
13

14
// If we decide to support non-string config options, we'll need to extend the mechanism for parsing
15
// env variables.
16
export interface UserConfig {
17
    apiBaseUrl: string;
18
    apiClientId?: string;
19
    apiClientSecret?: string;
20
    telemetry?: "enabled" | "disabled";
21
    logPath: string;
22
    connectionString?: string;
23
    connectOptions: ConnectOptions;
24
    disabledTools: Array<string>;
25
    readOnly?: boolean;
26
}
27

28
const defaults: UserConfig = {
35✔
29
    apiBaseUrl: "https://cloud.mongodb.com/",
30
    logPath: getLogPath(),
31
    connectOptions: {
32
        readConcern: "local",
33
        readPreference: "secondaryPreferred",
34
        writeConcern: "majority",
35
        timeoutMS: 30_000,
36
    },
37
    disabledTools: [],
38
    telemetry: "enabled",
39
    readOnly: false,
40
};
41

42
export const config = {
35✔
43
    ...defaults,
44
    ...getEnvConfig(),
45
    ...getCliConfig(),
46
};
47

48
function getLogPath(): string {
49
    const localDataPath =
50
        process.platform === "win32"
35!
51
            ? path.join(process.env.LOCALAPPDATA || process.env.APPDATA || os.homedir(), "mongodb")
×
52
            : path.join(os.homedir(), ".mongodb");
53

54
    const logPath = path.join(localDataPath, "mongodb-mcp", ".app-logs");
35✔
55

56
    return logPath;
35✔
57
}
58

59
// Gets the config supplied by the user as environment variables. The variable names
60
// are prefixed with `MDB_MCP_` and the keys match the UserConfig keys, but are converted
61
// to SNAKE_UPPER_CASE.
62
function getEnvConfig(): Partial<UserConfig> {
63
    function setValue(obj: Record<string, unknown>, path: string[], value: string): void {
64
        const currentField = path.shift();
18✔
65
        if (!currentField) {
18!
66
            return;
×
67
        }
68
        if (path.length === 0) {
18✔
69
            const numberValue = Number(value);
18✔
70
            if (!isNaN(numberValue)) {
18!
71
                obj[currentField] = numberValue;
×
72
                return;
×
73
            }
74

75
            const booleanValue = value.toLocaleLowerCase();
18✔
76
            if (booleanValue === "true" || booleanValue === "false") {
18!
77
                obj[currentField] = booleanValue === "true";
×
78
                return;
×
79
            }
80

81
            // Try to parse an array of values
82
            if (value.indexOf(",") !== -1) {
18!
83
                obj[currentField] = value.split(",").map((v) => v.trim());
×
84
                return;
×
85
            }
86

87
            obj[currentField] = value;
18✔
88
            return;
18✔
89
        }
90

91
        if (!obj[currentField]) {
×
92
            obj[currentField] = {};
×
93
        }
94

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

98
    const result: Record<string, unknown> = {};
35✔
99
    const mcpVariables = Object.entries(process.env).filter(
35✔
100
        ([key, value]) => value !== undefined && key.startsWith("MDB_MCP_")
5,338✔
101
    ) as [string, string][];
102
    for (const [key, value] of mcpVariables) {
35✔
103
        const fieldPath = key
18✔
104
            .replace("MDB_MCP_", "")
105
            .split(".")
106
            .map((part) => SNAKE_CASE_toCamelCase(part));
18✔
107

108
        setValue(result, fieldPath, value);
18✔
109
    }
110

111
    return result;
35✔
112
}
113

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

118
// Reads the cli args and parses them into a UserConfig object.
119
function getCliConfig() {
120
    return argv(process.argv.slice(2), {
35✔
121
        array: ["disabledTools"],
122
    }) as unknown as Partial<UserConfig>;
123
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc