upload_file
Upload CSV data to prepare for linear regression analysis, reading file content and returning dataset shape information.
Instructions
This function read the csv data and stores it in the class variable.
Args: Absolute path to the .csv file.
Returns: String which shows the shape of the data.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- server.py:36-68 (handler)The main handler function for the 'upload_file' tool. It loads a CSV file using pandas, stores the DataFrame in a global DataContext instance, and returns the shape of the data or an error message.@mcp.tool() def upload_file(path: str) -> str: """ This function read the csv data and stores it in the class variable. Args: Absolute path to the .csv file. Returns: String which shows the shape of the data. """ if not os.path.exists(path): return f"Error: The file at '{path}' does not exist." # Check if file has a .csv extension if not path.lower().endswith('.csv'): return "Error: The file must be a CSV file." try: # Try to read the CSV file using pandas data = pd.read_csv(path) # Store the data in the DataContext class context.set_data(data) # Store the shape of the data (rows, columns) data_shape = context.get_data().shape return f"Data successfully loaded. Shape: {data_shape}" except Exception as e: return f"An unexpected error occured: {str(e)}"
- server.py:14-35 (helper)Helper class DataContext used by upload_file to store and retrieve the loaded DataFrame globally.@dataclass class DataContext(): """ A class that stores the DataFrame in the context. """ _data: pd.DataFrame = None def set_data(self, new_data: pd.DataFrame): """ Method to set or update the data. """ self._data = new_data def get_data(self) -> pd.DataFrame: """ Method to get the data from the context. """ return self._data # Initialize the DataContext instance globally context = DataContext()
- server.py:36-36 (registration)Decorator that registers the upload_file function as an MCP tool.@mcp.tool()