An MCP (Model Context Protocol) server that enables running VBA macros in Excel workbooks using xlwings.
- Execute VBA macros in Excel workbooks programmatically
- Inject custom VBA code into workbooks
- Save workbooks after macro execution
- Asynchronous execution using FastMCP
- Python 3.10 or higher
- Microsoft Excel installed on the system
- xlwings Python package
- Clone the repository:
git clone https://github.com/BenjiKCF/excel-vba-server.git
cd excel-vba-server- Install dependencies using uv:
uv syncStart the MCP server:
uv run python server.pyThe server communicates via stdio and can be integrated with MCP-compatible clients.
Executes a VBA macro in an Excel workbook.
Parameters:
filename: Path to the Excel workbook file (.xlsm, .xlsx)macro_name: Name of the VBA macro to executevba_code: VBA code to inject and run
Example:
await run_vba_macro(
filename="workbook.xlsm",
macro_name="MyMacro",
vba_code="""
Sub MyMacro()
MsgBox "Hello from VBA!"
End Sub
"""
)- Opens the specified Excel workbook using xlwings
- Creates or updates a VBA module named "AutoModule"
- Injects the provided VBA code into the module
- Saves the workbook
- Executes the specified macro
- Returns the result or any error messages
This tool allows execution of arbitrary VBA code in Excel workbooks. Ensure you trust the source of the VBA code and workbook files to avoid potential security risks.
xlwings: For Excel automationmcp[cli]: Model Context Protocol implementationhttpx: HTTP client (currently unused but included for future extensions)
[Add your license here]
[Add contribution guidelines here]