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

mongodb-js / mongodb-mcp-server / 16650135474

31 Jul 2025 01:19PM UTC coverage: 81.208% (+0.2%) from 81.014%
16650135474

Pull #406

github

web-flow
Merge 72f1ab810 into 0083493a5
Pull Request #406: feat: update connectionString appName param - [MCP-68]

606 of 783 branches covered (77.39%)

Branch coverage included in aggregate %.

69 of 81 new or added lines in 8 files covered. (85.19%)

60 existing lines in 5 files now uncovered.

3305 of 4033 relevant lines covered (81.95%)

54.05 hits per line

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

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

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

16
export type SessionEvents = {
17
    connect: [];
18
    close: [];
19
    disconnect: [];
20
    "connection-error": [string];
21
};
22

23
export class Session extends EventEmitter<SessionEvents> {
2✔
24
    sessionId?: string;
25
    serviceProvider?: NodeDriverServiceProvider;
26
    apiClient: ApiClient;
27
    agentRunner?: {
28
        name: string;
29
        version: string;
30
    };
31
    connectedAtlasCluster?: {
32
        username: string;
33
        projectId: string;
34
        clusterName: string;
35
        expiryDate: Date;
36
    };
37

38
    constructor({ apiBaseUrl, apiClientId, apiClientSecret }: SessionOptions) {
2✔
39
        super();
80✔
40

41
        const credentials: ApiClientCredentials | undefined =
80✔
42
            apiClientId && apiClientSecret
80✔
43
                ? {
36✔
44
                      clientId: apiClientId,
36✔
45
                      clientSecret: apiClientSecret,
36✔
46
                  }
36✔
47
                : undefined;
44✔
48

49
        this.apiClient = new ApiClient({
80✔
50
            baseUrl: apiBaseUrl,
80✔
51
            credentials,
80✔
52
        });
80✔
53
    }
80✔
54

55
    setAgentRunner(agentRunner: Implementation | undefined) {
2✔
56
        if (agentRunner?.name && agentRunner?.version) {
66✔
57
            this.agentRunner = {
66✔
58
                name: agentRunner.name,
66✔
59
                version: agentRunner.version,
66✔
60
            };
66✔
61
        }
66✔
62
    }
66✔
63

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

98
    async close(): Promise<void> {
2✔
99
        await this.disconnect();
64✔
100
        await this.apiClient.close();
64✔
101
        this.emit("close");
63✔
102
    }
64✔
103

104
    async connectToMongoDB(connectionString: string, connectOptions: ConnectOptions): Promise<void> {
2✔
105
        // Use the extended appName format with deviceId and clientName
106
        connectionString = await setAppNameParamIfMissing({
269✔
107
            connectionString,
269✔
108
            components: {
269✔
109
                appName: `${packageInfo.mcpServerName} ${packageInfo.version}`,
269✔
110
                clientName: this.agentRunner?.name || "unknown",
269✔
111
            },
269✔
112
        });
269✔
113

114
        try {
269✔
115
            this.serviceProvider = await NodeDriverServiceProvider.connect(connectionString, {
269✔
116
                productDocsLink: "https://github.com/mongodb-js/mongodb-mcp-server/",
269✔
117
                productName: "MongoDB MCP",
269✔
118
                readConcern: {
269✔
119
                    level: connectOptions.readConcern,
269✔
120
                },
269✔
121
                readPreference: connectOptions.readPreference,
269✔
122
                writeConcern: {
269✔
123
                    w: connectOptions.writeConcern,
269✔
124
                },
269✔
125
                timeoutMS: connectOptions.timeoutMS,
269✔
126
                proxy: { useEnvironmentVariableProxies: true },
269✔
127
                applyProxyToOIDC: true,
269✔
128
            });
269✔
129

130
            await this.serviceProvider?.runCommand?.("admin", { hello: 1 });
265✔
131
        } catch (error: unknown) {
269✔
132
            const message = error instanceof Error ? error.message : `${error as string}`;
4!
133
            this.emit("connection-error", message);
4✔
134
            throw error;
4✔
135
        }
4✔
136

137
        this.emit("connect");
265✔
138
    }
269✔
139
}
2✔
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