-
Notifications
You must be signed in to change notification settings - Fork 139
feat: introduces drop-index tool for regular index #644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+306
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bd9e30a
feat: introduces drop-index tool for regular index
himanshusinghs fc90e7f
chore: fix tests
himanshusinghs 2fcf046
chore: add accuracy tests for drop-index tool
himanshusinghs a0f708f
chore: addresses PR feedback
himanshusinghs a6ba8ff
chore: use common message
himanshusinghs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import z from "zod"; | ||
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; | ||
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."), | ||
}; | ||
public operationType: OperationType = "delete"; | ||
|
||
protected async execute({ | ||
database, | ||
collection, | ||
indexName, | ||
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { | ||
const provider = await this.ensureConnected(); | ||
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, | ||
}; | ||
} | ||
|
||
protected getConfirmationMessage({ database, collection, indexName }: ToolArgs<typeof this.argsShape>): string { | ||
return ( | ||
`You are about to drop the \`${indexName}\` index 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?**" | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; | ||
import { describeAccuracyTests } from "./sdk/describeAccuracyTests.js"; | ||
import { Matcher } from "./sdk/matcher.js"; | ||
|
||
// We don't want to delete actual indexes | ||
const mockedTools = { | ||
"drop-index": ({ indexName, database, collection }: Record<string, unknown>): CallToolResult => { | ||
return { | ||
content: [ | ||
{ | ||
text: `Successfully dropped the index with name "${String(indexName)}" from the provided namespace "${String(database)}.${String(collection)}".`, | ||
type: "text", | ||
}, | ||
], | ||
}; | ||
}, | ||
} as const; | ||
|
||
describeAccuracyTests([ | ||
{ | ||
prompt: "Delete the index called year_1 from mflix.movies namespace", | ||
expectedToolCalls: [ | ||
{ | ||
toolName: "drop-index", | ||
parameters: { | ||
database: "mflix", | ||
collection: "movies", | ||
indexName: "year_1", | ||
}, | ||
}, | ||
], | ||
mockedTools, | ||
}, | ||
{ | ||
prompt: "First create a text index on field 'title' in 'mflix.movies' namespace and then drop all the indexes from 'mflix.movies' namespace", | ||
expectedToolCalls: [ | ||
{ | ||
toolName: "create-index", | ||
parameters: { | ||
database: "mflix", | ||
collection: "movies", | ||
name: Matcher.anyOf(Matcher.undefined, Matcher.string()), | ||
keys: { | ||
title: "text", | ||
}, | ||
}, | ||
}, | ||
{ | ||
toolName: "collection-indexes", | ||
parameters: { | ||
database: "mflix", | ||
collection: "movies", | ||
}, | ||
}, | ||
{ | ||
toolName: "drop-index", | ||
parameters: { | ||
database: "mflix", | ||
collection: "movies", | ||
indexName: Matcher.string(), | ||
}, | ||
}, | ||
{ | ||
toolName: "drop-index", | ||
parameters: { | ||
database: "mflix", | ||
collection: "movies", | ||
indexName: Matcher.string(), | ||
}, | ||
}, | ||
], | ||
mockedTools, | ||
}, | ||
]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
tests/integration/tools/mongodb/delete/dropIndex.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { describe, beforeEach, it, afterEach, expect } from "vitest"; | ||
import type { Collection } from "mongodb"; | ||
import { | ||
databaseCollectionInvalidArgs, | ||
databaseCollectionParameters, | ||
defaultDriverOptions, | ||
defaultTestConfig, | ||
getDataFromUntrustedContent, | ||
getResponseContent, | ||
setupIntegrationTest, | ||
validateThrowsForInvalidArguments, | ||
validateToolMetadata, | ||
} from "../../../helpers.js"; | ||
import { describeWithMongoDB, setupMongoDBIntegrationTest } from "../mongodbHelpers.js"; | ||
import { createMockElicitInput } from "../../../../utils/elicitationMocks.js"; | ||
import { Elicitation } from "../../../../../src/elicitation.js"; | ||
|
||
describeWithMongoDB("drop-index tool", (integration) => { | ||
let moviesCollection: Collection; | ||
let indexName: string; | ||
beforeEach(async () => { | ||
await integration.connectMcpClient(); | ||
const client = integration.mongoClient(); | ||
moviesCollection = client.db("mflix").collection("movies"); | ||
await moviesCollection.insertMany([ | ||
{ | ||
name: "Movie1", | ||
year: 1994, | ||
}, | ||
{ | ||
name: "Movie2", | ||
year: 2001, | ||
}, | ||
]); | ||
indexName = await moviesCollection.createIndex({ year: 1 }); | ||
}); | ||
|
||
afterEach(async () => { | ||
await moviesCollection.drop(); | ||
}); | ||
|
||
validateToolMetadata(integration, "drop-index", "Drop an index for the provided database and collection.", [ | ||
...databaseCollectionParameters, | ||
{ | ||
name: "indexName", | ||
type: "string", | ||
description: "The name of the index to be dropped.", | ||
required: true, | ||
}, | ||
]); | ||
|
||
validateThrowsForInvalidArguments(integration, "drop-index", [ | ||
...databaseCollectionInvalidArgs, | ||
{ database: "test", collection: "testColl", indexName: null }, | ||
{ database: "test", collection: "testColl", indexName: undefined }, | ||
{ database: "test", collection: "testColl", indexName: [] }, | ||
{ database: "test", collection: "testColl", indexName: true }, | ||
{ database: "test", collection: "testColl", indexName: false }, | ||
{ database: "test", collection: "testColl", indexName: 0 }, | ||
{ database: "test", collection: "testColl", indexName: 12 }, | ||
{ database: "test", collection: "testColl", indexName: "" }, | ||
]); | ||
|
||
describe.each([ | ||
{ | ||
database: "mflix", | ||
collection: "non-existent", | ||
}, | ||
{ | ||
database: "non-db", | ||
collection: "non-coll", | ||
}, | ||
])( | ||
"when attempting to delete an index from non-existent namespace - $database $collection", | ||
({ database, collection }) => { | ||
it("should fail with error", async () => { | ||
const response = await integration.mcpClient().callTool({ | ||
name: "drop-index", | ||
arguments: { database, collection, indexName: "non-existent" }, | ||
}); | ||
expect(response.isError).toBe(true); | ||
const content = getResponseContent(response.content); | ||
expect(content).toEqual(`Error running drop-index: ns not found ${database}.${collection}`); | ||
}); | ||
} | ||
); | ||
|
||
describe("when attempting to delete an index that does not exist", () => { | ||
it("should fail with error", async () => { | ||
const response = await integration.mcpClient().callTool({ | ||
name: "drop-index", | ||
arguments: { database: "mflix", collection: "movies", indexName: "non-existent" }, | ||
}); | ||
expect(response.isError).toBe(true); | ||
const content = getResponseContent(response.content); | ||
expect(content).toEqual(`Error running drop-index: index not found with name [non-existent]`); | ||
}); | ||
}); | ||
|
||
describe("when attempting to delete an index that exists", () => { | ||
it("should succeed", async () => { | ||
const response = await integration.mcpClient().callTool({ | ||
name: "drop-index", | ||
// The index is created in beforeEach | ||
arguments: { database: "mflix", collection: "movies", indexName: indexName }, | ||
}); | ||
expect(response.isError).toBe(undefined); | ||
const content = getResponseContent(response.content); | ||
expect(content).toContain(`Successfully dropped the index from the provided namespace.`); | ||
const data = getDataFromUntrustedContent(content); | ||
expect(JSON.parse(data)).toMatchObject({ indexName, namespace: "mflix.movies" }); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("drop-index tool - when invoked via an elicitation enabled client", () => { | ||
const mockElicitInput = createMockElicitInput(); | ||
const mdbIntegration = setupMongoDBIntegrationTest(); | ||
const integration = setupIntegrationTest( | ||
() => defaultTestConfig, | ||
() => defaultDriverOptions, | ||
{ elicitInput: mockElicitInput } | ||
); | ||
let moviesCollection: Collection; | ||
let indexName: string; | ||
|
||
beforeEach(async () => { | ||
moviesCollection = mdbIntegration.mongoClient().db("mflix").collection("movies"); | ||
await moviesCollection.insertMany([ | ||
{ name: "Movie1", year: 1994 }, | ||
{ name: "Movie2", year: 2001 }, | ||
]); | ||
indexName = await moviesCollection.createIndex({ year: 1 }); | ||
await integration.mcpClient().callTool({ | ||
name: "connect", | ||
arguments: { | ||
connectionString: mdbIntegration.connectionString(), | ||
}, | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
await moviesCollection.drop(); | ||
}); | ||
|
||
it("should ask for confirmation before proceeding with tool call", async () => { | ||
expect(await moviesCollection.listIndexes().toArray()).toHaveLength(2); | ||
mockElicitInput.confirmYes(); | ||
await integration.mcpClient().callTool({ | ||
name: "drop-index", | ||
arguments: { database: "mflix", collection: "movies", indexName }, | ||
}); | ||
expect(mockElicitInput.mock).toHaveBeenCalledTimes(1); | ||
expect(mockElicitInput.mock).toHaveBeenCalledWith({ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
message: expect.stringContaining( | ||
"You are about to drop the `year_1` index from the `mflix.movies` namespace" | ||
), | ||
requestedSchema: Elicitation.CONFIRMATION_SCHEMA, | ||
}); | ||
expect(await moviesCollection.listIndexes().toArray()).toHaveLength(1); | ||
}); | ||
|
||
it("should not drop the index if the confirmation was not provided", async () => { | ||
expect(await moviesCollection.listIndexes().toArray()).toHaveLength(2); | ||
mockElicitInput.confirmNo(); | ||
await integration.mcpClient().callTool({ | ||
name: "drop-index", | ||
arguments: { database: "mflix", collection: "movies", indexName }, | ||
}); | ||
expect(mockElicitInput.mock).toHaveBeenCalledTimes(1); | ||
expect(mockElicitInput.mock).toHaveBeenCalledWith({ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
message: expect.stringContaining( | ||
"You are about to drop the `year_1` index from the `mflix.movies` namespace" | ||
), | ||
requestedSchema: Elicitation.CONFIRMATION_SCHEMA, | ||
}); | ||
expect(await moviesCollection.listIndexes().toArray()).toHaveLength(2); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.