load-csv
Easily load CSV files into a DataFrame for analysis within the Claude MCP Data Explorer, enabling efficient data exploration and insights generation through advanced scripting capabilities.
Instructions
Load a CSV file into a DataFrame for analysis
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| csv_path | Yes | Path to the CSV file to load | |
| df_name | No | Name for the DataFrame (optional, defaults to df_1, df_2, etc.) |
Implementation Reference
- src/tools/data-loader.ts:19-72 (handler)Core handler function that loads CSV using Papa.parse, stores data globally, generates column-wise summary statistics (range, mean, median for numerics; uniques, top values for strings), returns formatted text response.export async function loadCsv(args: LoadCsvArgs): Promise<{ type: string, text: string }[]> { const { csv_path, df_name } = args; if (!csv_path) { return [{ type: 'text', text: 'Error: csv_path is required' }]; } // Normalize path for Windows const normalizedPath = path.normalize(csv_path); // Generate a default name if none provided const dataFrameName = df_name || `df_${dfCounter++}`; try { // Check if file exists if (!fs.existsSync(normalizedPath)) { return [{ type: 'text', text: `Error: File not found: ${normalizedPath}` }]; } // Get file size const stats = fs.statSync(normalizedPath); const fileSizeMb = stats.size / (1024 * 1024); // Read file content const fileContent = fs.readFileSync(normalizedPath, 'utf8'); // Parse CSV const parseResult = Papa.parse(fileContent, { header: true, dynamicTyping: true, skipEmptyLines: true }); if (parseResult.errors && parseResult.errors.length > 0) { return [{ type: 'text', text: `Error parsing CSV: ${parseResult.errors[0].message}` }]; } const data = parseResult.data as any[]; // Store in global storage dataFrames[dataFrameName] = data; // Generate summary const summary = generateSummary(data, parseResult.meta.fields || []); return [{ type: 'text', text: `Successfully loaded ${normalizedPath} as ${dataFrameName} (${data.length} rows × ${parseResult.meta.fields?.length || 0} columns)\n\n${summary}` }]; } catch (error: any) { return [{ type: 'text', text: `Error loading CSV: ${error.message}` }]; } }
- src/index.ts:58-71 (schema)Input schema definition for the load-csv tool, specifying csv_path as required string and optional df_name.inputSchema: { type: "object", properties: { csv_path: { type: "string", description: "Path to the CSV file to load" }, df_name: { type: "string", description: "Name for the DataFrame (optional, defaults to df_1, df_2, etc.)" } }, required: ["csv_path"] }
- src/index.ts:94-102 (registration)Registration/dispatch logic in the CallToolRequestSchema handler that invokes the loadCsv function for 'load-csv' tool calls.if (request.params.name === "load-csv") { try { const args = request.params.arguments as any; const result = await loadCsv(args); return { content: result }; } catch (error) { log(`Error executing load-csv: ${error}`); throw new McpError(ErrorCode.InternalError, `Error: ${error}`); }
- Python implementation of the load-csv handler using pandas.read_csv (with chunking for large files >100MB), stores dataframes globally, generates summary with shape, per-column stats (min/max/mean for numerics, uniques/missings).async def handle_load_csv(arguments): """Handle the load-csv tool""" global _df_counter csv_path = arguments.get("csv_path") df_name = arguments.get("df_name") if not csv_path: return [TextContent(type="text", text="Error: csv_path is required")] # Normalize path for Windows csv_path = os.path.normpath(csv_path) # Generate a default name if none provided if not df_name: df_name = f"df_{_df_counter}" _df_counter += 1 try: # Get file size file_size_mb = os.path.getsize(csv_path) / (1024 * 1024) logging.info(f"Loading CSV file: {csv_path} ({file_size_mb:.2f} MB)") # Use chunking for large files if file_size_mb > 100: return await _load_large_csv(csv_path, df_name) else: return await _load_small_csv(csv_path, df_name) except FileNotFoundError: return [TextContent( type="text", text=f"Error: File not found: {csv_path}" )] except Exception as e: error_message = f"Error loading CSV: {str(e)}\n{traceback.format_exc()}" logging.error(error_message) return [TextContent( type="text", text=f"Error loading CSV: {str(e)}" )]
- Input schema for load-csv in the Python list_tools handler.inputSchema={ "type": "object", "properties": { "csv_path": { "type": "string", "description": "Path to the CSV file to load" }, "df_name": { "type": "string", "description": "Name for the DataFrame (optional, defaults to df_1, df_2, etc.)" } }, "required": ["csv_path"] }