@@ -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 ( )
26+ . optional ( )
27+ . describe ( "Desired connection type (standard, private, or privateEndpoint) to an Atlas cluster" ) ,
2528} ;
2629
2730export class ConnectClusterTool extends AtlasToolBase {
@@ -69,12 +72,17 @@ 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 ( `Connection string for type "${ connectionType } " not available` ) ;
7886 }
7987
8088 const username = `mcpUser${ Math . floor ( Math . random ( ) * 100000 ) } ` ;
@@ -113,13 +121,26 @@ export class ConnectClusterTool extends AtlasToolBase {
113121 expiryDate,
114122 } ;
115123
116- const cn = new URL ( cluster . connectionString ) ;
124+ this . session . logger . debug ( {
125+ id : LogId . atlasConnectFailure ,
126+ context : "atlas-connect-cluster" ,
127+ message : `Connection string received: ${ connectionString } ` ,
128+ } ) ;
129+ const cn = new URL ( connectionString ) ;
117130 cn . username = username ;
118131 cn . password = password ;
119- cn . searchParams . set ( "authSource" , "admin" ) ;
132+ if ( connectionType !== "privateEndpoint" ) {
133+ cn . searchParams . set ( "authSource" , "admin" ) ;
134+ }
120135
121136 this . session . keychain . register ( username , "user" ) ;
122137 this . session . keychain . register ( password , "password" ) ;
138+ const thing = cn . toString ( ) ;
139+ this . session . logger . debug ( {
140+ id : LogId . atlasConnectFailure ,
141+ context : "atlas-connect-cluster" ,
142+ message : `>>>>>> Connection string used: ${ thing } ` ,
143+ } ) ;
123144
124145 return { connectionString : cn . toString ( ) , atlas : connectedAtlasCluster } ;
125146 }
@@ -200,7 +221,11 @@ export class ConnectClusterTool extends AtlasToolBase {
200221 } ) ;
201222 }
202223
203- protected async execute ( { projectId, clusterName } : ToolArgs < typeof this . argsShape > ) : Promise < CallToolResult > {
224+ protected async execute ( {
225+ projectId,
226+ clusterName,
227+ connectionType,
228+ } : ToolArgs < typeof this . argsShape > ) : Promise < CallToolResult > {
204229 const ipAccessListUpdated = await ensureCurrentIpInAccessList ( this . session . apiClient , projectId ) ;
205230 let createdUser = false ;
206231
@@ -239,7 +264,11 @@ export class ConnectClusterTool extends AtlasToolBase {
239264 case "disconnected" :
240265 default : {
241266 await this . session . disconnect ( ) ;
242- const { connectionString, atlas } = await this . prepareClusterConnection ( projectId , clusterName ) ;
267+ const { connectionString, atlas } = await this . prepareClusterConnection (
268+ projectId ,
269+ clusterName ,
270+ connectionType
271+ ) ;
243272
244273 createdUser = true ;
245274 // try to connect for about 5 minutes asynchronously
0 commit comments