delete_entity
Remove an entity and its associated relationships from the knowledge graph to maintain accurate Infrastructure-as-Code component tracking.
Instructions
Remove an entity and its relationships from the knowledge graph
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | Entity ID |
Input Schema (JSON Schema)
{
"description": "Remove an entity and its relationships from the knowledge graph",
"properties": {
"id": {
"description": "Entity ID",
"type": "string"
}
},
"required": [
"id"
],
"type": "object"
}
Implementation Reference
- Primary MCP tool handler for 'delete_entity' that orchestrates logging, execution, and error handling.async def handle_delete_entity(db: Any, arguments: Dict[str, Any], operation_id: str) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: """Handle delete_entity tool.""" try: logger.info( "Deleting entity", extra={ "entity_id": arguments.get("entity_id"), "operation_id": operation_id, }, ) # Execute deletion return execute_delete_entity(db, arguments) except Exception as e: error_msg = f"Failed to delete entity: {str(e)}" logger.error(error_msg, extra={"operation_id": operation_id}) raise McpError( types.ErrorData( code=types.INTERNAL_ERROR, message=error_msg, data={ "tool": "delete_entity", "operation_id": operation_id, }, ) )
- JSON schema definition for the 'delete_entity' tool input parameters."delete_entity": { "type": "object", "description": "Remove an entity and its relationships from the knowledge graph", "required": ["id"], "properties": {"id": {"type": "string", "description": "Entity ID"}}, },
- src/iac_memory_mcp_server/tools/entity.py:150-155 (registration)Registration of the 'delete_entity' handler in the entity tool handlers dictionary.entity_tool_handlers = { "create_entity": handle_create_entity, "update_entity": handle_update_entity, "delete_entity": handle_delete_entity, "view_relationships": handle_view_relationships, }
- Helper function that wraps the core delete_entity DB operation and formats the MCP response.def execute_delete_entity( db: DatabaseManager, arguments: Dict[str, Any] ) -> List[TextContent]: """Execute delete entity operation.""" logger.info("Deleting entity", extra={"args": arguments}) success = delete_entity(db, arguments["id"]) if not success: raise DatabaseError(f"Entity not found: {arguments['id']}") return [TextContent(type="text", text=f"Deleted entity {arguments['id']}")]
- Core database deletion logic for entities and associated observations.def delete_entity(db: DatabaseManager, entity_id: str) -> bool: """Delete an entity and its related observations.""" try: with db.get_connection() as conn: conn.execute("BEGIN TRANSACTION") conn.execute("DELETE FROM observations WHERE entity_id = ?", (entity_id,)) cursor = conn.execute("DELETE FROM entities WHERE id = ?", (entity_id,)) affected = cursor.rowcount > 0 conn.commit() return affected except sqlite3.Error as e: raise DatabaseError(f"Delete failed: {str(e)}")