Skip to content

Commit 016af0e

Browse files
committed
Clean up PA retrieval code
1 parent 3217e95 commit 016af0e

File tree

3 files changed

+100
-148
lines changed

3 files changed

+100
-148
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

src/tools/atlas/read/listPerformanceAdvisor.ts

Lines changed: 0 additions & 146 deletions
This file was deleted.

src/tools/atlas/tools.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { CreateProjectTool } from "./create/createProject.js";
1010
import { ListOrganizationsTool } from "./read/listOrgs.js";
1111
import { ConnectClusterTool } from "./connect/connectCluster.js";
1212
import { ListAlertsTool } from "./read/listAlerts.js";
13-
import { ListPerformanceAdvisorTool } from "./read/listPerformanceAdvisor.js";
13+
import { GetPerformanceAdvisorTool } from "./read/getPerformanceAdvisor.js";
1414

1515
export const AtlasTools = [
1616
ListClustersTool,
@@ -25,5 +25,5 @@ export const AtlasTools = [
2525
ListOrganizationsTool,
2626
ConnectClusterTool,
2727
ListAlertsTool,
28-
ListPerformanceAdvisorTool,
28+
GetPerformanceAdvisorTool,
2929
];

0 commit comments

Comments
 (0)