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

mongodb-js / mongodb-mcp-server / 19146805236

06 Nov 2025 07:05PM UTC coverage: 80.192% (+0.04%) from 80.151%
19146805236

Pull #716

github

web-flow
Merge 3b9e5951c into 454e81617
Pull Request #716: chore: cleanup telemetry

1357 of 1802 branches covered (75.31%)

Branch coverage included in aggregate %.

2 of 6 new or added lines in 4 files covered. (33.33%)

31 existing lines in 4 files now uncovered.

6477 of 7967 relevant lines covered (81.3%)

69.52 hits per line

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

83.93
/src/common/session.ts
1
import { ObjectId } from "bson";
3✔
2
import type { ApiClientCredentials } from "./atlas/apiClient.js";
3
import { ApiClient } from "./atlas/apiClient.js";
3✔
4
import type { Implementation } from "@modelcontextprotocol/sdk/types.js";
5
import type { CompositeLogger } from "./logger.js";
6
import { LogId } from "./logger.js";
3✔
7
import EventEmitter from "events";
3✔
8
import type {
9
    AtlasClusterConnectionInfo,
10
    ConnectionManager,
11
    ConnectionSettings,
12
    ConnectionStateConnected,
13
    ConnectionStateErrored,
14
} from "./connectionManager.js";
15
import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
16
import { ErrorCodes, MongoDBError } from "./errors.js";
3✔
17
import type { ExportsManager } from "./exportsManager.js";
18
import type { Client } from "@mongodb-js/atlas-local";
19
import type { Keychain } from "./keychain.js";
20
import type { VectorSearchEmbeddingsManager } from "./search/vectorSearchEmbeddingsManager.js";
21

22
export interface SessionOptions {
23
    apiBaseUrl: string;
24
    apiClientId?: string;
25
    apiClientSecret?: string;
26
    logger: CompositeLogger;
27
    exportsManager: ExportsManager;
28
    connectionManager: ConnectionManager;
29
    keychain: Keychain;
30
    atlasLocalClient?: Client;
31
    vectorSearchEmbeddingsManager: VectorSearchEmbeddingsManager;
32
}
33

34
export type SessionEvents = {
35
    connect: [];
36
    close: [];
37
    disconnect: [];
38
    "connection-error": [ConnectionStateErrored];
39
};
40

