|
| 1 | +import { z } from "zod"; |
| 2 | +import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 3 | +import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js"; |
| 4 | +import { ToolArgs, OperationType } from "../../tool.js"; |
| 5 | +import { EJSON } from "bson"; |
| 6 | +import { SortDirection } from "mongodb"; |
| 7 | +import { ErrorCodes, MongoDBError } from "../../../common/errors.js"; |
| 8 | +import { ExplainTool } from "../metadata/explain.js"; |
| 9 | + |
| 10 | +export const FindToolArgs = { |
| 11 | + filter: z |
| 12 | + .record(z.string(), z.unknown()) |
| 13 | + .optional() |
| 14 | + .describe("The query filter, matching the syntax of the query argument of db.collection.find()"), |
| 15 | + projection: z |
| 16 | + .record(z.string(), z.unknown()) |
| 17 | + .optional() |
| 18 | + .describe("The projection, matching the syntax of the projection argument of db.collection.find(). Only used when filter is provided."), |
| 19 | + limit: z.number().optional().default(10).describe("The maximum number of documents to return. Only used when filter is provided."), |
| 20 | + sort: z |
| 21 | + .record(z.string(), z.custom<SortDirection>()) |
| 22 | + .optional() |
| 23 | + .describe("A document, describing the sort order, matching the syntax of the sort argument of cursor.sort(). Only used when filter is provided."), |
| 24 | + pipeline: z.array(z.record(z.string(), z.unknown())).optional().describe("An array of aggregation stages to execute, matching the syntax of the aggregation pipeline in db.collection.aggregate(). It's ignored in filter is provided."), |
| 25 | + explain: z.boolean().optional().default(false).describe("If true, returns the explain plan of the query or aggregation pipeline."), |
| 26 | + count: z.boolean().optional().default(false).describe("If true, returns the count of the documents matching the filter. Only used when filter is provided. For aggregation pipelines, use the $count stage in the pipeline instead."), |
| 27 | +}; |
| 28 | + |
| 29 | +export class MongoDbFindTool extends MongoDBToolBase { |
| 30 | + public name = "mongodb-find"; |
| 31 | + protected description = ` |
| 32 | + This tool retrieves documents, the explain plan or the count of documents from MongoDB collections based on an specific filter or an aggregation pipeline, excluding $out and $merge stages. |
| 33 | + `; |
| 34 | + |
| 35 | + protected argsShape = { |
| 36 | + ...DbOperationArgs, |
| 37 | + ...FindToolArgs, |
| 38 | + }; |
| 39 | + public operationType: OperationType = "read"; |
| 40 | + |
| 41 | + protected async execute({ |
| 42 | + database, |
| 43 | + collection, |
| 44 | + filter, |
| 45 | + projection, |
| 46 | + limit, |
| 47 | + sort, |
| 48 | + pipeline, |
| 49 | + explain, |
| 50 | + count, |
| 51 | + }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { |
| 52 | + const provider = await this.ensureConnected(); |
| 53 | + |
| 54 | + if (filter) { |
| 55 | + if (count && !explain) { |
| 56 | + const count = await provider.count(database, collection, filter); |
| 57 | + |
| 58 | + return { |
| 59 | + content: [ |
| 60 | + { |
| 61 | + text: `Found ${count} documents in the collection "${collection}"`, |
| 62 | + type: "text", |
| 63 | + }, |
| 64 | + ], |
| 65 | + }; |
| 66 | + } else if (count) { |
| 67 | + const result = await provider.runCommandWithCheck(database, { |
| 68 | + explain: { |
| 69 | + count: collection, |
| 70 | + query: filter, |
| 71 | + }, |
| 72 | + verbosity: ExplainTool.defaultVerbosity, |
| 73 | + }); |
| 74 | + |
| 75 | + return { |
| 76 | + content: [ |
| 77 | + { |
| 78 | + text: `Here is some information about the winning plan chosen by the query optimizer for running the given \`count\` operation in "${database}.${collection}". This information can be used to understand how the query was executed and to optimize the query performance.`, |
| 79 | + type: "text", |
| 80 | + }, |
| 81 | + { |
| 82 | + text: JSON.stringify(result), |
| 83 | + type: "text", |
| 84 | + }, |
| 85 | + ], |
| 86 | + }; |
| 87 | + } else if (explain) { |
| 88 | + const result = await provider |
| 89 | + .find(database, collection, filter, { projection, limit, sort }) |
| 90 | + .explain(ExplainTool.defaultVerbosity); |
| 91 | + |
| 92 | + return { |
| 93 | + content: [ |
| 94 | + { |
| 95 | + text: `Here is some information about the winning plan chosen by the query optimizer for running the given \`find\` operation in "${database}.${collection}". This information can be used to understand how the query was executed and to optimize the query performance.`, |
| 96 | + type: "text", |
| 97 | + }, |
| 98 | + { |
| 99 | + text: JSON.stringify(result), |
| 100 | + type: "text", |
| 101 | + }, |
| 102 | + ], |
| 103 | + }; |
| 104 | + } else { |
| 105 | + const documents = await provider.find(database, collection, filter, { projection, limit, sort }).toArray(); |
| 106 | + |
| 107 | + const content: Array<{ text: string; type: "text" }> = [ |
| 108 | + { |
| 109 | + text: `Found ${documents.length} documents in the collection "${collection}":`, |
| 110 | + type: "text", |
| 111 | + }, |
| 112 | + ...documents.map((doc) => { |
| 113 | + return { |
| 114 | + text: EJSON.stringify(doc), |
| 115 | + type: "text", |
| 116 | + } as { text: string; type: "text" }; |
| 117 | + }), |
| 118 | + ]; |
| 119 | + |
| 120 | + return { content }; |
| 121 | + } |
| 122 | + } else if (pipeline) { |
| 123 | + if (explain) { |
| 124 | + const result = await provider |
| 125 | + .aggregate( |
| 126 | + database, |
| 127 | + collection, |
| 128 | + pipeline, |
| 129 | + {}, |
| 130 | + { |
| 131 | + writeConcern: undefined, |
| 132 | + } |
| 133 | + ) |
| 134 | + .explain(ExplainTool.defaultVerbosity); |
| 135 | + |
| 136 | + return { |
| 137 | + content: [ |
| 138 | + { |
| 139 | + text: `Here is some information about the winning plan chosen by the query optimizer for running the given \`aggregate\` operation in "${database}.${collection}". This information can be used to understand how the query was executed and to optimize the query performance.`, |
| 140 | + type: "text", |
| 141 | + }, |
| 142 | + { |
| 143 | + text: JSON.stringify(result), |
| 144 | + type: "text", |
| 145 | + }, |
| 146 | + ], |
| 147 | + }; |
| 148 | + } else { |
| 149 | + const documents = await provider.aggregate(database, collection, pipeline).toArray(); |
| 150 | + |
| 151 | + const content: Array<{ text: string; type: "text" }> = [ |
| 152 | + { |
| 153 | + text: `Found ${documents.length} documents in the collection "${collection}":`, |
| 154 | + type: "text", |
| 155 | + }, |
| 156 | + ...documents.map((doc) => { |
| 157 | + return { |
| 158 | + text: EJSON.stringify(doc), |
| 159 | + type: "text", |
| 160 | + } as { text: string; type: "text" }; |
| 161 | + }), |
| 162 | + ]; |
| 163 | + |
| 164 | + return { |
| 165 | + content, |
| 166 | + }; |
| 167 | + } |
| 168 | + } else { |
| 169 | + const documents = await provider.find(database, collection, {}, { projection, limit, sort }).toArray(); |
| 170 | + |
| 171 | + const content: Array<{ text: string; type: "text" }> = [ |
| 172 | + { |
| 173 | + text: `Found ${documents.length} documents in the collection "${collection}":`, |
| 174 | + type: "text", |
| 175 | + }, |
| 176 | + ...documents.map((doc) => { |
| 177 | + return { |
| 178 | + text: EJSON.stringify(doc), |
| 179 | + type: "text", |
| 180 | + } as { text: string; type: "text" }; |
| 181 | + }), |
| 182 | + ]; |
| 183 | + |
| 184 | + return { content }; |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments