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 };
}