python-fastmcp-polars

raoagbajdos/python-fastmcp-polars

3.2

If you are the rightful owner of python-fastmcp-polars and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

A Model Context Protocol (MCP) server that converts Excel files to Polars DataFrames using FastMCP, designed for advanced actuarial data analysis and reporting.

Tools
  1. read_excel

    Convert an Excel file to Polars DataFrame with configurable options

  2. list_sheets

    List all sheet names in an Excel file

  3. read_excel_sheet

    Read a specific sheet from an Excel file

Excel to Polars MCP Server

A Model Context Protocol (MCP) server that converts Excel files to Polars DataFrames using FastMCP. Includes comprehensive actuarial data examples and advanced analytics capabilities.

Features

  • Convert Excel files (.xlsx, .xls) to Polars DataFrames
  • Support for multiple sheets with automatic detection
  • Configurable data type inference and schema validation
  • Memory-efficient processing using Polars
  • Multiple output formats: Parquet, CSV, JSON
  • Schema generation with statistics and metadata
  • Professional actuarial data examples
  • Advanced Polars analytics and reporting
  • Comprehensive test suite with realistic data

Installation

This project uses uv for dependency management. Make sure you have uv installed:

curl -LsSf https://astral.sh/uv/install.sh | sh

Then install the project:

uv sync

Usage

Running the MCP Server

uv run excel-polars-mcp

Available MCP Tools

  • read_excel: Convert an Excel file to Polars DataFrame with configurable options
  • list_sheets: List all sheet names in an Excel file
  • read_excel_sheet: Read a specific sheet from an Excel file

API Usage

import polars as pl

# Load converted actuarial data
life_table = pl.read_parquet('output/life_table.parquet')
policies = pl.read_parquet('output/policies.parquet')
claims = pl.read_parquet('output/claims.parquet')
reserves = pl.read_parquet('output/reserves.parquet')

# Perform actuarial analysis
loss_ratios = policies.join(claims, on='Policy_ID').group_by('Policy_Type').agg([
    pl.col('Claim_Amount').sum() / pl.col('Face_Amount').sum() * 100
])

# High-value claims analysis
high_value_claims = claims.filter(pl.col('Claim_Amount') > 400000)

# Mortality analysis
high_mortality = life_table.filter(pl.col('Mortality_Rate_qx') > 0.1)

Output Formats

Each Excel sheet is converted to multiple formats:

FormatUse CaseFile Extension
ParquetHigh-performance analytics, data science.parquet
CSVHuman-readable, Excel compatibility.csv
JSONWeb APIs, structured data exchange.json
SchemaMetadata, statistics, data validation_schema.json

Sample Data

The project includes comprehensive actuarial datasets:

DatasetRecordsDescription
Life Table101Mortality rates, survival probabilities by age (0-100)
Policies1,000Insurance policies with demographics and financial data
Claims150Insurance claims with amounts, status, and investigation data
Reserves25Financial reserves by product type and valuation year

Examples

Basic Usage

python3 examples/demo.py

Actuarial Data Example

A comprehensive example that creates sample actuarial data and converts it to Polars format:

python3 run_actuarial_example.py

This example:

  1. Generates a multi-sheet Excel file with realistic actuarial data:

    • Life tables with mortality rates, survival probabilities, and life expectancy (101 ages)
    • Insurance policies with demographics, premiums, face amounts (1,000 policies)
    • Claims data with types, amounts, status, and investigation times (150 claims)
    • Reserve calculations by product type and valuation year (25 records)
  2. Converts each sheet to multiple formats:

    • Parquet files for efficient data analysis
    • CSV files for human readability and Excel compatibility
    • JSON files for web applications and APIs
    • Schema files with metadata, statistics, and data types
  3. Generates comprehensive analytics:

    • Summary statistics and data quality reports
    • Loss ratio analysis by policy type
    • Mortality analysis and high-risk identification
    • Reserve adequacy and trend analysis

Advanced Analytics Example

Perform comprehensive actuarial analysis on the converted data:

python3 analyze_polars_data.py

Features include:

  • Claims Analysis: By status, type, and high-value claims
  • Policy Analysis: Premium rates, face amounts, and status distribution
  • Mortality Analysis: High mortality ages and life expectancy statistics
  • Reserve Analysis: Trends by year and product type
  • Loss Ratios: Policy-to-claim analysis with advanced joins
  • Data Export: Filtered datasets and summary reports

Project Structure

ā”œā”€ā”€ excel_polars_mcp/          # Core MCP server implementation
│   ā”œā”€ā”€ __init__.py
│   └── server.py              # FastMCP server with Excel conversion tools
ā”œā”€ā”€ examples/                  # Example scripts and demos
│   ā”œā”€ā”€ demo.py               # Basic usage demonstration
│   ā”œā”€ā”€ create_actuarial_data.py  # Generate sample actuarial Excel file
│   └── convert_actuarial_data.py # Convert Excel to Polars formats
ā”œā”€ā”€ sample_data/               # Generated sample data
│   └── actuarial_data.xlsx   # Multi-sheet actuarial Excel file
ā”œā”€ā”€ output/                    # Converted data in multiple formats
│   ā”œā”€ā”€ *.parquet             # Efficient binary format
│   ā”œā”€ā”€ *.csv                 # Human-readable format
│   ā”œā”€ā”€ *.json                # Structured data format
│   ā”œā”€ā”€ *_schema.json         # Metadata and statistics
│   └── conversion_summary.md  # Detailed conversion report
ā”œā”€ā”€ tests/                     # Comprehensive test suite
ā”œā”€ā”€ run_actuarial_example.py  # One-command actuarial demo
ā”œā”€ā”€ analyze_polars_data.py    # Advanced analytics demonstration
ā”œā”€ā”€ view_actuarial_data.py    # Data inspection utility
└── pyproject.toml            # Project configuration with uv

## Development

Install development dependencies:

```bash
uv sync --dev

Run tests:

uv run pytest

Format code:

uv run black .
uv run ruff check --fix .

Type checking:

uv run mypy .

Quick Start

  1. Clone and setup:

    git clone <repository>
    cd python-mcp-excel-polars
    uv sync
    
  2. Run the actuarial example:

    python3 run_actuarial_example.py
    
  3. Analyze the results:

    python3 analyze_polars_data.py
    
  4. Explore the data:

    python3 view_actuarial_data.py
    

Dependencies

  • Polars: High-performance DataFrames
  • FastMCP: Model Context Protocol implementation
  • openpyxl: Excel file reading/writing
  • xlsxwriter: Multi-sheet Excel creation
  • fastexcel: Optimized Excel processing

Use Cases

  • Actuarial Analysis: Life tables, mortality studies, reserve calculations
  • Insurance Analytics: Policy analysis, claims processing, loss ratios
  • Data Migration: Excel to modern data formats (Parquet, JSON)
  • Financial Modeling: Risk assessment, statistical analysis
  • Business Intelligence: Automated reporting and data transformation

License

MIT License - see LICENSE file for details.