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

mongodb-js / mongodb-mcp-server / 14752728881

30 Apr 2025 10:44AM UTC coverage: 73.499%. First build
14752728881

Pull #178

github

fmenezes
fix: delete current user async so we do not spend too much time on disconnection
Pull Request #178: fix: bad auth

149 of 279 branches covered (53.41%)

Branch coverage included in aggregate %.

5 of 10 new or added lines in 2 files covered. (50.0%)

622 of 770 relevant lines covered (80.78%)

40.78 hits per line

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

77.78
/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

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

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

32
    constructor({ apiBaseUrl, apiClientId, apiClientSecret }: SessionOptions) {
33
        super();
27✔
34

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

43
        this.apiClient = new ApiClient({
27✔
44
            baseUrl: apiBaseUrl,
45
            credentials,
46
        });
47
    }
48

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

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

92
        this.emit("disconnect");
1✔
93
    }
94

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

100
    async connectToMongoDB(connectionString: string, connectOptions: ConnectOptions): Promise<void> {
101
        const provider = await NodeDriverServiceProvider.connect(connectionString, {
105✔
102
            productDocsLink: "https://docs.mongodb.com/todo-mcp",
103
            productName: "MongoDB MCP",
104
            readConcern: {
105
                level: connectOptions.readConcern,
106
            },
107
            readPreference: connectOptions.readPreference,
108
            writeConcern: {
109
                w: connectOptions.writeConcern,
110
            },
111
            timeoutMS: connectOptions.timeoutMS,
112
        });
113

114
        this.serviceProvider = provider;
105✔
115
    }
116
}
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