|
| 1 | +import { z } from "zod"; |
| 2 | +import { CallToolResult, McpError } from "@modelcontextprotocol/sdk/types.js"; |
| 3 | +import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver"; |
| 4 | +import { MongoDBToolBase } from "./mongodbTool.js"; |
| 5 | +import { ToolArgs } from "../tool"; |
| 6 | + |
| 7 | +const argsShape = { |
| 8 | + connectionStringOrClusterName: z |
| 9 | + .string() |
| 10 | + .optional() |
| 11 | + .describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format) or cluster name"), |
| 12 | +}; |
| 13 | + |
| 14 | +export class ConnectTool extends MongoDBToolBase<typeof argsShape> { |
| 15 | + protected name = "connect"; |
| 16 | + protected description = "Connect to a MongoDB instance"; |
| 17 | + protected argsShape = argsShape; |
| 18 | + |
| 19 | + protected async execute({ connectionStringOrClusterName }: ToolArgs<typeof argsShape>): Promise<CallToolResult> { |
| 20 | + try { |
| 21 | + if (!connectionStringOrClusterName) { |
| 22 | + // TODO: try reconnecting to the default connection |
| 23 | + return { |
| 24 | + content: [ |
| 25 | + { type: "text", text: "No connection details provided." }, |
| 26 | + { type: "text", text: "Please provide either a connection string or a cluster name" }, |
| 27 | + { |
| 28 | + type: "text", |
| 29 | + text: "Alternatively, you can use the default deployment at mongodb://localhost:27017", |
| 30 | + }, |
| 31 | + ], |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + let connectionString: string; |
| 36 | + |
| 37 | + if (typeof connectionStringOrClusterName === "string") { |
| 38 | + if ( |
| 39 | + connectionStringOrClusterName.startsWith("mongodb://") || |
| 40 | + connectionStringOrClusterName.startsWith("mongodb+srv://") |
| 41 | + ) { |
| 42 | + connectionString = connectionStringOrClusterName; |
| 43 | + } else { |
| 44 | + // TODO: |
| 45 | + return { |
| 46 | + content: [ |
| 47 | + { |
| 48 | + type: "text", |
| 49 | + text: `Connecting via cluster name not supported yet. Please provide a connection string.`, |
| 50 | + }, |
| 51 | + ], |
| 52 | + }; |
| 53 | + } |
| 54 | + } else { |
| 55 | + throw new McpError(2, "Invalid connection options"); |
| 56 | + } |
| 57 | + |
| 58 | + await this.connect(connectionString); |
| 59 | + |
| 60 | + return { |
| 61 | + content: [{ type: "text", text: `Successfully connected to ${connectionString}.` }], |
| 62 | + }; |
| 63 | + } catch (error) { |
| 64 | + return { |
| 65 | + content: [{ type: "text", text: "Failed to get cluster connection string" }], |
| 66 | + isError: true, |
| 67 | + }; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private async connect(connectionString: string): Promise<void> { |
| 72 | + const provider = await NodeDriverServiceProvider.connect(connectionString, { |
| 73 | + productDocsLink: "https://docs.mongodb.com/todo-mcp", |
| 74 | + productName: "MongoDB MCP", |
| 75 | + }); |
| 76 | + |
| 77 | + this.mongodbState.serviceProvider = provider; |
| 78 | + } |
| 79 | +} |
0 commit comments