VBA MCP Server
Enables AI assistants to directly read and write VBA code in Excel workbooks via COM, eliminating manual export/import cycles.
README
VBA MCP Server
Let AI read and write your Excel VBA code directly — no manual export/import needed.
An MCP (Model Context Protocol) server that gives Claude (and other AI assistants) the ability to directly access and modify VBA code inside Excel workbooks (
.xlsm,.xlsb,.xls,.xla,.xlam).
Why This Exists
If you've ever worked with Excel VBA and AI coding assistants, you know the pain:
- Open VBA Editor, manually copy code
- Paste into chat, ask AI for help
- Copy AI's response back
- Paste into VBA Editor, test, repeat...
VBA MCP Server eliminates this entirely. Claude can now read, write, create, delete, and backup VBA modules directly — just like it works with regular source code files.
Features
- Direct VBA Access — Read and write VBA code in-place, no export/import cycle
- Smart Workbook Handling — Detects already-open workbooks, avoids conflicts
- Auto-Save — Workbooks are saved automatically after writes
- Full Module Management — Create, delete, read, write, list, and backup modules
- Workbook Discovery — Find all
.xlsmfiles in a directory tree - Backup with Manifest — Export all modules to text files with JSON metadata
- Document Module Protection — Prevents accidental deletion of Sheet/ThisWorkbook modules
- Lightweight — Single Python file (~300 lines), two dependencies
Prerequisites
| Requirement | Details |
|---|---|
| OS | Windows (uses COM/Win32 API) |
| Excel | Microsoft Excel (local installation) |
| Python | 3.10 or later |
| Trust Setting | See Excel Configuration below |
Installation
Option 1: pip install (Recommended)
pip install vba-mcp-server
Then add to your MCP config:
{
"mcpServers": {
"vba-mcp-server": {
"command": "vba-mcp-server",
"args": []
}
}
}
Option 2: Claude Code (Manual)
Add to your Claude Code MCP settings (~/.claude.json or project .mcp.json):
{
"mcpServers": {
"vba-mcp-server": {
"command": "python",
"args": ["C:/path/to/vba-mcp-server/server.py"],
"env": {}
}
}
}
Option 3: Claude Desktop
Add to your Claude Desktop config (%APPDATA%/Claude/claude_desktop_config.json):
{
"mcpServers": {
"vba-mcp-server": {
"command": "python",
"args": ["C:/path/to/vba-mcp-server/server.py"]
}
}
}
Option 4: From Source
# Clone the repository
git clone https://github.com/xiongchenghou/vba-mcp-server.git
cd vba-mcp-server
# Create virtual environment (recommended)
python -m venv .venv
.venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
Excel Configuration
Required one-time setup — Excel must allow programmatic access to VBA:
- Open Excel
- Go to File > Options > Trust Center > Trust Center Settings
- Click Macro Settings
- Check "Trust access to the VBA project object model"
- Click OK
Without this setting, all tools will return an error with a helpful hint message.
Available Tools
vba_list_modules
List all VBA modules in a workbook with metadata.
→ vba_list_modules("C:/Projects/MyWorkbook.xlsm")
[
{"name": "Module1", "type": "StandardModule", "line_count": 342},
{"name": "Sheet1", "type": "Document", "line_count": 15},
{"name": "MyClass", "type": "ClassModule", "line_count": 89}
]
vba_read_module
Read the source code of a specific module.
→ vba_read_module("C:/Projects/MyWorkbook.xlsm", "Module1")
' === Module: Module1 (StandardModule) ===
Sub HelloWorld()
MsgBox "Hello from VBA!"
End Sub
vba_read_all
Read ALL modules at once — perfect for full codebase review.
→ vba_read_all("C:/Projects/MyWorkbook.xlsm")
============================================================
' Module: Module1
' Type: StandardModule
' Lines: 342
============================================================
[full source code...]
============================================================
' Module: Sheet1
' Type: Document
' Lines: 15
============================================================
[full source code...]
vba_write_module
Write or replace a module's code. Auto-saves the workbook. Creates the module if it doesn't exist.
→ vba_write_module("C:/Projects/MyWorkbook.xlsm", "Module1", "Sub Test()\n MsgBox \"Updated!\"\nEnd Sub")
{"success": true, "module": "Module1", "lines_written": 3, "message": "Module 'Module1' updated (3 lines)"}
| Parameter | Type | Default | Description |
|---|---|---|---|
file_path |
string | required | Path to the workbook |
module_name |
string | required | Name of the module to write |
code |
string | required | VBA source code |
create_if_missing |
bool | true |
Create module if it doesn't exist |
vba_create_module
Create a new VBA module (standard or class).
→ vba_create_module("C:/Projects/MyWorkbook.xlsm", "MyNewModule", "Option Explicit", "standard")
{"success": true, "module": "MyNewModule", "type": "StandardModule", "message": "Module 'MyNewModule' created"}
vba_delete_module
Delete a VBA module. Document modules (Sheet/ThisWorkbook) cannot be deleted — only their code can be cleared via vba_write_module.
→ vba_delete_module("C:/Projects/MyWorkbook.xlsm", "OldModule")
{"success": true, "message": "Module 'OldModule' deleted"}
vba_backup
Export all VBA modules to text files with a JSON manifest.
→ vba_backup("C:/Projects/MyWorkbook.xlsm")
{
"success": true,
"backup_dir": "C:/Projects/VBA_Backup/20260324_120000",
"file_count": 8,
"files": [
{"name": "Module1", "type": "StandardModule", "file": "...", "lines": 342},
...
]
}
vba_list_workbooks
Find all .xlsm files in a directory.
→ vba_list_workbooks("C:/Projects", recursive=True)
[
{"path": "C:/Projects/MyWorkbook.xlsm", "name": "MyWorkbook.xlsm", "size_kb": 245.3, "modified": "2026-03-24 12:00:00"},
...
]
Usage Examples
Refactoring VBA Code
"Read Module1 from MyWorkbook.xlsm, refactor the
CalculateTotalfunction to handle edge cases, and write it back."
Claude will:
- Call
vba_read_moduleto get the current code - Analyze and refactor the function
- Call
vba_write_moduleto save the updated code
Code Review
"Read all VBA code from MyWorkbook.xlsm and review it for potential bugs, security issues, and best practices."
Claude will:
- Call
vba_read_allto get the complete codebase - Provide a comprehensive code review
Migration Assistance
"Read the VBA code from Legacy.xlsm and help me convert it to Python/JavaScript."
Claude will:
- Call
vba_read_allto understand the full VBA codebase - Analyze the business logic and data flow
- Generate equivalent code in the target language
Batch Operations
"Find all .xlsm files in C:/Projects, list their modules, and backup everything."
Claude will:
- Call
vba_list_workbookswithrecursive=True - Call
vba_list_modulesfor each workbook - Call
vba_backupfor each workbook
How It Works
┌─────────────┐ stdio/MCP ┌──────────────┐ COM/Win32 ┌─────────┐
│ Claude / │ ◄────────────────► │ VBA MCP │ ◄────────────────► │ Excel │
│ AI Client │ JSON-RPC 2.0 │ Server │ pywin32 │ VBA │
│ │ │ (server.py) │ │ Project │
└─────────────┘ └──────────────┘ └─────────┘
- Claude sends tool calls via MCP (stdio transport)
- VBA MCP Server receives the call, connects to Excel via COM
- Excel provides access to the VBProject object model
- Server reads/writes VBA code and returns results to Claude
Troubleshooting
"Trust access to the VBA project object model" error
Follow the Excel Configuration steps above. This is a one-time setting.
Excel is not responding
If Excel is busy (dialog box open, cell editing mode), COM calls will fail. Make sure Excel is idle before using the tools.
Module not found
Use vba_list_modules first to see available module names. Module names are case-sensitive.
File path issues
Use forward slashes (/) or escaped backslashes (\\) in file paths. The server normalizes paths automatically.
Cannot delete Sheet module
Sheet and ThisWorkbook modules are Document modules — they cannot be deleted from VBProject. Use vba_write_module with empty code to clear their contents instead.
Contributing
Contributions are welcome! Here are some ways you can help:
- Bug reports — Found an issue? Open an issue
- Feature requests — Have an idea? Let's discuss it
- Pull requests — Code improvements, new features, documentation
- Testing — Try it with different Excel versions and report results
- Documentation — Help improve the docs or add translations
Development Setup
git clone https://github.com/xiongchenghou/vba-mcp-server.git
cd vba-mcp-server
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt
# Run the server directly for testing
python server.py
Areas for Improvement
- [ ] UserForm support (read/write
.frmfiles with controls) - [ ] Diff-based editing (modify specific lines instead of full replacement)
- [ ] Excel cell/range read/write (beyond VBA code)
- [ ] Watch mode (detect VBA changes in real-time)
- [ ] Support for multiple simultaneous workbooks
- [ ] macOS support via alternative COM bridges
License
MIT — Use it freely, commercially or personally.
Star History
If this tool saves you time, please give it a star! It helps others discover it.
<p align="center"> <b>Stop copy-pasting VBA code. Let AI work with it directly.</b> </p>
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。