8000 fix(lint-errors): resolve ESLint formatting and type safety issues · mongodb-js/mongodb-mcp-server@9511652 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9511652

Browse files
committed
fix(lint-errors): resolve ESLint formatting and type safety issues
1 parent 06f65f8 commit 9511652

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ The MongoDB MCP Server can be configured using multiple methods, with the follow
267267
| `logPath` | Folder to store logs. |
268268
| `disabledTools` | An array of tool names, operation types, and/or categories of tools that will be disabled. |
269269
| `readOnly` | When set to true, only allows read and metadata operation types, disabling create/update/delete operations. |
270-
| `indexCheck` | When set to true, enforces that query operations must use an index, rejecting queries that perform a collection scan. |
270+
| `indexCheck` | When set to true, enforces that query operations must use an index, rejecting queries that perform a collection scan. |
271271
| `telemetry` | When set to disabled, disables telemetry collection. |
272272

273273
#### Log Path

src/helpers/indexCheck.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import { Document } from "mongodb";
22
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
33
import { ErrorCodes, MongoDBError } from "../errors.js";
44

5-
65
/**
76
* Check if the query plan uses an index
87
* @param explainResult The result of the explain query
98
* @returns true if an index is used, false if it's a full collection scan
109
*/
1110
export function usesIndex(explainResult: Document): boolean {
12-
const stage = explainResult?.queryPlanner?.winningPlan?.stage;
13-
const inputStage = explainResult?.queryPlanner?.winningPlan?.inputStage;
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;
1415

1516
// Check for index scan stages (including MongoDB 8.0+ stages)
1617
const indexScanStages = [
@@ -20,14 +21,14 @@ export function usesIndex(explainResult: Document): boolean {
2021
"EXPRESS_CLUSTERED_IXSCAN",
2122
"EXPRESS_UPDATE",
2223
"EXPRESS_DELETE",
23-
"IDHACK"
24+
"IDHACK",
2425
];
2526

26-
if (indexScanStages.includes(stage)) {
27+
if (stage && indexScanStages.includes(stage)) {
2728
return true;
2829
}
2930

30-
if (inputStage && indexScanStages.includes(inputStage.stage)) {
31+
if (inputStage && inputStage.stage && indexScanStages.includes(inputStage.stage as string)) {
3132
return true;
3233
}
3334

@@ -65,7 +66,10 @@ export async function checkIndexUsage(
6566
const explainResult = await explainCallback();
6667

6768
if (!usesIndex(explainResult)) {
68-
throw new MongoDBError(ErrorCodes.ForbiddenCollscan, getIndexCheckErrorMessage(database, collection, operation));
69+
throw new MongoDBError(
70+
ErrorCodes.ForbiddenCollscan,
71+
getIndexCheckErrorMessage(database, collection, operation)
72+
);
6973
}
7074
} catch (error) {
7175
if (error instanceof Error && error.message.includes("Index check failed")) {
@@ -76,4 +80,4 @@ export async function checkIndexUsage(
7680
// This avoids blocking normal queries in special cases (e.g., permission issues)
7781
console.warn(`Index check failed to execute explain for ${operation} on ${database}.${collection}:`, error);
7882
}
79-
}
83+
}

src/tools/mongodb/mongodbTool.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ export abstract class MongoDBToolBase extends ToolBase {
6464
],
6565
isError: true,
6666
};
67+
case ErrorCodes.ForbiddenCollscan:
68+
return {
69+
content: [
70+
{
71+
type: "text",
72+
text: error.message,
73+
},
74+
],
75+
isError: true,
76+
};
6777
}
6878
}
6979

src/tools/mongodb/read/find.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ export class FindTool extends MongoDBToolBase {
4444
// Check if find operation uses an index if enabled
4545
if (this.config.indexCheck) {
4646
await checkIndexUsage(provider, database, collection, "find", async () => {
47-
return provider
48-
.find(database, collection, filter, { projection, limit, sort })
49-
.explain("queryPlanner");
47+
return provider.find(database, collection, filter, { projection, limit, sort }).explain("queryPlanner");
5048
});
5149
}
5250

tests/unit/indexCheck.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,4 @@ describe("indexCheck", () => {
146146
expect(message).toContain("MDB_MCP_INDEX_CHECK");
147147
});
148148
});
149-
});
149+
});

0 commit comments

Comments
 (0)
0