Skip to content

Commit c55ddde

Browse files
chore: tests for collection-indexes tool
1 parent 3d2c033 commit c55ddde

File tree

2 files changed

+93
-23
lines changed

2 files changed

+93
-23
lines changed

src/tools/mongodb/read/collectionIndexes.ts

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,44 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
22
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
33
import { ToolArgs, OperationType } from "../../tool.js";
44

5+
export function collectionIndexesResponse({
6+
database,
7+
collection,
8+
indexes = [],
9+
namespaceNotFound,
10+
}: {
11+
database: string;
12+
collection: string;
13+
indexes?: { name: string; key: string }[];
14+
namespaceNotFound?: boolean;
15+
}): CallToolResult {
16+
if (namespaceNotFound) {
17+
return {
18+
content: [
19+
{
20+
text: `The indexes for "${database}.${collection}" cannot be determined because the collection does not exist.`,
21+
type: "text",
22+
},
23+
],
24+
};
25+
}
26+
27+
return {
28+
content: [
29+
{
30+
text: `Found ${indexes.length} indexes in the collection "${collection}":`,
31+
type: "text",
32+
},
33+
...(indexes.map((indexDefinition) => {
34+
return {
35+
text: `Name "${indexDefinition.name}", definition: ${JSON.stringify(indexDefinition.key)}`,
36+
type: "text",
37+
};
38+
}) as { text: string; type: "text" }[]),
39+
],
40+
};
41+
}
42+
543
export class CollectionIndexesTool extends MongoDBToolBase {
644
protected name = "collection-indexes";
745
protected description = "Describe the indexes for a collection";
@@ -11,36 +49,26 @@ export class CollectionIndexesTool extends MongoDBToolBase {
1149
protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
1250
const provider = await this.ensureConnected();
1351
const indexes = await provider.getIndexes(database, collection);
14-
15-
return {
16-
content: [
17-
{
18-
text: `Found ${indexes.length} indexes in the collection "${collection}":`,
19-
type: "text",
20-
},
21-
...(indexes.map((indexDefinition) => {
22-
return {
23-
text: `Name "${indexDefinition.name}", definition: ${JSON.stringify(indexDefinition.key)}`,
24-
type: "text",
25-
};
26-
}) as { text: string; type: "text" }[]),
27-
],
28-
};
52+
return collectionIndexesResponse({
53+
database,
54+
collection,
55+
indexes: indexes.map((index) => ({
56+
name: `${index.name}`,
57+
key: JSON.stringify(index.key),
58+
})),
59+
});
2960
}
3061

3162
protected handleError(
3263
error: unknown,
3364
args: ToolArgs<typeof this.argsShape>
3465
): Promise<CallToolResult> | CallToolResult {
3566
if (error instanceof Error && "codeName" in error && error.codeName === "NamespaceNotFound") {
36-
return {
37-
content: [
38-
{
39-
text: `The indexes for "${args.database}.${args.collection}" cannot be determined because the collection does not exist.`,
40-
type: "text",
41-
},
42-
],
43-
};
67+
return collectionIndexesResponse({
68+
database: args.database,
69+
collection: args.collection,
70+
namespaceNotFound: true,
71+
});
4472
}
4573

4674
return super.handleError(error, args);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describeAccuracyTests } from "./sdk/describe-accuracy-tests.js";
2+
import { getAvailableModels } from "./sdk/models.js";
3+
import { AccuracyTestConfig } from "./sdk/describe-accuracy-tests.js";
4+
import { collectionIndexesResponse } from "../../src/tools/mongodb/read/collectionIndexes.js";
5+
6+
function callsCollectionIndexes(prompt: string): AccuracyTestConfig {
7+
return {
8+
injectConnectedAssumption: true,
9+
prompt: prompt,
10+
mockedTools: {
11+
"collection-indexes": function collectionIndexes() {
12+
return collectionIndexesResponse({
13+
database: "db1",
14+
collection: "coll1",
15+
indexes: [
16+
{
17+
name: "year",
18+
key: JSON.stringify({ _id: 1 }),
19+
},
20+
],
21+
});
22+
},
23+
},
24+
expectedToolCalls: [
25+
{
26+
toolName: "collection-indexes",
27+
parameters: {
28+
database: "db1",
29+
collection: "coll1",
30+
},
31+
},
32+
],
33+
};
34+
}
35+
36+
describeAccuracyTests("collection-indexes", getAvailableModels(), [
37+
callsCollectionIndexes("How many indexes do I have in 'db1.coll1' namespace?"),
38+
callsCollectionIndexes("List all the indexes in coll1 collection in db1 database"),
39+
callsCollectionIndexes(
40+
`Will this query: ${JSON.stringify({ year: 1994 })} on the namespace 'db1.coll1' be a collection scan?`
41+
),
42+
]);

0 commit comments

Comments
 (0)