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

mongodb-js / mongodb-mcp-server / 14735750222

29 Apr 2025 03:54PM UTC coverage: 78.141%. First build
14735750222

Pull #167

github

fmenezes
fix: issue template with title twice
Pull Request #167: fix: issue template with title twice

134 of 244 branches covered (54.92%)

Branch coverage included in aggregate %.

774 of 918 relevant lines covered (84.31%)

39.05 hits per line

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

64.15
/src/config.ts
1
import path from "path";
32✔
2
import os from "os";
32✔
3
import argv from "yargs-parser";
32✔
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 = {
32✔
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 = {
32✔
43
    ...defaults,
44
    ...getEnvConfig(),
45
    ...getCliConfig(),
46
};
47

48
function getLogPath(): string {
49
    const localDataPath =
50
        process.platform === "win32"
32!
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");
32✔
55

56
    return logPath;
32✔
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();
15✔
65
        if (!currentField) {
15!
66
            return;
×
67
        }
68
        if (path.length === 0) {
15✔
69
            const numberValue = Number(value);
15✔
70
            if (!isNaN(numberValue)) {
15!
71
                obj[currentField] = numberValue;
×
72
                return;
×
73
            }
74

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

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

87
            obj[currentField] = value;
15✔
88
            return;
15✔
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> = {};
32✔
99
    const mcpVariables = Object.entries(process.env).filter(
32✔
100
        ([key, value]) => value !== undefined && key.startsWith("MDB_MCP_")
4,879✔
101
    ) as [string, string][];
102
    for (const [key, value] of mcpVariables) {
32✔
103
        const fieldPath = key
15✔
104
            .replace("MDB_MCP_", "")
105
            .split(".")
106
            .map((part) => SNAKE_CASE_toCamelCase(part));
15✔
107

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

111
    return result;
32✔
112
}
113

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

118
// Reads the cli args and parses them into a UserConfig object.
119
function getCliConfig() {
120
    return argv(process.argv.slice(2), {
32✔
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

© 2025 Coveralls, Inc