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

mongodb-js / mongodb-mcp-server / 14993354207

13 May 2025 09:39AM UTC coverage: 74.808%. First build
14993354207

Pull #230

github

web-flow
Merge d2948f551 into d54c92107
Pull Request #230: feat: Alerts Listing

205 of 357 branches covered (57.42%)

Branch coverage included in aggregate %.

10 of 17 new or added lines in 2 files covered. (58.82%)

769 of 945 relevant lines covered (81.38%)

49.79 hits per line

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

67.86
/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 = {
33✔
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";
600✔
15

16
    protected async ensureConnected(): Promise<NodeDriverServiceProvider> {
17
        if (!this.session.serviceProvider && this.config.connectionString) {
127✔
18
            try {
19✔
19
                await this.connectToMongoDB(this.config.connectionString);
19✔
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) {
127✔
31
            throw new MongoDBError(ErrorCodes.NotConnectedToMongoDB, "Not connected to MongoDB");
19✔
32
        }
33

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

37
    protected handleError(
38
        error: unknown,
39
        args: ToolArgs<typeof this.argsShape>
40
    ): Promise<CallToolResult> | CallToolResult {
41
        if (error instanceof MongoDBError) {
23✔
42
            switch (error.code) {
19!
43
                case ErrorCodes.NotConnectedToMongoDB:
44
                    return {
19✔
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);
4✔
71
    }
72

73
    protected connectToMongoDB(connectionString: string): Promise<void> {
74
        return this.session.connectToMongoDB(connectionString, this.config.connectOptions);
110✔
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