filter_excel
Extract specific data from Excel files using a pandas query string. Specify file path, query conditions, and sheet name to filter and return targeted information for analysis.
Instructions
Filter Excel data using a pandas query string.
Args:
file_path: Path to the Excel file
query: Pandas query string (e.g., "Age > 30 and Department == 'Sales'")
sheet_name: Name of the sheet to filter (for Excel files)
Returns:
Filtered data as string
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| query | Yes | ||
| sheet_name | No |
Input Schema (JSON Schema)
{
"properties": {
"file_path": {
"title": "File Path",
"type": "string"
},
"query": {
"title": "Query",
"type": "string"
},
"sheet_name": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Sheet Name"
}
},
"required": [
"file_path",
"query"
],
"title": "filter_excelArguments",
"type": "object"
}
Implementation Reference
- mcp_excel_server/server.py:368-410 (handler)The handler function for the 'filter_excel' tool, decorated with @mcp.tool() for automatic registration. It reads data from various file formats (Excel, CSV, etc.), applies a pandas query filter based on the provided query string, and returns the filtered results as a formatted string. The function signature defines the input schema (file_path: str, query: str, sheet_name: Optional[str]).@mcp.tool() def filter_excel(file_path: str, query: str, sheet_name: Optional[str] = None) -> str: """ Filter Excel data using a pandas query string. Args: file_path: Path to the Excel file query: Pandas query string (e.g., "Age > 30 and Department == 'Sales'") sheet_name: Name of the sheet to filter (for Excel files) Returns: Filtered data as string """ try: # Read file _, ext = os.path.splitext(file_path) ext = ext.lower() read_params = {} if ext in ['.xlsx', '.xls', '.xlsm'] and sheet_name is not None: read_params["sheet_name"] = sheet_name if ext in ['.xlsx', '.xls', '.xlsm']: df = pd.read_excel(file_path, **read_params) elif ext == '.csv': df = pd.read_csv(file_path) elif ext == '.tsv': df = pd.read_csv(file_path, sep='\t') elif ext == '.json': df = pd.read_json(file_path) else: return f"Unsupported file extension: {ext}" # Apply filter filtered_df = df.query(query) # Return results if filtered_df.empty: return "No data matches the filter criteria." return filtered_df.to_string(index=False) except Exception as e: return f"Error filtering data: {str(e)}"