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

mongodb-js / mongodb-mcp-server / 14520810167

17 Apr 2025 04:56PM UTC coverage: 50.67% (-0.3%) from 50.98%
14520810167

Pull #83

github

fmenezes
test: add cluster tests
Pull Request #83: chore: add atlas tests

23 of 146 branches covered (15.75%)

Branch coverage included in aggregate %.

0 of 5 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

393 of 675 relevant lines covered (58.22%)

9.58 hits per line

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

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

7
export class ListClustersTool extends AtlasToolBase {
7✔
8
    protected name = "atlas-list-clusters";
6✔
9
    protected description = "List MongoDB Atlas clusters";
6✔
10
    protected operationType: OperationType = "read";
6✔
11
    protected argsShape = {
6✔
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();
×
17

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

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

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

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

43
            return this.formatClustersTable(project, data);
×
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.map((cluster) => {
×
UNCOV
62
                return `${cluster.groupName} (${cluster.groupId}) | ${cluster.name}`;
×
63
            })
64
            .join("\n");
65
        return {
×
66
            content: [
67
                {
68
                    type: "text",
69
                    text: `Project | Cluster Name
70
----------------|----------------
71
${rows}`,
72
                },
73
            ],
74
        };
75
    }
76

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