Skip to content

Commit 859c7bd

Browse files
Support for listing search indexes
1 parent 816dec1 commit 859c7bd

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {DbOperationArgs, MongoDBToolBase} from "../mongodbTool.js";
2+
import {CallToolResult} from "@modelcontextprotocol/sdk/types.js";
3+
import { ToolArgs, OperationType } from "../../tool.js";
4+
import {z} from "zod";
5+
6+
export const ListSearchIndexesArgs = {
7+
indexName: z
8+
.string()
9+
.default('')
10+
.optional()
11+
.describe("The name of the index to return information about. Returns all indexes on collection if not provided."),
12+
}
13+
14+
export class CollectionSearchIndexesTool extends MongoDBToolBase {
15+
protected name = "collection-search-indexes";
16+
protected description = "List one or all search indexes on a collection";
17+
protected argsShape = {
18+
...DbOperationArgs,
19+
...ListSearchIndexesArgs,
20+
};
21+
22+
protected operationType: OperationType = "read";
23+
24+
protected async execute({ database, collection, indexName }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
25+
const provider = await this.ensureConnected();
26+
const indexes = await provider
27+
.getSearchIndexes(database, collection, indexName);
28+
29+
return {
30+
content: [
31+
{
32+
text: indexName
33+
? `Found ${indexes.length} search indexes in the collection "${collection}" with name "${indexName}":`
34+
: `Found ${indexes.length} search indexes in the collection "${collection}"`,
35+
type: "text",
36+
},
37+
...(indexes.map((indexDefinition) => {
38+
return {
39+
text: `Name "${indexDefinition.name}", definition: ${JSON.stringify(indexDefinition.latestDefinition)}`,
40+
type: "text",
41+
};
42+
}) as { text: string; type: "text" }[]),
43+
],
44+
};
45+
}
46+
}

src/tools/mongodb/tools.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import { DropCollectionTool } from "./delete/dropCollection.js";
1818
import { ExplainTool } from "./metadata/explain.js";
1919
import { CreateCollectionTool } from "./create/createCollection.js";
2020
import { LogsTool } from "./metadata/logs.js";
21+
import {CollectionSearchIndexesTool} from "./read/collectionSearchIndexes.js";
2122

2223
export const MongoDbTools = [
2324
ConnectTool,
2425
ListCollectionsTool,
2526
ListDatabasesTool,
2627
CollectionIndexesTool,
28+
CollectionSearchIndexesTool,
2729
CreateIndexTool,
2830
CollectionSchemaTool,
2931
FindTool,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describeWithMongoDB } from "../mongodbHelpers.js";
2+
3+
import {
4+
databaseCollectionParameters,
5+
databaseCollectionInvalidArgs,
6+
validateThrowsForInvalidArguments,
7+
validateToolMetadata,
8+
} from "../../../helpers.js";
9+
10+
describeWithMongoDB("collectionSearchIndexes tool", (integration) => {
11+
validateToolMetadata(
12+
integration,
13+
"collection-search-indexes",
14+
"Describe the search indexes for a collection",
15+
databaseCollectionParameters
16+
);
17+
validateThrowsForInvalidArguments(integration, "collection-search-indexes", databaseCollectionInvalidArgs);
18+
19+
// Real tests to be added once search indexes are supported in test env.
20+
});

0 commit comments

Comments
 (0)