Skip to content

Commit 6fe785f

Browse files
committed
chore: Use a hardcoded constant for the maximum upper bounds instead of config.maxDocumentsPerQuery
1 parent e0324c2 commit 6fe785f

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

src/tools/mongodb/metadata/collectionSchema.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ import { ONE_MB } from "../../../helpers/constants.js";
88
import { collectCursorUntilMaxBytesLimit } from "../../../helpers/collectCursorUntilMaxBytes.js";
99
import { isObjectEmpty } from "../../../helpers/isObjectEmpty.js";
1010

11+
const MAXIMUM_SAMPLE_SIZE_HARD_LIMIT = 50_000;
12+
1113
export class CollectionSchemaTool extends MongoDBToolBase {
1214
public name = "collection-schema";
1315
protected description = "Describe the schema for a collection";
1416
protected argsShape = {
1517
...DbOperationArgs,
1618
sampleSize: z.number().optional().default(50).describe("Number of documents to sample for schema inference"),
17-
responseBytesLimit: z.number().optional().default(ONE_MB).describe(`The maximum number of bytes to return in the response. This value is capped by the server’s configured maxBytesPerQuery and cannot be exceeded.`),
19+
responseBytesLimit: z
20+
.number()
21+
.optional()
22+
.default(ONE_MB)
23+
.describe(
24+
`The maximum number of bytes to return in the response. This value is capped by the server’s configured maxBytesPerQuery and cannot be exceeded.`
25+
),
1826
};
1927

2028
public operationType: OperationType = "metadata";
@@ -24,7 +32,9 @@ export class CollectionSchemaTool extends MongoDBToolBase {
2432
{ signal }: ToolExecutionContext
2533
): Promise<CallToolResult> {
2634
const provider = await this.ensureConnected();
27-
const cursor = provider.aggregate(database, collection, [{ $sample: { size: Math.min(sampleSize, this.config.maxDocumentsPerQuery) } }]);
35+
const cursor = provider.aggregate(database, collection, [
36+
{ $sample: { size: Math.min(sampleSize, MAXIMUM_SAMPLE_SIZE_HARD_LIMIT) } },
37+
]);
2838
const { cappedBy, documents } = await collectCursorUntilMaxBytesLimit({
2939
cursor,
3040
configuredMaxBytesPerQuery: this.config.maxBytesPerQuery,
@@ -45,13 +55,13 @@ export class CollectionSchemaTool extends MongoDBToolBase {
4555
}
4656

4757
const header = `Found ${fieldsCount} fields in the schema for "${database}.${collection}"`;
48-
const cappedWarning = cappedBy !== undefined ? `\nThe schema was inferred from a subset of documents due to the response size limit. (${cappedBy})` : "";
58+
const cappedWarning =
59+
cappedBy !== undefined
60+
? `\nThe schema was inferred from a subset of documents due to the response size limit. (${cappedBy})`
61+
: "";
4962

5063
return {
51-
content: formatUntrustedData(
52-
`${header}${cappedWarning}`,
53-
JSON.stringify(schema)
54-
),
64+
content: formatUntrustedData(`${header}${cappedWarning}`, JSON.stringify(schema)),
5565
};
5666
}
5767
}

tests/integration/common/isObjectEmpty.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe("isObjectEmpty", () => {
55
it("returns true for null", () => {
66
expect(isObjectEmpty(null)).toBe(true);
77
});
8-
8+
99
it("returns true for undefined", () => {
1010
expect(isObjectEmpty(undefined)).toBe(true);
1111
});

tests/integration/tools/mongodb/metadata/collectionSchema.test.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,21 @@ import type { SimplifiedSchema } from "mongodb-schema";
1515
import { describe, expect, it } from "vitest";
1616

1717
describeWithMongoDB("collectionSchema tool", (integration) => {
18-
validateToolMetadata(
19-
integration,
20-
"collection-schema",
21-
"Describe the schema for a collection",
22-
[
23-
...databaseCollectionParameters,
24-
{
25-
name: "sampleSize",
26-
type: "number",
27-
description: "Number of documents to sample for schema inference",
28-
required: false,
29-
},
30-
{
31-
name: "responseBytesLimit",
32-
type: "number",
33-
description: `The maximum number of bytes to return in the response. This value is capped by the server’s configured maxBytesPerQuery and cannot be exceeded.`,
34-
required: false,
35-
}
36-
]
37-
);
18+
validateToolMetadata(integration, "collection-schema", "Describe the schema for a collection", [
19+
...databaseCollectionParameters,
20+
{
21+
name: "sampleSize",
22+
type: "number",
23+
description: "Number of documents to sample for schema inference",
24+
required: false,
25+
},
26+
{
27+
name: "responseBytesLimit",
28+
type: "number",
29+
description: `The maximum number of bytes to return in the response. This value is capped by the server’s configured maxBytesPerQuery and cannot be exceeded.`,
30+
required: false,
31+
},
32+
]);
3833

3934
validateThrowsForInvalidArguments(integration, "collection-schema", databaseCollectionInvalidArgs);
4035

0 commit comments

Comments
 (0)