Skip to main content
Glama
xlwings Excel MCP Server

xlwings Excel MCP Server

by hyunjae-labs

xlwings-mcp-server

Version Python License MCP

A robust Model Context Protocol (MCP) server for Excel automation using xlwings. This server provides comprehensive Excel file manipulation capabilities through a session-based architecture, designed for high-performance and reliable Excel operations.

๐Ÿš€ Features

Core Capabilities

  • Session-based Architecture: Persistent Excel workbook sessions for optimal performance

  • Comprehensive Excel Operations: Full support for data manipulation, formulas, formatting, and visualization

  • Thread-safe Operations: Concurrent access with per-session locking

  • Automatic Resource Management: TTL-based session cleanup and LRU eviction policies

  • Zero-Error Design: Katherine Johnson principle compliance with comprehensive error handling

Excel Operations

  • Workbook Management: Open, create, list, and close Excel workbooks

  • Worksheet Operations: Create, copy, rename, and delete worksheets

  • Data Manipulation: Read, write, and modify Excel data with full type support

  • Formula Support: Apply and validate Excel formulas with syntax checking

  • Advanced Formatting: Cell styling, conditional formatting, and range formatting

  • Visualization: Chart creation with multiple chart types

  • Table Operations: Native Excel table creation and management

  • Range Operations: Cell merging, copying, and deletion

Related MCP server: Excel MCP Server

๐Ÿ› ๏ธ Installation

Prerequisites

  • Python 3.10 or higher

  • Windows OS (required for xlwings COM integration)

  • Microsoft Excel installed

Using pip

pip install xlwings-mcp-server

From Source

git clone https://github.com/yourusername/xlwings-mcp-server.git
cd xlwings-mcp-server
pip install -e .
uv add xlwings-mcp-server

โšก Quick Start

1. Basic Usage

Start the MCP server:

xlwings-mcp-server

Or run directly:

python -m xlwings_mcp

2. Session-based Workflow

# Example using MCP client
import mcp

# Open a workbook session
session_result = client.call_tool("mcp__xlwings-mcp-server__open_workbook", {
    "filepath": "C:/path/to/your/file.xlsx",
    "visible": False,
    "read_only": False
})

session_id = session_result["session_id"]

# Write data
client.call_tool("mcp__xlwings-mcp-server__write_data_to_excel", {
    "session_id": session_id,
    "sheet_name": "Sheet1",
    "data": [["Name", "Age", "Score"], ["Alice", 25, 95], ["Bob", 30, 87]]
})

# Apply formulas
client.call_tool("mcp__xlwings-mcp-server__apply_formula", {
    "session_id": session_id,
    "sheet_name": "Sheet1",
    "cell": "D2",
    "formula": "=B2+C2"
})

# Create chart
client.call_tool("mcp__xlwings-mcp-server__create_chart", {
    "session_id": session_id,
    "sheet_name": "Sheet1",
    "data_range": "A1:C3",
    "chart_type": "column",
    "target_cell": "E1"
})

# Close session
client.call_tool("mcp__xlwings-mcp-server__close_workbook", {
    "session_id": session_id
})

๐Ÿ”ง Configuration

Environment Variables

# Session management
EXCEL_MCP_SESSION_TTL=600          # Session TTL in seconds (default: 600)
EXCEL_MCP_MAX_SESSIONS=8           # Maximum concurrent sessions (default: 8)
EXCEL_MCP_DEBUG_LOG=1              # Enable debug logging (default: 0)

# Excel settings
EXCEL_MCP_VISIBLE=false            # Show Excel windows (default: false)
EXCEL_MCP_CALC_MODE=automatic      # Calculation mode (default: automatic)

MCP Configuration (.mcp.json)

{
  "name": "xlwings-mcp-server",
  "version": "1.0.0",
  "transport": {
    "type": "stdio"
  },
  "tools": {
    "prefix": "mcp__xlwings-mcp-server__"
  }
}

๐Ÿ“š API Reference

Session Management

  • open_workbook(filepath, visible=False, read_only=False): Create new session

  • close_workbook(session_id): Close session and save workbook

  • list_workbooks(): List active sessions

  • force_close_workbook_by_path(filepath): Force close by file path

Data Operations

  • write_data_to_excel(session_id, sheet_name, data, start_cell=None)

  • read_data_from_excel(session_id, sheet_name, start_cell=None, end_cell=None)

  • apply_formula(session_id, sheet_name, cell, formula)

  • validate_formula_syntax(session_id, sheet_name, cell, formula)

Worksheet Management

  • create_worksheet(session_id, sheet_name)

  • copy_worksheet(session_id, source_sheet, target_sheet)

  • rename_worksheet(session_id, old_name, new_name)

  • delete_worksheet(session_id, sheet_name)

