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

mongodb-js / mongodb-mcp-server / 14590701503

22 Apr 2025 08:50AM UTC coverage: 50.118% (-0.9%) from 50.98%
14590701503

Pull #82

github

blva
lint
Pull Request #82: add create project tool

23 of 150 branches covered (15.33%)

Branch coverage included in aggregate %.

8 of 31 new or added lines in 3 files covered. (25.81%)

4 existing lines in 1 file now uncovered.

401 of 696 relevant lines covered (57.61%)

36.62 hits per line

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

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

5
import packageJson from "../package.json" with { type: "json" };
6✔
6
import { ReadConcernLevel, ReadPreferenceMode, W } from "mongodb";
7

8
// If we decide to support non-string config options, we'll need to extend the mechanism for parsing
9
// env variables.
10
interface UserConfig {
11
    apiBaseUrl?: string;
12
    apiClientId?: string;
13
    apiClientSecret?: string;
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 = {
6✔
26
    logPath: getLogPath(),
27
    connectOptions: {
28
        readConcern: "local",
29
        readPreference: "secondaryPreferred",
30
        writeConcern: "majority",
31
        timeoutMS: 30_000,
32
    },
33
    disabledTools: [],
34
};
35

36
const mergedUserConfig = {
6✔
37
    ...defaults,
38
    ...getEnvConfig(),
39
    ...getCliConfig(),
40
};
41

42
const config = {
6✔
43
    ...mergedUserConfig,
44
    version: packageJson.version,
45
};
46

47
export default config;
6✔
48

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

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

57
    return logPath;
6✔
58
}
59

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

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

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

88
            obj[currentField] = value;
×
89
            return;
×
90
        }
91

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

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

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

109
        setValue(result, fieldPath, value);
×
110
    }
111

112
    return result;
6✔
113
}
114

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

119
// Reads the cli args and parses them into a UserConfig object.
120
function getCliConfig() {
121
    return argv(process.argv.slice(2), {
6✔
122
        array: ["disabledTools"],
123
    }) as unknown as Partial<UserConfig>;
124
}
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