-
Notifications
You must be signed in to change notification settings - Fork 104
feat(index-check): add index check functionality before query #309
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
Merged
kmruiz
merged 8 commits into
mongodb-js:main
from
Crushdada:feature/index-check-before-query
Jun 26, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
91e7b9b
feat(index-check): add index check functionality before query
Crushdada 92e68b0
feat(errors): enhance index check and proper error handling
Crushdada e8e3065
fix(lint-errors): resolve ESLint formatting and type safety issues
Crushdada 2dd9f95
Update src/helpers/indexCheck.ts
Crushdada af5e401
fix: use runCommandWithCheck for database commands
Crushdada 3933710
feat: add integration tests for indexCheck config
Crushdada 16a9130
fix: code style check failed
Crushdada e5ce5fc
Merge branch 'main' into feature/index-check-before-query
kmruiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Document } from "mongodb"; | ||
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver"; | ||
import { ErrorCodes, MongoDBError } from "../errors.js"; | ||
|
||
/** | ||
* Check if the query plan uses an index | ||
* @param explainResult The result of the explain query | ||
* @returns true if an index is used, false if it's a full collection scan | ||
*/ | ||
export function usesIndex(explainResult: Document): boolean { | ||
const queryPlanner = explainResult?.queryPlanner as Document | undefined; | ||
const winningPlan = queryPlanner?.winningPlan as Document | undefined; | ||
const stage = winningPlan?.stage as string | undefined; | ||
const inputStage = winningPlan?.inputStage as Document | undefined; | ||
|
||
// Check for index scan stages (including MongoDB 8.0+ stages) | ||
const indexScanStages = [ | ||
"IXSCAN", | ||
"COUNT_SCAN", | ||
"EXPRESS_IXSCAN", | ||
"EXPRESS_CLUSTERED_IXSCAN", | ||
"EXPRESS_UPDATE", | ||
"EXPRESS_DELETE", | ||
"IDHACK", | ||
]; | ||
|
||
if (stage && indexScanStages.includes(stage)) { | ||
return true; | ||
} | ||
|
||
if (inputStage && inputStage.stage && indexScanStages.includes(inputStage.stage as string)) { | ||
return true; | ||
} | ||
|
||
// Recursively check deeper stages | ||
if (inputStage && inputStage.inputStage) { | ||
return usesIndex({ queryPlanner: { winningPlan: inputStage } }); | ||
} | ||
|
||
if (stage === "COLLSCAN") { | ||
return false; | ||
} | ||
|
||
// Default to false (conservative approach) | ||
return false; | ||
} | ||
|
||
/** | ||
* Generate an error message for index check failure | ||
*/ | ||
export function getIndexCheckErrorMessage(database: string, collection: string, operation: string): string { | ||
return `Index check failed: The ${operation} operation on "${database}.${collection}" performs a collection scan (COLLSCAN) instead of using an index. Consider adding an index for better performance. Use 'explain' tool for query plan analysis or 'collection-indexes' to view existing indexes. To disable this check, set MDB_MCP_INDEX_CHECK to false.`; | ||
} | ||
|
||
/** | ||
* Generic function to perform index usage check | ||
*/ | ||
export async function checkIndexUsage( | ||
provider: NodeDriverServiceProvider, | ||
database: string, | ||
collection: string, | ||
operation: string, | ||
explainCallback: () => Promise<Document> | ||
): Promise<void> { | ||
try { | ||
const explainResult = await explainCallback(); | ||
|
||
if (!usesIndex(explainResult)) { | ||
throw new MongoDBError( | ||
ErrorCodes.ForbiddenCollscan, | ||
getIndexCheckErrorMessage(database, collection, operation) | ||
); | ||
} | ||
} catch (error) { | ||
if (error instanceof MongoDBError && error.code === ErrorCodes.ForbiddenCollscan) { | ||
throw error; | ||
} | ||
|
||
// If explain itself fails, log but do not prevent query execution | ||
// This avoids blocking normal queries in special cases (e.g., permission issues) | ||
console.warn(`Index check failed to execute explain for ${operation} on ${database}.${collection}:`, error); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.