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

mongodb-js / mongodb-mcp-server / 16779406063

06 Aug 2025 02:11PM UTC coverage: 81.424% (-0.4%) from 81.781%
16779406063

Pull #420

github

web-flow
Merge cc01ac54e into a35d18dd6
Pull Request #420: chore: allow logging unredacted messages MCP-103

679 of 874 branches covered (77.69%)

Branch coverage included in aggregate %.

161 of 291 new or added lines in 16 files covered. (55.33%)

7 existing lines in 4 files now uncovered.

3485 of 4240 relevant lines covered (82.19%)

63.42 hits per line

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

84.55
/src/common/session.ts
1
import { ApiClient, ApiClientCredentials } from "./atlas/apiClient.js";
2✔
2
import { Implementation } from "@modelcontextprotocol/sdk/types.js";
3
import logger, { LogId } from "./logger.js";
2✔
4
import EventEmitter from "events";
2✔
5
import {
2✔
6
    AtlasClusterConnectionInfo,
7
    ConnectionManager,
8
    ConnectionSettings,
9
    ConnectionStateConnected,
10
} from "./connectionManager.js";
11
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
12
import { ErrorCodes, MongoDBError } from "./errors.js";
2✔
13

14
export interface SessionOptions {
15
    apiBaseUrl: string;
16
    apiClientId?: string;
17
    apiClientSecret?: string;
18
    connectionManager?: ConnectionManager;
19
}
20

21
export type SessionEvents = {
22
    connect: [];
23
    close: [];
24
    disconnect: [];
25
    "connection-error": [string];
26
};
27

28
export class Session extends EventEmitter<SessionEvents> {
2✔
29
    sessionId?: string;
30
    connectionManager: ConnectionManager;
31
    apiClient: ApiClient;
32
    agentRunner?: {
33
        name: string;
34
        version: string;
35
    };
36

37
    constructor({ apiBaseUrl, apiClientId, apiClientSecret, connectionManager }: SessionOptions) {
2✔
38
        super();
80✔
39

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

48
        this.apiClient = new ApiClient({ baseUrl: apiBaseUrl, credentials });
80✔
49

50
        this.connectionManager = connectionManager ?? new ConnectionManager();
80✔
51
        this.connectionManager.on("connection-succeeded", () => this.emit("connect"));
80✔
52
        this.connectionManager.on("connection-timed-out", (error) => this.emit("connection-error", error.errorReason));
80✔
53
        this.connectionManager.on("connection-closed", () => this.emit("disconnect"));
80✔
54
        this.connectionManager.on("connection-errored", (error) => this.emit("connection-error", error.errorReason));
80✔
55
    }
80✔
56

57
    setAgentRunner(agentRunner: Implementation | undefined) {
2✔
58
        if (agentRunner?.name && agentRunner?.version) {
68✔
59
            this.agentRunner = {
68✔
60
                name: agentRunner.name,
68✔
61
                version: agentRunner.version,
68✔
62
            };
68✔
63
        }
68✔
64
    }
68✔
65

66
    async disconnect(): Promise<void> {
2✔
67
        const atlasCluster = this.connectedAtlasCluster;
684✔
68

69
        try {
684✔
70
            await this.connectionManager.disconnect();
684✔
71
        } catch (err: unknown) {
684!
72
            const error = err instanceof Error ? err : new Error(String(err));
×
NEW
73
            logger.error({
×
NEW
74
                id: LogId.mongodbDisconnectFailure,
×
NEW
75
                context: "session",
×
NEW
76
                message: `Error closing service provider: ${error.message}`,
×
NEW
77
            });
×
UNCOV
78
        }
×
79

80
        if (atlasCluster?.username && atlasCluster?.projectId) {
684✔
81
            void this.apiClient
1✔
82
                .deleteDatabaseUser({
1✔
83
                    params: {
1✔
84
                        path: {
1✔
85
                            groupId: atlasCluster.projectId,
1✔
86
                            username: atlasCluster.username,
1✔
87
                            databaseName: "admin",
1✔
88
                        },
1✔
89
                    },
1✔
90
                })
1✔
91
                .catch((err: unknown) => {
1✔
92
                    const error = err instanceof Error ? err : new Error(String(err));
×
NEW
93
                    logger.error({
×
NEW
94
                        id: LogId.atlasDeleteDatabaseUserFailure,
×
NEW
95
                        context: "session",
×
NEW
96
                        message: `Error deleting previous database user: ${error.message}`,
×
NEW
97
                    });
×
98
                });
1✔
99
        }
1✔
100
    }
684✔
101

102
    async close(): Promise<void> {
2✔
103
        await this.disconnect();
68✔
104
        await this.apiClient.close();
68✔
105
        this.emit("close");
67✔
106
    }
68✔
107

108
    async connectToMongoDB(settings: ConnectionSettings): Promise<void> {
2✔
109
        try {
297✔
110
            await this.connectionManager.connect({ ...settings });
297✔
111
        } catch (error: unknown) {
297✔
112
            const message = error instanceof Error ? error.message : (error as string);
21!
113
            this.emit("connection-error", message);
21✔
114
            throw error;
21✔
115
        }
21✔
116
    }
297✔
117

118
    get isConnectedToMongoDB(): boolean {
2✔
119
        return this.connectionManager.currentConnectionState.tag === "connected";
1,738✔
120
    }
1,738✔
121

122
    get serviceProvider(): NodeDriverServiceProvider {
2✔
123
        if (this.isConnectedToMongoDB) {
258✔
124
            const state = this.connectionManager.currentConnectionState as ConnectionStateConnected;
258✔
125
            return state.serviceProvider;
258✔
126
        }
258!
127

128
        throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
×
129
    }
258✔
130

131
    get connectedAtlasCluster(): AtlasClusterConnectionInfo | undefined {
2✔
132
        return this.connectionManager.currentConnectionState.connectedAtlasCluster;
910✔
133
    }
910✔
134
}
2✔
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