Formatting & Visualization

  • format_range(session_id, sheet_name, start_cell, **formatting_options)

  • create_chart(session_id, sheet_name, data_range, chart_type, target_cell)

  • create_table(session_id, sheet_name, data_range, table_name=None)

Range Operations

  • merge_cells(session_id, sheet_name, start_cell, end_cell)

  • unmerge_cells(session_id, sheet_name, start_cell, end_cell)

  • copy_range(session_id, sheet_name, source_start, source_end, target_start)

  • delete_range(session_id, sheet_name, start_cell, end_cell)

๐Ÿ—๏ธ Architecture

Session-based Design

The server implements a sophisticated session management system:

  • ExcelSessionManager: Singleton pattern managing all Excel sessions

  • Per-session Isolation: Each session has independent Excel Application instance

  • Thread Safety: RLock per session preventing concurrent access issues

  • Resource Management: Automatic cleanup with TTL and LRU policies

  • Error Recovery: Comprehensive error handling and session recovery

Performance Optimizations

  • Session Reuse: Eliminates Excel restart overhead between operations

  • Connection Pooling: Efficient COM object management

  • Batch Operations: Optimized for multiple operations on same workbook

  • Memory Management: Proactive cleanup of Excel processes

๐Ÿงช Testing

Run Tests

# Run all tests
python -m pytest test/

# Run specific test categories  
python -m pytest test/test_session.py      # Session management
python -m pytest test/test_functions.py   # MCP function tests
python -m pytest test/test_integration.py # Integration tests

Test Coverage

The project maintains 100% test coverage for:

  • All MCP tool functions (17 functions tested)

  • Session lifecycle management

  • Error handling and recovery

  • Performance benchmarks

๐Ÿ”’ Security Considerations

  • File System Access: Server operates within specified directory permissions

  • Excel Process Isolation: Each session runs in separate Excel instance

  • Resource Limits: Configurable session limits prevent resource exhaustion

  • Input Validation: All inputs validated before Excel API calls

  • Safe Defaults: Read-only mode available, invisible Excel instances by default

๐Ÿค Contributing

  1. Fork the repository

  2. Create a feature branch (git checkout -b feature/amazing-feature)

  3. Commit your changes (git commit -m 'Add amazing feature')

  4. Push to the branch (git push origin feature/amazing-feature)

  5. Open a Pull Request

Development Setup

git clone https://github.com/yourusername/xlwings-mcp-server.git
cd xlwings-mcp-server
uv venv
uv sync
uv run python -m xlwings_mcp

๐Ÿ“ Changelog

See CHANGELOG.md for detailed version history.

๐Ÿ› Troubleshooting

Common Issues

Excel COM Error: Ensure Excel is properly installed and not running in safe mode

# Check Excel installation
excel --version

Session Not Found: Verify session hasn't expired (default TTL: 10 minutes)

# List active sessions
client.call_tool("mcp__xlwings-mcp-server__list_workbooks")

Permission Denied: Run with appropriate file system permissions

# Windows: Run as administrator if needed

Debug Mode

Enable detailed logging:

export EXCEL_MCP_DEBUG_LOG=1
xlwings-mcp-server

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • xlwings - Excellent Python-Excel integration library

  • Model Context Protocol - Standardized AI-tool communication

  • Claude Code - Development assistance

  • Katherine Johnson - Inspiration for zero-error engineering principles

๐Ÿ“ž Support


Made with โค๏ธ for the Excel automation community

Available Tools

29 tools
apply_formulaB
Apply Excel formula to cell.

Args:
    session_id: Session ID from open_workbook (required)
    sheet_name: Name of worksheet
    cell: Cell address (e.g., "A1")
    formula: Excel formula to apply

Inputs: session_id (required), sheet_name (required), cell (required), formula (required). Returns: result

close_workbookA
Close a workbook session.

Args:
    session_id: Session ID from open_workbook
    save: Whether to save changes (default: True)
    
Returns:
    Success message

Inputs: session_id (required), save. Returns: result

copy_rangeA
Copy a range of cells to another location.

Args:
    sheet_name: Name of source worksheet
    source_start: Starting cell of source range
    source_end: Ending cell of source range
    target_start: Starting cell of target range
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    target_sheet: Target worksheet (optional, uses source sheet if not provided)
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), source_start (required), source_end (required), target_start (required), +3 more. Returns: result

copy_worksheetC
Copy worksheet within workbook.

Args:
    session_id: Session ID from open_workbook (required)
    source_sheet: Name of the source worksheet
    target_sheet: Name of the target worksheet

Inputs: session_id (required), source_sheet (required), target_sheet (required). Returns: result

create_chartB
Create chart in worksheet.

