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 2 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
46 changes: 46 additions & 0 deletions src/tools/mongodb/read/collectionSearchIndexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {DbOperationArgs, MongoDBToolBase} from "../mongodbTool.js";

Check failure on line 1 in src/tools/mongodb/read/collectionSearchIndexes.ts

View workflow job for this annotation

GitHub Actions / check-style

Replace `DbOperationArgs,·MongoDBToolBase` with `·DbOperationArgs,·MongoDBToolBase·`
import {CallToolResult} from "@modelcontextprotocol/sdk/types.js";

Check failure on line 2 in src/tools/mongodb/read/collectionSearchIndexes.ts

View workflow job for this annotation

GitHub Actions / check-style

Replace `CallToolResult` with `·CallToolResult·`
import { ToolArgs, OperationType } from "../../tool.js";
import {z} from "zod";

Check failure on line 4 in src/tools/mongodb/read/collectionSearchIndexes.ts

View workflow job for this annotation

GitHub Actions / check-style

Replace `z` with `·z·`

export const ListSearchIndexesArgs = {
indexName: z

Check failure on line 7 in src/tools/mongodb/read/collectionSearchIndexes.ts

View workflow job for this annotation

GitHub Actions / check-style

Insert `··`
.string()

Check failure on line 8 in src/tools/mongodb/read/collectionSearchIndexes.ts

View workflow job for this annotation

GitHub Actions / check-style

Insert `······`
.default('')

Check failure on line 9 in src/tools/mongodb/read/collectionSearchIndexes.ts

View workflow job for this annotation

GitHub Actions / check-style

Replace `.default(''` with `······.default(""`
.optional()

Check failure on line 10 in src/tools/mongodb/read/collectionSearchIndexes.ts

View workflow job for this annotation

GitHub Actions / check-style

Insert `······`
.describe("The name of the index to return information about. Returns all indexes on collection if not provided."),

Check failure on line 11 in src/tools/mongodb/read/collectionSearchIndexes.ts

View workflow job for this annotation

GitHub Actions / check-style

Replace `.describe("The·name·of·the·index·to·return·information·about.·Returns·all·indexes·on·collection·if·not·provided."` with `······.describe(⏎············"The·name·of·the·index·to·return·information·about.·Returns·all·indexes·on·collection·if·not·provided."⏎········`
}

Check failure on line 12 in src/tools/mongodb/read/collectionSearchIndexes.ts

View workflow job for this annotation

GitHub Actions / check-style

Insert `;`

export class CollectionSearchIndexesTool extends MongoDBToolBase {
protected name = "collection-search-indexes";

Check failure on line 15 in src/tools/mongodb/read/collectionSearchIndexes.ts

View workflow job for this annotation

GitHub Actions / check-style

Insert `··`
protected description = "List one or all search indexes on 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);

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)}`,
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