@@ -3,7 +3,7 @@ import { type OperationType, type ToolArgs } from "../../tool.js";
33import { AtlasToolBase } from "../atlasTool.js" ;
44import { generateSecurePassword } from "../../../helpers/generatePassword.js" ;
55import { LogId } from "../../../common/logger.js" ;
6- import { inspectCluster } from "../../../common/atlas/cluster.js" ;
6+ import { getConnectionString , inspectCluster } from "../../../common/atlas/cluster.js" ;
77import { ensureCurrentIpInAccessList } from "../../../common/atlas/accessListUtils.js" ;
88import type { AtlasClusterConnectionInfo } from "../../../common/connectionManager.js" ;
99import { getDefaultRoleFromConfig } from "../../../common/atlas/roles.js" ;
@@ -22,6 +22,9 @@ function sleep(ms: number): Promise<void> {
2222export const ConnectClusterArgs = {
2323 projectId : AtlasArgs . projectId ( ) . describe ( "Atlas project ID" ) ,
2424 clusterName : AtlasArgs . clusterName ( ) . describe ( "Atlas cluster name" ) ,
25+ connectionType : AtlasArgs . connectionType ( ) . describe (
26+ "Type of connection (standard, private, or privateEndpoint) to an Atlas cluster"
27+ ) ,
2528} ;
2629
2730export class ConnectClusterTool extends AtlasToolBase {
@@ -69,12 +72,19 @@ export class ConnectClusterTool extends AtlasToolBase {
6972
7073 private async prepareClusterConnection (
7174 projectId : string ,
72- clusterName : string
75+ clusterName : string ,
76+ connectionType : "standard" | "private" | "privateEndpoint" | undefined = "standard"
7377 ) : Promise < { connectionString : string ; atlas : AtlasClusterConnectionInfo } > {
7478 const cluster = await inspectCluster ( this . session . apiClient , projectId , clusterName ) ;
7579
76- if ( ! cluster . connectionString ) {
77- throw new Error ( "Connection string not available" ) ;
80+ if ( cluster . connectionStrings === undefined ) {
81+ throw new Error ( "Connection strings not available" ) ;
82+ }
83+ const connectionString = getConnectionString ( cluster . connectionStrings , connectionType ) ;
84+ if ( connectionString === undefined ) {
85+ throw new Error (
86+ `Connection string for connection type "${ connectionType } " is not available. Please ensure this connection type is set up in Atlas. See https://www.mongodb.com/docs/atlas/connect-to-database-deployment/#connect-to-an-atlas-cluster.`
87+ ) ;
7888 }
7989
8090 const username = `mcpUser${ Math . floor ( Math . random ( ) * 100000 ) } ` ;
@@ -113,7 +123,7 @@ export class ConnectClusterTool extends AtlasToolBase {
113123 expiryDate,
114124 } ;
115125
116- const cn = new URL ( cluster . connectionString ) ;
126+ const cn = new URL ( connectionString ) ;
117127 cn . username = username ;
118128 cn . password = password ;
119129 cn . searchParams . set ( "authSource" , "admin" ) ;
@@ -200,7 +210,11 @@ export class ConnectClusterTool extends AtlasToolBase {
200210 } ) ;
201211 }
202212
203- protected async execute ( { projectId, clusterName } : ToolArgs < typeof this . argsShape > ) : Promise < CallToolResult > {
213+ protected async execute ( {
214+ projectId,
215+ clusterName,
216+ connectionType,
217+ } : ToolArgs < typeof this . argsShape > ) : Promise < CallToolResult > {
204218 const ipAccessListUpdated = await ensureCurrentIpInAccessList ( this . session . apiClient , projectId ) ;
205219 let createdUser = false ;
206220
@@ -239,7 +253,11 @@ export class ConnectClusterTool extends AtlasToolBase {
239253 case "disconnected" :
240254 default : {
241255 await this . session . disconnect ( ) ;
242- const { connectionString, atlas } = await this . prepareClusterConnection ( projectId , clusterName ) ;
256+ const { connectionString, atlas } = await this . prepareClusterConnection (
257+ projectId ,
258+ clusterName ,
259+ connectionType
260+ ) ;
243261
244262 createdUser = true ;
245263 // try to connect for about 5 minutes asynchronously
0 commit comments