|
| 1 | +import { z } from "zod"; |
| 2 | +import { AtlasToolBase } from "../atlasTool.js"; |
| 3 | +import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 4 | +import type { OperationType, ToolArgs } from "../../tool.js"; |
| 5 | +import { formatUntrustedData } from "../../tool.js"; |
| 6 | +import { |
| 7 | + getSuggestedIndexes, |
| 8 | + getDropIndexSuggestions, |
| 9 | + getSchemaAdvice, |
| 10 | + getSlowQueries, |
| 11 | +} from "../../../common/atlas/performanceAdvisorUtils.js"; |
| 12 | + |
| 13 | +const PerformanceAdvisorOperationType = z.enum([ |
| 14 | + "suggestedIndexes", |
| 15 | + "dropIndexSuggestions", |
| 16 | + "slowQueryLogs", |
| 17 | + "schemaSuggestions", |
| 18 | +]); |
| 19 | + |
| 20 | +export class GetPerformanceAdvisorTool extends AtlasToolBase { |
| 21 | + public name = "atlas-get-performance-advisor"; |
| 22 | + protected description = |
| 23 | + "Get MongoDB Atlas performance advisor recommendations, which includes the operations: suggested indexes, drop index suggestions, slow query logs, and schema suggestions"; |
| 24 | + public operationType: OperationType = "read"; |
| 25 | + protected argsShape = { |
| 26 | + projectId: z.string().describe("Atlas project ID to get performance advisor recommendations"), |
| 27 | + clusterName: z.string().describe("Atlas cluster name to get performance advisor recommendations"), |
| 28 | + operations: z |
| 29 | + .array(PerformanceAdvisorOperationType) |
| 30 | + .default(PerformanceAdvisorOperationType.options) |
| 31 | + .describe("Operations to get performance advisor recommendations"), |
| 32 | + since: z.date().describe("Date to get slow query logs since").optional(), |
| 33 | + namespaces: z |
| 34 | + .array(z.string()) |
| 35 | + .describe("Namespaces to get slow query logs. Only relevant for the slowQueryLogs operation.") |
| 36 | + .optional(), |
| 37 | + }; |
| 38 | + |
| 39 | + protected async execute({ |
| 40 | + projectId, |
| 41 | + clusterName, |
| 42 | + operations, |
| 43 | + since, |
| 44 | + namespaces, |
| 45 | + }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { |
| 46 | + try { |
| 47 | + const [suggestedIndexesResult, dropIndexSuggestionsResult, slowQueryLogsResult, schemaSuggestionsResult] = |
| 48 | + await Promise.all([ |
| 49 | + operations.includes("suggestedIndexes") |
| 50 | + ? getSuggestedIndexes(this.session.apiClient, projectId, clusterName) |
| 51 | + : { suggestedIndexes: [] }, |
| 52 | + operations.includes("dropIndexSuggestions") |
| 53 | + ? getDropIndexSuggestions(this.session.apiClient, projectId, clusterName) |
| 54 | + : { hiddenIndexes: [], redundantIndexes: [], unusedIndexes: [] }, |
| 55 | + operations.includes("slowQueryLogs") |
| 56 | + ? getSlowQueries(this.session.apiClient, projectId, clusterName, since, namespaces) |
| 57 | + : { slowQueryLogs: [] }, |
| 58 | + operations.includes("schemaSuggestions") |
| 59 | + ? getSchemaAdvice(this.session.apiClient, projectId, clusterName) |
| 60 | + : { recommendations: [] }, |
| 61 | + ]); |
| 62 | + |
| 63 | + const performanceAdvisorData = [ |
| 64 | + suggestedIndexesResult?.suggestedIndexes?.length > 0 |
| 65 | + ? `## Suggested Indexes\n${JSON.stringify(suggestedIndexesResult.suggestedIndexes)}` |
| 66 | + : "No suggested indexes found.", |
| 67 | + dropIndexSuggestionsResult |
| 68 | + ? `## Drop Index Suggestions\n${JSON.stringify(dropIndexSuggestionsResult)}` |
| 69 | + : "No drop index suggestions found.", |
| 70 | + slowQueryLogsResult?.slowQueryLogs?.length > 0 |
| 71 | + ? `## Slow Query Logs\n${JSON.stringify(slowQueryLogsResult.slowQueryLogs)}` |
| 72 | + : "No slow query logs found.", |
| 73 | + schemaSuggestionsResult?.recommendations?.length > 0 |
| 74 | + ? `## Schema Suggestions\n${JSON.stringify(schemaSuggestionsResult.recommendations)}` |
| 75 | + : "No schema suggestions found.", |
| 76 | + ]; |
| 77 | + |
| 78 | + if (performanceAdvisorData.length === 0) { |
| 79 | + return { |
| 80 | + content: [{ type: "text", text: "No performance advisor recommendations found." }], |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + return { |
| 85 | + content: formatUntrustedData("Performance advisor data", performanceAdvisorData.join("\n\n")), |
| 86 | + }; |
| 87 | + } catch (error) { |
| 88 | + return { |
| 89 | + content: [ |
| 90 | + { |
| 91 | + type: "text", |
| 92 | + text: `Error retrieving performance advisor data: ${error instanceof Error ? error.message : String(error)}`, |
| 93 | + }, |
| 94 | + ], |
| 95 | + }; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments