fix(mongodb): support collection drop shell command

This commit is contained in:
Guoyu Su authored and GitHub committed 2026-07-09 14:16:32 +08:00
1 parent 315c6e235b
commit ca9e11217e
14 files changed
+206 -14

No files matched your search

@@ -39,7 +39,7 @@ export interface MongoCollectionStatsCommand {
scale?: number;
}
type MongoWriteKind = "insert" | "update" | "delete" | "createIndex" | "dropIndex" | "dropIndexes";
type MongoWriteKind = "insert" | "update" | "delete" | "createIndex" | "dropIndex" | "dropIndexes" | "dropCollection";
export type MongoCommand =
| ({ kind: "find" } & MongoFindCommand)
@@ -54,7 +54,8 @@ export type MongoCommand =
| { kind: "delete"; collection: string; filter: string; many: boolean }
| { kind: "createIndex"; collection: string; keys: string; options?: string }
| { kind: "dropIndex"; collection: string; index: string }
| { kind: "dropIndexes"; collection: string; indexes?: string };
| { kind: "dropIndexes"; collection: string; indexes?: string }
| { kind: "dropCollection"; collection: string };
export type MongoWriteCommand = Extract<MongoCommand, { kind: MongoWriteKind }>;
@@ -340,6 +341,13 @@ export function parseMongoWriteCommand(input: string): MongoWriteCommand | null
return indexes !== null ? { kind: "dropIndexes", collection: dropIndexes.collection, ...(indexes ? { indexes } : {}) } : null;
}
const dropCollection = parseCollectionMethodTarget(source, "drop");
if (dropCollection) {
const args = parseMethodArgs(source, dropCollection.methodCallIndex);
if (!args || args.some((arg) => arg.trim())) return null;
return { kind: "dropCollection", collection: dropCollection.collection };
}
return null;
}
@@ -427,6 +435,12 @@ export function evaluateMongoWriteSafety(command: MongoWriteCommand, options: Mo
reason: "MongoDB dropIndexes() without a specific single index requires DBX_MCP_ALLOW_DANGEROUS_SQL=1.",
};
}
if (!options.allowDangerous && command.kind === "dropCollection") {
return {
allowed: false,
reason: "MongoDB drop() requires DBX_MCP_ALLOW_DANGEROUS_SQL=1.",
};
}
return { allowed: true };
}
+5 -1
View File
@@ -2246,7 +2246,8 @@ export const useQueryStore = defineStore("query", () => {
case "delete":
case "createIndex":
case "dropIndex":
case "dropIndexes": {
case "dropIndexes":
case "dropCollection": {
if (options?.mongoSafety) {
const safety = evaluateMongoWriteSafety(mongoCommand, options.mongoSafety);
if (!safety.allowed) throw new Error(safety.reason);
@@ -2270,6 +2271,9 @@ export const useQueryStore = defineStore("query", () => {
} else if (mongoCommand.kind === "dropIndex" || mongoCommand.kind === "dropIndexes") {
const result = await api.mongoDropIndexes(tab.connectionId, currentDatabase, mongoCommand.collection, mongoCommand.kind === "dropIndex" ? mongoCommand.index : mongoCommand.indexes, mongoCommand.kind === "dropIndex");
allResults.push(markQueryResultRowsRaw(annotateQueryResultSource(mongoDroppedIndexesToQueryResult(result.dropped_names, performance.now() - commandStartedAt), sourceStatement)));
} else if (mongoCommand.kind === "dropCollection") {
await api.mongoDropCollection(tab.connectionId, currentDatabase, mongoCommand.collection);
allResults.push(markQueryResultRowsRaw(annotateQueryResultSource(mongoWriteToQueryResult(1, performance.now() - commandStartedAt), sourceStatement)));
} else {
const result = await api.mongoDeleteDocuments(tab.connectionId, currentDatabase, mongoCommand.collection, mongoCommand.filter, mongoCommand.many);
allResults.push(markQueryResultRowsRaw(annotateQueryResultSource(mongoWriteToQueryResult(result.affected_rows, performance.now() - commandStartedAt), sourceStatement)));