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

mongodb-js / mongodb-mcp-server / 16219386259

11 Jul 2025 11:54AM UTC coverage: 75.27% (-0.06%) from 75.33%
16219386259

Pull #357

github

web-flow
Merge f3facc175 into c6ba47b57
Pull Request #357: chore: always disconnect the session after a test run

368 of 579 branches covered (63.56%)

Branch coverage included in aggregate %.

886 of 1087 relevant lines covered (81.51%)

60.66 hits per line

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

77.5
/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();
38✔
36

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

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

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

60
    async disconnect(): Promise<void> {
61
        if (this.serviceProvider) {
340✔
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) {
340✔
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) => {
82
                    const error = err instanceof Error ? err : new Error(String(err));
×
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");
340✔
92
    }
93

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

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