Skip to content

feat: support for listing search indexes #245

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/tools/mongodb/read/collectionSearchIndexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { ToolArgs, OperationType } from "../../tool.js";
import { z } from "zod";

export const ListSearchIndexesArgs = {
indexName: z
.string()
.default("")
.optional()
.describe(
"The name of the index to return information about. Returns all indexes on collection if not provided."
),
};
export interface SearchIndex {
name: string;
latestDefinition: Record<string, unknown>;
}

export class CollectionSearchIndexesTool extends MongoDBToolBase {
protected name = "collection-search-indexes";
protected description = "Describe the search indexes for a collection";
protected argsShape = {
...DbOperationArgs,
...ListSearchIndexesArgs,
};

protected operationType: OperationType = "read";

protected async execute({
database,
collection,
indexName,
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
const provider = await this.ensureConnected();

const indexes: SearchIndex[] = (
(await provider.getSearchIndexes(database, collection, indexName)) as SearchIndex[]
).map((doc) => ({
name: doc.name,
latestDefinition: doc.latestDefinition,
}));

return {
content: [
{
text: indexName
? `Found ${indexes.length} search indexes in the collection "${collection}" with name "${indexName}":`
: `Found ${indexes.length} search indexes in the collection "${collection}"`,
type: "text",
},
...(indexes.map((indexDefinition) => {
return {
text: `\nName: "${indexDefinition.name}"\nDefinition: ${JSON.stringify(indexDefinition.latestDefinition, null, 2)}\n`,
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider removing the leading newline in the index detail message to improve the output formatting.

Suggested change
text: `\nName: "${indexDefinition.name}"\nDefinition: ${JSON.stringify(indexDefinition.latestDefinition, null, 2)}\n`,
text: `Name: "${indexDefinition.name}"\nDefinition: ${JSON.stringify(indexDefinition.latestDefinition, null, 2)}\n`,

Copilot uses AI. Check for mistakes.

type: "text",
};
}) as { text: string; type: "text" }[]),
],
};
}
}
2 changes: 2 additions & 0 deletions src/tools/mongodb/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import { DropCollectionTool } from "./delete/dropCollection.js";
import { ExplainTool } from "./metadata/explain.js";
import { CreateCollectionTool } from "./create/createCollection.js";
import { LogsTool } from "./metadata/logs.js";
import { CollectionSearchIndexesTool } from "./read/collectionSearchIndexes.js";

export const MongoDbTools = [
ConnectTool,
ListCollectionsTool,
ListDatabasesTool,
CollectionIndexesTool,
CollectionSearchIndexesTool,
CreateIndexTool,
CollectionSchemaTool,
FindTool,
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ export const databaseCollectionParameters: ParameterInfo[] = [
{ name: "collection", type: "string", description: "Collection name", required: true },
];

export const collectionWithSearchIndexParameters: ParameterInfo[] = [
...databaseCollectionParameters,
{
name: "indexName",
type: "string",
description:
"The name of the index to return information about. Returns all indexes on collection if not provided.",
required: false,
},
];

export const databaseCollectionInvalidArgs = [
{},
{ database: "test" },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describeWithMongoDB } from "../../mongodb/mongodbHelpers.js";
import {
databaseCollectionInvalidArgs,
validateThrowsForInvalidArguments,
validateToolMetadata,
collectionWithSearchIndexParameters,
} from "../../../helpers.js";

describeWithMongoDB("collectionSearchIndexes tool", (integration) => {
validateToolMetadata(
integration,
"collection-search-indexes",
"Describe the search indexes for a collection",
collectionWithSearchIndexParameters
);
validateThrowsForInvalidArguments(integration, "collection-search-indexes", databaseCollectionInvalidArgs);

// Real tests to be added once search indexes are supported in test env.
});
Loading