@@ -19,7 +19,7 @@ import {
1919 UnsubscribeRequestSchema ,
2020} from "@modelcontextprotocol/sdk/types.js" ;
2121import assert from "assert" ;
22- import type { ToolBase } from "./tools/tool.js" ;
22+ import type { ToolBase , ToolConstructor } from "./tools/tool.js" ;
2323import { validateConnectionString } from "./helpers/connectionOptions.js" ;
2424import { packageInfo } from "./common/packageInfo.js" ;
2525import { type ConnectionErrorHandler } from "./common/connectionErrorHandler.js" ;
@@ -69,6 +69,9 @@ export class Server {
6969 // TODO: Eventually we might want to make tools reactive too instead of relying on custom logic.
7070 this . registerTools ( ) ;
7171
72+ // Atlas Local tools are optional and require async initialization
73+ void this . registerAtlasLocalTools ( ) ;
74+
7275 // This is a workaround for an issue we've seen with some models, where they'll see that everything in the `arguments`
7376 // object is optional, and then not pass it at all. However, the MCP server expects the `arguments` object to be if
7477 // the tool accepts any arguments, even if they're all optional.
@@ -197,8 +200,41 @@ export class Server {
197200 this . telemetry . emitEvents ( [ event ] ) . catch ( ( ) => { } ) ;
198201 }
199202
203+ private async registerAtlasLocalTools ( ) : Promise < void > {
204+ // If Atlas Local tools are disabled, don't attempt to connect to the client
205+ if ( this . userConfig . disabledTools . includes ( "atlas-local" ) ) {
206+ return ;
207+ }
208+
209+ try {
210+ // Import Atlas Local client asyncronously
211+ // This will fail on unsupported platforms
212+ const { Client : AtlasLocalClient } = await import ( "@mongodb-js-preview/atlas-local" ) ;
213+
214+ // Connect to Atlas Local client
215+ // This will fail if docker is not running
216+ const client = AtlasLocalClient . connect ( ) ;
217+
218+ // Set Atlas Local client
219+ this . session . setAtlasLocalClient ( client ) ;
220+
221+ // Register Atlas Local tools
222+ this . registerToolInstances ( AtlasLocalTools ) ;
223+ } catch ( error ) {
224+ console . warn (
225+ "Failed to initialize Atlas Local client, atlas-local tools will be disabled (error: " ,
226+ error ,
227+ ")"
228+ ) ;
229+ }
230+ }
231+
200232 private registerTools ( ) : void {
201- for ( const toolConstructor of [ ...AtlasTools , ...AtlasLocalTools , ...MongoDbTools ] ) {
233+ this . registerToolInstances ( [ ...AtlasTools , ...MongoDbTools ] ) ;
234+ }
235+
236+ private registerToolInstances ( tools : Array < ToolConstructor > ) : void {
237+ for ( const toolConstructor of tools ) {
202238 const tool = new toolConstructor ( this . session , this . userConfig , this . telemetry ) ;
203239 if ( tool . register ( this ) ) {
204240 this . tools . push ( tool ) ;
0 commit comments