read_csv
Read CSV file contents and return data with metadata for analysis and processing. Supports optional row limits to manage data size.
Instructions
Read and return CSV file contents.
Args:
filename: Name of the CSV file to read
limit: Optional limit on number of rows to return
Returns:
Dictionary with file contents and metadata
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| limit | No |
Implementation Reference
- csv_mcp_server/server.py:67-83 (handler)MCP tool handler and registration for 'read_csv'. Thin wrapper that delegates to CSVManager.read_csv and handles exceptions. Function signature defines the input schema.@mcp.tool() def read_csv(filename: str, limit: Optional[int] = None) -> Dict[str, Any]: """ Read and return CSV file contents. Args: filename: Name of the CSV file to read limit: Optional limit on number of rows to return Returns: Dictionary with file contents and metadata """ try: return csv_manager.read_csv(filename, limit) except Exception as e: return {"success": False, "error": str(e)}
- csv_mcp_server/csv_manager.py:163-188 (handler)Core handler logic for reading CSV files. Resolves filepath, loads with pandas.read_csv, applies row limit if specified, and returns structured data.def read_csv(self, filename: str, limit: Optional[int] = None) -> Dict[str, Any]: """Read CSV file contents.""" filepath = self._get_file_path(filename) if not filepath.exists(): raise FileNotFoundError(f"CSV file '{filename}' not found") try: df = pd.read_csv(filepath) # Apply limit if specified if limit and limit > 0: df = df.head(limit) return { "success": True, "filename": filename, "data": df.to_dict('records'), "columns": list(df.columns), "total_rows": len(df), "shape": df.shape } except Exception as e: logger.error(f"Failed to read CSV: {e}") raise