export_history
Export the history of CSV editing operations to a file for auditing or review.
Instructions
Export operation history to a file.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| session_id | Yes | ||
| file_path | Yes | ||
| format | No | json |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- Main handler for the export_history tool. Retrieves the session, validates it, and calls history_manager.export_history() to write history to a file in JSON or CSV format.
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: {e!s}") if ctx: await ctx.error(f"Failed to export history: {e!s}") return OperationResult( success=False, message="Failed to export history", error=str(e) ).model_dump() - Core logic for exporting history. Supports JSON format (serializes all operations with metadata) and CSV format (flattens operations into a DataFrame). Returns boolean success status.
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: {e!s}") return False - src/csv_editor/server.py:515-520 (registration)Registration of export_history as an MCP tool via @mcp.tool decorator. Defines the tool's signature (session_id, file_path, format) and delegates to the handler.
@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) - src/csv_editor/server.py:474-474 (registration)Import of the export_history handler from the tools.history_operations module into server.py, aliased as _export_history.
from .tools.history_operations import export_history as _export_history