|
| 1 | +import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 2 | +import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js"; |
| 3 | +import * as bson from "bson"; |
| 4 | +import { OperationType, ToolArgs } from "../../tool.js"; |
| 5 | +import z from "zod"; |
| 6 | +import { getSimplifiedSchema } from "mongodb-schema"; |
| 7 | + |
| 8 | +export class MongoDBDDLTool extends MongoDBToolBase { |
| 9 | + protected name = "mongodb-ddl"; |
| 10 | + protected description = |
| 11 | + "List databases, collections, indexes and describe the schema of a collection in a MongoDB database"; |
| 12 | + protected argsShape = { |
| 13 | + command: z.discriminatedUnion("name", [ |
| 14 | + z |
| 15 | + .object({ |
| 16 | + name: z.literal("list-databases"), |
| 17 | + parameters: z.object({}), |
| 18 | + }) |
| 19 | + .describe("List all databases for a MongoDB connection"), |
| 20 | + z |
| 21 | + .object({ |
| 22 | + name: z.literal("list-collections"), |
| 23 | + parameters: z.object({ |
| 24 | + database: DbOperationArgs.database, |
| 25 | + }), |
| 26 | + }) |
| 27 | + .describe("List all collections for a given database"), |
| 28 | + z |
| 29 | + .object({ |
| 30 | + name: z.literal("collection-indexes"), |
| 31 | + parameters: z.object(DbOperationArgs), |
| 32 | + }) |
| 33 | + .describe("Describe the indexes for a collection"), |
| 34 | + z |
| 35 | + .object({ |
| 36 | + name: z.literal("collection-schema"), |
| 37 | + parameters: z.object(DbOperationArgs), |
| 38 | + }) |
| 39 | + .describe("Describe the schema for a collection"), |
| 40 | + ]), |
| 41 | + }; |
| 42 | + protected operationType: OperationType = "read"; |
| 43 | + |
| 44 | + protected async execute({ command }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { |
| 45 | + const provider = await this.ensureConnected(); |
| 46 | + if (command.name === "list-databases") { |
| 47 | + const dbs = (await provider.listDatabases("")).databases as { name: string; sizeOnDisk: bson.Long }[]; |
| 48 | + |
| 49 | + return { |
| 50 | + content: dbs.map((db) => { |
| 51 | + return { |
| 52 | + text: `Name: ${db.name}, Size: ${db.sizeOnDisk.toString()} bytes`, |
| 53 | + type: "text", |
| 54 | + }; |
| 55 | + }), |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + if (command.name === "list-collections") { |
| 60 | + const { database } = command.parameters; |
| 61 | + const collections = await provider.listCollections(database); |
| 62 | + |
| 63 | + if (collections.length === 0) { |
| 64 | + return { |
| 65 | + content: [ |
| 66 | + { |
| 67 | + type: "text", |
| 68 | + text: `No collections found for database "${database}". To create a collection, use the "create-collection" tool.`, |
| 69 | + }, |
| 70 | + ], |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + return { |
| 75 | + content: collections.map((collection) => { |
| 76 | + return { |
| 77 | + text: `Name: "${collection.name}"`, |
| 78 | + type: "text", |
| 79 | + }; |
| 80 | + }), |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + if (command.name === "collection-indexes") { |
| 85 | + const { database, collection } = command.parameters; |
| 86 | + const indexes = await provider.getIndexes(database, collection); |
| 87 | + |
| 88 | + return { |
| 89 | + content: [ |
| 90 | + { |
| 91 | + text: `Found ${indexes.length} indexes in the collection "${collection}":`, |
| 92 | + type: "text", |
| 93 | + }, |
| 94 | + ...(indexes.map((indexDefinition) => { |
| 95 | + return { |
| 96 | + text: `Name "${indexDefinition.name}", definition: ${JSON.stringify(indexDefinition.key)}`, |
| 97 | + type: "text", |
| 98 | + }; |
| 99 | + }) as { text: string; type: "text" }[]), |
| 100 | + ], |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + if (command.name === "collection-schema") { |
| 105 | + const { database, collection } = command.parameters; |
| 106 | + const documents = await provider.find(database, collection, {}, { limit: 5 }).toArray(); |
| 107 | + const schema = await getSimplifiedSchema(documents); |
| 108 | + |
| 109 | + const fieldsCount = Object.entries(schema).length; |
| 110 | + if (fieldsCount === 0) { |
| 111 | + return { |
| 112 | + content: [ |
| 113 | + { |
| 114 | + text: `Could not deduce the schema for "${database}.${collection}". This may be because it doesn't exist or is empty.`, |
| 115 | + type: "text", |
| 116 | + }, |
| 117 | + ], |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + return { |
| 122 | + content: [ |
| 123 | + { |
| 124 | + text: `Found ${fieldsCount} fields in the schema for "${database}.${collection}"`, |
| 125 | + type: "text", |
| 126 | + }, |
| 127 | + { |
| 128 | + text: JSON.stringify(schema), |
| 129 | + type: "text", |
| 130 | + }, |
| 131 | + ], |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + return { |
| 136 | + content: [ |
| 137 | + { |
| 138 | + text: `Unknown command provided to the tool.`, |
| 139 | + type: "text", |
| 140 | + }, |
| 141 | + ], |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + protected handleError( |
| 146 | + error: unknown, |
| 147 | + args: ToolArgs<typeof this.argsShape> |
| 148 | + ): Promise<CallToolResult> | CallToolResult { |
| 149 | + if (args.command.name === "collection-indexes") { |
| 150 | + if (error instanceof Error && "codeName" in error && error.codeName === "NamespaceNotFound") { |
| 151 | + return { |
| 152 | + content: [ |
| 153 | + { |
| 154 | + text: `The indexes for "${args.command.parameters.database}.${args.command.parameters.collection}" cannot be determined because the collection does not exist.`, |
| 155 | + type: "text", |
| 156 | + }, |
| 157 | + ], |
| 158 | + }; |
| 159 | + } |
| 160 | + } |
| 161 | + return super.handleError(error, args); |
| 162 | + } |
| 163 | +} |
0 commit comments