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

mongodb-js / mongodb-mcp-server / 14672998392

25 Apr 2025 08:31PM UTC coverage: 82.446%. Remained the same
14672998392

Pull #137

github

nirinchev
Update name for fork workflow
Pull Request #137: chore: revamp gha workflows

135 of 212 branches covered (63.68%)

Branch coverage included in aggregate %.

748 of 859 relevant lines covered (87.08%)

48.14 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";
31✔
2
import os from "os";
31✔
3
import argv from "yargs-parser";
31✔
4

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

7
// If we decide to support non-string config options, we'll need to extend the mechanism for parsing
8
// env variables.
9
export interface UserConfig {
10
    apiBaseUrl?: string;
11
    apiClientId?: string;
12
    apiClientSecret?: string;
13
    telemetry?: "enabled" | "disabled";
14
    logPath: string;
15
    connectionString?: string;
16
    connectOptions: {
17
        readConcern: ReadConcernLevel;
18
        readPreference: ReadPreferenceMode;
19
        writeConcern: W;
20
        timeoutMS: number;
21
    };
22
    disabledTools: Array<string>;
23
}
24

25
const defaults: UserConfig = {
31✔
26
    logPath: getLogPath(),
27
    connectOptions: {
28
        readConcern: "local",
29
        readPreference: "secondaryPreferred",
30
        writeConcern: "majority",
31
        timeoutMS: 30_000,
32
    },
33
    disabledTools: [],
34
    telemetry: "disabled",
35
};
36

37
export const config = {
31✔
38
    ...defaults,
39
    ...getEnvConfig(),
40
    ...getCliConfig(),
41
};
42

43
function getLogPath(): string {
44
    const localDataPath =
45
        process.platform === "win32"
31!
46
            ? path.join(process.env.LOCALAPPDATA || process.env.APPDATA || os.homedir(), "mongodb")
×
47
            : path.join(os.homedir(), ".mongodb");
48

49
    const logPath = path.join(localDataPath, "mongodb-mcp", ".app-logs");
31✔
50

51
    return logPath;
31✔
52
}
53

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

70
            const booleanValue = value.toLocaleLowerCase();
15✔
71
            if (booleanValue === "true" || booleanValue === "false") {
15!
72
                obj[currentField] = booleanValue === "true";
×
73
                return;
×
74
            }
75

76
            // Try to parse an array of values
77
            if (value.indexOf(",") !== -1) {
15!
78
                obj[currentField] = value.split(",").map((v) => v.trim());
×
79
                return;
×
80
            }
81

82
            obj[currentField] = value;
15✔
83
            return;
15✔
84
        }
85

86
        if (!obj[currentField]) {
×
87
            obj[currentField] = {};
×
88
        }
89

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

93
    const result: Record<string, unknown> = {};
31✔
94
    const mcpVariables = Object.entries(process.env).filter(
31✔
95
        ([key, value]) => value !== undefined && key.startsWith("MDB_MCP_")
4,727✔
96
    ) as [string, string][];
97
    for (const [key, value] of mcpVariables) {
31✔
98
        const fieldPath = key
15✔
99
            .replace("MDB_MCP_", "")
100
            .split(".")
101
            .map((part) => SNAKE_CASE_toCamelCase(part));
15✔
102

103
        setValue(result, fieldPath, value);
15✔
104
    }
105

106
    return result;
31✔
107
}
108

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

113
// Reads the cli args and parses them into a UserConfig object.
114
function getCliConfig() {
115
    return argv(process.argv.slice(2), {
31✔
116
        array: ["disabledTools"],
117
    }) as unknown as Partial<UserConfig>;
118
}
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