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

mongodb-js / mongodb-mcp-server / 14705884283

28 Apr 2025 10:45AM UTC coverage: 82.547% (+0.9%) from 81.696%
14705884283

Pull #131

github

fmenezes
fix: make ip changes smooth
Pull Request #131: feat: add atlas-connect-cluster tool

147 of 229 branches covered (64.19%)

Branch coverage included in aggregate %.

26 of 31 new or added lines in 4 files covered. (83.87%)

60 existing lines in 8 files now uncovered.

780 of 894 relevant lines covered (87.25%)

49.5 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 = {
26
    logPath: getLogPath(),
31✔
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 = {
38
    ...defaults,
39
    ...getEnvConfig(),
31✔
40
    ...getCliConfig(),
41
};
42

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

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

51
    return logPath;
31✔
52
}
53

31✔
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();
60
        if (!currentField) {
61
            return;
15✔
62
        }
15!
UNCOV
63
        if (path.length === 0) {
×
64
            const numberValue = Number(value);
65
            if (!isNaN(numberValue)) {
15✔
66
                obj[currentField] = numberValue;
15✔
67
                return;
15!
UNCOV
68
            }
×
UNCOV
69

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

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

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

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

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

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

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

15✔
106
    return result;
107
}
108

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

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