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

mongodb-js / mongodb-mcp-server / 16790782045

06 Aug 2025 11:09PM UTC coverage: 81.233% (-0.07%) from 81.298%
16790782045

Pull #425

github

web-flow
Merge 7712da066 into 53ac631ea
Pull Request #425: fix: remove global logger MCP-103

681 of 881 branches covered (77.3%)

Branch coverage included in aggregate %.

158 of 207 new or added lines in 17 files covered. (76.33%)

6 existing lines in 2 files now uncovered.

3522 of 4293 relevant lines covered (82.04%)

58.12 hits per line

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

84.68
/src/common/session.ts
1
import { ApiClient, ApiClientCredentials } from "./atlas/apiClient.js";
2✔
2
import { Implementation } from "@modelcontextprotocol/sdk/types.js";
3
import { CompositeLogger, 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
    logger: CompositeLogger;
20
}
21

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

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

38
    public logger: CompositeLogger;
39

40
    constructor({ apiBaseUrl, apiClientId, apiClientSecret, connectionManager, logger }: SessionOptions) {
2✔
41
        super();
80✔
42

43
        this.logger = logger;
80✔
44

45
        const credentials: ApiClientCredentials | undefined =
80✔
46
            apiClientId && apiClientSecret
80✔
47
                ? {
38✔
48
                      clientId: apiClientId,
38✔
49
                      clientSecret: apiClientSecret,
38✔
50
                  }
38✔
51
                : undefined;
42✔
52

53
        this.apiClient = new ApiClient({ baseUrl: apiBaseUrl, credentials }, logger);
80✔
54

55
        this.connectionManager = connectionManager ?? new ConnectionManager();
80✔
56
        this.connectionManager.on("connection-succeeded", () => this.emit("connect"));
80✔
57
        this.connectionManager.on("connection-timed-out", (error) => this.emit("connection-error", error.errorReason));
80✔
58
        this.connectionManager.on("connection-closed", () => this.emit("disconnect"));
80✔
59
        this.connectionManager.on("connection-errored", (error) => this.emit("connection-error", error.errorReason));
80✔
60
    }
80✔
61

62
    setAgentRunner(agentRunner: Implementation | undefined) {
2✔
63
        if (agentRunner?.name && agentRunner?.version) {
68✔
64
            this.agentRunner = {
68✔
65
                name: agentRunner.name,
68✔
66
                version: agentRunner.version,
68✔
67
            };
68✔
68
        }
68✔
69
    }
68✔
70

71
    async disconnect(): Promise<void> {
2✔
72
        const atlasCluster = this.connectedAtlasCluster;
685✔
73

74
        try {
685✔
75
            await this.connectionManager.disconnect();
685✔
76
        } catch (err: unknown) {
685!
77
            const error = err instanceof Error ? err : new Error(String(err));
×
NEW
78
            this.logger.error({
×
79
                id: LogId.mongodbDisconnectFailure,
×
80
                context: "session",
×
81
                message: `Error closing service provider: ${error.message}`,
×
82
            });
×
83
        }
×
84

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

107
    async close(): Promise<void> {
2✔
108
        await this.disconnect();
68✔
109
        await this.apiClient.close();
68✔
110
        this.emit("close");
67✔
111
    }
68✔
112

113
    async connectToMongoDB(settings: ConnectionSettings): Promise<void> {
2✔
114
        try {
291✔
115
            await this.connectionManager.connect({ ...settings });
291✔
116
        } catch (error: unknown) {
291✔
117
            const message = error instanceof Error ? error.message : (error as string);
14!
118
            this.emit("connection-error", message);
14✔
119
            throw error;
14✔
120
        }
14✔
121
    }
291✔
122

123
    get isConnectedToMongoDB(): boolean {
2✔
124
        return this.connectionManager.currentConnectionState.tag === "connected";
1,740✔
125
    }
1,740✔
126

127
    get serviceProvider(): NodeDriverServiceProvider {
2✔
128
        if (this.isConnectedToMongoDB) {
258✔
129
            const state = this.connectionManager.currentConnectionState as ConnectionStateConnected;
258✔
130
            return state.serviceProvider;
258✔
131
        }
258!
132

133
        throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
×
134
    }
258✔
135

136
    get connectedAtlasCluster(): AtlasClusterConnectionInfo | undefined {
2✔
137
        return this.connectionManager.currentConnectionState.connectedAtlasCluster;
842✔
138
    }
842✔
139
}
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