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

mongodb-js / mongodb-mcp-server / 14591543426

22 Apr 2025 09:33AM UTC coverage: 51.548% (+0.6%) from 50.98%
14591543426

Pull #84

github

nirinchev
CR comments
Pull Request #84: chore: add integration tests for create-index

27 of 148 branches covered (18.24%)

Branch coverage included in aggregate %.

5 of 6 new or added lines in 2 files covered. (83.33%)

2 existing lines in 1 file now uncovered.

406 of 692 relevant lines covered (58.67%)

60.08 hits per line

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

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

8
export class ConnectTool extends MongoDBToolBase {
7✔
9
    protected name = "connect";
70✔
10
    protected description = "Connect to a MongoDB instance";
70✔
11
    protected argsShape = {
70✔
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";
70✔
19

20
    protected async execute({
21
        connectionStringOrClusterName,
22
    }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
23
        connectionStringOrClusterName ??= config.connectionString;
50✔
24
        if (!connectionStringOrClusterName) {
50✔
25
            return {
1✔
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
                ],
34
            };
35
        }
36

37
        let connectionString: string;
38

39
        if (
49!
40
            connectionStringOrClusterName.startsWith("mongodb://") ||
49!
41
            connectionStringOrClusterName.startsWith("mongodb+srv://")
42
        ) {
43
            connectionString = connectionStringOrClusterName;
49✔
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.
NEW
48
            return {
×
49
                content: [
50
                    {
51
                        type: "text",
52
                        text: `Connecting via cluster name not supported yet. Please provide a connection string.`,
53
                    },
54
                ],
55
            };
56
        }
57

58
        try {
49✔
59
            await this.connectToMongoDB(connectionString);
49✔
60
            return {
46✔
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 (
3✔
67
                config.connectionString &&
7✔
68
                error instanceof DriverError &&
69
                config.connectionString !== connectionString
70
            ) {
71
                return {
1✔
72
                    content: [
73
                        {
74
                            type: "text",
75
                            text:
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
                    ],
80
                };
81
            }
82

83
            throw error;
2✔
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