|
| 1 | +import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 2 | +import { DbOperationArgs, DbOperationType, MongoDBToolBase } from "../mongodbTool.js"; |
| 3 | +import { ToolArgs } from "../../tool.js"; |
| 4 | +import { z } from "zod"; |
| 5 | +import { ExplainVerbosity, Document } from "mongodb"; |
| 6 | +import { AggregateArgs } from "../read/aggregate.js"; |
| 7 | +import { FindArgs } from "../read/find.js"; |
| 8 | +import { CountArgs } from "../read/count.js"; |
| 9 | + |
| 10 | +export class ExplainTool extends MongoDBToolBase { |
| 11 | + protected name = "explain"; |
| 12 | + protected description = |
| 13 | + "Returns statistics describing the execution of the winning plan chosen by the query optimizer for the evaluated method"; |
| 14 | + |
| 15 | + protected argsShape = { |
| 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: DbOperationType = "metadata"; |
| 38 | + |
| 39 | + static readonly defaultVerbosity = ExplainVerbosity.queryPlanner; |
| 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 | +} |
0 commit comments