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

mongodb-js / mongodb-mcp-server / 19925638108

04 Dec 2025 10:23AM UTC coverage: 80.575% (-0.03%) from 80.607%
19925638108

Pull #779

github

web-flow
Merge 1e90969c7 into e12068a03
Pull Request #779: chore: bump mcp SDK, refactor tool arguments

1469 of 1905 branches covered (77.11%)

Branch coverage included in aggregate %.

49 of 66 new or added lines in 12 files covered. (74.24%)

2 existing lines in 2 files now uncovered.

6736 of 8278 relevant lines covered (81.37%)

78.78 hits per line

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

32.0
/src/tools/atlas/read/getPerformanceAdvisor.ts
1
import { z } from "zod";
3✔
2
import { AtlasToolBase } from "../atlasTool.js";
3✔
3
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
4
import type { OperationType, ToolArgs } from "../../tool.js";
5
import { formatUntrustedData } from "../../tool.js";
3✔
6
import {
3✔
7
    getSuggestedIndexes,
8
    getDropIndexSuggestions,
9
    getSchemaAdvice,
10
    getSlowQueries,
11
    DEFAULT_SLOW_QUERY_LOGS_LIMIT,
12
    SUGGESTED_INDEXES_COPY,
13
    SLOW_QUERY_LOGS_COPY,
14
} from "../../../common/atlas/performanceAdvisorUtils.js";
15
import { AtlasArgs } from "../../args.js";
3✔
16
import type { PerfAdvisorToolMetadata } from "../../../telemetry/types.js";
17

18
const PerformanceAdvisorOperationType = z.enum([
3✔
19
    "suggestedIndexes",
3✔
20
    "dropIndexSuggestions",
3✔
21
    "slowQueryLogs",
3✔
22
    "schemaSuggestions",
3✔
23
]);
3✔
24

