-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathdropIndex.ts
More file actions
97 lines (90 loc) · 3.86 KB
/
dropIndex.ts
File metadata and controls
97 lines (90 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import z from "zod";
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
import { type ToolArgs, type OperationType, formatUntrustedData } from "../../tool.js";
export class DropIndexTool extends MongoDBToolBase {
public name = "drop-index";
protected description = "Drop an index for the provided database and collection.";
protected argsShape = {
...DbOperationArgs,
indexName: z.string().nonempty().describe("The name of the index to be dropped."),
type: this.isFeatureEnabled("search")
? z
.enum(["classic", "search"])
.describe(
"The type of index to be deleted. Use 'classic' for standard indexes and 'search' for atlas search and vector search indexes."
)
: z
.literal("classic")
.default("classic")
.describe("The type of index to be deleted. Is always set to 'classic'."),
};
public operationType: OperationType = "delete";
protected async execute(toolArgs: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
const provider = await this.ensureConnected();
switch (toolArgs.type) {
case "classic":
return this.dropClassicIndex(provider, toolArgs);
case "search":
return this.dropSearchIndex(provider, toolArgs);
}
}
private async dropClassicIndex(
provider: NodeDriverServiceProvider,
{ database, collection, indexName }: ToolArgs<typeof this.argsShape>
): Promise<CallToolResult> {
const result = await provider.runCommand(database, {
dropIndexes: collection,
index: indexName,
});
return {
content: formatUntrustedData(
`${result.ok ? "Successfully dropped" : "Failed to drop"} the index from the provided namespace.`,
JSON.stringify({
indexName,
namespace: `${database}.${collection}`,
})
),
isError: result.ok ? undefined : true,
};
}
private async dropSearchIndex(
provider: NodeDriverServiceProvider,
{ database, collection, indexName }: ToolArgs<typeof this.argsShape>
): Promise<CallToolResult> {
await this.ensureSearchIsSupported();
const indexes = await provider.getSearchIndexes(database, collection, indexName);
if (indexes.length === 0) {
return {
content: formatUntrustedData(
"Index does not exist in the provided namespace.",
JSON.stringify({ indexName, namespace: `${database}.${collection}` })
),
isError: true,
};
}
await provider.dropSearchIndex(database, collection, indexName);
return {
content: formatUntrustedData(
"Successfully dropped the index from the provided namespace.",
JSON.stringify({
indexName,
namespace: `${database}.${collection}`,
})
),
};
}
protected getConfirmationMessage({
database,
collection,
indexName,
type,
}: ToolArgs<typeof this.argsShape>): string {
return (
`You are about to drop the ${type === "search" ? "search index" : "index"} named \`${indexName}\` from the \`${database}.${collection}\` namespace:\n\n` +
"This operation will permanently remove the index and might affect the performance of queries relying on this index.\n\n" +
"**Do you confirm the execution of the action?**"
);
}
}