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

mongodb-js / mongodb-mcp-server / 19532221561

20 Nov 2025 09:34AM UTC coverage: 80.12% (-0.2%) from 80.322%
19532221561

Pull #740

github

web-flow
Merge a49fad576 into 40cb62e9f
Pull Request #740: chore: add createSessionConfig hook MCP-294

1338 of 1762 branches covered (75.94%)

Branch coverage included in aggregate %.

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

34 existing lines in 3 files now uncovered.

6388 of 7881 relevant lines covered (81.06%)

73.18 hits per line

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

85.08
/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
    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
}
34

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

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

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

58
    public logger: CompositeLogger;
59

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

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

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

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

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

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

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

116
        await this.connectionManager.close();
717✔
117

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

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

147
    async connectToConfiguredConnection(): Promise<void> {
3✔
148
        const connectionInfo = generateConnectionInfoFromCliArgs({
33✔
149
            ...this.userConfig,
33✔
150
            connectionSpecifier: this.userConfig.connectionString,
33✔
151
        });
33✔
152
        await this.connectToMongoDB(connectionInfo);
33✔
153
    }
33✔
154

155
    async connectToMongoDB(settings: ConnectionSettings): Promise<void> {
3✔
156
        await this.connectionManager.connect({ ...settings });
336✔
157
    }
336✔
158

159
    get isConnectedToMongoDB(): boolean {
3✔
160
        return this.connectionManager.currentConnectionState.tag === "connected";
1,714✔
161
    }
1,714✔
162

163
    async isSearchSupported(): Promise<boolean> {
3✔
164
        const state = this.connectionManager.currentConnectionState;
55✔
165
        if (state.tag === "connected") {
55✔
166
            return await state.isSearchSupported();
54✔
167
        }
54✔
168

169
        return false;
1✔
170
    }
55✔
171

172
    async assertSearchSupported(): Promise<void> {
3✔
173
        const isSearchSupported = await this.isSearchSupported();
15✔
174
        if (!isSearchSupported) {
15✔
175
            throw new MongoDBError(
3✔
176
                ErrorCodes.AtlasSearchNotSupported,
3✔
177
                "Atlas Search is not supported in the current cluster."
3✔
178
            );
3✔
179
        }
3✔
180
    }
15✔
181

182
    get serviceProvider(): NodeDriverServiceProvider {
3✔
183
        if (this.isConnectedToMongoDB) {
238✔
184
            const state = this.connectionManager.currentConnectionState as ConnectionStateConnected;
238✔
185
            return state.serviceProvider;
238✔
186
        }
238!
187

188
        throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
×
189
    }
238✔
190

191
    get connectedAtlasCluster(): AtlasClusterConnectionInfo | undefined {
3✔
192
        return this.connectionManager.currentConnectionState.connectedAtlasCluster;
1,148✔
193
    }
1,148✔
194

195
    get connectionStringAuthType(): ConnectionStringAuthType | undefined {
3✔
196
        return this.connectionManager.currentConnectionState.connectionStringAuthType;
2✔
197
    }
2✔
198
}
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

© 2025 Coveralls, Inc