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

mongodb-js / mongodb-mcp-server / 16194572196

10 Jul 2025 12:03PM UTC coverage: 74.831% (-0.8%) from 75.613%
16194572196

Pull #343

github

web-flow
Merge bf41f0d84 into 5ac06d0fb
Pull Request #343: fix: turn atlas-connect-cluster async

356 of 564 branches covered (63.12%)

Branch coverage included in aggregate %.

33 of 51 new or added lines in 3 files covered. (64.71%)

2 existing lines in 1 file now uncovered.

863 of 1065 relevant lines covered (81.03%)

56.78 hits per line

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

69.23
/src/tools/mongodb/mongodbTool.ts
1
import { z } from "zod";
2
import { ToolArgs, ToolBase, ToolCategory, TelemetryToolMetadata } from "../tool.js";
3
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
4
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
5
import { ErrorCodes, MongoDBError } from "../../errors.js";
6
import logger, { LogId } from "../../logger.js";
7

8
export const DbOperationArgs = {
34✔
9
    database: z.string().describe("Database name"),
10
    collection: z.string().describe("Collection name"),
11
};
12

13
export abstract class MongoDBToolBase extends ToolBase {
14
    protected category: ToolCategory = "mongodb";
660✔
15

16
    protected async ensureConnected(): Promise<NodeDriverServiceProvider> {
17
        if (!this.session.serviceProvider) {
144✔
18
            if (this.session.connectedAtlasCluster) {
38!
NEW
19
                throw new MongoDBError(
×
20
                    ErrorCodes.NotConnectedToMongoDB,
21
                    `Attempting to connect to Atlas cluster "${this.session.connectedAtlasCluster.clusterName}", try again in a few seconds.`
22
                );
23
            }
24

25
            if (this.config.connectionString) {
38✔
26
                try {
19✔
27
                    await this.connectToMongoDB(this.config.connectionString);
19✔
28
                } catch (error) {
NEW
29
                    logger.error(
×
30
                        LogId.mongodbConnectFailure,
31
                        "mongodbTool",
32
                        `Failed to connect to MongoDB instance using the connection string from the config: ${error as string}`
33
                    );
NEW
34
                    throw new MongoDBError(ErrorCodes.MisconfiguredConnectionString, "Not connected to MongoDB.");
×
35
                }
36
            }
37
        }
38

39
        if (!this.session.serviceProvider) {
144✔
40
            throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
19✔
41
        }
42

43
        return this.session.serviceProvider;
125✔
44
    }
45

46
    protected handleError(
47
        error: unknown,
48
        args: ToolArgs<typeof this.argsShape>
49
    ): Promise<CallToolResult> | CallToolResult {
50
        if (error instanceof MongoDBError) {
28✔
51
            switch (error.code) {
24!
52
                case ErrorCodes.NotConnectedToMongoDB:
53
                    return {
19✔
54
                        content: [
55
                            {
56
                                type: "text",
57
                                text: "You need to connect to a MongoDB instance before you can access its data.",
58
                            },
59
                            {
60
                                type: "text",
61
                                text: "Please use the 'connect' or 'switch-connection' tool to connect to a MongoDB instance.",
62
                            },
63
                        ],
64
                        isError: true,
65
                    };
66
                case ErrorCodes.MisconfiguredConnectionString:
67
                    return {
×
68
                        content: [
69
                            {
70
                                type: "text",
71
                                text: "The configured connection string is not valid. Please check the connection string and confirm it points to a valid MongoDB instance. Alternatively, use the 'switch-connection' tool to connect to a different instance.",
72
                            },
73
                        ],
74
                        isError: true,
75
                    };
76
                case ErrorCodes.ForbiddenCollscan:
77
                    return {
5✔
78
                        content: [
79
                            {
80
                                type: "text",
81
                                text: error.message,
82
                            },
83
                        ],
84
                        isError: true,
85
                    };
86
            }
87
        }
88

89
        return super.handleError(error, args);
4✔
90
    }
91

92
    protected connectToMongoDB(connectionString: string): Promise<void> {
93
        return this.session.connectToMongoDB(connectionString, this.config.connectOptions);
127✔
94
    }
95

96
    protected resolveTelemetryMetadata(
97
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
98
        args: ToolArgs<typeof this.argsShape>
99
    ): TelemetryToolMetadata {
100
        const metadata: TelemetryToolMetadata = {};
×
101

102
        // Add projectId to the metadata if running a MongoDB operation to an Atlas cluster
103
        if (this.session.connectedAtlasCluster?.projectId) {
×
104
            metadata.projectId = this.session.connectedAtlasCluster.projectId;
×
105
        }
106

107
        return metadata;
×
108
    }
109
}
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