Args:
    sheet_name: Name of worksheet
    data_range: Data range for chart
    chart_type: Type of chart
    target_cell: Cell where chart will be placed
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    title: Chart title (optional)
    x_axis: X-axis label (optional)
    y_axis: Y-axis label (optional)
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), data_range (required), chart_type (required), target_cell (required), +5 more. Returns: result

create_pivot_tableA
Create pivot table in worksheet.

Args:
    sheet_name: Name of worksheet containing source data
    data_range: Source data range (e.g., "A1:E100" or "Sheet2!A1:E100")
    rows: Field names for row labels
    values: Field names for values
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    columns: Field names for column labels (optional)
    agg_func: Aggregation function (sum, count, average, max, min)
    target_sheet: Target sheet for pivot table (optional, auto-created if not exists)
    target_cell: Target cell for pivot table (optional, finds empty area if not provided)
    pivot_name: Custom name for pivot table (optional, auto-generated if not provided)
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), data_range (required), rows (required), values (required), +7 more. Returns: result

create_tableA
Creates a native Excel table from a specified range of data.

Args:
    sheet_name: Name of worksheet
    data_range: Range of data to create table from
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    table_name: Name for the table (optional)
    table_style: Style for the table (optional)
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), data_range (required), session_id, filepath, +2 more. Returns: result

create_workbookB
Create new Excel workbook.

Args:
    session_id: Session ID for creating workbook in existing session (optional)
    filepath: Path to create new Excel file (legacy, deprecated)
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: session_id, filepath. Returns: result

create_worksheetC
Create new worksheet in workbook.

Args:
    session_id: Session ID from open_workbook (required)
    sheet_name: Name of the new worksheet

Inputs: session_id (required), sheet_name (required). Returns: result

delete_rangeA
Delete a range of cells and shift remaining cells.

Args:
    sheet_name: Name of worksheet
    start_cell: Starting cell
    end_cell: Ending cell
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    shift_direction: Direction to shift cells ("up" or "left")
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), start_cell (required), end_cell (required), session_id, +2 more. Returns: result

delete_sheet_columnsB
Delete one or more columns starting at the specified column.

Args:
    sheet_name: Name of worksheet
    start_col: Column number to start deleting from
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    count: Number of columns to delete
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), start_col (required), session_id, filepath, +1 more. Returns: result

delete_sheet_rowsB
Delete one or more rows starting at the specified row.

Args:
    sheet_name: Name of worksheet
    start_row: Row number to start deleting from
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    count: Number of rows to delete
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), start_row (required), session_id, filepath, +1 more. Returns: result

delete_worksheetB
Delete worksheet from workbook.

Args:
    session_id: Session ID from open_workbook (required)
    sheet_name: Name of the worksheet to delete

Inputs: session_id (required), sheet_name (required). Returns: result

force_close_workbook_by_path_toolA
Force close a specific workbook by file path (without saving).

Args:
    filepath: Path to the workbook to force close
    
Returns:
    Dictionary with 'closed' (bool) and 'message' (str)

Inputs: filepath (required). Returns: result

format_rangeB
Apply formatting to a range of cells.

Args:
    sheet_name: Name of worksheet
    start_cell: Starting cell
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    end_cell: Ending cell (optional)
    bold: Apply bold formatting
    italic: Apply italic formatting
    underline: Apply underline formatting
    font_size: Font size
    font_color: Font color
    bg_color: Background color
    border_style: Border style
    border_color: Border color
    number_format: Number format
    alignment: Text alignment
    wrap_text: Enable text wrapping
    merge_cells: Merge cells in range
    protection: Cell protection settings (optional)
    conditional_format: Conditional formatting settings (optional)
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), start_cell (required), session_id, filepath, +15 more. Returns: result

get_data_validation_infoA
Get all data validation rules in a worksheet.

This tool helps identify which cell ranges have validation rules
and what types of validation are applied.

Args:
    sheet_name: Name of worksheet
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    
Note: Use session_id for better performance. filepath parameter is deprecated.
    
Returns:
    JSON string containing all validation rules in the worksheet

Inputs: sheet_name (required), session_id, filepath. Returns: result

get_merged_cellsA
Get merged cells in a worksheet.

Args:
    sheet_name: Name of worksheet
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), session_id, filepath. Returns: result

get_workbook_metadataA
Get metadata about workbook including sheets, ranges, etc.

Args:
    session_id: Session ID from open_workbook (required)
    include_ranges: Whether to include range information

Inputs: session_id (required), include_ranges. Returns: result

insert_columnsB
Insert one or more columns starting at the specified column.

Args:
    sheet_name: Name of worksheet
    start_col: Column number to start inserting at
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    count: Number of columns to insert
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), start_col (required), session_id, filepath, +1 more. Returns: result

insert_rowsA
Insert one or more rows starting at the specified row.

