|
| 1 | +import { Document } from "mongodb"; |
| 2 | +import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver"; |
| 3 | +import { ErrorCodes, MongoDBError } from "../errors.js"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Check if the query plan uses an index |
| 7 | + * @param explainResult The result of the explain query |
| 8 | + * @returns true if an index is used, false if it's a full collection scan |
| 9 | + */ |
| 10 | +export function usesIndex(explainResult: Document): boolean { |
| 11 | + const queryPlanner = explainResult?.queryPlanner as Document | undefined; |
| 12 | + const winningPlan = queryPlanner?.winningPlan as Document | undefined; |
| 13 | + const stage = winningPlan?.stage as string | undefined; |
| 14 | + const inputStage = winningPlan?.inputStage as Document | undefined; |
| 15 | + |
| 16 | + // Check for index scan stages (including MongoDB 8.0+ stages) |
| 17 | + const indexScanStages = [ |
| 18 | + "IXSCAN", |
| 19 | + "COUNT_SCAN", |
| 20 | + "EXPRESS_IXSCAN", |
| 21 | + "EXPRESS_CLUSTERED_IXSCAN", |
| 22 | + "EXPRESS_UPDATE", |
| 23 | + "EXPRESS_DELETE", |
| 24 | + "IDHACK", |
| 25 | + ]; |
| 26 | + |
| 27 | + if (stage && indexScanStages.includes(stage)) { |
| 28 | + return true; |
| 29 | + } |
| 30 | + |
| 31 | + if (inputStage && inputStage.stage && indexScanStages.includes(inputStage.stage as string)) { |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + // Recursively check deeper stages |
| 36 | + if (inputStage && inputStage.inputStage) { |
| 37 | + return usesIndex({ queryPlanner: { winningPlan: inputStage } }); |
| 38 | + } |
| 39 | + |
| 40 | + if (stage === "COLLSCAN") { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + // Default to false (conservative approach) |
| 45 | + return false; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Generate an error message for index check failure |
| 50 | + */ |
| 51 | +export function getIndexCheckErrorMessage(database: string, collection: string, operation: string): string { |
| 52 | + 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.`; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Generic function to perform index usage check |
| 57 | + */ |
| 58 | +export async function checkIndexUsage( |
| 59 | + provider: NodeDriverServiceProvider, |
| 60 | + database: string, |
| 61 | + collection: string, |
| 62 | + operation: string, |
| 63 | + explainCallback: () => Promise<Document> |
| 64 | +): Promise<void> { |
| 65 | + try { |
| 66 | + const explainResult = await explainCallback(); |
| 67 | + |
| 68 | + if (!usesIndex(explainResult)) { |
| 69 | + throw new MongoDBError( |
| 70 | + ErrorCodes.ForbiddenCollscan, |
| 71 | + getIndexCheckErrorMessage(database, collection, operation) |
| 72 | + ); |
| 73 | + } |
| 74 | + } catch (error) { |
| 75 | + if (error instanceof MongoDBError && error.code === ErrorCodes.ForbiddenCollscan) { |
| 76 | + throw error; |
| 77 | + } |
| 78 | + |
| 79 | + // If explain itself fails, log but do not prevent query execution |
| 80 | + // This avoids blocking normal queries in special cases (e.g., permission issues) |
| 81 | + console.warn(`Index check failed to execute explain for ${operation} on ${database}.${collection}:`, error); |
| 82 | + } |
| 83 | +} |
0 commit comments