Skip to content

Commit c79a479

Browse files
committed
chore: Inherit always preconfigured default driver options
The driver options can not be sent by the agent, so we are going to use the ones specified in the CLI arguments / ENV Vars. This is relevant because new connections should use any inherited configuration like TLS settings or FIPS by default.
1 parent 1164b91 commit c79a479

File tree

10 files changed

+38
-43
lines changed

10 files changed

+38
-43
lines changed

src/common/config.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from "path";
22
import os from "os";
33
import argv from "yargs-parser";
44
import type { CliOptions } from "@mongosh/arg-parser";
5-
import { ReadConcernLevel, ReadPreferenceMode, W } from "mongodb";
5+
import type { ConnectionInfo } from "@mongosh/arg-parser";
66

77
// From: https://github.com/mongodb-js/mongosh/blob/main/packages/cli-repl/src/arg-parser.ts
88
const OPTIONS = {
@@ -98,13 +98,6 @@ function isConnectionSpecifier(arg: string | undefined): boolean {
9898
);
9999
}
100100

101-
export interface ConnectOptions {
102-
readConcern: ReadConcernLevel;
103-
readPreference: ReadPreferenceMode;
104-
writeConcern: W;
105-
timeoutMS: number;
106-
}
107-
108101
// If we decide to support non-string config options, we'll need to extend the mechanism for parsing
109102
// env variables.
110103
export interface UserConfig extends CliOptions {
@@ -114,7 +107,6 @@ export interface UserConfig extends CliOptions {
114107
telemetry: "enabled" | "disabled";
115108
logPath: string;
116109
connectionString?: string;
117-
connectOptions: ConnectOptions;
118110
disabledTools: Array<string>;
119111
readOnly?: boolean;
120112
indexCheck?: boolean;
@@ -129,12 +121,6 @@ export interface UserConfig extends CliOptions {
129121
const defaults: UserConfig = {
130122
apiBaseUrl: "https://cloud.mongodb.com/",
131123
logPath: getLogPath(),
132-
connectOptions: {
133-
readConcern: "local",
134-
readPreference: "secondaryPreferred",
135-
writeConcern: "majority",
136-
timeoutMS: 30_000,
137-
},
138124
disabledTools: [],
139125
telemetry: "enabled",
140126
readOnly: false,
@@ -153,6 +139,19 @@ export const config = setupUserConfig({
153139
env: process.env,
154140
});
155141

142+
export const defaultDriverOptions: ConnectionInfo["driverOptions"] = {
143+
readConcern: {
144+
level: "local",
145+
},
146+
readPreference: "secondaryPreferred",
147+
writeConcern: {
148+
w: "majority",
149+
},
150+
timeoutMS: 30_000,
151+
proxy: { useEnvironmentVariableProxies: true },
152+
applyProxyToOIDC: true,
153+
};
154+
156155
function getLogPath(): string {
157156
const localDataPath =
158157
process.platform === "win32"
@@ -292,3 +291,16 @@ export function setupUserConfig({
292291

293292
return userConfig;
294293
}
294+
295+
/**
296+
readConcern: {
297+
level: settings.readConcern,
298+
},
299+
readPreference: settings.readPreference,
300+
writeConcern: {
301+
w: settings.writeConcern,
302+
},
303+
timeoutMS: settings.timeoutMS,
304+
proxy: { useEnvironmentVariableProxies: true },
305+
applyProxyToOIDC: true,
306+
**/

src/common/connectionManager.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ConnectOptions } from "./config.js";
1+
import { defaultDriverOptions } from "./config.js";
22
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
33
import EventEmitter from "events";
44
import { setAppNameParamIfMissing } from "../helpers/connectionOptions.js";
@@ -14,7 +14,7 @@ export interface AtlasClusterConnectionInfo {
1414
expiryDate: Date;
1515
}
1616

17-
export interface ConnectionSettings extends ConnectOptions {
17+
export interface ConnectionSettings {
1818
connectionString: string;
1919
atlas?: AtlasClusterConnectionInfo;
2020
}
@@ -70,6 +70,7 @@ export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
7070

7171
constructor() {
7272
super();
73+
7374
this.state = { tag: "disconnected" };
7475
}
7576

@@ -91,16 +92,7 @@ export class ConnectionManager extends EventEmitter<ConnectionManagerEvents> {
9192
serviceProvider = await NodeDriverServiceProvider.connect(settings.connectionString, {
9293
productDocsLink: "https://github.com/mongodb-js/mongodb-mcp-server/",
9394
productName: "MongoDB MCP",
94-
readConcern: {
95-
level: settings.readConcern,
96-
},
97-
readPreference: settings.readPreference,
98-
writeConcern: {
99-
w: settings.writeConcern,
100-
},
101-
timeoutMS: settings.timeoutMS,
102-
proxy: { useEnvironmentVariableProxies: true },
103-
applyProxyToOIDC: true,
95+
...defaultDriverOptions,
10496
});
10597
} catch (error: unknown) {
10698
const errorReason = error instanceof Error ? error.message : `${error as string}`;

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ function assertFIPSMode() {
111111
}
112112

113113
function assertHelpMode() {
114-
if (!!config.help) {
114+
if (config.help) {
115115
console.log("For usage information refer to the README.md:");
116116
console.log("https://github.com/mongodb-js/mongodb-mcp-server?tab=readme-ov-file#quick-start");
117117
process.exit(0);
118118
}
119119
}
120120

121121
function assertVersionMode() {
122-
if (!!config.version) {
122+
if (config.version) {
123123
console.log(packageInfo.version);
124124
process.exit(0);
125125
}

src/resources/common/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ReactiveResource } from "../resource.js";
2-
import { config } from "../../common/config.js";
2+
import { config, defaultDriverOptions } from "../../common/config.js";
33
import type { UserConfig } from "../../common/config.js";
44
import type { Server } from "../../server.js";
55
import type { Telemetry } from "../../telemetry/telemetry.js";
@@ -37,7 +37,7 @@ export class ConfigResource extends ReactiveResource<UserConfig, readonly []> {
3737
connectionString: this.current.connectionString
3838
? "set; access to MongoDB tools are currently available to use"
3939
: "not set; before using any MongoDB tool, you need to configure a connection string, alternatively you can setup MongoDB Atlas access, more info at 'https://github.com/mongodb-js/mongodb-mcp-server'.",
40-
connectOptions: this.current.connectOptions,
40+
connectOptions: defaultDriverOptions,
4141
atlas:
4242
this.current.apiClientId && this.current.apiClientSecret
4343
? "set; MongoDB Atlas tools are currently available to use"

src/server.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ export class Server {
187187
try {
188188
await this.session.connectToMongoDB({
189189
connectionString: this.userConfig.connectionString,
190-
...this.userConfig.connectOptions,
191190
});
192191
} catch (error) {
193192
console.error(

src/tools/atlas/connect/connectCluster.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class ConnectClusterTool extends AtlasToolBase {
139139
try {
140140
lastError = undefined;
141141

142-
await this.session.connectToMongoDB({ connectionString, ...this.config.connectOptions, atlas });
142+
await this.session.connectToMongoDB({ connectionString, atlas });
143143
break;
144144
} catch (err: unknown) {
145145
const error = err instanceof Error ? err : new Error(String(err));

src/tools/mongodb/mongodbTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export abstract class MongoDBToolBase extends ToolBase {
117117
}
118118

119119
protected connectToMongoDB(connectionString: string): Promise<void> {
120-
return this.session.connectToMongoDB({ connectionString, ...this.config.connectOptions });
120+
return this.session.connectToMongoDB({ connectionString });
121121
}
122122

123123
protected resolveTelemetryMetadata(

tests/integration/common/connectionManager.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
} from "../../../src/common/connectionManager.js";
77
import { describeWithMongoDB } from "../tools/mongodb/mongodbHelpers.js";
88
import { describe, beforeEach, expect, it, vi, afterEach } from "vitest";
9-
import { config } from "../../../src/common/config.js";
109

1110
describeWithMongoDB("Connection Manager", (integration) => {
1211
function connectionManager(): ConnectionManager {
@@ -48,7 +47,6 @@ describeWithMongoDB("Connection Manager", (integration) => {
4847

4948
await connectionManager().connect({
5049
connectionString: integration.connectionString(),
51-
...integration.mcpServer().userConfig.connectOptions,
5250
});
5351
});
5452

@@ -88,7 +86,6 @@ describeWithMongoDB("Connection Manager", (integration) => {
8886
beforeEach(async () => {
8987
await connectionManager().connect({
9088
connectionString: integration.connectionString(),
91-
...integration.mcpServer().userConfig.connectOptions,
9289
});
9390
});
9491

@@ -110,7 +107,6 @@ describeWithMongoDB("Connection Manager", (integration) => {
110107
try {
111108
await connectionManager().connect({
112109
connectionString: "mongodb://localhost:xxxxx",
113-
...integration.mcpServer().userConfig.connectOptions,
114110
});
115111
} catch (_error: unknown) {
116112
void _error;
@@ -158,7 +154,6 @@ describe("Connection Manager connection type inference", () => {
158154
it(`infers ${connectionType} from ${connectionString}`, () => {
159155
const actualConnectionType = ConnectionManager.inferConnectionTypeFromSettings({
160156
connectionString,
161-
...config.connectOptions,
162157
});
163158

164159
expect(actualConnectionType).toBe(connectionType);

tests/integration/tools/mongodb/connect/connect.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ describeWithMongoDB(
1515
beforeEach(async () => {
1616
await integration.mcpServer().session.connectToMongoDB({
1717
connectionString: integration.connectionString(),
18-
...config.connectOptions,
1918
});
2019
});
2120

tests/unit/common/session.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
33
import { Session } from "../../../src/common/session.js";
4-
import { config } from "../../../src/common/config.js";
54
import { CompositeLogger } from "../../../src/common/logger.js";
65

76
vi.mock("@mongosh/service-provider-node-driver");
@@ -47,7 +46,6 @@ describe("Session", () => {
4746
it(`should update connection string for ${testCase.name}`, async () => {
4847
await session.connectToMongoDB({
4948
connectionString: testCase.connectionString,
50-
...config.connectOptions,
5149
});
5250
expect(session.serviceProvider).toBeDefined();
5351

@@ -63,7 +61,7 @@ describe("Session", () => {
6361
}
6462

6563
it("should configure the proxy to use environment variables", async () => {
66-
await session.connectToMongoDB({ connectionString: "mongodb://localhost", ...config.connectOptions });
64+
await session.connectToMongoDB({ connectionString: "mongodb://localhost" });
6765
expect(session.serviceProvider).toBeDefined();
6866

6967
const connectMock = MockNodeDriverServiceProvider.connect;

0 commit comments

Comments
 (0)