delete_graph
Remove a knowledge graph from the Mnemosyne MCP server with optional backup creation and required confirmation for safety.
Instructions
Delete a graph after confirmation.
Args:
graph_id: Graph to delete
confirm: Confirmation flag (required for safety)
backup: Whether to create backup before deletion
ctx: MCP context for user authentication
Returns:
JSON response with deletion confirmation and backup info
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| graph_id | Yes | ||
| confirm | No | ||
| backup | No |
Implementation Reference
- The primary handler implementation for the 'delete_graph' MCP tool. Decorated with @mcp_server.tool(), it enforces confirmation safety check, validates user access via session, makes a DELETE request to the API endpoint /graphs/{graph_id} with optional backup parameter, constructs McpDeleteGraphResponse from the API reply, and handles errors with detailed guidance.@mcp_server.tool() async def delete_graph( graph_id: str, ctx: Context, confirm: bool = False, backup: bool = True ) -> str: """ Delete a graph after confirmation. Args: graph_id: Graph to delete confirm: Confirmation flag (required for safety) backup: Whether to create backup before deletion ctx: MCP context for user authentication Returns: JSON response with deletion confirmation and backup info """ operation_id = str(uuid.uuid4()) with logger.operation_context("delete_graph", operation_id=operation_id, graph_id=graph_id): try: # Safety check - require explicit confirmation if not confirm: error_response = McpErrorResponse( error_type="CONFIRMATION_REQUIRED", error_message="Graph deletion requires explicit confirmation", help={ "required_parameter": "confirm=true", "warning": "This operation is irreversible", "suggestion": "Set confirm=true to proceed with deletion" } ) return error_response.to_json() # Get session context auth_token, session_data = await get_session_context(ctx) user_id = auth_token # Validate graph access if session_data: accessible_graphs = session_data.get("accessible_graphs", []) if graph_id not in accessible_graphs: logger.warning( "Graph access denied for deletion", extra_context={ "user_id": user_id, "graph_id": graph_id, "accessible_graphs": accessible_graphs } ) error_response = McpErrorResponse( error_type="GRAPH_ACCESS_DENIED", error_message=f"Access denied for graph {graph_id}", help={ "graph_id": graph_id, "suggestion": "Use list_graphs to see accessible graphs" } ) return error_response.to_json() logger.info( "Deleting graph", extra_context={ "operation_id": operation_id, "user_id": user_id, "graph_id": graph_id, "backup": backup } ) # Call API server api_response = await api_client._make_request( "DELETE", f"/graphs/{graph_id}", headers={"Authorization": f"Bearer {auth_token}"}, params={"backup": backup} ) logger.info( "Graph deleted successfully", extra_context={ "operation_id": operation_id, "graph_id": graph_id, "user_id": user_id } ) # Transform to enhanced MCP format mcp_response = McpDeleteGraphResponse.from_api_response( api_response, operation_id=operation_id, graph_id=graph_id, user_id=user_id ) return mcp_response.to_json() except Exception as e: logger.error( "Graph deletion failed", extra_context={ "operation_id": operation_id, "graph_id": graph_id, "error": str(e), "error_type": type(e).__name__ } ) error_response = McpErrorResponse.from_exception( e, error_type="GRAPH_DELETION_FAILED", help_info={ "operation_id": operation_id, "graph_id": graph_id, "suggestions": [ "Check that graph exists and is accessible", "Verify deletion permissions", "Ensure graph is not currently in use" ] } ) return error_response.to_json()
- The McpDeleteGraphResponse dataclass defines the structured output schema for delete_graph operations, including operation summary, deletion details, backup information, and cleanup statistics. Includes a from_api_response classmethod for transforming raw API responses.class McpDeleteGraphResponse(McpResponseObject): """Response for graph deletion operations""" operation_summary: McpOperationSummary deletion_info: Dict[str, Any] backup_info: Optional[Dict[str, Any]] cleanup_results: Dict[str, Any] success: bool = True @classmethod def from_api_response(cls, api_response: Dict[str, Any], operation_id: str, graph_id: str, user_id: Optional[str] = None) -> 'McpDeleteGraphResponse': """Transform API graph deletion response to MCP format""" import datetime return cls( operation_summary=McpOperationSummary( operation_type="delete_graph", operation_id=operation_id, timestamp=datetime.datetime.now().isoformat(), user_id=user_id ), deletion_info={ "graph_id": graph_id, "deleted_at": datetime.datetime.now().isoformat(), "confirmed": True }, backup_info=api_response.get("backup_info"), cleanup_results={ "files_removed": api_response.get("files_removed", 0), "space_freed_mb": api_response.get("space_freed_mb", 0), "cache_cleared": True } )