export_history
Export CSV editing history to a file for backup, analysis, or documentation purposes. Supports JSON format and requires session ID and file path.
Instructions
Export operation history to a file.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| session_id | Yes | ||
| file_path | Yes | ||
| format | No | json |
Implementation Reference
- Core implementation of the export_history tool logic, which serializes and writes the session's operation history to a JSON or CSV file.def export_history(self, file_path: str, format: str = "json") -> bool: """Export history to a file.""" try: if format == "json": data = { "session_id": self.session_id, "exported_at": datetime.utcnow().isoformat(), "total_operations": len(self.history), "current_position": self.current_index, "operations": self.get_history() } with open(file_path, 'w') as f: json.dump(data, f, indent=2) elif format == "csv": # Export as CSV with operation details history_data = [] for entry in self.history: history_data.append({ "timestamp": entry.timestamp.isoformat(), "operation_type": entry.operation_type, "details": json.dumps(entry.details), "has_snapshot": entry.data_snapshot is not None }) df = pd.DataFrame(history_data) df.to_csv(file_path, index=False) logger.info(f"Exported history to {file_path}") return True except Exception as e: logger.error(f"Error exporting history: {str(e)}") return False
- src/csv_editor/server.py:542-550 (registration)MCP tool registration for 'export_history' using @mcp.tool decorator, which delegates to the implementation in history_operations.@mcp.tool async def export_history( session_id: str, file_path: str, format: str = "json", ctx: Context = None ) -> Dict[str, Any]: """Export operation history to a file.""" return await _export_history(session_id, file_path, format, ctx)
- Helper function _export_history that handles session retrieval, validation, and delegation to HistoryManager.export_history, returning standardized OperationResult.async def export_history( session_id: str, file_path: str, format: str = "json", ctx: Context = None ) -> Dict[str, Any]: """ Export operation history to a file. Args: session_id: Session identifier file_path: Path to export history to format: Export format ('json' or 'csv') 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"Exporting history for session {session_id} to {file_path}") success = session.history_manager.export_history(file_path, format) if success: return OperationResult( success=True, message=f"History exported to {file_path}", session_id=session_id, data={"file_path": file_path, "format": format} ).model_dump() else: return OperationResult( success=False, message="Failed to export history", error="Export operation failed" ).model_dump() except Exception as e: logger.error(f"Error exporting history: {str(e)}") if ctx: await ctx.error(f"Failed to export history: {str(e)}") return OperationResult( success=False, message="Failed to export history", error=str(e) ).model_dump()