load_file
Load CSV or XLSX files into DataFrames for analysis. Specify sheet names for XLSX files and customize DataFrame names as needed.
Instructions
Load Data File Tool
Purpose: Load a local CSV or XLSX file into a DataFrame.
Usage Notes: • If a df_name is not provided, the tool will automatically assign names sequentially as df_1, df_2, and so on. • For XLSX files, you can specify the sheet_name. If not provided, the first sheet will be loaded.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| df_name | No | ||
| sheet_name | No |
Implementation Reference
- src/mcp_data_analyzer/server.py:174-198 (handler)The core handler function for the 'load_file' tool, implemented in the ScriptRunner class. It loads CSV or XLSX files into pandas DataFrames, assigns a name if not provided, handles errors, and returns success/error messages.def load_file(self, file_path: str, df_name:str = None, sheet_name: Optional[str] = None): self.df_count += 1 if not df_name: df_name = f"df_{self.df_count}" try: file_extension = os.path.splitext(file_path)[1].lower() if file_extension == ".csv": self.data[df_name] = pd.read_csv(file_path) self.notes.append(f"Successfully loaded CSV into dataframe '{df_name}' from '{file_path}'") elif file_extension == ".xlsx": self.data[df_name] = pd.read_excel(file_path, sheet_name=sheet_name) self.notes.append(f"Successfully loaded XLSX into dataframe '{df_name}' from '{file_path}' (sheet: {sheet_name or 'first'})") else: raise ValueError(f"Unsupported file type: {file_extension}. Only .csv and .xlsx are supported.") return [ TextContent(type="text", text=f"Successfully loaded data into dataframe '{df_name}'") ] except Exception as e: error_message = f"Error loading file: {str(e)}" self.notes.append(f"ERROR: {error_message}") return [ TextContent(type="text", text=f"Error: {error_message}") ]
- Pydantic BaseModel schema defining the input parameters for the 'load_file' tool.class LoadFile(BaseModel): file_path: str df_name: Optional[str] = None sheet_name: Optional[str] = None
- src/mcp_data_analyzer/server.py:296-301 (registration)Registration of the 'load_file' tool in the MCP server's list_tools() method, including name, description, and schema.@server.list_tools() async def handle_list_tools() -> list[Tool]: return [ Tool(name=DataExplorationTools.LOAD_FILE, description=LOAD_FILE_TOOL_DESCRIPTION, inputSchema=LoadFile.model_json_schema()), Tool(name=DataExplorationTools.RUN_SCRIPT, description=RUN_SCRIPT_TOOL_DESCRIPTION, inputSchema=RunScript.model_json_schema()), ]
- src/mcp_data_analyzer/server.py:303-309 (registration)MCP server call_tool handler that dispatches to the 'load_file' implementation when the tool name matches.@server.call_tool() async def handle_call_tool(name: str, arguments: dict | None) -> list[TextContent | EmbeddedResource]: if name == DataExplorationTools.LOAD_FILE: return script_runner.load_file(arguments.get("file_path"), arguments.get("df_name"), arguments.get("sheet_name")) elif name == DataExplorationTools.RUN_SCRIPT: return script_runner.safe_eval(arguments.get("script"), arguments.get("save_to_memory")) raise McpError(f"{INTERNAL_ERROR[1]}: Unknown tool: {name}")
- Enum defining the tool names, including 'load_file'.class DataExplorationTools(str, Enum): LOAD_FILE = "load_file" RUN_SCRIPT = "run_script"