read_csv
Extract data from CSV files by specifying a filename and optional row limit, returning structured content with metadata for analysis or processing.
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-82 (handler)MCP tool handler for 'read_csv'. Decorated with @mcp.tool(), defines input parameters (filename, optional limit) and delegates to CSVManager.read_csv, handling exceptions.@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)}
- Core implementation in CSVManager class: resolves file path, loads CSV with pandas.read_csv, applies row limit if specified, converts to dict records, returns structured response with metadata.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
- csv_mcp_server/server.py:67-82 (registration)The @mcp.tool() decorator registers this function as the 'read_csv' tool in the FastMCP server.@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)}