create_csv_at_path
Create a CSV file at a specified location with custom headers and optional data rows for structured data storage.
Instructions
Create a new CSV file at a specific path (absolute or relative).
Args:
filepath: Full path where the CSV file should be created
headers: List of column headers
data: Optional list of rows, where each row is a list of values
Returns:
Dictionary with creation results and file information
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | ||
| headers | Yes | ||
| data | No |
Implementation Reference
- csv_mcp_server/server.py:324-344 (handler)The main tool handler function for 'create_csv_at_path', decorated with @mcp.tool(). It delegates the core logic to CSVManager.create_csv while providing error handling and tool interface.@mcp.tool() def create_csv_at_path( filepath: str, headers: List[str], data: Optional[List[List[Any]]] = None ) -> Dict[str, Any]: """ Create a new CSV file at a specific path (absolute or relative). Args: filepath: Full path where the CSV file should be created headers: List of column headers data: Optional list of rows, where each row is a list of values Returns: Dictionary with creation results and file information """ try: return csv_manager.create_csv(filepath, headers, data) except Exception as e: return {"success": False, "error": str(e)}
- Core implementation of CSV creation logic in CSVManager class. Handles path resolution (relative/absolute), directory creation, DataFrame setup, CSV writing, size validation, and returns detailed results.def create_csv(self, filename: str, headers: List[str], data: Optional[List[List[Any]]] = None) -> Dict[str, Any]: """Create a new CSV file with headers and optional initial data.""" filepath = self._get_file_path(filename) if filepath.exists(): raise FileExistsError(f"CSV file '{filename}' already exists") try: # Ensure directory exists for absolute paths self._ensure_directory_exists(filepath) # Create DataFrame df = pd.DataFrame(columns=headers) if data: df = pd.DataFrame(data, columns=headers) # Save to CSV df.to_csv(filepath, index=False) self._validate_file_size(filepath) logger.info(f"Created CSV file: {filepath}") return { "success": True, "filename": filename, "filepath": str(filepath), "rows_created": len(df), "columns": headers } except Exception as e: logger.error(f"Failed to create CSV: {e}") raise