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

mongodb-js / mongodb-mcp-server / 14614164106

23 Apr 2025 09:00AM UTC coverage: 76.675% (+23.5%) from 53.208%
14614164106

Pull #83

github

fmenezes
fix: styles
Pull Request #83: chore: add atlas tests

81 of 152 branches covered (53.29%)

Branch coverage included in aggregate %.

28 of 39 new or added lines in 6 files covered. (71.79%)

4 existing lines in 3 files now uncovered.

560 of 684 relevant lines covered (81.87%)

26.26 hits per line

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

94.12
/src/tools/mongodb/metadata/connect.ts
1
import { z } from "zod";
16✔
2
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3
import { MongoDBToolBase } from "../mongodbTool.js";
16✔
4
import { ToolArgs, OperationType } from "../../tool.js";
5
import config from "../../../config.js";
16✔
6
import { MongoError as DriverError } from "mongodb";
16✔
7

8
export class ConnectTool extends MongoDBToolBase {
16✔
9
    protected name = "connect";
16✔
10
    protected description = "Connect to a MongoDB instance";
16✔
11
    protected argsShape = {
16✔
12
        connectionStringOrClusterName: z
13
            .string()
14
            .optional()
15
            .describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format) or cluster name"),
16
    };
17

18
    protected operationType: OperationType = "metadata";
19

20
    protected async execute({
21
        connectionStringOrClusterName,
22
    }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
23
        connectionStringOrClusterName ??= config.connectionString;
24
        if (!connectionStringOrClusterName) {
25
            return {
26
                content: [
27
                    { type: "text", text: "No connection details provided." },
28
                    { type: "text", text: "Please provide either a connection string or a cluster name" },
29
                    {
30
                        type: "text",
31
                        text: "Alternatively, you can use the default deployment at mongodb://localhost:27017",
32
                    },
33
                ],
16✔
34
            };
35
        }
36

79✔
37
        let connectionString: string;
38

79✔
39
        if (
1✔
40
            connectionStringOrClusterName.startsWith("mongodb://") ||
41
            connectionStringOrClusterName.startsWith("mongodb+srv://")
42
        ) {
43
            connectionString = connectionStringOrClusterName;
44
        } else {
45
            // TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/19
46
            // We don't support connecting via cluster name since we'd need to obtain the user credentials
47
            // and fill in the connection string.
78✔
48
            return {
49
                content: [
1✔
50
                    {
77!
51
                        type: "text",
77✔
52
                        text: `Connecting via cluster name not supported yet. Please provide a connection string.`,
53
                    },
54
                ],
55
            };
UNCOV
56
        }
×
57

58
        try {
59
            await this.connectToMongoDB(connectionString);
60
            return {
61
                content: [{ type: "text", text: `Successfully connected to ${connectionString}.` }],
62
            };
63
        } catch (error) {
64
            // Sometimes the model will supply an incorrect connection string. If the user has configured
65
            // a different one as environment variable or a cli argument, suggest using that one instead.
66
            if (
78✔
67
                config.connectionString &&
78✔
68
                error instanceof DriverError &&
75✔
69
                config.connectionString !== connectionString
70
            ) {
71
                return {
72
                    content: [
73
                        {
74
                            type: "text",
3✔
75
                            text:
7✔
76
                                `Failed to connect to MongoDB at '${connectionString}' due to error: '${error.message}.` +
77
                                `Your config lists a different connection string: '${config.connectionString}' - do you want to try connecting to it instead?`,
78
                        },
79
                    ],
1✔
80
                };
81
            }
82

83
            throw error;
84
        }
85
    }
86
}
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