delete_document
Remove a specific document by its ID from a collection in Typesense databases to manage and maintain accurate data storage and organization.
Instructions
Deletes a single document by its ID from a specific collection.
Args:
ctx (Context): The MCP context.
collection_name (str): The name of the collection.
document_id (str): The ID of the document to delete.
Returns:
dict | str: The deleted document dictionary or an error message string.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection_name | Yes | ||
| document_id | Yes |
Input Schema (JSON Schema)
{
"properties": {
"collection_name": {
"title": "Collection Name",
"type": "string"
},
"document_id": {
"title": "Document Id",
"type": "string"
}
},
"required": [
"collection_name",
"document_id"
],
"title": "delete_documentArguments",
"type": "object"
}
Implementation Reference
- main.py:704-736 (handler)The handler function for the 'delete_document' tool, decorated with @mcp.tool() which also serves as registration. It deletes a document by ID from a Typesense collection using the client, with error handling for not found cases and other exceptions.@mcp.tool() async def delete_document(ctx: Context, collection_name: str, document_id: str) -> dict | str: """ Deletes a single document by its ID from a specific collection. Args: ctx (Context): The MCP context. collection_name (str): The name of the collection. document_id (str): The ID of the document to delete. Returns: dict | str: The deleted document dictionary or an error message string. """ if not collection_name: return "Error: collection_name parameter is required." if not document_id: return "Error: document_id parameter is required." try: client: typesense.Client = ctx.request_context.lifespan_context.client # NOTE: Assuming document delete is synchronous based on collection delete pattern. deleted_doc = client.collections[collection_name].documents[document_id].delete() return deleted_doc except typesense.exceptions.ObjectNotFound: # Could be collection or document not found return f"Error: Collection '{collection_name}' or Document ID '{document_id}' not found." except typesense.exceptions.TypesenseClientError as e: print(f"Error deleting document '{document_id}' from '{collection_name}': {e}") return f"Error deleting document '{document_id}' from '{collection_name}': {e}" except Exception as e: print(f"An unexpected error occurred while deleting document '{document_id}' from '{collection_name}': {e}") return f"An unexpected error occurred: {e}"