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

mongodb-js / mongodb-mcp-server / 14618487127

23 Apr 2025 12:45PM UTC coverage: 78.42% (+1.7%) from 76.675%
14618487127

Pull #91

github

nirinchev
PR comments
Pull Request #91: chore: add tests for metadata actions

90 of 161 branches covered (55.9%)

Branch coverage included in aggregate %.

22 of 23 new or added lines in 4 files covered. (95.65%)

30 existing lines in 12 files now uncovered.

575 of 687 relevant lines covered (83.7%)

30.41 hits per line

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

93.33
/src/tools/mongodb/metadata/collectionStorageSize.ts
1
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
18✔
3
import { ToolArgs, OperationType } from "../../tool.js";
4

5
export class CollectionStorageSizeTool extends MongoDBToolBase {
18✔
6
    protected name = "collection-storage-size";
18✔
7
    protected description = "Gets the size of the collection";
18✔
8
    protected argsShape = DbOperationArgs;
18✔
9

10
    protected operationType: OperationType = "metadata";
18✔
11

12
    protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
13
        const provider = await this.ensureConnected();
6✔
14
        const [{ value }] = (await provider
5✔
15
            .aggregate(database, collection, [
16
                { $collStats: { storageStats: {} } },
17
                { $group: { _id: null, value: { $sum: "$storageStats.size" } } },
18
            ])
19
            .toArray()) as [{ value: number }];
20

21
        const { units, value: scaledValue } = CollectionStorageSizeTool.getStats(value);
3✔
22

23
        return {
3✔
24
            content: [
25
                {
26
                    text: `The size of "${database}.${collection}" is \`${scaledValue.toFixed(2)} ${units}\``,
27
                    type: "text",
28
                },
29
            ],
30
        };
31
    }
32

33
    protected handleError(
34
        error: unknown,
35
        args: ToolArgs<typeof this.argsShape>
36
    ): Promise<CallToolResult> | CallToolResult {
37
        if (error instanceof Error && "codeName" in error && error.codeName === "NamespaceNotFound") {
3✔
38
            return {
2✔
39
                content: [
40
                    {
41
                        text: `The size of "${args.database}.${args.collection}" cannot be determined because the collection does not exist.`,
42
                        type: "text",
43
                    },
44
                ],
45
            };
46
        }
47

48
        return super.handleError(error, args);
1✔
49
    }
50

51
    private static getStats(value: number): { value: number; units: string } {
52
        const kb = 1024;
3✔
53
        const mb = kb * 1024;
3✔
54
        const gb = mb * 1024;
3✔
55

56
        if (value > gb) {
3!
NEW
57
            return { value: value / gb, units: "GB" };
×
58
        }
59

60
        if (value > mb) {
3✔
61
            return { value: value / mb, units: "MB" };
1✔
62
        }
63
        if (value > kb) {
2✔
64
            return { value: value / kb, units: "KB" };
1✔
65
        }
66
        return { value, units: "bytes" };
1✔
67
    }
68
}
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