• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mongodb-js / mongodb-mcp-server / 17914435472

22 Sep 2025 11:54AM UTC coverage: 82.472% (+0.1%) from 82.33%
17914435472

Pull #580

github

web-flow
Merge 8f3189816 into 9f4c48b78
Pull Request #580: fix(schema): Use sample instead of find for schema sampling

1090 of 1433 branches covered (76.06%)

Branch coverage included in aggregate %.

47 of 48 new or added lines in 2 files covered. (97.92%)

5276 of 6286 relevant lines covered (83.93%)

64.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.97
/src/tools/mongodb/metadata/collectionSchema.ts
1
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
2✔
3
import type { ToolArgs, OperationType, ToolExecutionContext } from "../../tool.js";
4
import { formatUntrustedData } from "../../tool.js";
2✔
5
import { getSimplifiedSchema } from "mongodb-schema";
2✔
6
import z from "zod";
2✔
7
import { ONE_MB } from "../../../helpers/constants.js";
2✔
8
import { collectCursorUntilMaxBytesLimit } from "../../../helpers/collectCursorUntilMaxBytes.js";
2✔
9
import { isObjectEmpty } from "../../../helpers/isObjectEmpty.js";
2✔
10

11
const MAXIMUM_SAMPLE_SIZE_HARD_LIMIT = 50_000;
2✔
12

13
export class CollectionSchemaTool extends MongoDBToolBase {
2✔
14
    public name = "collection-schema";
76✔
15
    protected description = "Describe the schema for a collection";
76✔
16
    protected argsShape = {
76✔
17
        ...DbOperationArgs,
76✔
18
        sampleSize: z.number().optional().default(50).describe("Number of documents to sample for schema inference"),
76✔
19
        responseBytesLimit: z
76✔
20
            .number()
76✔
21
            .optional()
76✔
22
            .default(ONE_MB)
76✔
23
            .describe(
76✔
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.`
76✔
25
            ),
76✔
26
    };
76✔
27

28
    public operationType: OperationType = "metadata";
76✔
29

30
    protected async execute(
2✔
31
        { database, collection, sampleSize, responseBytesLimit }: ToolArgs<typeof this.argsShape>,
6✔
32
        { signal }: ToolExecutionContext
6✔
33
    ): Promise<CallToolResult> {
6✔
34
        const provider = await this.ensureConnected();
6✔
35
        const cursor = provider.aggregate(database, collection, [
5✔
36
            { $sample: { size: Math.min(sampleSize, MAXIMUM_SAMPLE_SIZE_HARD_LIMIT) } },
5✔
37
        ]);
5✔
38
        const { cappedBy, documents } = await collectCursorUntilMaxBytesLimit({
5✔
39
            cursor,
5✔
40
            configuredMaxBytesPerQuery: this.config.maxBytesPerQuery,
5✔
41
            toolResponseBytesLimit: responseBytesLimit,
5✔
42
            abortSignal: signal,
5✔
43
        });
5✔
44
        const schema = await getSimplifiedSchema(documents);
5✔
45

46
        if (isObjectEmpty(schema)) {
6✔
47
            return {
2✔
48
                content: [
2✔
49
                    {
2✔
50
                        text: `Could not deduce the schema for "${database}.${collection}". This may be because it doesn't exist or is empty.`,
2✔
51
                        type: "text",
2✔
52
                    },
2✔
53
                ],
2✔
54
            };
2✔
55
        }
2✔
56

57
        const fieldsCount = Object.keys(schema).length;
3✔
58
        const header = `Found ${fieldsCount} fields in the schema for "${database}.${collection}"`;
3✔
59
        const cappedWarning =
3✔
60
            cappedBy !== undefined
3!
NEW
61
                ? `\nThe schema was inferred from a subset of documents due to the response size limit. (${cappedBy})`
✔
62
                : "";
3✔
63

64
        return {
6✔
65
            content: formatUntrustedData(`${header}${cappedWarning}`, JSON.stringify(schema)),
6✔
66
        };
6✔
67
    }
6✔
68
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc