• 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

51.06
/src/tools/atlas/listClusters.ts
1
import { z } from "zod";
16✔
2
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3
import { AtlasToolBase } from "./atlasTool.js";
16✔
4
import { ToolArgs, OperationType } from "../tool.js";
5
import { PaginatedClusterDescription20240805, PaginatedOrgGroupView, Group } from "../../common/atlas/openapi.js";
6

7
export class ListClustersTool extends AtlasToolBase {
16✔
8
    protected name = "atlas-list-clusters";
16✔
9
    protected description = "List MongoDB Atlas clusters";
16✔
10
    protected operationType: OperationType = "read";
16✔
11
    protected argsShape = {
16✔
12
        projectId: z.string().describe("Atlas project ID to filter clusters").optional(),
13
    };
14

15
    protected async execute({ projectId }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
16
        this.session.ensureAuthenticated();
1✔
17

18
        if (!projectId) {
1!
19
            const data = await this.session.apiClient.listClustersForAllProjects();
×
20

21
            return this.formatAllClustersTable(data);
×
22
        } else {
23
            const project = await this.session.apiClient.getProject({
1✔
24
                params: {
25
                    path: {
26
                        groupId: projectId,
27
                    },
28
                },
29
            });
30

31
            if (!project?.id) {
1!
32
                throw new Error(`Project with ID "${projectId}" not found.`);
×
33
            }
34

35
            const data = await this.session.apiClient.listClusters({
1✔
36
                params: {
37
                    path: {
38
                        groupId: project.id || "",
1!
39
                    },
40
                },
41
            });
42

43
            return this.formatClustersTable(project, data);
1✔
44
        }
45
    }
46

47
    private formatAllClustersTable(clusters?: PaginatedOrgGroupView): CallToolResult {
48
        if (!clusters?.results?.length) {
×
49
            throw new Error("No clusters found.");
×
50
        }
NEW
51
        const formattedClusters = clusters.results
×
52
            .map((result) => {
53
                return (result.clusters || []).map((cluster) => {
×
54
                    return { ...result, ...cluster, clusters: undefined };
×
55
                });
56
            })
57
            .flat();
NEW
58
        if (!formattedClusters.length) {
×
NEW
59
            throw new Error("No clusters found.");
×
60
        }
NEW
61
        const rows = formattedClusters
×
62
            .map((cluster) => {
63
                return `${cluster.groupName} (${cluster.groupId}) | ${cluster.name}`;
×
64
            })
65
            .join("\n");
66
        return {
×
67
            content: [
68
                {
69
                    type: "text",
70
                    text: `Project | Cluster Name
71
----------------|----------------
72
${rows}`,
73
                },
74
            ],
75
        };
76
    }
77

78
    private formatClustersTable(project: Group, clusters?: PaginatedClusterDescription20240805): CallToolResult {
79
        if (!clusters?.results?.length) {
1!
80
            throw new Error("No clusters found.");
×
81
        }
82
        const rows = clusters.results
1✔
83
            .map((cluster) => {
84
                const connectionString = cluster.connectionStrings?.standard || "N/A";
1✔
85
                const mongoDBVersion = cluster.mongoDBVersion || "N/A";
1!
86
                return `${cluster.name} | ${cluster.stateName} | ${mongoDBVersion} | ${connectionString}`;
1✔
87
            })
88
            .join("\n");
89
        return {
1✔
90
            content: [
91
                {
92
                    type: "text",
93
                    text: `Here are your MongoDB Atlas clusters in project "${project.name}" (${project.id}):`,
94
                },
95
                {
96
                    type: "text",
97
                    text: `Cluster Name | State | MongoDB Version | Connection String
98
----------------|----------------|----------------|----------------|----------------
99
${rows}`,
100
                },
101
            ],
102
        };
103
    }
104
}
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