Skip to content

Commit 6e9b586

Browse files
committed
CR comments - move connectToMongoDB to the session
1 parent 4a02ecf commit 6e9b586

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

src/config.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import argv from "yargs-parser";
44

55
import { ReadConcernLevel, ReadPreferenceMode, W } from "mongodb";
66

7+
export interface ConnectOptions {
8+
readConcern: ReadConcernLevel;
9+
readPreference: ReadPreferenceMode;
10+
writeConcern: W;
11+
timeoutMS: number;
12+
}
13+
714
// If we decide to support non-string config options, we'll need to extend the mechanism for parsing
815
// env variables.
916
export interface UserConfig {
@@ -13,12 +20,7 @@ export interface UserConfig {
1320
telemetry?: "enabled" | "disabled";
1421
logPath: string;
1522
connectionString?: string;
16-
connectOptions: {
17-
readConcern: ReadConcernLevel;
18-
readPreference: ReadPreferenceMode;
19-
writeConcern: W;
20-
timeoutMS: number;
21-
};
23+
connectOptions: ConnectOptions;
2224
disabledTools: Array<string>;
2325
readOnly?: boolean;
2426
}

src/server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { type ServerEvent } from "./telemetry/types.js";
1111
import { type ServerCommand } from "./telemetry/types.js";
1212
import { CallToolRequestSchema, CallToolResult } from "@modelcontextprotocol/sdk/types.js";
1313
import assert from "assert";
14-
import { connectToMongoDB } from "./tools/mongodb/mongodbTool.js";
1514

1615
export interface ServerOptions {
1716
session: Session;
@@ -201,7 +200,7 @@ export class Server {
201200

202201
if (this.userConfig.connectionString) {
203202
try {
204-
await connectToMongoDB(this.userConfig.connectionString, this.userConfig, this.session);
203+
await this.session.connectToMongoDB(this.userConfig.connectionString, this.userConfig.connectOptions);
205204
} catch (error) {
206205
console.error(
207206
"Failed to connect to MongoDB instance using the connection string from the config: ",

src/session.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver
22
import { ApiClient, ApiClientCredentials } from "./common/atlas/apiClient.js";
33
import { Implementation } from "@modelcontextprotocol/sdk/types.js";
44
import EventEmitter from "events";
5+
import { ConnectOptions } from "./config.js";
56

67
export interface SessionOptions {
78
apiBaseUrl: string;
@@ -58,4 +59,21 @@ export class Session extends EventEmitter<{
5859
this.emit("close");
5960
}
6061
}
62+
63+
async connectToMongoDB(connectionString: string, connectOptions: ConnectOptions): Promise<void> {
64+
const provider = await NodeDriverServiceProvider.connect(connectionString, {
65+
productDocsLink: "https://docs.mongodb.com/todo-mcp",
66+
productName: "MongoDB MCP",
67+
readConcern: {
68+
level: connectOptions.readConcern,
69+
},
70+
readPreference: connectOptions.readPreference,
71+
writeConcern: {
72+
w: connectOptions.writeConcern,
73+
},
74+
timeoutMS: connectOptions.timeoutMS,
75+
});
76+
77+
this.serviceProvider = provider;
78+
}
6179
}

src/tools/mongodb/mongodbTool.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,12 @@ import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver
44
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
55
import { ErrorCodes, MongoDBError } from "../../errors.js";
66
import logger, { LogId } from "../../logger.js";
7-
import { UserConfig } from "../../config.js";
8-
import { Session } from "../../session.js";
97

108
export const DbOperationArgs = {
119
database: z.string().describe("Database name"),
1210
collection: z.string().describe("Collection name"),
1311
};
1412

15-
export async function connectToMongoDB(connectionString: string, config: UserConfig, session: Session): Promise<void> {
16-
const provider = await NodeDriverServiceProvider.connect(connectionString, {
17-
productDocsLink: "https://docs.mongodb.com/todo-mcp",
18-
productName: "MongoDB MCP",
19-
readConcern: {
20-
level: config.connectOptions.readConcern,
21-
},
22-
readPreference: config.connectOptions.readPreference,
23-
writeConcern: {
24-
w: config.connectOptions.writeConcern,
25-
},
26-
timeoutMS: config.connectOptions.timeoutMS,
27-
});
28-
29-
session.serviceProvider = provider;
30-
}
31-
3213
export abstract class MongoDBToolBase extends ToolBase {
3314
protected category: ToolCategory = "mongodb";
3415

@@ -90,6 +71,6 @@ export abstract class MongoDBToolBase extends ToolBase {
9071
}
9172

9273
protected connectToMongoDB(connectionString: string): Promise<void> {
93-
return connectToMongoDB(connectionString, this.config, this.session);
74+
return this.session.connectToMongoDB(connectionString, this.config.connectOptions);
9475
}
9576
}

0 commit comments

Comments
 (0)