Skip to content

Commit da43f52

Browse files
feat: support for listing search indexes (#245)
Co-authored-by: Copilot <[email protected]>
1 parent 816dec1 commit da43f52

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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(
12+
"The name of the index to return information about. Returns all indexes on collection if not provided."
13+
),
14+
};
15+
export interface SearchIndex {
16+
name: string;
17+
latestDefinition: Record<string, unknown>;
18+
}
19+
20+
export class CollectionSearchIndexesTool extends MongoDBToolBase {
21+
protected name = "collection-search-indexes";
22+
protected description = "Describe the search indexes for a collection";
23+
protected argsShape = {
24+
...DbOperationArgs,
25+
...ListSearchIndexesArgs,
26+
};
27+
28+
protected operationType: OperationType = "read";
29+
30+
protected async execute({
31+
database,
32+
collection,
33+
indexName,
34+
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
35+
const provider = await this.ensureConnected();
36+
37+
const indexes: SearchIndex[] = (
38+
(await provider.getSearchIndexes(database, collection, indexName)) as SearchIndex[]
39+
).map((doc) => ({
40+
name: doc.name,
41+
latestDefinition: doc.latestDefinition,
42+
}));
43+
44+
return {
45+
content: [
46+
{
47+
text: indexName
48+
? `Found ${indexes.length} search indexes in the collection "${collection}" with name "${indexName}":`
49+
: `Found ${indexes.length} search indexes in the collection "${collection}"`,
50+
type: "text",
51+
},
52+
...(indexes.map((indexDefinition) => {
53+
return {
54+
text: `\nName: "${indexDefinition.name}"\nDefinition: ${JSON.stringify(indexDefinition.latestDefinition, null, 2)}\n`,
55+
type: "text",
56+
};
57+
}) as { text: string; type: "text" }[]),
58+
],
59+
};
60+
}
61+
}

src/tools/mongodb/tools.ts

+2
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,

tests/integration/helpers.ts

+11
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ export const databaseCollectionParameters: ParameterInfo[] = [
182182
{ name: "collection", type: "string", description: "Collection name", required: true },
183183
];
184184

185+
export const collectionWithSearchIndexParameters: ParameterInfo[] = [
186+
...databaseCollectionParameters,
187+
{
188+
name: "indexName",
189+
type: "string",
190+
description:
191+
"The name of the index to return information about. Returns all indexes on collection if not provided.",
192+
required: false,
193+
},
194+
];
195+
185196
export const databaseCollectionInvalidArgs = [
186197
{},
187198
{ database: "test" },
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describeWithMongoDB } from "../../mongodb/mongodbHelpers.js";
2+
import {
3+
databaseCollectionInvalidArgs,
4+
validateThrowsForInvalidArguments,
5+
validateToolMetadata,
6+
collectionWithSearchIndexParameters,
7+
} from "../../../helpers.js";
8+
9+
describeWithMongoDB("collectionSearchIndexes tool", (integration) => {
10+
validateToolMetadata(
11+
integration,
12+
"collection-search-indexes",
13+
"Describe the search indexes for a collection",
14+
collectionWithSearchIndexParameters
15+
);
16+
validateThrowsForInvalidArguments(integration, "collection-search-indexes", databaseCollectionInvalidArgs);
17+
18+
// Real tests to be added once search indexes are supported in test env.
19+
});

0 commit comments

Comments
 (0)