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

mongodb-js / mongodb-mcp-server / 19146805236

06 Nov 2025 07:05PM UTC coverage: 80.192% (+0.04%) from 80.151%
19146805236

Pull #716

github

web-flow
Merge 3b9e5951c into 454e81617
Pull Request #716: chore: cleanup telemetry

1357 of 1802 branches covered (75.31%)

Branch coverage included in aggregate %.

2 of 6 new or added lines in 4 files covered. (33.33%)

31 existing lines in 4 files now uncovered.

6477 of 7967 relevant lines covered (81.3%)

69.52 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, ServerNotification, ServerRequest } 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 { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
17
import type { PerfAdvisorToolMetadata } from "../../../telemetry/types.js";
18

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

26
export class GetPerformanceAdvisorTool extends AtlasToolBase {
3✔
27
    public name = "atlas-get-performance-advisor";
85✔
28
    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`;
85✔
29
    public operationType: OperationType = "read";
85✔
30
    protected argsShape = {
85✔
31
        projectId: AtlasArgs.projectId().describe(
85✔
32
            "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."
85✔
33
        ),
85✔
34
        clusterName: AtlasArgs.clusterName().describe("Atlas cluster name to get performance advisor recommendations"),
85✔
35
        operations: z
85✔
36
            .array(PerformanceAdvisorOperationType)
85✔
37
            .default(PerformanceAdvisorOperationType.options)
85✔
38
            .describe("Operations to get performance advisor recommendations"),
85✔
39
        since: z
85✔
40
            .string()
85✔
41
            .datetime()
85✔
42
            .describe(
85✔
43
                "Date to get slow query logs since. Must be a string in ISO 8601 format. Only relevant for the slowQueryLogs operation."
85✔
44
            )
85✔
45
            .optional(),
85✔
46
        namespaces: z
85✔
47
            .array(z.string())
85✔
48
            .describe("Namespaces to get slow query logs. Only relevant for the slowQueryLogs operation.")
85✔
49
            .optional(),
85✔
50
    };
85✔
51

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

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

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

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

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

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