@@ -5,7 +5,6 @@ import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver
5
5
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
6
6
import { ErrorCodes , MongoDBError } from "../../errors.js" ;
7
7
import config from "../../config.js" ;
8
- import { connectToMongoDB } from "../../common/mongodb/connect.js" ;
9
8
10
9
export const DbOperationArgs = {
11
10
database : z . string ( ) . describe ( "Database name" ) ,
@@ -24,7 +23,7 @@ export abstract class MongoDBToolBase extends ToolBase {
24
23
protected async ensureConnected ( ) : Promise < NodeDriverServiceProvider > {
25
24
const provider = this . state . serviceProvider ;
26
25
if ( ! provider && config . connectionString ) {
27
- await connectToMongoDB ( config . connectionString , this . state ) ;
26
+ await this . connectToMongoDB ( config . connectionString , this . state ) ;
28
27
}
29
28
30
29
if ( ! provider ) {
@@ -52,4 +51,23 @@ export abstract class MongoDBToolBase extends ToolBase {
52
51
53
52
return undefined ;
54
53
}
54
+
55
+ protected async connectToMongoDB ( connectionString : string , state : State ) : Promise < void > {
56
+ const provider = await NodeDriverServiceProvider . connect ( connectionString , {
57
+ productDocsLink : "https://docs.mongodb.com/todo-mcp" ,
58
+ productName : "MongoDB MCP" ,
59
+ readConcern : {
60
+ level : config . connectOptions . readConcern ,
61
+ } ,
62
+ readPreference : config . connectOptions . readPreference ,
63
+ writeConcern : {
64
+ w : config . connectOptions . writeConcern ,
65
+ } ,
66
+ timeoutMS : config . connectOptions . timeoutMS ,
67
+ } ) ;
68
+
69
+ state . serviceProvider = provider ;
70
+ state . credentials . connectionString = connectionString ;
71
+ await state . persistCredentials ( ) ;
72
+ }
55
73
}
0 commit comments