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

mongodb-js / mongodb-mcp-server / 14727203334

29 Apr 2025 08:50AM UTC coverage: 82.713%. First build
14727203334

Pull #150

github

fmenezes
fix: address comments
Pull Request #150: chore: add bug issue template

142 of 235 branches covered (60.43%)

Branch coverage included in aggregate %.

791 of 893 relevant lines covered (88.58%)

48.07 hits per line

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

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