add_row
Add a new row of data to a CSV file by specifying column names and values. This tool inserts records into CSV files for data management.
Instructions
Add a new row to the CSV file.
Args:
filename: Name of the CSV file
row_data: Dictionary mapping column names to values
Returns:
Dictionary with addition results
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| row_data | Yes |
Implementation Reference
- csv_mcp_server/server.py:110-125 (handler)MCP tool handler for 'add_row': decorated with @mcp.tool(), defines input schema via type hints and docstring, delegates execution to CSVManager.add_row with error handling.@mcp.tool() def add_row(filename: str, row_data: Dict[str, Any]) -> Dict[str, Any]: """ Add a new row to the CSV file. Args: filename: Name of the CSV file row_data: Dictionary mapping column names to values Returns: Dictionary with addition results """ try: return csv_manager.add_row(filename, row_data) except Exception as e: return {"success": False, "error": str(e)}
- Core logic implementation in CSVManager.add_row: loads CSV with pandas, creates new row from dict, appends it, saves with backup and size validation.def add_row(self, filename: str, row_data: Dict[str, Any]) -> Dict[str, Any]: """Add a new row to 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) # Create new row DataFrame new_row = pd.DataFrame([row_data]) # Append the new row df = pd.concat([df, new_row], ignore_index=True) df.to_csv(filepath, index=False) self._validate_file_size(filepath) logger.info(f"Added row to CSV file: {filepath}") return { "success": True, "filename": filename, "row_added": row_data, "new_total_rows": len(df) } except Exception as e: logger.error(f"Failed to add row: {e}") raise
- csv_mcp_server/server.py:110-110 (registration)Registration of the 'add_row' tool via FastMCP @mcp.tool() decorator.@mcp.tool()
- csv_mcp_server/server.py:111-111 (schema)Input schema defined by function signature: filename (str), row_data (Dict[str, Any]); output Dict[str, Any].def add_row(filename: str, row_data: Dict[str, Any]) -> Dict[str, Any]: