Skip to content

Commit 75d1221

Browse files
chore: use default driver options for connection strings
Last change missed using default driver options for connection info without driver options. We are bringing that back now. Also the ensureConnected is modified to use correct connectionInfo.
1 parent b8e490e commit 75d1221

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/common/connectionManager.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { EventEmitter } from "events";
22
import type { MongoClientOptions } from "mongodb";
33
import { ConnectionString } from "mongodb-connection-string-url";
44
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
5-
import { type ConnectionInfo } from "@mongosh/arg-parser";
5+
import { generateConnectionInfoFromCliArgs, type ConnectionInfo } from "@mongosh/arg-parser";
66
import type { DeviceId } from "../helpers/deviceId.js";
77
import { type UserConfig } from "./config/userConfig.js";
88
import { MongoDBError, ErrorCodes } from "./errors.js";
@@ -33,6 +33,20 @@ export interface ConnectionState {
3333
}
3434

3535
const MCP_TEST_DATABASE = "#mongodb-mcp";
36+
37+
export const defaultDriverOptions: ConnectionInfo["driverOptions"] = {
38+
readConcern: {
39+
level: "local",
40+
},
41+
readPreference: "secondaryPreferred",
42+
writeConcern: {
43+
w: "majority",
44+
},
45+
timeoutMS: 30_000,
46+
proxy: { useEnvironmentVariableProxies: true },
47+
applyProxyToOIDC: true,
48+
};
49+
3650
export class ConnectionStateConnected implements ConnectionState {
3751
public tag = "connected" as const;
3852

@@ -171,10 +185,15 @@ export class MCPConnectionManager extends ConnectionManager {
171185
components: appNameComponents,
172186
});
173187

174-
const connectionInfo = {
175-
connectionString: settings.connectionString,
176-
driverOptions: settings.driverOptions ?? {},
177-
};
188+
const connectionInfo: ConnectionInfo = settings.driverOptions
189+
? {
190+
connectionString: settings.connectionString,
191+
driverOptions: settings.driverOptions,
192+
}
193+
: generateConnectionInfoFromCliArgs({
194+
...defaultDriverOptions,
195+
connectionSpecifier: settings.connectionString,
196+
});
178197

179198
if (connectionInfo.driverOptions.oidc) {
180199
connectionInfo.driverOptions.oidc.allowedFlows ??= ["auth-code"];

src/tools/mongodb/connect/connect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class ConnectTool extends MongoDBToolBase {
6868
break;
6969
}
7070

71-
await this.connectToMongoDB(connectionString);
71+
await this.session.connectToMongoDB({ connectionString });
7272
this.updateMetadata();
7373

7474
return {

src/tools/mongodb/mongodbTool.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { LogId } from "../../common/logger.js";
88
import type { Server } from "../../server.js";
99
import type { ConnectionMetadata } from "../../telemetry/types.js";
1010
import type { ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
11+
import { generateConnectionInfoFromCliArgs } from "@mongosh/arg-parser";
1112

1213
export const DbOperationArgs = {
1314
database: z.string().describe("Database name"),
@@ -29,7 +30,11 @@ export abstract class MongoDBToolBase extends ToolBase {
2930

3031
if (this.config.connectionString) {
3132
try {
32-
await this.connectToMongoDB(this.config.connectionString);
33+
const connectionInfo = generateConnectionInfoFromCliArgs({
34+
...this.config,
35+
connectionSpecifier: this.config.connectionString,
36+
});
37+
await this.session.connectToMongoDB(connectionInfo);
3338
} catch (error) {
3439
this.session.logger.error({
3540
id: LogId.mongodbConnectFailure,
@@ -108,10 +113,6 @@ export abstract class MongoDBToolBase extends ToolBase {
108113
return super.handleError(error, args);
109114
}
110115

111-
protected connectToMongoDB(connectionString: string): Promise<void> {
112-
return this.session.connectToMongoDB({ connectionString });
113-
}
114-
115116
/**
116117
* Resolves the tool metadata from the arguments passed to the mongoDB tools.
117118
*

0 commit comments

Comments
 (0)