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

mongodb-js / mongodb-mcp-server / 14640870200

24 Apr 2025 11:49AM UTC coverage: 81.603% (+4.1%) from 77.5%
14640870200

Pull #105

github

nirinchev
throw an error
Pull Request #105: chore: add remaining mongodb integration tests

123 of 199 branches covered (61.81%)

Branch coverage included in aggregate %.

13 of 13 new or added lines in 3 files covered. (100.0%)

671 of 774 relevant lines covered (86.69%)

45.35 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";
30✔
2
import os from "os";
30✔
3
import argv from "yargs-parser";
30✔
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 = {
30✔
26
    logPath: getLogPath(),
27
    connectOptions: {
28
        readConcern: "local",
29
        readPreference: "secondaryPreferred",
30
        writeConcern: "majority",
31
        timeoutMS: 30_000,
32
    },
33
    disabledTools: [],
34
};
35

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

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

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

50
    return logPath;
30✔
51
}
52

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

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

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

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

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

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

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

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

105
    return result;
30✔
106
}
107

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

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