Skip to content
Open
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: 52 additions & 9 deletions src/tools/mongodb/metadata/collectionIndexes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
import type { ToolArgs, OperationType } from "../../tool.js";
import { formatUntrustedData } from "../../tool.js";
import { FeatureFlags, formatUntrustedData } from "../../tool.js";

type SearchIndexStatus = {
name: string;
type: string;
status: string;
queryable: boolean;
latestDefinition: Document;
};

type IndexStatus = {
name: string;
key: Document;
};

export class CollectionIndexesTool extends MongoDBToolBase {
public name = "collection-indexes";
Expand All @@ -12,16 +25,30 @@ export class CollectionIndexesTool extends MongoDBToolBase {
protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
const provider = await this.ensureConnected();
const indexes = await provider.getIndexes(database, collection);
const indexDefinitions: IndexStatus[] = indexes.map((index) => ({
name: index.name as string,
key: index.key as Document,
}));

const searchIndexDefinitions: SearchIndexStatus[] = [];
if (this.isFeatureFlagEnabled(FeatureFlags.VectorSearch) && (await this.session.isSearchSupported())) {
const searchIndexes = await provider.getSearchIndexes(database, collection);
searchIndexDefinitions.push(...this.extractSearchIndexDetails(searchIndexes));
}

return {
content: formatUntrustedData(
`Found ${indexes.length} indexes in the collection "${collection}":`,
indexes.length > 0
? indexes
.map((index) => `Name: "${index.name}", definition: ${JSON.stringify(index.key)}`)
.join("\n")
: undefined
),
content: [
...formatUntrustedData(
`Found ${indexDefinitions.length} indexes in the collection "${collection}":`,
indexDefinitions.length > 0 ? JSON.stringify(indexDefinitions, null, 2) : undefined
),
...(searchIndexDefinitions.length > 0
? formatUntrustedData(
`Found ${searchIndexDefinitions.length} search and vector search indexes in the collection "${collection}":`,
JSON.stringify(searchIndexDefinitions, null, 2)
)
: []),
Comment on lines +41 to +50
Copy link

Copilot AI Oct 15, 2025

Choose a reason for hiding this comment

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

The previous list-search-indexes tool serialized index data with EJSON to preserve extended BSON types; switching to JSON.stringify may lose fidelity (e.g., ObjectId, Date) in future index definitions. Recommend importing EJSON from bson and using EJSON.stringify(..., { relaxed: false }) for both sets to maintain consistency and prevent data loss.

Copilot uses AI. Check for mistakes.

],
};
}

Expand All @@ -43,4 +70,20 @@ export class CollectionIndexesTool extends MongoDBToolBase {

return super.handleError(error, args);
}

/**
* Atlas Search index status contains a lot of information that is not relevant for the agent at this stage.
* Like for example, the status on each of the dedicated nodes. We only care about the main status, if it's
* queryable and the index name. We are also picking the index definition as it can be used by the agent to
* understand which fields are available for searching.
**/
protected extractSearchIndexDetails(indexes: Record<string, unknown>[]): SearchIndexStatus[] {
return indexes.map((index) => ({
name: (index["name"] ?? "default") as string,
type: (index["type"] ?? "UNKNOWN") as string,
status: (index["status"] ?? "UNKNOWN") as string,
queryable: (index["queryable"] ?? false) as boolean,
latestDefinition: index["latestDefinition"] as Document,
}));
}
}
81 changes: 0 additions & 81 deletions src/tools/mongodb/search/listSearchIndexes.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/tools/mongodb/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { ExplainTool } from "./metadata/explain.js";
import { CreateCollectionTool } from "./create/createCollection.js";
import { LogsTool } from "./metadata/logs.js";
import { ExportTool } from "./read/export.js";
import { ListSearchIndexesTool } from "./search/listSearchIndexes.js";
import { DropIndexTool } from "./delete/dropIndex.js";

export const MongoDbTools = [
Expand All @@ -45,5 +44,4 @@ export const MongoDbTools = [
CreateCollectionTool,
LogsTool,
ExportTool,
ListSearchIndexesTool,
];
24 changes: 24 additions & 0 deletions tests/accuracy/collectionIndexes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,28 @@ describeAccuracyTests([
},
],
},
{
prompt: "how many search indexes do I have in the collection mydb.mycoll?",
expectedToolCalls: [
{
toolName: "collection-indexes",
parameters: {
database: "mydb",
collection: "mycoll",
},
},
],
},
{
prompt: "which vector search indexes do I have in mydb.mycoll?",
expectedToolCalls: [
{
toolName: "collection-indexes",
parameters: {
database: "mydb",
collection: "mycoll",
},
},
],
},
]);
28 changes: 0 additions & 28 deletions tests/accuracy/listSearchIndexes.test.ts

This file was deleted.

10 changes: 4 additions & 6 deletions tests/integration/tools/mongodb/create/createIndex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,10 @@ describeWithMongoDB(
});
},
{
getUserConfig: () => {
return {
...defaultTestConfig,
voyageApiKey: "valid_key",
};
},
getUserConfig: () => ({
...defaultTestConfig,
voyageApiKey: "valid_key",
}),
downloadOptions: {
search: true,
},
Expand Down
Loading
Loading