Skip to content

Commit 764115a

Browse files
chore: merge drop-search-index into drop-index tool
1 parent d6f1b91 commit 764115a

File tree

5 files changed

+482
-423
lines changed

5 files changed

+482
-423
lines changed

src/tools/mongodb/delete/dropIndex.ts

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
import z from "zod";
22
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3+
import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
34
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
4-
import { type ToolArgs, type OperationType, formatUntrustedData } from "../../tool.js";
5+
import { type ToolArgs, type OperationType, formatUntrustedData, FeatureFlags } from "../../tool.js";
6+
import { ListSearchIndexesTool } from "../search/listSearchIndexes.js";
57

68
export class DropIndexTool extends MongoDBToolBase {
79
public name = "drop-index";
810
protected description = "Drop an index for the provided database and collection.";
911
protected argsShape = {
1012
...DbOperationArgs,
1113
indexName: z.string().nonempty().describe("The name of the index to be dropped."),
14+
type: this.isFeatureFlagEnabled(FeatureFlags.VectorSearch)
15+
? z
16+
.enum(["classic", "search"])
17+
.describe(
18+
"The type of index to be deleted. Use 'classic' for standard indexes and 'search' for atlas search and vector search indexes."
19+
)
20+
: z
21+
.literal("classic")
22+
.default("classic")
23+
.describe("The type of index to be deleted. Is always set to 'classic'."),
1224
};
1325
public operationType: OperationType = "delete";
1426

15-
protected async execute({
16-
database,
17-
collection,
18-
indexName,
19-
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
27+
protected async execute(toolArgs: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
2028
const provider = await this.ensureConnected();
29+
switch (toolArgs.type) {
30+
case "classic":
31+
return this.dropClassicIndex(provider, toolArgs);
32+
case "search":
33+
return this.dropSearchIndex(provider, toolArgs);
34+
}
35+
}
36+
37+
private async dropClassicIndex(
38+
provider: NodeDriverServiceProvider,
39+
{ database, collection, indexName }: ToolArgs<typeof this.argsShape>
40+
): Promise<CallToolResult> {
2141
const result = await provider.runCommand(database, {
2242
dropIndexes: collection,
2343
index: indexName,
@@ -35,11 +55,57 @@ export class DropIndexTool extends MongoDBToolBase {
3555
};
3656
}
3757

58+
private async dropSearchIndex(
59+
provider: NodeDriverServiceProvider,
60+
{ database, collection, indexName }: ToolArgs<typeof this.argsShape>
61+
): Promise<CallToolResult> {
62+
const searchIndexes = await ListSearchIndexesTool.getSearchIndexes(provider, database, collection);
63+
const indexDoesNotExist = !searchIndexes.find((index) => index.name === indexName);
64+
if (indexDoesNotExist) {
65+
return {
66+
content: formatUntrustedData(
67+
"Index does not exist in the provided namespace.",
68+
JSON.stringify({ indexName, namespace: `${database}.${collection}` })
69+
),
70+
isError: true,
71+
};
72+
}
73+
74+
await provider.dropSearchIndex(database, collection, indexName);
75+
return {
76+
content: formatUntrustedData(
77+
"Successfully dropped the index from the provided namespace.",
78+
JSON.stringify({
79+
indexName,
80+
namespace: `${database}.${collection}`,
81+
})
82+
),
83+
};
84+
}
85+
3886
protected getConfirmationMessage({ database, collection, indexName }: ToolArgs<typeof this.argsShape>): string {
3987
return (
4088
`You are about to drop the \`${indexName}\` index from the \`${database}.${collection}\` namespace:\n\n` +
4189
"This operation will permanently remove the index and might affect the performance of queries relying on this index.\n\n" +
4290
"**Do you confirm the execution of the action?**"
4391
);
4492
}
93+
94+
protected handleError(
95+
error: unknown,
96+
args: ToolArgs<typeof DbOperationArgs>
97+
): Promise<CallToolResult> | CallToolResult {
98+
if (error instanceof Error && "codeName" in error && error.codeName === "SearchNotEnabled") {
99+
return {
100+
content: [
101+
{
102+
text: "This MongoDB cluster does not support Search Indexes. Make sure you are using an Atlas Cluster, either remotely in Atlas or using the Atlas Local image, or your cluster supports MongoDB Search.",
103+
type: "text",
104+
},
105+
],
106+
isError: true,
107+
};
108+
}
109+
return super.handleError(error, args);
110+
}
45111
}

src/tools/mongodb/delete/dropSearchIndex.ts

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

0 commit comments

Comments
 (0)