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

mongodb-js / mongodb-mcp-server / 20991879511

14 Jan 2026 11:06AM UTC coverage: 80.222% (-0.3%) from 80.547%
20991879511

push

github

web-flow
chore: release v1.4.1-prerelease.5 (#861)

Co-authored-by: mongodb-devtools-bot[bot] <189715634+mongodb-devtools-bot[bot]@users.noreply.github.com>

1538 of 2006 branches covered (76.67%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

34 existing lines in 3 files now uncovered.

7142 of 8814 relevant lines covered (81.03%)

86.75 hits per line

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

84.38
/src/common/session.ts
1
import { ObjectId } from "bson";
3✔
2
import type { ApiClient } from "./atlas/apiClient.js";
3
import { createAtlasApiClient } 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
    ConnectionStringAuthType,
15
} from "./connectionManager.js";
16
import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
17
import { ErrorCodes, MongoDBError } from "./errors.js";
3✔
18
import type { ExportsManager } from "./exportsManager.js";
19
import type { Client } from "@mongodb-js/atlas-local";
20
import type { Keychain } from "./keychain.js";
21
import type { VectorSearchEmbeddingsManager } from "./search/vectorSearchEmbeddingsManager.js";
22
import { generateConnectionInfoFromCliArgs } from "@mongosh/arg-parser";
3✔
23
import { type UserConfig } from "../common/config/userConfig.js";
24

25
export interface SessionOptions {
26
    userConfig: UserConfig;
27
    logger: CompositeLogger;
28
    exportsManager: ExportsManager;
29
    connectionManager: ConnectionManager;
30
    keychain: Keychain;
31
    atlasLocalClient?: Client;
32
    vectorSearchEmbeddingsManager: VectorSearchEmbeddingsManager;
33
    apiClient?: ApiClient;
34
}
35

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

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

53
    mcpClient?: {
54
        name?: string;
55
        version?: string;
56
        title?: string;
57
    };
58

59
    public logger: CompositeLogger;
60

61
    constructor({
3✔
62
        userConfig,
145✔
63
        logger,
145✔
64
        connectionManager,
145✔
65
        exportsManager,
145✔
66
        keychain,
145✔
67
        atlasLocalClient,
145✔
68
        vectorSearchEmbeddingsManager,
145✔
69
        apiClient,
145✔
70
    }: SessionOptions) {
145✔
71
        super();
145✔
72

73
        this.userConfig = userConfig;
145✔
74
        this.keychain = keychain;
145✔
75
        this.logger = logger;
145✔
76
        this.apiClient = apiClient;
145✔
77

78
        // Create default API client if not provided in the constructor and Atlas tools are enabled (credentials are provided)
79
        if (!this.apiClient && userConfig.apiClientId && userConfig.apiClientSecret) {
145✔
80
            this.apiClient = createAtlasApiClient(
13✔
81
                {
13✔
82
                    baseUrl: userConfig.apiBaseUrl,
13✔
83
                    credentials: {
13✔
84
                        clientId: userConfig.apiClientId,
13✔
85
                        clientSecret: userConfig.apiClientSecret,
13✔
86
                    },
13✔
87
                },
13✔
88
                logger
13✔
89
            );
13✔
90
        }
13✔
91

92
        this.atlasLocalClient = atlasLocalClient;
145✔
93
        this.exportsManager = exportsManager;
145✔
94
        this.connectionManager = connectionManager;
145✔
95
        this.vectorSearchEmbeddingsManager = vectorSearchEmbeddingsManager;
145✔
96
        this.connectionManager.events.on("connection-success", () => this.emit("connect"));
145✔
97
        this.connectionManager.events.on("connection-time-out", (error) => this.emit("connection-error", error));
145✔
98
        this.connectionManager.events.on("connection-close", () => this.emit("disconnect"));
145✔
99
        this.connectionManager.events.on("connection-error", (error) => this.emit("connection-error", error));
145✔
100
    }
145✔
101

102
    setMcpClient(mcpClient: Implementation | undefined): void {
3✔
103
        if (!mcpClient) {
123!
104
            this.connectionManager.setClientName("unknown");
×
105
            this.logger.debug({
×
106
                id: LogId.serverMcpClientSet,
×
107
                context: "session",
×
108
                message: "MCP client info not found",
×
109
            });
×
110
        }
×
111

112
        this.mcpClient = {
123✔
113
            name: mcpClient?.name || "unknown",
123!
114
            version: mcpClient?.version || "unknown",
123!
115
            title: mcpClient?.title || "unknown",
123✔
116
        };
123✔
117

118
        // Set the client name on the connection manager for appName generation
119
        this.connectionManager.setClientName(this.mcpClient.name || "unknown");
123!
120
    }
123✔
121

122
    async disconnect(): Promise<void> {
3✔
123
        const atlasCluster = this.connectedAtlasCluster;
760✔
124

125
        await this.connectionManager.close();
760✔
126

127
        if (atlasCluster?.username && atlasCluster?.projectId && this.apiClient) {
760!
128
            void this.apiClient
3✔
129
                .deleteDatabaseUser({
3✔
130
                    params: {
3✔
131
                        path: {
3✔
132
                            groupId: atlasCluster.projectId,
3✔
133
                            username: atlasCluster.username,
3✔
134
                            databaseName: "admin",
3✔
135
                        },
3✔
136
                    },
3✔
137
                })
3✔
138
                .catch((err: unknown) => {
3✔
UNCOV
139
                    const error = err instanceof Error ? err : new Error(String(err));
×
UNCOV
140
                    this.logger.error({
×
UNCOV
141
                        id: LogId.atlasDeleteDatabaseUserFailure,
×
UNCOV
142
                        context: "session",
×
UNCOV
143
                        message: `Error deleting previous database user: ${error.message}`,
×
UNCOV
144
                    });
×
145
                });
3✔
146
        }
3✔
147
    }
760✔
148

149
    async close(): Promise<void> {
3✔
150
        await this.disconnect();
123✔
151
        await this.apiClient?.close();
123✔
152
        await this.exportsManager.close();
123✔
153
        this.emit("close");
123✔
154
    }
123✔
155

156
    async connectToConfiguredConnection(): Promise<void> {
3✔
157
        const connectionInfo = generateConnectionInfoFromCliArgs({
35✔
158
            ...this.userConfig,
35✔
159
            connectionSpecifier: this.userConfig.connectionString,
35✔
160
        });
35✔
161
        await this.connectToMongoDB(connectionInfo);
35✔
162
    }
35✔
163

164
    async connectToMongoDB(settings: ConnectionSettings): Promise<void> {
3✔
165
        await this.connectionManager.connect({ ...settings });
332✔
166
    }
332✔
167

168
    get isConnectedToMongoDB(): boolean {
3✔
169
        return this.connectionManager.currentConnectionState.tag === "connected";
1,089✔
170
    }
1,089✔
171

172
    async isSearchSupported(): Promise<boolean> {
3✔
173
        const state = this.connectionManager.currentConnectionState;
76✔
174
        if (state.tag === "connected") {
76✔
175
            return await state.isSearchSupported();
75✔
176
        }
75✔
177

178
        return false;
1✔
179
    }
76✔
180

181
    async assertSearchSupported(): Promise<void> {
3✔
182
        const isSearchSupported = await this.isSearchSupported();
23✔
183
        if (!isSearchSupported) {
23✔
184
            throw new MongoDBError(
3✔
185
                ErrorCodes.AtlasSearchNotSupported,
3✔
186
                "Atlas Search is not supported in the current cluster."
3✔
187
            );
3✔
188
        }
3✔
189
    }
23✔
190

191
    get serviceProvider(): NodeDriverServiceProvider {
3✔
192
        if (this.isConnectedToMongoDB) {
258✔
193
            const state = this.connectionManager.currentConnectionState as ConnectionStateConnected;
258✔
194
            return state.serviceProvider;
258✔
195
        }
258!
196

197
        throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
×
198
    }
258✔
199

200
    get connectedAtlasCluster(): AtlasClusterConnectionInfo | undefined {
3✔
201
        return this.connectionManager.currentConnectionState.connectedAtlasCluster;
1,080✔
202
    }
1,080✔
203

204
    get connectionStringAuthType(): ConnectionStringAuthType | undefined {
3✔
205
        return this.connectionManager.currentConnectionState.connectionStringAuthType;
24✔
206
    }
24✔
207
}
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