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

mongodb-js / mongodb-mcp-server / 14793258815

02 May 2025 10:12AM CUT coverage: 72.437% (-2.2%) from 74.619%
14793258815

push

github

web-flow
Merge branch 'main' into main

173 of 324 branches covered (53.4%)

Branch coverage included in aggregate %.

44 of 72 new or added lines in 2 files covered. (61.11%)

3 existing lines in 2 files now uncovered.

689 of 866 relevant lines covered (79.56%)

38.99 hits per line

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

46.43
/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 = {
31✔
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";
513✔
15

16
    protected async ensureConnected(): Promise<NodeDriverServiceProvider> {
17
        if (!this.session.serviceProvider && this.config.connectionString) {
88✔
18
            try {
84✔
19
                await this.connectToMongoDB(this.config.connectionString);
84✔
20
            } catch (error) {
21
                logger.error(
×
22
                    LogId.mongodbConnectFailure,
23
                    "mongodbTool",
24
                    `Failed to connect to MongoDB instance using the connection string from the config: ${error as string}`
25
                );
26
                throw new MongoDBError(ErrorCodes.MisconfiguredConnectionString, "Not connected to MongoDB.");
×
27
            }
28
        }
29

30
        if (!this.session.serviceProvider) {
88!
31
            throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
×
32
        }
33

34
        return this.session.serviceProvider;
88✔
35
    }
36

37
    protected handleError(
38
        error: unknown,
39
        args: ToolArgs<typeof this.argsShape>
40
    ): Promise<CallToolResult> | CallToolResult {
41
        if (error instanceof MongoDBError) {
1!
42
            switch (error.code) {
×
43
                case ErrorCodes.NotConnectedToMongoDB:
44
                    return {
×
45
                        content: [
46
                            {
47
                                type: "text",
48
                                text: "You need to connect to a MongoDB instance before you can access its data.",
49
                            },
50
                            {
51
                                type: "text",
52
                                text: "Please use the 'connect' or 'switch-connection' tool to connect to a MongoDB instance.",
53
                            },
54
                        ],
55
                        isError: true,
56
                    };
57
                case ErrorCodes.MisconfiguredConnectionString:
58
                    return {
×
59
                        content: [
60
                            {
61
                                type: "text",
62
                                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.",
63
                            },
64
                        ],
65
                        isError: true,
66
                    };
67
            }
68
        }
69

70
        return super.handleError(error, args);
1✔
71
    }
72

73
    protected connectToMongoDB(connectionString: string): Promise<void> {
74
        return this.session.connectToMongoDB(connectionString, this.config.connectOptions);
84✔
75
    }
76

77
    protected resolveTelemetryMetadata(
78
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
79
        args: ToolArgs<typeof this.argsShape>
80
    ): TelemetryToolMetadata {
81
        const metadata: TelemetryToolMetadata = {};
×
82

83
        // Add projectId to the metadata if running a MongoDB operation to an Atlas cluster
84
        if (this.session.connectedAtlasCluster?.projectId) {
×
85
            metadata.projectId = this.session.connectedAtlasCluster.projectId;
×
86
        }
87

88
        return metadata;
×
89
    }
90
}
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