Skip to content

Commit 3315b04

Browse files
committed
chore: simplify drop-index API
1 parent 56a50b7 commit 3315b04

File tree

3 files changed

+293
-453
lines changed

3 files changed

+293
-453
lines changed

src/tools/mongodb/delete/dropIndex.ts

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

@@ -10,86 +9,62 @@ export class DropIndexTool extends MongoDBToolBase {
109
protected argsShape = {
1110
...DbOperationArgs,
1211
indexName: z.string().nonempty().describe("The name of the index to be dropped."),
13-
type: this.isFeatureFlagEnabled(FeatureFlags.VectorSearch)
14-
? z
15-
.enum(["classic", "search"])
16-
.describe(
17-
"The type of index to be deleted. Use 'classic' for standard indexes and 'search' for atlas search and vector search indexes."
18-
)
19-
: z
20-
.literal("classic")
21-
.default("classic")
22-
.describe("The type of index to be deleted. Is always set to 'classic'."),
2312
};
2413
public operationType: OperationType = "delete";
2514

26-
protected async execute(toolArgs: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
15+
protected async execute({
16+
database,
17+
collection,
18+
indexName,
19+
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
2720
const provider = await this.ensureConnected();
28-
switch (toolArgs.type) {
29-
case "classic":
30-
return this.dropClassicIndex(provider, toolArgs);
31-
case "search":
32-
return this.dropSearchIndex(provider, toolArgs);
33-
}
34-
}
21+
const existingIndex = (await provider.getIndexes(database, collection)).find((idx) => idx.name === indexName);
22+
if (existingIndex) {
23+
const result = await provider.runCommand(database, {
24+
dropIndexes: collection,
25+
index: indexName,
26+
});
3527

36-
private async dropClassicIndex(
37-
provider: NodeDriverServiceProvider,
38-
{ database, collection, indexName }: ToolArgs<typeof this.argsShape>
39-
): Promise<CallToolResult> {
40-
const result = await provider.runCommand(database, {
41-
dropIndexes: collection,
42-
index: indexName,
43-
});
44-
45-
return {
46-
content: formatUntrustedData(
47-
`${result.ok ? "Successfully dropped" : "Failed to drop"} the index from the provided namespace.`,
48-
JSON.stringify({
49-
indexName,
50-
namespace: `${database}.${collection}`,
51-
})
52-
),
53-
isError: result.ok ? undefined : true,
54-
};
55-
}
56-
57-
private async dropSearchIndex(
58-
provider: NodeDriverServiceProvider,
59-
{ database, collection, indexName }: ToolArgs<typeof this.argsShape>
60-
): Promise<CallToolResult> {
61-
await this.ensureSearchIsSupported();
62-
const indexes = await provider.getSearchIndexes(database, collection, indexName);
63-
if (indexes.length === 0) {
6428
return {
6529
content: formatUntrustedData(
66-
"Index does not exist in the provided namespace.",
67-
JSON.stringify({ indexName, namespace: `${database}.${collection}` })
30+
`${result.ok ? "Successfully dropped" : "Failed to drop"} the index from the provided namespace.`,
31+
JSON.stringify({
32+
indexName,
33+
namespace: `${database}.${collection}`,
34+
})
6835
),
69-
isError: true,
36+
isError: result.ok ? undefined : true,
7037
};
7138
}
7239

73-
await provider.dropSearchIndex(database, collection, indexName);
40+
if (this.isFeatureFlagEnabled(FeatureFlags.VectorSearch) && (await this.session.isSearchSupported())) {
41+
const existingSearchIndex = (await provider.getSearchIndexes(database, collection, indexName))[0];
42+
if (existingSearchIndex) {
43+
await provider.dropSearchIndex(database, collection, indexName);
44+
return {
45+
content: formatUntrustedData(
46+
"Successfully dropped the index from the provided namespace.",
47+
JSON.stringify({
48+
indexName,
49+
namespace: `${database}.${collection}`,
50+
})
51+
),
52+
};
53+
}
54+
}
55+
7456
return {
7557
content: formatUntrustedData(
76-
"Successfully dropped the index from the provided namespace.",
77-
JSON.stringify({
78-
indexName,
79-
namespace: `${database}.${collection}`,
80-
})
58+
"Index does not exist in the provided namespace.",
59+
JSON.stringify({ indexName, namespace: `${database}.${collection}` })
8160
),
61+
isError: true,
8262
};
8363
}
8464

85-
protected getConfirmationMessage({
86-
database,
87-
collection,
88-
indexName,
89-
type,
90-
}: ToolArgs<typeof this.argsShape>): string {
65+
protected getConfirmationMessage({ database, collection, indexName }: ToolArgs<typeof this.argsShape>): string {
9166
return (
92-
`You are about to drop the ${type === "search" ? "search index" : "index"} named \`${indexName}\` from the \`${database}.${collection}\` namespace:\n\n` +
67+
`You are about to drop the index named \`${indexName}\` from the \`${database}.${collection}\` namespace:\n\n` +
9368
"This operation will permanently remove the index and might affect the performance of queries relying on this index.\n\n" +
9469
"**Do you confirm the execution of the action?**"
9570
);

tests/accuracy/dropIndex.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ describeAccuracyTests(
2929
database: "mflix",
3030
collection: "movies",
3131
indexName: "year_1",
32-
type: "classic",
3332
},
3433
},
3534
],
@@ -49,7 +48,6 @@ describeAccuracyTests(
4948
keys: {
5049
title: "text",
5150
},
52-
type: "classic",
5351
},
5452
],
5553
},
@@ -67,7 +65,6 @@ describeAccuracyTests(
6765
database: "mflix",
6866
collection: "movies",
6967
indexName: Matcher.string(),
70-
type: "classic",
7168
},
7269
},
7370
{
@@ -76,7 +73,6 @@ describeAccuracyTests(
7673
database: "mflix",
7774
collection: "movies",
7875
indexName: Matcher.string(),
79-
type: "classic",
8076
},
8177
},
8278
],
@@ -118,7 +114,6 @@ describeAccuracyTests(
118114
database: "mflix",
119115
collection: "movies",
120116
indexName: Matcher.string(),
121-
type: "search",
122117
},
123118
},
124119
],

0 commit comments

Comments
 (0)