11import z from "zod" ;
22import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
3- import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver" ;
43import { DbOperationArgs , MongoDBToolBase } from "../mongodbTool.js" ;
54import { 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 ) ;
0 commit comments