get_history
Retrieve the operation history for a specific session in the CSV Editor MCP server, using a session ID to track and manage data processing actions effectively.
Instructions
Get operation history for a session.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | ||
| session_id | Yes |
Implementation Reference
- src/csv_editor/server.py:516-523 (registration)Registration of the 'get_history' MCP tool using @mcp.tool decorator. Thin wrapper delegating to _get_operation_history.@mcp.tool async def get_history( session_id: str, limit: Optional[int] = None, ctx: Context = None ) -> Dict[str, Any]: """Get operation history for a session.""" return await _get_operation_history(session_id, limit, ctx)
- Primary handler function for get_history tool. Retrieves the CSV session and calls session.get_history(limit), wraps the result in OperationResult format.async def get_operation_history( session_id: str, limit: Optional[int] = None, ctx: Context = None ) -> Dict[str, Any]: """ Get operation history for a session. Args: session_id: Session identifier limit: Maximum number of operations to return ctx: FastMCP context Returns: Dict with history and statistics """ 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 ctx: await ctx.info(f"Getting operation history for session {session_id}") result = session.get_history(limit) if result["success"]: return OperationResult( success=True, message="History retrieved successfully", session_id=session_id, data=result ).model_dump() else: return OperationResult( success=False, message="Failed to get history", error=result.get("error") ).model_dump() except Exception as e: logger.error(f"Error getting history: {str(e)}") if ctx: await ctx.error(f"Failed to get history: {str(e)}") return OperationResult( success=False, message="Failed to get history", error=str(e) ).model_dump()
- CSVSession.get_history method: delegates to history_manager.get_history and adds statistics.def get_history(self, limit: Optional[int] = None) -> Dict[str, Any]: """Get operation history.""" if not self.history_manager: # Return legacy history if new history is not enabled return { "success": True, "history": self.operations_history[-limit:] if limit else self.operations_history, "total": len(self.operations_history) } try: history = self.history_manager.get_history(limit) stats = self.history_manager.get_statistics() return { "success": True, "history": history, "statistics": stats } except Exception as e: logger.error(f"Error getting history: {str(e)}") return {"success": False, "error": str(e)}
- Core implementation of get_history: slices the history list up to limit, converts OperationHistory entries to dicts with additional metadata (index, is_current, can_restore).def get_history(self, limit: Optional[int] = None) -> List[Dict[str, Any]]: """Get operation history.""" history_list = [] start = 0 if limit is None else max(0, len(self.history) - limit) for i, entry in enumerate(self.history[start:], start=start): history_dict = entry.to_dict() history_dict["index"] = i history_dict["is_current"] = (i == self.current_index) history_dict["can_restore"] = entry.data_snapshot is not None history_list.append(history_dict) return history_list