load_csv_from_url
Load CSV data directly from a URL for quick processing and manipulation. Supports custom encoding and delimiters for accurate file handling within a robust CSV editing environment.
Instructions
Load a CSV file from a URL.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| delimiter | No | , | |
| encoding | No | utf-8 | |
| session_id | No | ||
| url | Yes |
Implementation Reference
- Core handler function that validates the URL, downloads and parses the CSV using pandas.read_csv, loads it into a CSV session, and returns an OperationResult with session details and data preview.async def load_csv_from_url( url: str, encoding: str = "utf-8", delimiter: str = ",", session_id: Optional[str] = None, ctx: Context = None ) -> Dict[str, Any]: """Load a CSV file from a URL. Args: url: URL of the CSV file encoding: File encoding delimiter: Column delimiter session_id: Optional existing session ID ctx: FastMCP context Returns: Operation result with session ID and data info """ try: # Validate URL is_valid, validated_url = validate_url(url) if not is_valid: return OperationResult( success=False, message=f"Invalid URL: {validated_url}", error=validated_url ).model_dump() if ctx: await ctx.info(f"Loading CSV from URL: {url}") await ctx.report_progress(0.1, "Downloading file...") # Download CSV using pandas (it handles URLs directly) df = pd.read_csv(url, encoding=encoding, delimiter=delimiter) if ctx: await ctx.report_progress(0.8, "Processing data...") # Get or create session session_manager = get_session_manager() session = session_manager.get_or_create_session(session_id) session.load_data(df, url) if ctx: await ctx.report_progress(1.0, "Complete!") await ctx.info(f"Loaded {len(df)} rows and {len(df.columns)} columns") return OperationResult( success=True, message=f"Successfully loaded CSV from URL", session_id=session.session_id, rows_affected=len(df), columns_affected=df.columns.tolist(), data={ "shape": df.shape, "source_url": url, "preview": df.head(5).to_dict('records') } ).model_dump() except Exception as e: if ctx: await ctx.error(f"Failed to load CSV from URL: {str(e)}") return OperationResult( success=False, message="Failed to load CSV from URL", error=str(e) ).model_dump()
- src/csv_editor/server.py:121-131 (registration)MCP tool registration decorator (@mcp.tool) with function signature that delegates to the internal _load_csv_from_url handler from io_operations.py.@mcp.tool async def load_csv_from_url( url: str, encoding: str = "utf-8", delimiter: str = ",", session_id: Optional[str] = None, ctx: Context = None ) -> Dict[str, Any]: """Load a CSV file from a URL.""" return await _load_csv_from_url(url, encoding, delimiter, session_id, ctx)
- src/csv_editor/server.py:98-106 (registration)Import of the internal load_csv_from_url handler aliased as _load_csv_from_url for use in the registered tool.from .tools.io_operations import ( load_csv as _load_csv, load_csv_from_url as _load_csv_from_url, load_csv_from_content as _load_csv_from_content, export_csv as _export_csv, get_session_info as _get_session_info, list_sessions as _list_sessions, close_session as _close_session )