|
1 | | -import { z } from "zod"; |
| 1 | +import { AnyZodObject, z, ZodRawShape } from "zod"; |
2 | 2 | import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
3 | 3 | import { MongoDBToolBase } from "../mongodbTool.js"; |
4 | 4 | import { ToolArgs, OperationType } from "../../tool.js"; |
5 | | -import { MongoError as DriverError } from "mongodb"; |
| 5 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 6 | +import assert from "assert"; |
| 7 | +import { UserConfig } from "../../../config.js"; |
| 8 | +import { Telemetry } from "../../../telemetry/telemetry.js"; |
| 9 | +import { Session } from "../../../session.js"; |
| 10 | + |
| 11 | +const disconnectedSchema = z |
| 12 | + .object({ |
| 13 | + connectionString: z.string().describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format)"), |
| 14 | + }) |
| 15 | + .describe("Options for connecting to MongoDB."); |
| 16 | + |
| 17 | +const connectedSchema = z |
| 18 | + .object({ |
| 19 | + connectionString: z |
| 20 | + .string() |
| 21 | + .optional() |
| 22 | + .describe("MongoDB connection string to switch to (in the mongodb:// or mongodb+srv:// format)"), |
| 23 | + }) |
| 24 | + .describe( |
| 25 | + "Options for switching the current MongoDB connection. If a connection string is not provided, the connection string from the config will be used." |
| 26 | + ); |
| 27 | + |
| 28 | +const connectedName = "switch-connection" as const; |
| 29 | +const disconnectedName = "connect" as const; |
| 30 | + |
| 31 | +const connectedDescription = |
| 32 | + "Switch to a different MongoDB connection. If the user has configured a connection string or has previously called the connect tool, a connection is already established and there's no need to call this tool unless the user has explicitly requested to switch to a new instance."; |
| 33 | +const disconnectedDescription = "Connect to a MongoDB instance"; |
6 | 34 |
|
7 | 35 | export class ConnectTool extends MongoDBToolBase { |
8 | | - protected name = "connect"; |
9 | | - protected description = "Connect to a MongoDB instance"; |
| 36 | + protected name: typeof connectedName | typeof disconnectedName = disconnectedName; |
| 37 | + protected description: typeof connectedDescription | typeof disconnectedDescription = disconnectedDescription; |
| 38 | + |
| 39 | + // Here the default is empty just to trigger registration, but we're going to override it with the correct |
| 40 | + // schema in the register method. |
10 | 41 | protected argsShape = { |
11 | | - options: z |
12 | | - .array( |
13 | | - z |
14 | | - .union([ |
15 | | - z.object({ |
16 | | - connectionString: z |
17 | | - .string() |
18 | | - .describe("MongoDB connection string (in the mongodb:// or mongodb+srv:// format)"), |
19 | | - }), |
20 | | - z.object({ |
21 | | - clusterName: z.string().describe("MongoDB cluster name"), |
22 | | - }), |
23 | | - ]) |
24 | | - .optional() |
25 | | - ) |
26 | | - .optional() |
27 | | - .describe( |
28 | | - "Options for connecting to MongoDB. If not provided, the connection string from the config://connection-string resource will be used. If the user hasn't specified Atlas cluster name or a connection string explicitly and the `config://connection-string` resource is present, always invoke this with no arguments." |
29 | | - ), |
| 42 | + connectionString: z.string().optional(), |
30 | 43 | }; |
31 | 44 |
|
32 | 45 | protected operationType: OperationType = "metadata"; |
33 | 46 |
|
34 | | - protected async execute({ options: optionsArr }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { |
35 | | - const options = optionsArr?.[0]; |
36 | | - let connectionString: string; |
37 | | - if (!options && !this.config.connectionString) { |
38 | | - return { |
39 | | - content: [ |
40 | | - { type: "text", text: "No connection details provided." }, |
41 | | - { type: "text", text: "Please provide either a connection string or a cluster name" }, |
42 | | - ], |
43 | | - }; |
44 | | - } |
| 47 | + constructor(session: Session, config: UserConfig, telemetry: Telemetry) { |
| 48 | + super(session, config, telemetry); |
| 49 | + session.on("close", () => { |
| 50 | + this.updateMetadata(); |
| 51 | + }); |
| 52 | + } |
45 | 53 |
|
46 | | - if (!options) { |
47 | | - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
48 | | - connectionString = this.config.connectionString!; |
49 | | - } else if ("connectionString" in options) { |
50 | | - connectionString = options.connectionString; |
51 | | - } else { |
52 | | - // TODO: https://github.com/mongodb-js/mongodb-mcp-server/issues/19 |
53 | | - // We don't support connecting via cluster name since we'd need to obtain the user credentials |
54 | | - // and fill in the connection string. |
55 | | - return { |
56 | | - content: [ |
57 | | - { |
58 | | - type: "text", |
59 | | - text: `Connecting via cluster name not supported yet. Please provide a connection string.`, |
60 | | - }, |
61 | | - ], |
62 | | - }; |
| 54 | + protected async execute({ connectionString }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { |
| 55 | + switch (this.name) { |
| 56 | + case disconnectedName: |
| 57 | + assert(connectionString, "Connection string is required"); |
| 58 | + break; |
| 59 | + case connectedName: |
| 60 | + connectionString ??= this.config.connectionString; |
| 61 | + assert( |
| 62 | + connectionString, |
| 63 | + "Cannot switch to a new connection because no connection string was provided and no default connection string is configured." |
| 64 | + ); |
| 65 | + break; |
63 | 66 | } |
64 | 67 |
|
65 | | - try { |
66 | | - await this.connectToMongoDB(connectionString); |
67 | | - return { |
68 | | - content: [{ type: "text", text: `Successfully connected to ${connectionString}.` }], |
69 | | - }; |
70 | | - } catch (error) { |
71 | | - // Sometimes the model will supply an incorrect connection string. If the user has configured |
72 | | - // a different one as environment variable or a cli argument, suggest using that one instead. |
73 | | - if ( |
74 | | - this.config.connectionString && |
75 | | - error instanceof DriverError && |
76 | | - this.config.connectionString !== connectionString |
77 | | - ) { |
78 | | - return { |
79 | | - content: [ |
80 | | - { |
81 | | - type: "text", |
82 | | - text: |
83 | | - `Failed to connect to MongoDB at '${connectionString}' due to error: '${error.message}.` + |
84 | | - `Your config lists a different connection string: '${this.config.connectionString}' - do you want to try connecting to it instead?`, |
85 | | - }, |
86 | | - ], |
87 | | - }; |
88 | | - } |
| 68 | + await this.connectToMongoDB(connectionString); |
| 69 | + this.updateMetadata(); |
| 70 | + return { |
| 71 | + content: [{ type: "text", text: "Successfully connected to MongoDB." }], |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + public register(server: McpServer): void { |
| 76 | + super.register(server); |
89 | 77 |
|
90 | | - throw error; |
| 78 | + this.updateMetadata(); |
| 79 | + } |
| 80 | + |
| 81 | + private updateMetadata(): void { |
| 82 | + if (this.config.connectionString || this.session.serviceProvider) { |
| 83 | + this.update?.({ |
| 84 | + name: connectedName, |
| 85 | + description: connectedDescription, |
| 86 | + inputSchema: connectedSchema, |
| 87 | + }); |
| 88 | + } else { |
| 89 | + this.update?.({ |
| 90 | + name: disconnectedName, |
| 91 | + description: disconnectedDescription, |
| 92 | + inputSchema: disconnectedSchema, |
| 93 | + }); |
91 | 94 | } |
92 | 95 | } |
93 | 96 | } |
0 commit comments