update_csv
Modify specific cell values in CSV files by specifying row index and column name to update data precisely where needed.
Instructions
Update a specific cell in the CSV file.
Args:
filename: Name of the CSV file to update
row_index: Zero-based index of the row to update
column: Name of the column to update
value: New value for the cell
Returns:
Dictionary with update results
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| column | Yes | ||
| filename | Yes | ||
| row_index | Yes | ||
| value | Yes |
Implementation Reference
- csv_mcp_server/server.py:85-107 (handler)MCP tool handler for 'update_csv'. Registers the tool and defines input schema via function signature and docstring. Delegates execution to CSVManager instance.@mcp.tool() def update_csv( filename: str, row_index: int, column: str, value: Any ) -> Dict[str, Any]: """ Update a specific cell in the CSV file. Args: filename: Name of the CSV file to update row_index: Zero-based index of the row to update column: Name of the column to update value: New value for the cell Returns: Dictionary with update results """ try: return csv_manager.update_csv(filename, row_index, column, value) except Exception as e: return {"success": False, "error": str(e)}
- Core implementation of CSV update logic in CSVManager class. Loads CSV with pandas, validates inputs, updates the specific cell, saves back, and returns result with old/new values.def update_csv(self, filename: str, row_index: int, column: str, value: Any) -> Dict[str, Any]: """Update a specific cell in the CSV file.""" filepath = self._get_file_path(filename) if not filepath.exists(): raise FileNotFoundError(f"CSV file '{filename}' not found") # Create backup self._create_backup(filepath) try: df = pd.read_csv(filepath) if row_index >= len(df): raise IndexError(f"Row index {row_index} out of range (max: {len(df)-1})") if column not in df.columns: raise ValueError(f"Column '{column}' not found in CSV") old_value = df.loc[row_index, column] df.loc[row_index, column] = value df.to_csv(filepath, index=False) self._validate_file_size(filepath) logger.info(f"Updated CSV file: {filepath}") return { "success": True, "filename": filename, "row_index": row_index, "column": column, "old_value": old_value, "new_value": value } except Exception as e: logger.error(f"Failed to update CSV: {e}") raise