Skip to content

Commit f35f0e5

Browse files
authored
chore: merge list-search-indexes into collection-indexes (#654)
1 parent a8d789b commit f35f0e5

File tree

10 files changed

+450
-344
lines changed

10 files changed

+450
-344
lines changed

src/tools/mongodb/delete/dropIndex.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
33
import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
44
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
55
import { type ToolArgs, type OperationType, formatUntrustedData, FeatureFlags } from "../../tool.js";
6-
import { ListSearchIndexesTool } from "../search/listSearchIndexes.js";
76

87
export class DropIndexTool extends MongoDBToolBase {
98
public name = "drop-index";
@@ -60,9 +59,8 @@ export class DropIndexTool extends MongoDBToolBase {
6059
{ database, collection, indexName }: ToolArgs<typeof this.argsShape>
6160
): Promise<CallToolResult> {
6261
await this.ensureSearchIsSupported();
63-
const searchIndexes = await ListSearchIndexesTool.getSearchIndexes(provider, database, collection);
64-
const indexDoesNotExist = !searchIndexes.find((index) => index.name === indexName);
65-
if (indexDoesNotExist) {
62+
const indexes = await provider.getSearchIndexes(database, collection, indexName);
63+
if (indexes.length === 0) {
6664
return {
6765
content: formatUntrustedData(
6866
"Index does not exist in the provided namespace.",

src/tools/mongodb/metadata/collectionIndexes.ts

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
22
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
33
import type { ToolArgs, OperationType } from "../../tool.js";
4-
import { formatUntrustedData } from "../../tool.js";
4+
import { FeatureFlags, formatUntrustedData } from "../../tool.js";
5+
6+
type SearchIndexStatus = {
7+
name: string;
8+
type: string;
9+
status: string;
10+
queryable: boolean;
11+
latestDefinition: Document;
12+
};
13+
14+
type IndexStatus = {
15+
name: string;
16+
key: Document;
17+
};
518

619
export class CollectionIndexesTool extends MongoDBToolBase {
720
public name = "collection-indexes";
@@ -12,12 +25,30 @@ export class CollectionIndexesTool extends MongoDBToolBase {
1225
protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
1326
const provider = await this.ensureConnected();
1427
const indexes = await provider.getIndexes(database, collection);
28+
const indexDefinitions: IndexStatus[] = indexes.map((index) => ({
29+
name: index.name as string,
30+
key: index.key as Document,
31+
}));
32+
33+
const searchIndexDefinitions: SearchIndexStatus[] = [];
34+
if (this.isFeatureFlagEnabled(FeatureFlags.VectorSearch) && (await this.session.isSearchSupported())) {
35+
const searchIndexes = await provider.getSearchIndexes(database, collection);
36+
searchIndexDefinitions.push(...this.extractSearchIndexDetails(searchIndexes));
37+
}
1538

1639
return {
17-
content: formatUntrustedData(
18-
`Found ${indexes.length} indexes in the collection "${collection}":`,
19-
...indexes.map((index) => `Name: "${index.name}", definition: ${JSON.stringify(index.key)}`)
20-
),
40+
content: [
41+
...formatUntrustedData(
42+
`Found ${indexDefinitions.length} indexes in the collection "${collection}":`,
43+
...indexDefinitions.map((i) => JSON.stringify(i))
44+
),
45+
...(searchIndexDefinitions.length > 0
46+
? formatUntrustedData(
47+
`Found ${searchIndexDefinitions.length} search and vector search indexes in the collection "${collection}":`,
48+
...searchIndexDefinitions.map((i) => JSON.stringify(i))
49+
)
50+
: []),
51+
],
2152
};
2253
}
2354

@@ -39,4 +70,20 @@ export class CollectionIndexesTool extends MongoDBToolBase {
3970

4071
return super.handleError(error, args);
4172
}
73+
74+
/**
75+
* Atlas Search index status contains a lot of information that is not relevant for the agent at this stage.
76+
* Like for example, the status on each of the dedicated nodes. We only care about the main status, if it's
77+
* queryable and the index name. We are also picking the index definition as it can be used by the agent to
78+
* understand which fields are available for searching.
79+
**/
80+
protected extractSearchIndexDetails(indexes: Record<string, unknown>[]): SearchIndexStatus[] {
81+
return indexes.map((index) => ({
82+
name: (index["name"] ?? "default") as string,
83+
type: (index["type"] ?? "UNKNOWN") as string,
84+
status: (index["status"] ?? "UNKNOWN") as string,
85+
queryable: (index["queryable"] ?? false) as boolean,
86+
latestDefinition: index["latestDefinition"] as Document,
87+
}));
88+
}
4289
}

src/tools/mongodb/search/listSearchIndexes.ts

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/tools/mongodb/tools.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { ExplainTool } from "./metadata/explain.js";
1919
import { CreateCollectionTool } from "./create/createCollection.js";
2020
import { LogsTool } from "./metadata/logs.js";
2121
import { ExportTool } from "./read/export.js";
22-
import { ListSearchIndexesTool } from "./search/listSearchIndexes.js";
2322
import { DropIndexTool } from "./delete/dropIndex.js";
2423

2524
export const MongoDbTools = [
@@ -45,5 +44,4 @@ export const MongoDbTools = [
4544
CreateCollectionTool,
4645
LogsTool,
4746
ExportTool,
48-
ListSearchIndexesTool,
4947
];

tests/accuracy/collectionIndexes.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,28 @@ describeAccuracyTests([
3737
},
3838
],
3939
},
40+
{
41+
prompt: "how many search indexes do I have in the collection mydb.mycoll?",
42+
expectedToolCalls: [
43+
{
44+
toolName: "collection-indexes",
45+
parameters: {
46+
database: "mydb",
47+
collection: "mycoll",
48+
},
49+
},
50+
],
51+
},
52+
{
53+
prompt: "which vector search indexes do I have in mydb.mycoll?",
54+
expectedToolCalls: [
55+
{
56+
toolName: "collection-indexes",
57+
parameters: {
58+
database: "mydb",
59+
collection: "mycoll",
60+
},
61+
},
62+
],
63+
},
4064
]);

tests/accuracy/listSearchIndexes.test.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/integration/tools/mongodb/create/createIndex.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,10 @@ describeWithMongoDB(
505505
});
506506
},
507507
{
508-
getUserConfig: () => {
509-
return {
510-
...defaultTestConfig,
511-
voyageApiKey: "valid_key",
512-
};
513-
},
508+
getUserConfig: () => ({
509+
...defaultTestConfig,
510+
voyageApiKey: "valid_key",
511+
}),
514512
downloadOptions: {
515513
search: true,
516514
},

0 commit comments

Comments
 (0)