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

mongodb-js / mongodb-mcp-server / 16194572196

10 Jul 2025 12:03PM UTC coverage: 74.831% (-0.8%) from 75.613%
16194572196

Pull #343

github

web-flow
Merge bf41f0d84 into 5ac06d0fb
Pull Request #343: fix: turn atlas-connect-cluster async

356 of 564 branches covered (63.12%)

Branch coverage included in aggregate %.

33 of 51 new or added lines in 3 files covered. (64.71%)

2 existing lines in 1 file now uncovered.

863 of 1065 relevant lines covered (81.03%)

56.78 hits per line

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

76.92
/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 {
127✔
63
                await this.serviceProvider.close(true);
127✔
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;
127✔
69
        }
70
        if (this.connectedAtlasCluster?.username && this.connectedAtlasCluster?.projectId) {
337✔
71
            void this.apiClient
1✔
72
                .deleteDatabaseUser({
73
                    params: {
74
                        path: {
75
                            groupId: this.connectedAtlasCluster.projectId,
76
                            username: this.connectedAtlasCluster.username,
77
                            databaseName: "admin",
78
                        },
79
                    },
80
                })
81
                .catch((err: unknown) => {
NEW
82
                    const error = err instanceof Error ? err : new Error(String(err));
×
NEW
83
                    logger.error(
×
84
                        LogId.atlasDeleteDatabaseUserFailure,
85
                        "atlas-connect-cluster",
86
                        `Error deleting previous database user: ${error.message}`
87
                    );
88
                });
89
            this.connectedAtlasCluster = undefined;
1✔
90
        }
91
        this.emit("disconnect");
337✔
92
    }
93

94
    async close(): Promise<void> {
95
        await this.disconnect();
336✔
96
        this.emit("close");
336✔
97
    }
98

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