-
Notifications
You must be signed in to change notification settings - Fork 166
chore: split connect and switch-connection tool MCP-301 #749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
himanshusinghs
merged 3 commits into
main
from
chore/MCP-301-split-connect-and-switch-connection
Nov 25, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import z from "zod"; | ||
| import { type CallToolResult } from "@modelcontextprotocol/sdk/types.js"; | ||
|
|
||
| import { MongoDBToolBase } from "../mongodbTool.js"; | ||
| import { type ToolArgs, type OperationType, type ToolConstructorParams } from "../../tool.js"; | ||
| import type { Server } from "../../../server.js"; | ||
|
|
||
| export class SwitchConnectionTool extends MongoDBToolBase { | ||
| public override name = "switch-connection"; | ||
| protected override description = | ||
| "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."; | ||
|
|
||
| protected override argsShape = { | ||
| connectionString: z | ||
| .string() | ||
| .optional() | ||
| .describe( | ||
| "MongoDB connection string to switch to (in the mongodb:// or mongodb+srv:// format). If a connection string is not provided, the connection string from the config will be used." | ||
| ), | ||
| }; | ||
|
|
||
| public override operationType: OperationType = "connect"; | ||
|
|
||
| constructor({ session, config, telemetry, elicitation }: ToolConstructorParams) { | ||
| super({ session, config, telemetry, elicitation }); | ||
| session.on("connect", () => { | ||
| this.enable(); | ||
| }); | ||
|
|
||
| session.on("disconnect", () => { | ||
| this.disable(); | ||
| }); | ||
| } | ||
|
|
||
| public override register(server: Server): boolean { | ||
| const registrationSuccessful = super.register(server); | ||
| /** | ||
| * When connected to mongodb we want to swap connect with | ||
| * switch-connection tool. | ||
| */ | ||
| if (registrationSuccessful && !this.session.isConnectedToMongoDB) { | ||
| this.disable(); | ||
| } | ||
| return registrationSuccessful; | ||
| } | ||
|
|
||
| protected override async execute({ connectionString }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { | ||
| if (typeof connectionString !== "string") { | ||
| await this.session.connectToConfiguredConnection(); | ||
| } else { | ||
| await this.session.connectToMongoDB({ connectionString }); | ||
| } | ||
|
|
||
| return { | ||
| content: [{ type: "text", text: "Successfully connected to MongoDB." }], | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.