|
| 1 | +import { z } from "zod"; |
| 2 | +import { tool } from "../../tool.js"; |
| 3 | +import { mcpError, toContent } from "../../util.js"; |
| 4 | +import { getDocuments } from "../../../gcp/firestore.js"; |
| 5 | +import { FirestoreDelete } from "../../../firestore/delete.js"; |
| 6 | + |
| 7 | +export const delete_document = tool( |
| 8 | + { |
| 9 | + name: "delete_document", |
| 10 | + description: |
| 11 | + "Deletes a Firestore documents from a database in the current project by full document paths. Use this if you know the exact path of a document.", |
| 12 | + inputSchema: z.object({ |
| 13 | + // TODO: Support configurable database |
| 14 | + // database: z |
| 15 | + // .string() |
| 16 | + // .nullish() |
| 17 | + // .describe("Database id to use. Defaults to `(default)` if unspecified."), |
| 18 | + path: z |
| 19 | + .string() |
| 20 | + .describe( |
| 21 | + "A document path (e.g. `collectionName/documentId` or `parentCollection/parentDocument/collectionName/documentId`)", |
| 22 | + ), |
| 23 | + }), |
| 24 | + annotations: { |
| 25 | + title: "Delete Firestore document", |
| 26 | + destructiveHint: true, |
| 27 | + }, |
| 28 | + _meta: { |
| 29 | + requiresAuth: true, |
| 30 | + requiresProject: true, |
| 31 | + }, |
| 32 | + }, |
| 33 | + async ({ path }, { projectId }) => { |
| 34 | + // database ??= "(default)"; |
| 35 | + const { documents, missing } = await getDocuments(projectId!, [path]); |
| 36 | + if (missing.length > 0 && documents && documents.length === 0) { |
| 37 | + return mcpError(`None of the specified documents were found in project '${projectId}'`); |
| 38 | + } |
| 39 | + |
| 40 | + const firestoreDelete = new FirestoreDelete(projectId!, path, { databaseId: "(default)" }); |
| 41 | + |
| 42 | + await firestoreDelete.execute(); |
| 43 | + |
| 44 | + const { documents: postDeleteDocuments, missing: postDeleteMissing } = await getDocuments( |
| 45 | + projectId!, |
| 46 | + [path], |
| 47 | + ); |
| 48 | + if (postDeleteMissing.length > 0 && postDeleteDocuments.length === 0) { |
| 49 | + return toContent(`Successfully removed document located at : ${path}`); |
| 50 | + } |
| 51 | + |
| 52 | + return mcpError(`Failed to remove document located at : ${path}`); |
| 53 | + }, |
| 54 | +); |
0 commit comments