from __future__ import annotations from typing import Mapping from backend.mcp_server.common.database import delete_all_documents, delete_document from backend.mcp_server.common.tenant import TenantContext from backend.mcp_server.common.utils import ToolValidationError, tool_handler @tool_handler("rag.delete") async def rag_delete(context: TenantContext, payload: Mapping[str, object]) -> dict[str, object]: """ Delete one document by ID or purge all documents for the tenant. """ document_id = payload.get("document_id") delete_all = bool(payload.get("delete_all", False)) if delete_all: deleted = delete_all_documents(context.tenant_id) return { "tenant_id": context.tenant_id, "deleted_count": deleted, "mode": "all", } if document_id is None: raise ToolValidationError("document_id is required unless delete_all=true") try: doc_id_value = int(document_id) except (TypeError, ValueError): raise ToolValidationError("document_id must be an integer") if doc_id_value <= 0: raise ToolValidationError("document_id must be positive") deleted = delete_document(context.tenant_id, doc_id_value) if not deleted: raise ToolValidationError(f"Document {doc_id_value} not found or access denied") return { "tenant_id": context.tenant_id, "document_id": doc_id_value, "deleted": True, "mode": "single", }