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

mongodb-js / mongodb-mcp-server / 15979396440

30 Jun 2025 05:18PM UTC coverage: 74.286% (-0.7%) from 74.965%
15979396440

push

github

web-flow
revert: rollback "chore: add is_container_env to telemetry MCP-2 (#330)

233 of 392 branches covered (59.44%)

Branch coverage included in aggregate %.

41 of 45 new or added lines in 3 files covered. (91.11%)

24 existing lines in 3 files now uncovered.

807 of 1008 relevant lines covered (80.06%)

59.5 hits per line

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

69.44
/src/session.ts
1
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
2
import { ApiClient, ApiClientCredentials } from "./common/atlas/apiClient.js";
3
import { Implementation } from "@modelcontextprotocol/sdk/types.js";
4
import logger, { LogId } from "./logger.js";
5
import EventEmitter from "events";
6
import { ConnectOptions } from "./config.js";
7
import { setAppNameParamIfMissing } from "./helpers/connectionOptions.js";
8
import { packageInfo } from "./helpers/packageInfo.js";
9

10
export interface SessionOptions {
11
    apiBaseUrl: string;
12
    apiClientId?: string;
13
    apiClientSecret?: string;
14
}
15

16
export class Session extends EventEmitter<{
17
    close: [];
18
    disconnect: [];
19
}> {
20
    sessionId?: string;
21
    serviceProvider?: NodeDriverServiceProvider;
22
    apiClient: ApiClient;
23
    agentRunner?: {
24
        name: string;
25
        version: string;
26
    };
27
    connectedAtlasCluster?: {
28
        username: string;
29
        projectId: string;
30
        clusterName: string;
31
        expiryDate: Date;
32
    };
33

34
    constructor({ apiBaseUrl, apiClientId, apiClientSecret }: SessionOptions) {
35
        super();
37✔
36

37
        const credentials: ApiClientCredentials | undefined =
38
            apiClientId && apiClientSecret
37✔
39
                ? {
40
                      clientId: apiClientId,
41
                      clientSecret: apiClientSecret,
42
                  }
43
                : undefined;
44

45
        this.apiClient = new ApiClient({
37✔
46
            baseUrl: apiBaseUrl,
47
            credentials,
48
        });
49
    }
50

51
    setAgentRunner(agentRunner: Implementation | undefined) {
52
        if (agentRunner?.name && agentRunner?.version) {
33✔
53
            this.agentRunner = {
33✔
54
                name: agentRunner.name,
55
                version: agentRunner.version,
56
            };
57
        }
58
    }
59

60
    async disconnect(): Promise<void> {
61
        if (this.serviceProvider) {
337✔
62
            try {
126✔
63
                await this.serviceProvider.close(true);
126✔
64
            } catch (err: unknown) {
65
                const error = err instanceof Error ? err : new Error(String(err));
×
66
                logger.error(LogId.mongodbDisconnectFailure, "Error closing service provider:", error.message);
×
67
            }
68
            this.serviceProvider = undefined;
126✔
69
        }
70
        if (!this.connectedAtlasCluster) {
337✔
71
            this.emit("disconnect");
337✔
72
            return;
337✔
73
        }
UNCOV
74
        void this.apiClient
×
75
            .deleteDatabaseUser({
76
                params: {
77
                    path: {
78
                        groupId: this.connectedAtlasCluster.projectId,
79
                        username: this.connectedAtlasCluster.username,
80
                        databaseName: "admin",
81
                    },
82
                },
83
            })
84
            .catch((err: unknown) => {
85
                const error = err instanceof Error ? err : new Error(String(err));
×
86
                logger.error(
×
87
                    LogId.atlasDeleteDatabaseUserFailure,
88
                    "atlas-connect-cluster",
89
                    `Error deleting previous database user: ${error.message}`
90
                );
91
            });
UNCOV
92
        this.connectedAtlasCluster = undefined;
×
93

UNCOV
94
        this.emit("disconnect");
×
95
    }
96

97
    async close(): Promise<void> {
98
        await this.disconnect();
336✔
99
        this.emit("close");
336✔
100
    }
101

102
    async connectToMongoDB(connectionString: string, connectOptions: ConnectOptions): Promise<void> {
103
        connectionString = setAppNameParamIfMissing({
131✔
104
            connectionString,
105
            defaultAppName: `${packageInfo.mcpServerName} ${packageInfo.version}`,
106
        });
107
        this.serviceProvider = await NodeDriverServiceProvider.connect(connectionString, {
131✔
108
            productDocsLink: "https://github.com/mongodb-js/mongodb-mcp-server/",
109
            productName: "MongoDB MCP",
110
            readConcern: {
111
                level: connectOptions.readConcern,
112
            },
113
            readPreference: connectOptions.readPreference,
114
            writeConcern: {
115
                w: connectOptions.writeConcern,
116
            },
117
            timeoutMS: connectOptions.timeoutMS,
118
        });
119
    }
120
}
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

© 2026 Coveralls, Inc