-
Notifications
You must be signed in to change notification settings - Fork 224
fix: use dynamic database lookup instead of hardcoded #mongodb-mcp in isSearchSupported #1091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,10 @@ export interface ConnectionState { | |
| connectedAtlasCluster?: AtlasClusterConnectionInfo; | ||
| } | ||
|
|
||
| const MCP_TEST_DATABASE = "#mongodb-mcp"; | ||
| // Fallback database for search index check when no accessible database is found | ||
| const MCP_FALLBACK_TEST_DATABASE = "#mongodb-mcp"; | ||
| // System databases that should be skipped when searching for accessible databases | ||
| const SYSTEM_DATABASES = new Set(["admin", "local", "config", "$external"]); | ||
|
|
||
| export const defaultDriverOptions: ConnectionInfo["driverOptions"] = { | ||
| readConcern: { | ||
|
|
@@ -62,7 +65,10 @@ export class ConnectionStateConnected implements ConnectionState { | |
| // with a cursor otherwise will throw an Error. | ||
| // the Search Index Management Service might not be ready yet, but | ||
| // we assume that the agent can retry in that situation. | ||
| await this.serviceProvider.getSearchIndexes(MCP_TEST_DATABASE, "test"); | ||
| // Try to find an accessible database dynamically instead of using | ||
| // a hardcoded database name that may not exist or be accessible. | ||
| const databaseName = await this.findAccessibleDatabase(); | ||
| await this.serviceProvider.getSearchIndexes(databaseName, "test"); | ||
| this._isSearchSupported = true; | ||
| } catch { | ||
| this._isSearchSupported = false; | ||
|
|
@@ -71,6 +77,27 @@ export class ConnectionStateConnected implements ConnectionState { | |
|
|
||
| return this._isSearchSupported; | ||
| } | ||
|
|
||
| /** | ||
| * Find an accessible database for search index operations. | ||
| * Tries to list databases and find a non-system database that the user can access. | ||
| */ | ||
| private async findAccessibleDatabase(): Promise<string> { | ||
| try { | ||
| // List all databases from admin | ||
| const dbs = (await this.serviceProvider.listDatabases("")).databases as { | ||
| name: string; | ||
| }[]; | ||
| // Find first non-system database | ||
| const accessibleDb = dbs.find((db) => !SYSTEM_DATABASES.has(db.name)); | ||
| if (accessibleDb) { | ||
| return accessibleDb.name; | ||
|
Comment on lines
+88
to
+94
|
||
| } | ||
| } catch { | ||
| // If listing databases fails (e.g., permission issues), fall back to default | ||
| } | ||
| return MCP_FALLBACK_TEST_DATABASE; | ||
|
Comment on lines
+85
to
+99
|
||
| } | ||
|
Comment on lines
+96
to
+100
|
||
| } | ||
|
|
||
| export interface ConnectionStateConnecting extends ConnectionState { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says "List all databases from admin" but the call is
listDatabases(""). If the empty string is intentional (as inlist-databasestool), please update the comment to match the actual behavior to avoid confusion for future maintainers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, the comment is misleading. The call listDatabases("") uses an empty string to target the admin database. I'll update the comment to be more accurate.