25
export class GetPerformanceAdvisorTool extends AtlasToolBase {
3✔
26
    public name = "atlas-get-performance-advisor";
100✔
27
    protected description = `Get MongoDB Atlas performance advisor recommendations, which includes the operations: suggested indexes, drop index suggestions, schema suggestions, and a sample of the most recent (max ${DEFAULT_SLOW_QUERY_LOGS_LIMIT}) slow query logs`;
100✔
28
    static operationType: OperationType = "read";
100✔
29
    protected argsShape = {
100✔
30
        projectId: AtlasArgs.projectId().describe(
100✔
31
            "Atlas project ID to get performance advisor recommendations. The project ID is a hexadecimal identifier of 24 characters. If the user has only specified the name, use the `atlas-list-projects` tool to retrieve the user's projects with their ids."
100✔
32
        ),
100✔
33
        clusterName: AtlasArgs.clusterName().describe("Atlas cluster name to get performance advisor recommendations"),
100✔
34
        operations: z
100✔
35
            .array(PerformanceAdvisorOperationType)
100✔
36
            .default(PerformanceAdvisorOperationType.options)
100✔
37
            .describe("Operations to get performance advisor recommendations"),
100✔
38
        since: z
100✔
39
            .string()
100✔
40
            .datetime()
100✔
41
            .describe(
100✔
42
                "Date to get slow query logs since. Must be a string in ISO 8601 format. Only relevant for the slowQueryLogs operation."
100✔
43
            )
100✔
44
            .optional(),
100✔
45
        namespaces: z
100✔
46
            .array(z.string())
100✔
47
            .describe("Namespaces to get slow query logs. Only relevant for the slowQueryLogs operation.")
100✔
48
            .optional(),
100✔
49
    };
100✔
50

51
    protected async execute({
3✔
52
        projectId,
×
53
        clusterName,
×
54
        operations,
×
55
        since,
×
56
        namespaces,
×
57
    }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
×
58
        try {
×
59
            const [suggestedIndexesResult, dropIndexSuggestionsResult, slowQueryLogsResult, schemaSuggestionsResult] =
×
60
                await Promise.allSettled([
×
61
                    operations.includes("suggestedIndexes")
×
62
                        ? getSuggestedIndexes(this.session.apiClient, projectId, clusterName)
×
63
                        : Promise.resolve(undefined),
×
64
                    operations.includes("dropIndexSuggestions")
×
65
                        ? getDropIndexSuggestions(this.session.apiClient, projectId, clusterName)
×
66
                        : Promise.resolve(undefined),
×
67
                    operations.includes("slowQueryLogs")
×
68
                        ? getSlowQueries(
×
69
                              this.session.apiClient,
×
70
                              projectId,
×
71
                              clusterName,
×
72
                              since ? new Date(since) : undefined,
×
73
                              namespaces
×
74
                          )
×
75
                        : Promise.resolve(undefined),
×
76
                    operations.includes("schemaSuggestions")
×
77
                        ? getSchemaAdvice(this.session.apiClient, projectId, clusterName)
×
78
                        : Promise.resolve(undefined),
×
79
                ]);
×
80

81
            const hasSuggestedIndexes =
×
82
                suggestedIndexesResult.status === "fulfilled" &&
×
83
                suggestedIndexesResult.value?.suggestedIndexes &&
×
84
                suggestedIndexesResult.value.suggestedIndexes.length > 0;
×
85
            const hasDropIndexSuggestions =
×
86
                dropIndexSuggestionsResult.status === "fulfilled" &&
×
87
                dropIndexSuggestionsResult.value?.hiddenIndexes &&
×
88
                dropIndexSuggestionsResult.value?.redundantIndexes &&
×
89
                dropIndexSuggestionsResult.value?.unusedIndexes &&
×
90
                (dropIndexSuggestionsResult.value.hiddenIndexes.length > 0 ||
×
91
                    dropIndexSuggestionsResult.value.redundantIndexes.length > 0 ||
×
92
                    dropIndexSuggestionsResult.value.unusedIndexes.length > 0);
×
93
            const hasSlowQueryLogs =
×
94
                slowQueryLogsResult.status === "fulfilled" &&
×
95
                slowQueryLogsResult.value?.slowQueryLogs &&
×
96
                slowQueryLogsResult.value.slowQueryLogs.length > 0;
×
97
            const hasSchemaSuggestions =
×
98
                schemaSuggestionsResult.status === "fulfilled" &&
×
99
                schemaSuggestionsResult.value?.recommendations &&
×
100
                schemaSuggestionsResult.value.recommendations.length > 0;
×
101

102
            // Inserts the performance advisor data with the relevant section header if it exists
103
            const performanceAdvisorData = [
×
104
                `## Suggested Indexes\n${
×
105
                    hasSuggestedIndexes
×
106
                        ? `${SUGGESTED_INDEXES_COPY}\n${JSON.stringify(suggestedIndexesResult.value?.suggestedIndexes)}`
×
107
                        : "No suggested indexes found."
×
108
                }`,
×
109
                `## Drop Index Suggestions\n${hasDropIndexSuggestions ? JSON.stringify(dropIndexSuggestionsResult.value) : "No drop index suggestions found."}`,
×
110
                `## Slow Query Logs\n${hasSlowQueryLogs ? `${SLOW_QUERY_LOGS_COPY}\n${JSON.stringify(slowQueryLogsResult.value?.slowQueryLogs)}` : "No slow query logs found."}`,
×
111
                `## Schema Suggestions\n${hasSchemaSuggestions ? JSON.stringify(schemaSuggestionsResult.value?.recommendations) : "No schema suggestions found."}`,
×
112
            ];
×
113

114
            if (performanceAdvisorData.length === 0) {
×
115
                return {
×
116
                    content: [{ type: "text", text: "No performance advisor recommendations found." }],
×
117
                };
×
118
            }
×
119

120
            return {
×
121
                content: formatUntrustedData("Performance advisor data", performanceAdvisorData.join("\n\n")),
×
122
            };
×
123
        } catch (error) {
×
124
            return {
×
125
                content: [
×
126
                    {
×
127
                        type: "text",
×
128
                        text: `Error retrieving performance advisor data: ${error instanceof Error ? error.message : String(error)}`,
×
129
                    },
×
130
                ],
×
131
            };
×
132
        }
×
133
    }
×
134

135
    protected override resolveTelemetryMetadata(
3✔
136
        args: ToolArgs<typeof this.argsShape>,
×
NEW
137
        { result }: { result: CallToolResult }
×
138
    ): PerfAdvisorToolMetadata {
×
139
        return {
×
NEW
140
            ...super.resolveTelemetryMetadata(args, { result }),
×
141
            operations: args.operations,
×
142
        };
×
143
    }
×
144
}
3✔
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