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

mongodb-js / mongodb-mcp-server / 14640623132

24 Apr 2025 11:35AM UTC coverage: 77.5% (+1.7%) from 75.815%
14640623132

Pull #102

github

nirinchev
post-rebase fixes
Pull Request #102: chore: add tests for read operations

107 of 191 branches covered (56.02%)

Branch coverage included in aggregate %.

6 of 6 new or added lines in 3 files covered. (100.0%)

637 of 769 relevant lines covered (82.83%)

39.1 hits per line

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

38.71
/src/tools/mongodb/metadata/explain.ts
1
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
27✔
3
import { ToolArgs, OperationType } from "../../tool.js";
4
import { z } from "zod";
27✔
5
import { ExplainVerbosity, Document } from "mongodb";
27✔
6
import { AggregateArgs } from "../read/aggregate.js";
27✔
7
import { FindArgs } from "../read/find.js";
27✔
8
import { CountArgs } from "../read/count.js";
27✔
9

10
export class ExplainTool extends MongoDBToolBase {
27✔
11
    protected name = "explain";
23✔
12
    protected description =
13
        "Returns statistics describing the execution of the winning plan chosen by the query optimizer for the evaluated method";
23✔
14

15
    protected argsShape = {
23✔
16
        ...DbOperationArgs,
17
        method: z
18
            .array(
19
                z.union([
20
                    z.object({
21
                        name: z.literal("aggregate"),
22
                        arguments: z.object(AggregateArgs),
23
                    }),
24
                    z.object({
25
                        name: z.literal("find"),
26
                        arguments: z.object(FindArgs),
27
                    }),
28
                    z.object({
29
                        name: z.literal("count"),
30
                        arguments: z.object(CountArgs),
31
                    }),
32
                ])
33
            )
34
            .describe("The method and its arguments to run"),
35
    };
36

37
    protected operationType: OperationType = "metadata";
23✔
38

39
    static readonly defaultVerbosity = ExplainVerbosity.queryPlanner;
27✔
40

41
    protected async execute({
42
        database,
43
        collection,
44
        method: methods,
45
    }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
46
        const provider = await this.ensureConnected();
×
47
        const method = methods[0];
×
48

49
        if (!method) {
×
50
            throw new Error("No method provided");
×
51
        }
52

53
        let result: Document;
54
        switch (method.name) {
×
55
            case "aggregate": {
56
                const { pipeline } = method.arguments;
×
57
                result = await provider.aggregate(database, collection, pipeline).explain(ExplainTool.defaultVerbosity);
×
58
                break;
×
59
            }
60
            case "find": {
61
                const { filter, ...rest } = method.arguments;
×
62
                result = await provider
×
63
                    .find(database, collection, filter as Document, { ...rest })
64
                    .explain(ExplainTool.defaultVerbosity);
65
                break;
×
66
            }
67
            case "count": {
68
                const { query } = method.arguments;
×
69
                // This helper doesn't have explain() command but does have the argument explain
70
                result = (await provider.count(database, collection, query, {
×
71
                    explain: ExplainTool.defaultVerbosity,
72
                })) as unknown as Document;
73
                break;
×
74
            }
75
        }
76

77
        return {
×
78
            content: [
79
                {
80
                    text: `Here is some information about the winning plan chosen by the query optimizer for running the given \`${method.name}\` operation in \`${database}.${collection}\`. This information can be used to understand how the query was executed and to optimize the query performance.`,
81
                    type: "text",
82
                },
83
                {
84
                    text: JSON.stringify(result),
85
                    type: "text",
86
                },
87
            ],
88
        };
89
    }
90
}
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