xlwings Excel MCP Server

xlwings Excel MCP Server

Enables Excel automation via natural language, providing session-based workbook management, data manipulation, formulas, charts, and formatting through the Model Context Protocol.

Category
访问服务器

README

Add to Cursor Add to VS Code Add to Claude Add to ChatGPT Add to Codex Add to Gemini

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

🛠️ 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 .

Using uv (Recommended)

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

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选