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

mongodb-js / mongodb-mcp-server / 19462524052

18 Nov 2025 10:20AM UTC coverage: 80.328% (+0.1%) from 80.187%
19462524052

Pull #729

github

web-flow
Merge 8e32474a4 into 53143eecd
Pull Request #729: chore: refactor config initialisation for CLI to allow easy extension from other config sources as well MCP-288

1328 of 1750 branches covered (75.89%)

Branch coverage included in aggregate %.

332 of 346 new or added lines in 12 files covered. (95.95%)

15 existing lines in 2 files now uncovered.

6406 of 7878 relevant lines covered (81.32%)

72.35 hits per line

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

85.44
/src/transports/base.ts
1
import type { UserConfig } from "../common/config/userConfig.js";
2✔
2
import { packageInfo } from "../common/packageInfo.js";
1✔
3
import { Server } from "../server.js";
1✔
4
import { Session } from "../common/session.js";
1✔
5
import { Telemetry } from "../telemetry/telemetry.js";
1✔
6
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
1✔
7
import type { LoggerBase } from "../common/logger.js";
8
import { CompositeLogger, ConsoleLogger, DiskLogger, McpLogger } from "../common/logger.js";
1✔
9
import { ExportsManager } from "../common/exportsManager.js";
1✔
10
import { DeviceId } from "../helpers/deviceId.js";
1✔
11
import { Keychain } from "../common/keychain.js";
1✔
12
import { createMCPConnectionManager, type ConnectionManagerFactoryFn } from "../common/connectionManager.js";
1✔
13
import {
1✔
14
    type ConnectionErrorHandler,
15
    connectionErrorHandler as defaultConnectionErrorHandler,
16
} from "../common/connectionErrorHandler.js";
17
import type { CommonProperties } from "../telemetry/types.js";
18
import { Elicitation } from "../elicitation.js";
1✔
19
import type { AtlasLocalClientFactoryFn } from "../common/atlasLocal.js";
20
import { defaultCreateAtlasLocalClient } from "../common/atlasLocal.js";
1✔
21
import type { Client } from "@mongodb-js/atlas-local";
22
import { VectorSearchEmbeddingsManager } from "../common/search/vectorSearchEmbeddingsManager.js";
1✔
23
import type { ToolBase, ToolConstructorParams } from "../tools/tool.js";
24

25
export type TransportRunnerConfig = {
26
    userConfig: UserConfig;
27
    createConnectionManager?: ConnectionManagerFactoryFn;
28
    connectionErrorHandler?: ConnectionErrorHandler;
29
    createAtlasLocalClient?: AtlasLocalClientFactoryFn;
30
    additionalLoggers?: LoggerBase[];
31
    telemetryProperties?: Partial<CommonProperties>;
32
    tools?: (new (params: ToolConstructorParams) => ToolBase)[];
33
};
34

35
export abstract class TransportRunnerBase {
1✔
36
    public logger: LoggerBase;
37
    public deviceId: DeviceId;
38
    protected readonly userConfig: UserConfig;
39
    private readonly createConnectionManager: ConnectionManagerFactoryFn;
40
    private readonly connectionErrorHandler: ConnectionErrorHandler;
41
    private readonly atlasLocalClient: Promise<Client | undefined>;
42
    private readonly telemetryProperties: Partial<CommonProperties>;
43
    private readonly tools?: (new (params: ToolConstructorParams) => ToolBase)[];
44

45
    protected constructor({
1✔
46
        userConfig,
7✔
47
        createConnectionManager = createMCPConnectionManager,
7✔
48
        connectionErrorHandler = defaultConnectionErrorHandler,
7✔
49
        createAtlasLocalClient = defaultCreateAtlasLocalClient,
7✔
50
        additionalLoggers = [],
7✔
51
        telemetryProperties = {},
7✔
52
        tools,
7✔
53
    }: TransportRunnerConfig) {
7✔
54
        this.userConfig = userConfig;
7✔
55
        this.createConnectionManager = createConnectionManager;
7✔
56
        this.connectionErrorHandler = connectionErrorHandler;
7✔
57
        this.atlasLocalClient = createAtlasLocalClient();
7✔
58
        this.telemetryProperties = telemetryProperties;
7✔
59
        this.tools = tools;
7✔
60
        const loggers: LoggerBase[] = [...additionalLoggers];
7✔
61
        if (this.userConfig.loggers.includes("stderr")) {
7✔
62
            loggers.push(new ConsoleLogger(Keychain.root));
5✔
63
        }
5✔
64

65
        if (this.userConfig.loggers.includes("disk")) {
7!
UNCOV
66
            loggers.push(
×
UNCOV
67
                new DiskLogger(
×
UNCOV
68
                    this.userConfig.logPath,
×
UNCOV
69
                    (err) => {
×
70
                        // If the disk logger fails to initialize, we log the error to stderr and exit
71
                        console.error("Error initializing disk logger:", err);
×
72
                        process.exit(1);
×
73
                    },
×
UNCOV
74
                    Keychain.root
×
UNCOV
75
                )
×
UNCOV
76
            );
×
UNCOV
77
        }
×
78

79
        this.logger = new CompositeLogger(...loggers);
7✔
80
        this.deviceId = DeviceId.create(this.logger);
7✔
81
    }
7✔
82

83
    protected async setupServer(): Promise<Server> {
1✔
84
        const mcpServer = new McpServer({
7✔
85
            name: packageInfo.mcpServerName,
7✔
86
            version: packageInfo.version,
7✔
87
        });
7✔
88

89
        const logger = new CompositeLogger(this.logger);
7✔
90
        const exportsManager = ExportsManager.init(this.userConfig, logger);
7✔
91
        const connectionManager = await this.createConnectionManager({
7✔
92
            logger,
7✔
93
            userConfig: this.userConfig,
7✔
94
            deviceId: this.deviceId,
7✔
95
        });
7✔
96

97
        const session = new Session({
7✔
98
            userConfig: this.userConfig,
7✔
99
            atlasLocalClient: await this.atlasLocalClient,
7✔
100
            logger,
7✔
101
            exportsManager,
7✔
102
            connectionManager,
7✔
103
            keychain: Keychain.root,
7✔
104
            vectorSearchEmbeddingsManager: new VectorSearchEmbeddingsManager(this.userConfig, connectionManager),
7✔
105
        });
7✔
106

107
        const telemetry = Telemetry.create(session, this.userConfig, this.deviceId, {
7✔
108
            commonProperties: this.telemetryProperties,
7✔
109
        });
7✔
110

111
        const elicitation = new Elicitation({ server: mcpServer.server });
7✔
112

113
        const result = new Server({
7✔
114
            mcpServer,
7✔
115
            session,
7✔
116
            telemetry,
7✔
117
            userConfig: this.userConfig,
7✔
118
            connectionErrorHandler: this.connectionErrorHandler,
7✔
119
            elicitation,
7✔
120
            tools: this.tools,
7✔
121
        });
7✔
122

123
        // We need to create the MCP logger after the server is constructed
124
        // because it needs the server instance
125
        if (this.userConfig.loggers.includes("mcp")) {
7!
UNCOV
126
            logger.addLogger(new McpLogger(result, Keychain.root));
×
UNCOV
127
        }
×
128

129
        return result;
7✔
130
    }
7✔
131

132
    abstract start(): Promise<void>;
133

134
    abstract closeTransport(): Promise<void>;
135

136
    async close(): Promise<void> {
1✔
137
        try {
6✔
138
            await this.closeTransport();
6✔
139
        } finally {
6✔
140
            this.deviceId.close();
6✔
141
        }
6✔
142
    }
6✔
143
}
1✔
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