@@ -19,7 +19,7 @@ import {
19
19
UnsubscribeRequestSchema ,
20
20
} from "@modelcontextprotocol/sdk/types.js" ;
21
21
import assert from "assert" ;
22
- import type { ToolBase } from "./tools/tool.js" ;
22
+ import type { ToolBase , ToolConstructor } from "./tools/tool.js" ;
23
23
import { validateConnectionString } from "./helpers/connectionOptions.js" ;
24
24
import { packageInfo } from "./common/packageInfo.js" ;
25
25
import { type ConnectionErrorHandler } from "./common/connectionErrorHandler.js" ;
@@ -69,6 +69,9 @@ export class Server {
69
69
// TODO: Eventually we might want to make tools reactive too instead of relying on custom logic.
70
70
this . registerTools ( ) ;
71
71
72
+ // Atlas Local tools are optional and require async initialization
73
+ void this . registerAtlasLocalTools ( ) ;
74
+
72
75
// This is a workaround for an issue we've seen with some models, where they'll see that everything in the `arguments`
73
76
// object is optional, and then not pass it at all. However, the MCP server expects the `arguments` object to be if
74
77
// the tool accepts any arguments, even if they're all optional.
@@ -197,8 +200,41 @@ export class Server {
197
200
this . telemetry . emitEvents ( [ event ] ) . catch ( ( ) => { } ) ;
198
201
}
199
202
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
+
200
232
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 ) {
202
238
const tool = new toolConstructor ( this . session , this . userConfig , this . telemetry ) ;
203
239
if ( tool . register ( this ) ) {
204
240
this . tools . push ( tool ) ;
0 commit comments