Args:
    sheet_name: Name of worksheet
    start_row: Row number to start inserting at
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    count: Number of rows to insert
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), start_row (required), session_id, filepath, +1 more. Returns: result

list_workbooksA
List all open workbook sessions.

Returns:
    List of session information dictionaries

Inputs: no named parameters. Returns: result

merge_cellsB
Merge a range of cells.

Args:
    sheet_name: Name of worksheet
    start_cell: Starting cell
    end_cell: Ending cell
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), start_cell (required), end_cell (required), session_id, +1 more. Returns: result

open_workbookB
Open an Excel workbook and create a session.

Args:
    filepath: Path to Excel file
    visible: Whether to show Excel window (default: False)
    read_only: Whether to open in read-only mode (default: False)
    
Returns:
    Dictionary with session_id, filepath, visible, read_only, and sheets

Inputs: filepath (required), visible, read_only. Returns: result

read_data_from_excelA
Read data from Excel worksheet with cell metadata including validation rules.

Args:
    session_id: Session ID from open_workbook (required)
    sheet_name: Name of worksheet
    start_cell: Starting cell (default A1)
    end_cell: Ending cell (optional, auto-expands if not provided)
    preview_only: Whether to return preview only

Inputs: session_id (required), sheet_name (required), start_cell, end_cell, +1 more. Returns: result

rename_worksheetB
Rename worksheet in workbook.

Args:
    session_id: Session ID from open_workbook (required)
    old_name: Current name of the worksheet
    new_name: New name for the worksheet

Inputs: session_id (required), old_name (required), new_name (required). Returns: result

unmerge_cellsA
Unmerge a range of cells.

Args:
    sheet_name: Name of worksheet
    start_cell: Starting cell
    end_cell: Ending cell
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), start_cell (required), end_cell (required), session_id, +1 more. Returns: result

validate_excel_rangeC
Validate if a range exists and is properly formatted.

Args:
    sheet_name: Name of worksheet
    start_cell: Starting cell
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    end_cell: Ending cell (optional)
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), start_cell (required), session_id, filepath, +1 more. Returns: result

validate_formula_syntaxA
Validate Excel formula syntax without applying it.

Args:
    sheet_name: Name of worksheet
    cell: Cell address (e.g., "A1")
    formula: Excel formula to validate
    session_id: Session ID from open_workbook (preferred)
    filepath: Path to Excel file (legacy, deprecated)
    
Note: Use session_id for better performance. filepath parameter is deprecated.

Inputs: sheet_name (required), cell (required), formula (required), session_id, +1 more. Returns: result

write_data_to_excelB
Write data to Excel worksheet.
Excel formula will write to cell without any verification.

Args:
    session_id: Session ID from open_workbook (required)
    sheet_name: Name of worksheet to write to
    data: List of lists containing data to write to the worksheet, sublists are assumed to be rows
    start_cell: Cell to start writing to (optional, auto-finds appropriate location)

Inputs: session_id (required), sheet_name (required), data (required), start_cell. Returns: result

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelogโ€ฆ

    TDQS

    A3.7/5.0

    Scored across 29 tools

    Disambiguation4/5

    Most tools have distinct purposes targeting specific Excel operations like formulas, ranges, sheets, or charts, with clear boundaries. However, some overlap exists between 'delete_range' and 'delete_sheet_rows/columns', which could cause confusion about which to use for row/column deletion, and 'validate_excel_range' might be redundant with range validation implied in other tools.

    Naming Consistency5/5

    Tool names follow a highly consistent verb_noun pattern throughout, such as 'apply_formula', 'close_workbook', 'copy_range', and 'create_chart'. The only minor deviation is 'force_close_workbook_by_path_tool', which still adheres to the pattern but includes extra descriptors, maintaining overall readability and predictability.

    Tool Count3/5

    With 29 tools, the count is borderline high for an Excel server, potentially overwhelming for agents. While Excel is a complex domain, the set includes some specialized tools like 'get_merged_cells' and 'validate_formula_syntax' that might be less frequently used, suggesting the surface could be more streamlined without losing core functionality.

    Completeness5/5

    The tool set provides comprehensive coverage for Excel operations, including workbook management (open, close, create), sheet operations (create, delete, rename), data manipulation (read, write, copy, delete), formatting, charts, pivot tables, and validation. There are no obvious gaps; agents can perform full CRUD and lifecycle tasks for Excel files seamlessly.

    Maintenance

    ActivityInactive
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI agents to create, read, and manipulate Excel files without requiring Microsoft Excel installation. Supports comprehensive spreadsheet operations including formulas, formatting, charts, pivot tables, and data validation.
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI assistants to create, read, write, and manipulate Excel files (.xlsx, .xlsm) without requiring Microsoft Excel, including support for charts, pivot tables, data import/export, and professional formatting across Windows, macOS, and Linux.
      39 npm
      34
      MIT