Skip to content

Commit 02fe6a2

Browse files
feat: Allow configuration of temporary user timeout when connecting to atlas cluster (#544)
1 parent d6b84c7 commit 02fe6a2

File tree

4 files changed

+46
-26
lines changed

4 files changed

+46
-26
lines changed

README.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -338,25 +338,26 @@ The MongoDB MCP Server can be configured using multiple methods, with the follow
338338

339339
### Configuration Options
340340

341-
| CLI Option | Environment Variable | Default | Description |
342-
| ------------------------- | ------------------------------------ | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
343-
| `apiClientId` | `MDB_MCP_API_CLIENT_ID` | <not set> | Atlas API client ID for authentication. Required for running Atlas tools. |
344-
| `apiClientSecret` | `MDB_MCP_API_CLIENT_SECRET` | <not set> | Atlas API client secret for authentication. Required for running Atlas tools. |
345-
| `connectionString` | `MDB_MCP_CONNECTION_STRING` | <not set> | MongoDB connection string for direct database connections. Optional, if not set, you'll need to call the `connect` tool before interacting with MongoDB data. |
346-
| `loggers` | `MDB_MCP_LOGGERS` | disk,mcp | Comma separated values, possible values are `mcp`, `disk` and `stderr`. See [Logger Options](#logger-options) for details. |
347-
| `logPath` | `MDB_MCP_LOG_PATH` | see note\* | Folder to store logs. |
348-
| `disabledTools` | `MDB_MCP_DISABLED_TOOLS` | <not set> | An array of tool names, operation types, and/or categories of tools that will be disabled. |
349-
| `readOnly` | `MDB_MCP_READ_ONLY` | false | When set to true, only allows read, connect, and metadata operation types, disabling create/update/delete operations. |
350-
| `indexCheck` | `MDB_MCP_INDEX_CHECK` | false | When set to true, enforces that query operations must use an index, rejecting queries that perform a collection scan. |
351-
| `telemetry` | `MDB_MCP_TELEMETRY` | enabled | When set to disabled, disables telemetry collection. |
352-
| `transport` | `MDB_MCP_TRANSPORT` | stdio | Either 'stdio' or 'http'. |
353-
| `httpPort` | `MDB_MCP_HTTP_PORT` | 3000 | Port number. |
354-
| `httpHost` | `MDB_MCP_HTTP_HOST` | 127.0.0.1 | Host to bind the http server. |
355-
| `idleTimeoutMs` | `MDB_MCP_IDLE_TIMEOUT_MS` | 600000 | Idle timeout for a client to disconnect (only applies to http transport). |
356-
| `notificationTimeoutMs` | `MDB_MCP_NOTIFICATION_TIMEOUT_MS` | 540000 | Notification timeout for a client to be aware of diconnect (only applies to http transport). |
357-
| `exportsPath` | `MDB_MCP_EXPORTS_PATH` | see note\* | Folder to store exported data files. |
358-
| `exportTimeoutMs` | `MDB_MCP_EXPORT_TIMEOUT_MS` | 300000 | Time in milliseconds after which an export is considered expired and eligible for cleanup. |
359-
| `exportCleanupIntervalMs` | `MDB_MCP_EXPORT_CLEANUP_INTERVAL_MS` | 120000 | Time in milliseconds between export cleanup cycles that remove expired export files. |
341+
| CLI Option | Environment Variable | Default | Description |
342+
| -------------------------------------- | --------------------------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
343+
| `apiClientId` | `MDB_MCP_API_CLIENT_ID` | <not set> | Atlas API client ID for authentication. Required for running Atlas tools. |
344+
| `apiClientSecret` | `MDB_MCP_API_CLIENT_SECRET` | <not set> | Atlas API client secret for authentication. Required for running Atlas tools. |
345+
| `connectionString` | `MDB_MCP_CONNECTION_STRING` | <not set> | MongoDB connection string for direct database connections. Optional, if not set, you'll need to call the `connect` tool before interacting with MongoDB data. |
346+
| `loggers` | `MDB_MCP_LOGGERS` | disk,mcp | Comma separated values, possible values are `mcp`, `disk` and `stderr`. See [Logger Options](#logger-options) for details. |
347+
| `logPath` | `MDB_MCP_LOG_PATH` | see note\* | Folder to store logs. |
348+
| `disabledTools` | `MDB_MCP_DISABLED_TOOLS` | <not set> | An array of tool names, operation types, and/or categories of tools that will be disabled. |
349+
| `readOnly` | `MDB_MCP_READ_ONLY` | false | When set to true, only allows read, connect, and metadata operation types, disabling create/update/delete operations. |
350+
| `indexCheck` | `MDB_MCP_INDEX_CHECK` | false | When set to true, enforces that query operations must use an index, rejecting queries that perform a collection scan. |
351+
| `telemetry` | `MDB_MCP_TELEMETRY` | enabled | When set to disabled, disables telemetry collection. |
352+
| `transport` | `MDB_MCP_TRANSPORT` | stdio | Either 'stdio' or 'http'. |
353+
| `httpPort` | `MDB_MCP_HTTP_PORT` | 3000 | Port number. |
354+
| `httpHost` | `MDB_MCP_HTTP_HOST` | 127.0.0.1 | Host to bind the http server. |
355+
| `idleTimeoutMs` | `MDB_MCP_IDLE_TIMEOUT_MS` | 600000 | Idle timeout for a client to disconnect (only applies to http transport). |
356+
| `notificationTimeoutMs` | `MDB_MCP_NOTIFICATION_TIMEOUT_MS` | 540000 | Notification timeout for a client to be aware of diconnect (only applies to http transport). |
357+
| `exportsPath` | `MDB_MCP_EXPORTS_PATH` | see note\* | Folder to store exported data files. |
358+
| `exportTimeoutMs` | `MDB_MCP_EXPORT_TIMEOUT_MS` | 300000 | Time in milliseconds after which an export is considered expired and eligible for cleanup. |
359+
| `exportCleanupIntervalMs` | `MDB_MCP_EXPORT_CLEANUP_INTERVAL_MS` | 120000 | Time in milliseconds between export cleanup cycles that remove expired export files. |
360+
| `atlasTemporaryDatabaseUserLifetimeMs` | `MDB_MCP_ATLAS_TEMPORARY_DATABASE_USER_LIFETIME_MS` | 14400000 | Time in milliseconds that temporary database users created when connecting to MongoDB Atlas clusters will remain active before being automatically deleted. |
360361

361362
#### Logger Options
362363

src/common/config.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const OPTIONS = {
4848
"tlsCertificateSelector",
4949
"tlsDisabledProtocols",
5050
"username",
51+
"atlasTemporaryDatabaseUserLifetimeMs",
5152
],
5253
boolean: [
5354
"apiDeprecationErrors",
@@ -90,7 +91,15 @@ const OPTIONS = {
9091
"greedy-arrays": true,
9192
"short-option-groups": false,
9293
},
93-
} as const;
94+
} as Readonly<Options>;
95+
96+
interface Options {
97+
string: string[];
98+
boolean: string[];
99+
array: string[];
100+
alias: Record<string, string>;
101+
configuration: Record<string, boolean>;
102+
}
94103

95104
const ALL_CONFIG_KEYS = new Set(
96105
(OPTIONS.string as readonly string[])
@@ -161,14 +170,15 @@ export interface UserConfig extends CliOptions {
161170
loggers: Array<"stderr" | "disk" | "mcp">;
162171
idleTimeoutMs: number;
163172
notificationTimeoutMs: number;
173+
atlasTemporaryDatabaseUserLifetimeMs: number;
164174
}
165175

166176
export const defaultUserConfig: UserConfig = {
167177
apiBaseUrl: "https://cloud.mongodb.com/",
168178
logPath: getLogPath(),
169179
exportsPath: getExportsPath(),
170-
exportTimeoutMs: 300000, // 5 minutes
171-
exportCleanupIntervalMs: 120000, // 2 minutes
180+
exportTimeoutMs: 5 * 60 * 1000, // 5 minutes
181+
exportCleanupIntervalMs: 2 * 60 * 1000, // 2 minutes
172182
disabledTools: [],
173183
telemetry: "enabled",
174184
readOnly: false,
@@ -177,9 +187,10 @@ export const defaultUserConfig: UserConfig = {
177187
httpPort: 3000,
178188
httpHost: "127.0.0.1",
179189
loggers: ["disk", "mcp"],
180-
idleTimeoutMs: 600000, // 10 minutes
181-
notificationTimeoutMs: 540000, // 9 minutes
190+
idleTimeoutMs: 10 * 60 * 1000, // 10 minutes
191+
notificationTimeoutMs: 9 * 60 * 1000, // 9 minutes
182192
httpHeaders: {},
193+
atlasTemporaryDatabaseUserLifetimeMs: 4 * 60 * 60 * 1000, // 4 hours
183194
};
184195

185196
export const config = setupUserConfig({

src/tools/atlas/connect/connectCluster.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type { AtlasClusterConnectionInfo } from "../../../common/connectionManag
99
import { getDefaultRoleFromConfig } from "../../../common/atlas/roles.js";
1010
import { AtlasArgs } from "../../args.js";
1111

12-
const EXPIRY_MS = 1000 * 60 * 60 * 12; // 12 hours
1312
const addedIpAccessListMessage =
1413
"Note: Your current IP address has been added to the Atlas project's IP access list to enable secure connection.";
1514

@@ -81,7 +80,7 @@ export class ConnectClusterTool extends AtlasToolBase {
8180
const username = `mcpUser${Math.floor(Math.random() * 100000)}`;
8281
const password = await generateSecurePassword();
8382

84-
const expiryDate = new Date(Date.now() + EXPIRY_MS);
83+
const expiryDate = new Date(Date.now() + this.config.atlasTemporaryDatabaseUserLifetimeMs);
8584
const role = getDefaultRoleFromConfig(this.config);
8685

8786
await this.session.apiClient.createDatabaseUser({

tests/unit/common/config.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ describe("config", () => {
4141
{ envVar: "MDB_MCP_HTTP_HOST", property: "httpHost", value: "localhost" },
4242
{ envVar: "MDB_MCP_IDLE_TIMEOUT_MS", property: "idleTimeoutMs", value: 5000 },
4343
{ envVar: "MDB_MCP_NOTIFICATION_TIMEOUT_MS", property: "notificationTimeoutMs", value: 5000 },
44+
{
45+
envVar: "MDB_MCP_ATLAS_TEMPORARY_DATABASE_USER_LIFETIME_MS",
46+
property: "atlasTemporaryDatabaseUserLifetimeMs",
47+
value: 12345,
48+
},
4449
] as const;
4550

4651
for (const { envVar, property, value } of testCases) {
@@ -129,6 +134,10 @@ describe("config", () => {
129134
cli: ["--notificationTimeoutMs", "42"],
130135
expected: { notificationTimeoutMs: "42" },
131136
},
137+
{
138+
cli: ["--atlasTemporaryDatabaseUserLifetimeMs", "12345"],
139+
expected: { atlasTemporaryDatabaseUserLifetimeMs: "12345" },
140+
},
132141
{
133142
cli: ["--telemetry", "enabled"],
134143
expected: { telemetry: "enabled" },

0 commit comments

Comments
 (0)