@@ -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
25
25
export interface ServerOptions {
@@ -61,11 +61,12 @@ export class Server {
61
61
62
62
this . mcpServer . server . registerCapabilities ( { logging : { } , resources : { listChanged : true , subscribe : true } } ) ;
63
63
64
- await this . tryInitializeAtlasLocalClient ( ) ;
65
-
66
64
// TODO: Eventually we might want to make tools reactive too instead of relying on custom logic.
67
65
this . registerTools ( ) ;
68
66
67
+ // Atlas Local tools are optional and require async initialization
68
+ void this . registerAtlasLocalTools ( ) ;
69
+
69
70
// This is a workaround for an issue we've seen with some models, where they'll see that everything in the `arguments`
70
71
// object is optional, and then not pass it at all. However, the MCP server expects the `arguments` object to be if
71
72
// the tool accepts any arguments, even if they're all optional.
@@ -195,13 +196,27 @@ export class Server {
195
196
this . telemetry . emitEvents ( [ event ] ) . catch ( ( ) => { } ) ;
196
197
}
197
198
198
- private async tryInitializeAtlasLocalClient ( ) : Promise < void > {
199
+ private async registerAtlasLocalTools ( ) : Promise < void > {
199
200
try {
201
+ // Import Atlas Local client asyncronously
202
+ // This will fail on unsupported platforms
200
203
const { Client : AtlasLocalClient } = await import ( "@mongodb-js-preview/atlas-local" ) ;
201
204
205
+ // Connect to Atlas Local client
206
+ // This will fail if docker is not running
202
207
const client = AtlasLocalClient . connect ( ) ;
208
+
209
+ // Set Atlas Local client
203
210
this . session . setAtlasLocalClient ( client ) ;
211
+
212
+ // Register Atlas Local tools
213
+ this . registerToolInstances ( AtlasLocalTools ) ;
204
214
} catch ( error ) {
215
+ // If Atlas Local tools are disabled, don't log an error
216
+ if ( this . userConfig . disabledTools . includes ( "atlas-local" ) ) {
217
+ return ;
218
+ }
219
+
205
220
console . warn (
206
221
"Failed to initialize Atlas Local client, atlas-local tools will be disabled (error: " ,
207
222
error ,
@@ -211,7 +226,11 @@ export class Server {
211
226
}
212
227
213
228
private registerTools ( ) : void {
214
- for ( const toolConstructor of [ ...AtlasTools , ...AtlasLocalTools , ...MongoDbTools ] ) {
229
+ this . registerToolInstances ( [ ...AtlasTools , ...MongoDbTools ] ) ;
230
+ }
231
+
232
+ private registerToolInstances ( tools : Array < ToolConstructor > ) : void {
233
+ for ( const toolConstructor of tools ) {
215
234
const tool = new toolConstructor ( this . session , this . userConfig , this . telemetry ) ;
216
235
if ( tool . register ( this ) ) {
217
236
this . tools . push ( tool ) ;
0 commit comments