• 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

78.99
/src/tools/mongodb/mongodbTool.ts
1
import { z } from "zod";
3✔
2
import type { ToolArgs, ToolCategory } from "../tool.js";
3
import { ToolBase } from "../tool.js";
3✔
4
import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
5
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
6
import { ErrorCodes, MongoDBError } from "../../common/errors.js";
3✔
7
import { LogId } from "../../common/logger.js";
3✔
8
import type { Server } from "../../server.js";
9
import type { AtlasMetadata } from "../../telemetry/types.js";
10

11
export const DbOperationArgs = {
3✔
12
    database: z.string().describe("Database name"),
3✔
13
    collection: z.string().describe("Collection name"),
3✔
14
};
3✔
15

16
export abstract class MongoDBToolBase extends ToolBase {
3✔
17
    protected server?: Server;
18
    public category: ToolCategory = "mongodb";
2,194✔
19

20
    protected async ensureConnected(): Promise<NodeDriverServiceProvider> {
3✔
21
        if (!this.session.isConnectedToMongoDB) {
265✔
22
            if (this.session.connectedAtlasCluster) {
58!
UNCOV
23
                throw new MongoDBError(
×
24
                    ErrorCodes.NotConnectedToMongoDB,
×
25
                    `Attempting to connect to Atlas cluster "${this.session.connectedAtlasCluster.clusterName}", try again in a few seconds.`
×
26
                );
×
27
            }
×
28

29
            if (this.config.connectionString) {
58!
30
                try {
24✔
31
                    await this.connectToMongoDB(this.config.connectionString);
24✔
32
                } catch (error) {
22✔
33
                    this.session.logger.error({
2✔
34
                        id: LogId.mongodbConnectFailure,
2✔
35
                        context: "mongodbTool",
2✔
36
                        message: `Failed to connect to MongoDB instance using the connection string from the config: ${error as string}`,
2✔
37
                    });
2✔
38
                    throw new MongoDBError(ErrorCodes.MisconfiguredConnectionString, "Not connected to MongoDB.");
2✔
39
                }
2✔
40
            }
24✔
41
        }
58✔
42

43
        if (!this.session.isConnectedToMongoDB) {
265!
44
            throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
34✔
45
        }
34!
46

47
        return this.session.serviceProvider;
229✔
48
    }
265✔
49

50
    protected ensureSearchIsSupported(): Promise<void> {
3✔
51
        return this.session.assertSearchSupported();
13✔
52
    }
13✔
53

54
    public register(server: Server): boolean {
3✔
55
        this.server = server;
2,194✔
56
        return super.register(server);
2,194✔
57
    }
2,194✔
58

59
    protected handleError(
3✔
60
        error: unknown,
68✔
61
        args: ToolArgs<typeof this.argsShape>
68✔
62
    ): Promise<CallToolResult> | CallToolResult {
68✔
63
        if (error instanceof MongoDBError) {
68✔
64
            switch (error.code) {
56✔
65
                case ErrorCodes.NotConnectedToMongoDB:
56✔
66
                case ErrorCodes.MisconfiguredConnectionString: {
56✔
67
                    const connectionError = error as MongoDBError<
38✔
68
                        ErrorCodes.NotConnectedToMongoDB | ErrorCodes.MisconfiguredConnectionString
69
                    >;
70
                    const outcome = this.server?.connectionErrorHandler(connectionError, {
38✔
71
                        availableTools: this.server?.tools ?? [],
38!
72
                        connectionState: this.session.connectionManager.currentConnectionState,
38✔
73
                    });
38✔
74
                    if (outcome?.errorHandled) {
38✔
75
                        return outcome.result;
38✔
76
                    }
38!
77

UNCOV
78
                    return super.handleError(error, args);
×
79
                }
×
80
                case ErrorCodes.ForbiddenCollscan:
56!
81
                    return {
7✔
82
                        content: [
7✔
83
                            {
7✔
84
                                type: "text",
7✔
85
                                text: error.message,
7✔
86
                            },
7✔
87
                        ],
7✔
88
                        isError: true,
7✔
89
                    };
7✔
90
                case ErrorCodes.AtlasSearchNotSupported: {
56!
91
                    const CTA = this.server?.isToolCategoryAvailable("atlas-local" as unknown as ToolCategory)
2✔
92
                        ? "`atlas-local` tools"
2!
UNCOV
93
                        : "Atlas CLI";
×
94
                    return {
2✔
95
                        content: [
2✔
96
                            {
2✔
97
                                text: `The connected MongoDB deployment does not support vector search indexes. Either connect to a MongoDB Atlas cluster or use the ${CTA} to create and manage a local Atlas deployment.`,
2✔
98
                                type: "text",
2✔
99
                            },
2✔
100
                        ],
2✔
101
                        isError: true,
2✔
102
                    };
2✔
103
                }
2✔
104
            }
56✔
105
        }
56!
106

107
        return super.handleError(error, args);
21✔
108
    }
68✔
109

110
    protected connectToMongoDB(connectionString: string): Promise<void> {
3✔
111
        return this.session.connectToMongoDB({ connectionString });
255✔
112
    }
255✔
113

114
    protected resolveTelemetryMetadata(
3✔
115
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
116
        args: ToolArgs<typeof this.argsShape>
×
NEW
117
    ): AtlasMetadata {
×
NEW
118
        const metadata: AtlasMetadata = {};
×
119

120
        // Add projectId to the metadata if running a MongoDB operation to an Atlas cluster
121
        if (this.session.connectedAtlasCluster?.projectId) {
×
122
            metadata.project_id = this.session.connectedAtlasCluster.projectId;
×
123
        }
×
124

UNCOV
125
        return metadata;
×
126
    }
×
127
}
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