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

mongodb-js / mongodb-mcp-server / 14670404051

25 Apr 2025 05:45PM UTC coverage: 81.886% (+0.2%) from 81.696%
14670404051

Pull #118

github

nirinchev
Merge branch 'main' into ni/conditional-connect
Pull Request #118: feat: update the connect tool based on connectivity status

129 of 210 branches covered (61.43%)

Branch coverage included in aggregate %.

59 of 67 new or added lines in 9 files covered. (88.06%)

10 existing lines in 1 file now uncovered.

748 of 861 relevant lines covered (86.88%)

47.74 hits per line

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

86.67
/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";
31✔
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";
560✔
15

16
    protected async ensureConnected(): Promise<NodeDriverServiceProvider> {
17
        if (!this.session.serviceProvider && this.config.connectionString) {
126✔
18
            try {
19✔
19
                await this.connectToMongoDB(this.config.connectionString);
19✔
20
            } catch (error) {
NEW
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
                );
NEW
26
                throw new MongoDBError(ErrorCodes.MisconfiguredConnectionString, "Not connected to MongoDB.");
×
27
            }
28
        }
29

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

34
        return this.session.serviceProvider;
107✔
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:
NEW
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 async connectToMongoDB(connectionString: string): Promise<void> {
74
        const provider = await NodeDriverServiceProvider.connect(connectionString, {
109✔
75
            productDocsLink: "https://docs.mongodb.com/todo-mcp",
76
            productName: "MongoDB MCP",
77
            readConcern: {
78
                level: this.config.connectOptions.readConcern,
79
            },
80
            readPreference: this.config.connectOptions.readPreference,
81
            writeConcern: {
82
                w: this.config.connectOptions.writeConcern,
83
            },
84
            timeoutMS: this.config.connectOptions.timeoutMS,
85
        });
86

87
        this.session.serviceProvider = provider;
107✔
88
    }
89
}
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