export_history
Generate a file containing operation history from the CSV Editor MCP server. Specify session ID, file path, and format to export detailed records of data manipulation actions.
Instructions
Export operation history to a file.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| format | No | json | |
| session_id | Yes |
Implementation Reference
- Primary handler function for the 'export_history' tool. Retrieves the session, checks for history_manager, and calls its export_history method to perform the export.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()
- src/csv_editor/server.py:542-550 (registration)Registers the 'export_history' tool in the FastMCP server using the @mcp.tool decorator. This defines the tool's interface and delegates execution to the imported 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)
- Helper method in HistoryManager class that implements the core export logic, writing history data 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