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

mongodb-js / mongodb-mcp-server / 14705884283

28 Apr 2025 10:45AM UTC coverage: 82.547% (+0.9%) from 81.696%
14705884283

Pull #131

github

fmenezes
fix: make ip changes smooth
Pull Request #131: feat: add atlas-connect-cluster tool

147 of 229 branches covered (64.19%)

Branch coverage included in aggregate %.

26 of 31 new or added lines in 4 files covered. (83.87%)

60 existing lines in 8 files now uncovered.

780 of 894 relevant lines covered (87.25%)

49.5 hits per line

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

70.59
/src/tools/atlas/metadata/connectCluster.ts
1
import { z } from "zod";
31✔
2
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3
import { AtlasToolBase } from "../atlasTool.js";
31✔
4
import { ToolArgs, OperationType } from "../../tool.js";
5
import { sleep } from "../../../common/utils.js";
31✔
6

7
function generateSecurePassword(): string {
8
    // TODO: use a better password generator
9
    return `pwdMcp${Math.floor(Math.random() * 100000)}`;
1✔
10
}
11

12
export class ConnectClusterTool extends AtlasToolBase {
31✔
13
    protected name = "atlas-connect-cluster";
29✔
14
    protected description = "Connect to MongoDB Atlas cluster";
29✔
15
    protected operationType: OperationType = "metadata";
29✔
16
    protected argsShape = {
29✔
17
        projectId: z.string().describe("Atlas project ID"),
18
        clusterName: z.string().describe("Atlas cluster name"),
19
    };
20

21
    protected async execute({ projectId, clusterName }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
22
        const cluster = await this.session.apiClient.getCluster({
1✔
23
            params: {
24
                path: {
25
                    groupId: projectId,
26
                    clusterName,
27
                },
28
            },
29
        });
30

31
        if (!cluster) {
1!
NEW
32
            throw new Error("Cluster not found");
×
33
        }
34

35
        if (!cluster.connectionStrings?.standardSrv || !cluster.connectionStrings?.standard) {
1!
NEW
36
            throw new Error("Connection string not available");
×
37
        }
38

39
        const username = `usrMcp${Math.floor(Math.random() * 100000)}`;
1✔
40
        const password = generateSecurePassword();
1✔
41

42
        const expiryMs = 1000 * 60 * 60 * 12; // 12 hours
1✔
43
        const expiryDate = new Date(Date.now() + expiryMs);
1✔
44

45
        await this.session.apiClient.createDatabaseUser({
1✔
46
            params: {
47
                path: {
48
                    groupId: projectId,
49
                },
50
            },
51
            body: {
52
                databaseName: "admin",
53
                groupId: projectId,
54
                roles: [
55
                    {
56
                        roleName: "readWriteAnyDatabase",
57
                        databaseName: "admin",
58
                    },
59
                ],
60
                scopes: [{ type: "CLUSTER", name: clusterName }],
61
                username,
62
                password,
63
                awsIAMType: "NONE",
64
                ldapAuthType: "NONE",
65
                oidcAuthType: "NONE",
66
                x509Type: "NONE",
67
                deleteAfterDate: expiryDate.toISOString(),
68
            },
69
        });
70

71
        void sleep(expiryMs).then(async () => {
1✔
72
            // disconnect after 12 hours
NEW
73
            if (this.session.serviceProvider) {
×
NEW
74
                await this.session.serviceProvider.close(true);
×
NEW
75
                this.session.serviceProvider = undefined;
×
76
            }
77
        });
78

79
        const connectionString =
80
            (cluster.connectionStrings.standardSrv || cluster.connectionStrings.standard || "").replace(
1!
81
                "://",
82
                `://${username}:${password}@`
83
            ) + `?authSource=admin`;
84

85
        await this.connectToMongoDB(connectionString);
1✔
86

87
        return {
1✔
88
            content: [
89
                {
90
                    type: "text",
91
                    text: `Connected to cluster "${clusterName}"`,
92
                },
93
            ],
94
        };
95
    }
96
}
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