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 5 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
51 changes: 51 additions & 0 deletions src/tools/mongodb/read/collectionSearchIndexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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 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 = await provider.getSearchIndexes(database, collection, indexName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Would it make sense to cast the result of getSearchIndexes to a known interface with common fields (like name)

Ie I think in some of the tests, we may call getSearchIndexes as well and check if certain properties match what's expected


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: `Name "${indexDefinition.name}", definition: ${JSON.stringify(indexDefinition.latestDefinition)}`,
Copy link
Preview

Copilot AI May 13, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider formatting the JSON output for better readability if the output is intended for human review, or ensure that downstream consumers are designed to handle the unformatted JSON.

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

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describeWithMongoDB } from "../mongodbHelpers.js";
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the WIP PR https://github.com/mongodb-js/mongodb-mcp-server/tree/f556cc3691a62d8e98f374be4ebaa14c4a555642/tests/integration/tools/atlas-search, there's a separate directory for atlas search tests since the setup is different.

Is it worth keeping that directory structure and moving this into an atlas-search directory?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Altered directory structure to use proposal in #181


import {
databaseCollectionParameters,
databaseCollectionInvalidArgs,
validateThrowsForInvalidArguments,
validateToolMetadata,
} from "../../../helpers.js";

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

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