clear_history
Remove all operation history from a CSV editing session to reset the undo/redo stack and maintain data privacy.
Instructions
Clear all operation history for a session.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| session_id | Yes |
Implementation Reference
- Main handler function for the clear_history tool. Retrieves the CSV session, validates it has a history_manager, and invokes clear_history on the manager. Returns OperationResult indicating success or failure.async def clear_history( session_id: str, ctx: Context = None ) -> Dict[str, Any]: """ Clear all operation history for a session. Args: session_id: Session identifier ctx: FastMCP context Returns: Dict with success status """ try: manager = get_session_manager() session = manager.get_session(session_id) if not session: return OperationResult( success=False, message="Session not found", error=f"No session with ID: {session_id}" ).model_dump() if not session.history_manager: return OperationResult( success=False, message="History is not enabled for this session", error="History management is disabled" ).model_dump() if ctx: await ctx.info(f"Clearing history for session {session_id}") session.history_manager.clear_history() return OperationResult( success=True, message="History cleared successfully", session_id=session_id ).model_dump() except Exception as e: logger.error(f"Error clearing history: {str(e)}") if ctx: await ctx.error(f"Failed to clear history: {str(e)}") return OperationResult( success=False, message="Failed to clear history", error=str(e) ).model_dump()
- src/csv_editor/server.py:534-540 (registration)Registers the clear_history tool with FastMCP using the @mcp.tool decorator. This is a thin wrapper that delegates to the imported _clear_history implementation.@mcp.tool async def clear_history( session_id: str, ctx: Context = None ) -> Dict[str, Any]: """Clear all operation history for a session.""" return await _clear_history(session_id, ctx)
- Pydantic BaseModel defining the standardized output schema (OperationResult) used by clear_history and other tools for consistent response structure and validation.class OperationResult(BaseModel): """Result of a data operation.""" success: bool = Field(..., description="Whether operation succeeded") message: str = Field(..., description="Result message") session_id: Optional[str] = Field(None, description="Session ID") rows_affected: Optional[int] = Field(None, description="Number of rows affected") columns_affected: Optional[List[str]] = Field(None, description="Columns affected") data: Optional[Dict[str, Any]] = Field(None, description="Additional result data") error: Optional[str] = Field(None, description="Error message if failed") warnings: Optional[List[str]] = Field(None, description="Warning messages")
- Core helper method in HistoryManager class that clears in-memory history structures and removes any persistent storage files associated with the session's history.def clear_history(self): """Clear all history.""" self.history.clear() self.redo_stack.clear() self.current_index = -1 # Clean up files if self.storage_type != HistoryStorage.MEMORY: # Remove history file for ext in ["json", "pkl"]: history_file = self._get_history_file_path(ext) if os.path.exists(history_file): os.remove(history_file) # Remove snapshot files snapshot_dir = os.path.join(self.history_dir, "snapshots", self.session_id) if os.path.exists(snapshot_dir): import shutil shutil.rmtree(snapshot_dir) logger.info(f"Cleared history for session {self.session_id}")