-
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 all commits
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 using the current connection context | ||
| 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; | ||
| } | ||
| } 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.
findAccessibleDatabase()selects the first non-system DB name fromlistDatabases(), but that does not guarantee the user can actually access/read that database (a role may havelistDatabaseson the cluster but privileges only on specific DBs). This can lead to false negatives inisSearchSupported()ifgetSearchIndexes()is attempted against an unauthorized DB. Consider iterating candidate DBs and picking one that is actually usable (e.g., trylistCollections/a lightweight read or directly attemptgetSearchIndexesand continue on auth errors).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.
Thanks for the review. The suggestion to verify actual accessibility by trying listCollections or getSearchIndexes is valid, but implementing it would significantly expand the PR scope.
The current approach is a reasonable first step: it uses listDatabases which is already permitted for users who can call it, and filters out system databases. If listDatabases succeeds, the user has cluster-level access and any non-system database is likely accessible.
The fallback to #mongodb-mcp only triggers when listDatabases fails (permission denied), which means the user lacks cluster-level privileges. In that case, trying multiple databases would likely all fail with auth errors anyway.
If you'd prefer the more thorough approach, I can explore it as a follow-up, but it would be a larger change.