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

mongodb-js / mongodb-mcp-server / 14733421831

29 Apr 2025 02:11PM UTC coverage: 68.999%. First build
14733421831

Pull #164

github

gagik
test
Pull Request #164: test: dependencies check

109 of 235 branches covered (46.38%)

Branch coverage included in aggregate %.

670 of 894 relevant lines covered (74.94%)

45.18 hits per line

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

23.4
/src/tools/atlas/metadata/connectCluster.ts
1
import { z } from "zod";
30✔
2
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3
import { AtlasToolBase } from "../atlasTool.js";
30✔
4
import { ToolArgs, OperationType } from "../../tool.js";
5
import { randomBytes } from "crypto";
30✔
6
import { promisify } from "util";
30✔
7

8
const EXPIRY_MS = 1000 * 60 * 60 * 12; // 12 hours
30✔
9

10
const randomBytesAsync = promisify(randomBytes);
30✔
11

12
async function generateSecurePassword(): Promise<string> {
13
    const buf = await randomBytesAsync(16);
×
14
    const pass = buf.toString("base64url");
×
15
    return pass;
×
16
}
17

18
export class ConnectClusterTool extends AtlasToolBase {
30✔
19
    protected name = "atlas-connect-cluster";
26✔
20
    protected description = "Connect to MongoDB Atlas cluster";
26✔
21
    protected operationType: OperationType = "metadata";
26✔
22
    protected argsShape = {
26✔
23
        projectId: z.string().describe("Atlas project ID"),
24
        clusterName: z.string().describe("Atlas cluster name"),
25
    };
26

27
    protected async execute({ projectId, clusterName }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
28
        await this.session.disconnect();
×
29

30
        const cluster = await this.session.apiClient.getCluster({
×
31
            params: {
32
                path: {
33
                    groupId: projectId,
34
                    clusterName,
35
                },
36
            },
37
        });
38

39
        if (!cluster) {
×
40
            throw new Error("Cluster not found");
×
41
        }
42

43
        const baseConnectionString = cluster.connectionStrings?.standardSrv || cluster.connectionStrings?.standard;
×
44

45
        if (!baseConnectionString) {
×
46
            throw new Error("Connection string not available");
×
47
        }
48

49
        const username = `mcpUser${Math.floor(Math.random() * 100000)}`;
×
50
        const password = await generateSecurePassword();
×
51

52
        const expiryDate = new Date(Date.now() + EXPIRY_MS);
×
53

54
        const readOnly =
55
            this.config.readOnly ||
×
56
            (this.config.disabledTools?.includes("create") &&
57
                this.config.disabledTools?.includes("update") &&
58
                this.config.disabledTools?.includes("delete") &&
59
                !this.config.disabledTools?.includes("read") &&
60
                !this.config.disabledTools?.includes("metadata"));
61

62
        const roleName = readOnly ? "readAnyDatabase" : "readWriteAnyDatabase";
×
63

64
        await this.session.apiClient.createDatabaseUser({
×
65
            params: {
66
                path: {
67
                    groupId: projectId,
68
                },
69
            },
70
            body: {
71
                databaseName: "admin",
72
                groupId: projectId,
73
                roles: [
74
                    {
75
                        roleName,
76
                        databaseName: "admin",
77
                    },
78
                ],
79
                scopes: [{ type: "CLUSTER", name: clusterName }],
80
                username,
81
                password,
82
                awsIAMType: "NONE",
83
                ldapAuthType: "NONE",
84
                oidcAuthType: "NONE",
85
                x509Type: "NONE",
86
                deleteAfterDate: expiryDate.toISOString(),
87
            },
88
        });
89

90
        this.session.connectedAtlasCluster = {
×
91
            username,
92
            projectId,
93
            clusterName,
94
            expiryDate,
95
        };
96

97
        const cn = new URL(baseConnectionString);
×
98
        cn.username = username;
×
99
        cn.password = password;
×
100
        cn.searchParams.set("authSource", "admin");
×
101
        const connectionString = cn.toString();
×
102

103
        await this.session.connectToMongoDB(connectionString, this.config.connectOptions);
×
104

105
        return {
×
106
            content: [
107
                {
108
                    type: "text",
109
                    text: `Connected to cluster "${clusterName}"`,
110
                },
111
            ],
112
        };
113
    }
114
}
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