Skip to content

Commit 0d535a3

Browse files
chore: tests for collection-schema tool
1 parent 6688d83 commit 0d535a3

File tree

2 files changed

+80
-26
lines changed

2 files changed

+80
-26
lines changed
Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
22
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
33
import { ToolArgs, OperationType } from "../../tool.js";
4-
import { getSimplifiedSchema } from "mongodb-schema";
4+
import { getSimplifiedSchema, SimplifiedSchema } from "mongodb-schema";
5+
6+
export function collectionSchemaResponse(
7+
database: string,
8+
collection: string,
9+
schema: SimplifiedSchema
10+
): CallToolResult {
11+
const fieldsCount = Object.entries(schema).length;
12+
if (fieldsCount === 0) {
13+
return {
14+
content: [
15+
{
16+
text: `Could not deduce the schema for "${database}.${collection}". This may be because it doesn't exist or is empty.`,
17+
type: "text",
18+
},
19+
],
20+
};
21+
}
22+
23+
return {
24+
content: [
25+
{
26+
text: `Found ${fieldsCount} fields in the schema for "${database}.${collection}"`,
27+
type: "text",
28+
},
29+
{
30+
text: JSON.stringify(schema),
31+
type: "text",
32+
},
33+
],
34+
};
35+
}
536

637
export class CollectionSchemaTool extends MongoDBToolBase {
738
protected name = "collection-schema";
@@ -14,30 +45,6 @@ export class CollectionSchemaTool extends MongoDBToolBase {
1445
const provider = await this.ensureConnected();
1546
const documents = await provider.find(database, collection, {}, { limit: 5 }).toArray();
1647
const schema = await getSimplifiedSchema(documents);
17-
18-
const fieldsCount = Object.entries(schema).length;
19-
if (fieldsCount === 0) {
20-
return {
21-
content: [
22-
{
23-
text: `Could not deduce the schema for "${database}.${collection}". This may be because it doesn't exist or is empty.`,
24-
type: "text",
25-
},
26-
],
27-
};
28-
}
29-
30-
return {
31-
content: [
32-
{
33-
text: `Found ${fieldsCount} fields in the schema for "${database}.${collection}"`,
34-
type: "text",
35-
},
36-
{
37-
text: JSON.stringify(schema),
38-
type: "text",
39-
},
40-
],
41-
};
48+
return collectionSchemaResponse(database, collection, schema);
4249
}
4350
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 { collectionSchemaResponse } from "../../src/tools/mongodb/metadata/collectionSchema.js";
5+
import { getSimplifiedSchema } from "mongodb-schema";
6+
7+
function callsCollectionSchema(prompt: string): AccuracyTestConfig {
8+
return {
9+
injectConnectedAssumption: true,
10+
prompt: prompt,
11+
mockedTools: {
12+
"collection-schema": async function collectionSchema() {
13+
return collectionSchemaResponse(
14+
"db1",
15+
"coll1",
16+
await getSimplifiedSchema([
17+
{
18+
name: "Sample name1",
19+
dob: "28.11.2001",
20+
location: "NY",
21+
},
22+
{
23+
name: "Sample name1",
24+
dob: "28.11.2001",
25+
location: "NY",
26+
title: "Dr.",
27+
},
28+
])
29+
);
30+
},
31+
},
32+
expectedToolCalls: [
33+
{
34+
toolName: "collection-schema",
35+
parameters: {
36+
database: "db1",
37+
collection: "coll1",
38+
},
39+
},
40+
],
41+
};
42+
}
43+
44+
describeAccuracyTests("collection-schema", getAvailableModels(), [
45+
callsCollectionSchema("Is there a title field in 'db1.coll1' namespace?"),
46+
callsCollectionSchema("What is the type of value stored in title field in coll1 collection in db1 database?"),
47+
]);

0 commit comments

Comments
 (0)