41
export class Session extends EventEmitter<SessionEvents> {
3✔
42
    readonly sessionId: string = new ObjectId().toString();
3✔
43
    readonly exportsManager: ExportsManager;
44
    readonly connectionManager: ConnectionManager;
45
    readonly apiClient: ApiClient;
46
    readonly atlasLocalClient?: Client;
47
    readonly keychain: Keychain;
48
    readonly vectorSearchEmbeddingsManager: VectorSearchEmbeddingsManager;
49

50
    mcpClient?: {
51
        name?: string;
52
        version?: string;
53
        title?: string;
54
    };
55

56
    public logger: CompositeLogger;
57

58
    constructor({
3✔
59
        apiBaseUrl,
113✔
60
        apiClientId,
113✔
61
        apiClientSecret,
113✔
62
        logger,
113✔
63
        connectionManager,
113✔
64
        exportsManager,
113✔
65
        keychain,
113✔
66
        atlasLocalClient,
113✔
67
        vectorSearchEmbeddingsManager,
113✔
68
    }: SessionOptions) {
113✔
69
        super();
113✔
70

71
        this.keychain = keychain;
113✔
72
        this.logger = logger;
113✔
73
        const credentials: ApiClientCredentials | undefined =
113✔
74
            apiClientId && apiClientSecret
113✔
75
                ? {
13✔
76
                      clientId: apiClientId,
13✔
77
                      clientSecret: apiClientSecret,
13✔
78
                  }
13!
79
                : undefined;
100✔
80

81
        this.apiClient = new ApiClient({ baseUrl: apiBaseUrl, credentials }, logger);
113✔
82
        this.atlasLocalClient = atlasLocalClient;
113✔
83
        this.exportsManager = exportsManager;
113✔
84
        this.connectionManager = connectionManager;
113✔
85
        this.vectorSearchEmbeddingsManager = vectorSearchEmbeddingsManager;
113✔
86
        this.connectionManager.events.on("connection-success", () => this.emit("connect"));
113✔
87
        this.connectionManager.events.on("connection-time-out", (error) => this.emit("connection-error", error));
113✔
88
        this.connectionManager.events.on("connection-close", () => this.emit("disconnect"));
113✔
89
        this.connectionManager.events.on("connection-error", (error) => this.emit("connection-error", error));
113✔
90
    }
113✔
91

92
    setMcpClient(mcpClient: Implementation | undefined): void {
3✔
93
        if (!mcpClient) {
101!
UNCOV
94
            this.connectionManager.setClientName("unknown");
×
95
            this.logger.debug({
×
96
                id: LogId.serverMcpClientSet,
×
97
                context: "session",
×
98
                message: "MCP client info not found",
×
99
            });
×
100
        }
×
101

102
        this.mcpClient = {
101✔
103
            name: mcpClient?.name || "unknown",
101!
104
            version: mcpClient?.version || "unknown",
101!
105
            title: mcpClient?.title || "unknown",
101✔
106
        };
101✔
107

108
        // Set the client name on the connection manager for appName generation
109
        this.connectionManager.setClientName(this.mcpClient.name || "unknown");
101!
110
    }
101✔
111

112
    async disconnect(): Promise<void> {
3✔
113
        const atlasCluster = this.connectedAtlasCluster;
696✔
114

115
        await this.connectionManager.close();
696✔
116

117
        if (atlasCluster?.username && atlasCluster?.projectId) {
696!
118
            void this.apiClient
2✔
119
                .deleteDatabaseUser({
2✔
120
                    params: {
2✔
121
                        path: {
2✔
122
                            groupId: atlasCluster.projectId,
2✔
123
                            username: atlasCluster.username,
2✔
124
                            databaseName: "admin",
2✔
125
                        },
2✔
126
                    },
2✔
127
                })
2✔
128
                .catch((err: unknown) => {
2✔
UNCOV
129
                    const error = err instanceof Error ? err : new Error(String(err));
×
130
                    this.logger.error({
×
131
                        id: LogId.atlasDeleteDatabaseUserFailure,
×
132
                        context: "session",
×
133
                        message: `Error deleting previous database user: ${error.message}`,
×
134
                    });
×
135
                });
2✔
136
        }
2✔
137
    }
696✔
138

139
    async close(): Promise<void> {
3✔
140
        await this.disconnect();
100✔
141
        await this.apiClient.close();
100✔
142
        await this.exportsManager.close();
100✔
143
        this.emit("close");
100✔
144
    }
100✔
145

146
    async connectToMongoDB(settings: ConnectionSettings): Promise<void> {
3✔
147
        await this.connectionManager.connect({ ...settings });
292✔
148
    }
292✔
149

150
    get isConnectedToMongoDB(): boolean {
3✔
151
        return this.connectionManager.currentConnectionState.tag === "connected";
1,680✔
152
    }
1,680✔
153

154
    async isSearchSupported(): Promise<boolean> {
3✔
155
        const state = this.connectionManager.currentConnectionState;
55✔
156
        if (state.tag === "connected") {
55✔
157
            return await state.isSearchSupported();
54✔
158
        }
54✔
159

160
        return false;
1✔
161
    }
55✔
162

163
    async assertSearchSupported(): Promise<void> {
3✔
164
        const isSearchSupported = await this.isSearchSupported();
15✔
165
        if (!isSearchSupported) {
15✔
166
            throw new MongoDBError(
3✔
167
                ErrorCodes.AtlasSearchNotSupported,
3✔
168
                "Atlas Search is not supported in the current cluster."
3✔
169
            );
3✔
170
        }
3✔
171
    }
15✔
172

173
    get serviceProvider(): NodeDriverServiceProvider {
3✔
174
        if (this.isConnectedToMongoDB) {
237✔
175
            const state = this.connectionManager.currentConnectionState as ConnectionStateConnected;
237✔
176
            return state.serviceProvider;
237✔
177
        }
237!
178

UNCOV
179
        throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
×
180
    }
237✔
181

182
    get connectedAtlasCluster(): AtlasClusterConnectionInfo | undefined {
3✔
183
        return this.connectionManager.currentConnectionState.connectedAtlasCluster;
829✔
184
    }
829✔
185
}
3✔
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