Skip to content

Commit ef1c1d6

Browse files
sbroenneStefan Broenner
andauthored
fix(powerquery): refresh error propagation and new features (#399, #400) (#401)
* fix(powerquery): refresh error propagation (#399) BREAKING FIX: refresh action now properly throws on Power Query errors ROOT CAUSE: Connection.Refresh() silently swallowed errors for worksheet queries (InModel=false), returning success even when M code had errors. FIX: Route worksheet queries through QueryTable.Refresh(false) which properly throws exceptions. Data Model queries continue using Connection.Refresh() which does throw errors correctly. - Added RefreshQueryTableByName() helper for worksheet query refresh - Updated Refresh() to detect query type and route accordingly - 6 regression tests covering error propagation scenarios Closes #399 * feat(powerquery): add Evaluate action for ad-hoc M code execution (#400) New 'evaluate' action executes M code directly and returns tabular results WITHOUT creating a permanent query in the workbook. Useful for: - Testing M code before creating a permanent query - One-off queries without polluting the workbook - Ad-hoc data exploration and M code debugging Implementation: - Creates temp query + worksheet, refreshes, reads data, cleans up - Errors propagate properly (invalid M code throws with error message) - Returns PowerQueryEvaluateResult with columns, rows, counts Changes: - Added Evaluate to PowerQueryAction enum and ActionExtensions - Added IPowerQueryCommands.Evaluate interface method - Added PowerQueryCommands.Evaluate.cs implementation - Added PowerQueryEvaluateResult type - MCP Server: ExcelPowerQueryTool handler - CLI: ExcelDaemon handler + PowerQueryCommand case - 7 integration tests covering success/error scenarios - Updated skills/prompts with evaluate guidance - Updated operation counts: 210 → 211 Closes #400 * feat(powerquery): add mCodeFile parameter for M code from file MCP Server excel_powerquery now accepts mCodeFile parameter as alternative to inline mCode for create, update, and evaluate actions. Same behavior as CLI --mcode-file option: - If mCodeFile is provided, reads M code from file - If both provided, mCodeFile takes precedence - FileNotFoundException if file doesn't exist Useful for large/complex queries stored in .m or .pq files. * feat(vba): add vbaCodeFile parameter for VBA code from file MCP Server excel_vba now accepts vbaCodeFile parameter as alternative to inline vbaCode for import and update actions. Same behavior as CLI --code-file option: - If vbaCodeFile is provided, reads VBA code from file - If both provided, vbaCodeFile takes precedence - FileNotFoundException if file doesn't exist Useful for large modules stored in .bas or .vba files. * fix(table): auto-expand single cell range using CurrentRegion * test(llm): add allowed_tools filter to MCP test for faster LLM execution * docs: update CHANGELOG with mCodeFile, vbaCodeFile, and table CurrentRegion fix * fix: use specific COMException instead of generic catch clause Addresses CodeQL security finding - generic catch clause should specify exception type. The catch handles expected COMException when ListObject doesn't have a QueryTable. --------- Co-authored-by: Stefan Broenner <[email protected]>
1 parent e09743f commit ef1c1d6

File tree

28 files changed

+1126
-34
lines changed

28 files changed

+1126
-34
lines changed

CHANGELOG.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,38 @@ This changelog covers all components:
1010

1111
## [Unreleased]
1212

13+
### Fixed
14+
15+
- **Power Query Refresh Error Propagation** (#399): Fixed bug where `refresh` action returned `success: true` even when Power Query had formula errors
16+
- ROOT CAUSE: `Connection.Refresh()` silently swallows errors for worksheet queries (InModel=false)
17+
- FIX: Now uses `QueryTable.Refresh(false)` for worksheet queries which properly throws errors
18+
- Data Model queries (InModel=true) continue using `Connection.Refresh()` which does throw errors
19+
- Errors now surface clearly: `"[Expression.Error] The name 'Source' wasn't recognized..."`
20+
21+
- **Table Create Auto-Expand from Single Cell**: Fixed issue where `table create --range A1` created single-cell table
22+
- ROOT CAUSE: Excel's `ListObjects.Add()` doesn't auto-expand from a single cell
23+
- FIX: Now uses `Range.CurrentRegion` when single cell provided, capturing all contiguous data
24+
- Prevents Data Model issues where tables only contain header column
25+
26+
### Added
27+
28+
- **Power Query Evaluate** (#400): New `evaluate` action to execute M code directly and return results
29+
- Execute arbitrary M code without creating a permanent query
30+
- Returns tabular results (columns, rows) in JSON format
31+
- Automatically cleans up temporary query and worksheet
32+
- Errors propagate properly (e.g., invalid M syntax throws with error message)
33+
- Example: `excelcli powerquery evaluate --file data.xlsx --mcode "let Source = #table({\"Name\",...})"`
34+
35+
- **MCP Power Query mCodeFile Parameter**: Read M code from file instead of inline string
36+
- New `mCodeFile` parameter on `excel_powerquery` tool for `create`, `update`, `evaluate` actions
37+
- Avoids JSON escaping issues with complex M code containing special characters
38+
- File takes precedence if both `mCode` and `mCodeFile` provided
39+
40+
- **MCP VBA vbaCodeFile Parameter**: Read VBA code from file instead of inline string
41+
- New `vbaCodeFile` parameter on `excel_vba` tool for `create-module`, `update-module` actions
42+
- Handles VBA code with quotes and special characters cleanly
43+
- File takes precedence if both `vbaCode` and `vbaCodeFile` provided
44+
1345
## [1.5.14] - 2025-02-01
1446

1547
### Added

FEATURES.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ExcelMcp - Complete Feature Reference
22

3-
**22 specialized tools with 210 operations for comprehensive Excel automation**
3+
**22 specialized tools with 211 operations for comprehensive Excel automation**
44

55
---
66

@@ -15,7 +15,7 @@
1515

1616
---
1717

18-
## 🔄 Power Query & M Code (11 operations)
18+
## 🔄 Power Query & M Code (12 operations)
1919

2020
**Atomic Operations** - Single-call workflows:
2121
- **List:** List all Power Query queries in workbook
@@ -29,6 +29,7 @@
2929
- **Get Load Config:** Get current load configuration
3030
- **Unload:** Remove data from all destinations (keeps query definition)
3131
- **Delete:** Remove Power Query from workbook
32+
- **Evaluate:** Execute M code directly and return results (without creating a permanent query)
3233

3334
**Automatic M-Code Formatting:** M code is automatically formatted on write operations (Create, Update) using the powerqueryformatter.com API (by mogularGmbH, MIT License). Read operations return M code as stored in Excel. Formatting adds ~100-500ms network latency but dramatically improves readability with proper indentation, spacing, and line breaks. Graceful fallback returns original M code if formatting fails.
3435

@@ -390,7 +391,7 @@
390391
| Category | Operations |
391392
|----------|-----------|
392393
| File Operations | 6 |
393-
| Power Query | 10 |
394+
| Power Query | 12 |
394395
| Data Model/DAX | 19 |
395396
| Excel Tables | 27 |
396397
| PivotTables | 30 |
@@ -402,7 +403,7 @@
402403
| VBA Macros | 6 |
403404
| Slicers | 8 |
404405
| Conditional Formatting | 2 |
405-
| **Total** | **209** |
406+
| **Total** | **211** |
406407

407408
---
408409

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
**Automate Excel with AI - A Model Context Protocol (MCP) server for comprehensive Excel automation through conversational AI.**
2020

21-
**MCP Server for Excel** enables AI assistants (GitHub Copilot, Claude, ChatGPT) to automate Excel through natural language commands. Automate Power Query, DAX measures, VBA macros, PivotTables, Charts, formatting, and data transformations (22 tools with 210 operations).
21+
**MCP Server for Excel** enables AI assistants (GitHub Copilot, Claude, ChatGPT) to automate Excel through natural language commands. Automate Power Query, DAX measures, VBA macros, PivotTables, Charts, formatting, and data transformations (22 tools with 211 operations).
2222

2323
### CLI vs MCP Server
2424

@@ -81,7 +81,7 @@ Download the `.mcpb` file from the [latest release](https://github.com/sbroenne/
8181

8282
## 🎯 What You Can Do
8383

84-
**22 specialized tools with 210 operations:**
84+
**22 specialized tools with 211 operations:**
8585

8686
- 🔄 **Power Query** (1 tool, 11 ops) - Atomic workflows, M code management, load destinations
8787
- 📊 **Data Model/DAX** (2 tools, 18 ops) - Measures with auto-formatted DAX, relationships, model structure
@@ -97,7 +97,7 @@ Download the `.mcpb` file from the [latest release](https://github.com/sbroenne/
9797
- �️ **Slicers** (1 tool, 8 ops) - Interactive filtering for PivotTables and Tables
9898
- �🎨 **Conditional Formatting** (1 tool, 2 ops) - Rules and clearing
9999

100-
📚 **[Complete Feature Reference →](FEATURES.md)** - Detailed documentation of all 210 operations
100+
📚 **[Complete Feature Reference →](FEATURES.md)** - Detailed documentation of all 211 operations
101101

102102

103103
## 💬 Example Prompts

gh-pages/404.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The page you're looking for doesn't exist or has been moved.
1717

1818
## Quick Links
1919

20-
- [Features](/features/) — All 22 tools and 210 operations
20+
- [Features](/features/) — All 22 tools and 211 operations
2121
- [Installation](/installation/) — Setup guides for VS Code, Claude, and CLI
2222
- [Changelog](/changelog/) — Release notes and version history
2323
- [GitHub Repository](https://github.com/sbroenne/mcp-server-excel) — Source code and issues

gh-pages/features.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: "Complete Feature Reference"
4-
description: "22 specialized tools with 210 operations for comprehensive Excel automation. Power Query, DAX, VBA, Charts, PivotTables, and more."
4+
description: "22 specialized tools with 211 operations for comprehensive Excel automation. Power Query, DAX, VBA, Charts, PivotTables, and more."
55
keywords: "Excel MCP features, Power Query automation, DAX measures, VBA macros, Excel tools, MCP operations"
66
permalink: /features/
77
---
@@ -10,7 +10,7 @@ permalink: /features/
1010
<div class="container">
1111
<div class="hero-content">
1212
<h1 class="hero-title">Complete Feature Reference</h1>
13-
<p class="hero-subtitle">22 specialized tools with 210 operations for comprehensive Excel automation</p>
13+
<p class="hero-subtitle">22 specialized tools with 211 operations for comprehensive Excel automation</p>
1414
</div>
1515
</div>
1616
</div>

gh-pages/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ It works with any MCP-compatible AI assistant like GitHub Copilot, Claude Deskto
102102
</div>
103103
</div>
104104

105-
<p><a href="/features/">See all 22 tools and 210 operations →</a></p>
105+
<p><a href="/features/">See all 22 tools and 211 operations →</a></p>
106106

107107
## What Can You Do With It?
108108

@@ -182,7 +182,7 @@ excelcli -q session close --session 1 --save
182182

183183
## Documentation
184184

185-
📖 **[Complete Feature Reference](/features/)** — All 22 tools and 210 operations
185+
📖 **[Complete Feature Reference](/features/)** — All 22 tools and 211 operations
186186

187187
📥 **[Installation Guide](/installation/)** — Setup for VS Code, Claude Desktop, other MCP clients, and CLI
188188

mcpb/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Excel MCP Server lets you automate Excel through conversation with Claude:
1212
- **Format & Style** - Conditional formatting, number formats, table styles
1313
- **Automate** - VBA macros, batch operations, data refresh
1414

15-
**22 tools with 210 operations** for comprehensive Excel automation.
15+
**22 tools with 211 operations** for comprehensive Excel automation.
1616

1717
## Requirements
1818

mcpb/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"display_name": "Excel (Windows)",
55
"version": "1.1.5",
66
"description": "Manage Sheets, Power Query, DAX, VBA, PowerPivot, Tables, Ranges, Charts, Formatting, Validation & more - requires Excel to be installed",
7-
"long_description": "Enterprise-grade MCP server for Microsoft Excel automation. 22 specialized tools with 210 operations for Power Query, DAX, VBA, Charts, PivotTables, Conditional Formatting, and more. Windows-only, requires Microsoft Excel desktop application (2016 or later).",
7+
"long_description": "Enterprise-grade MCP server for Microsoft Excel automation. 22 specialized tools with 211 operations for Power Query, DAX, VBA, Charts, PivotTables, Conditional Formatting, and more. Windows-only, requires Microsoft Excel desktop application (2016 or later).",
88
"author": {
99
"name": "Stefan Broenner",
1010
"url": "https://github.com/sbroenne"

skills/shared/excel_powerquery.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Alternative path (for existing worksheet tables):
2828

2929
- create: Import NEW query using inline `mCode` (FAILS if query already exists - use update instead)
3030
- update: Update EXISTING query M code + refresh data (use this if query exists)
31+
- evaluate: Execute M code directly, return results WITHOUT creating a permanent query (test before create!)
3132
- rename: Change query name (requires both `queryName` and `newName` parameters)
3233
- load-to: Loads to worksheet or data model or both (not just config change) - CHECKS for sheet conflicts
3334
- unload: Removes data from ALL destinations (worksheet AND Data Model) - keeps query definition
@@ -49,6 +50,22 @@ Alternative path (for existing worksheet tables):
4950
- Query already exists? → Use update (create will error "already exists")
5051
- Not sure? → Check with list action first, then use update if exists or create if new
5152

53+
**RECOMMENDED WORKFLOW - Always evaluate before create**:
54+
55+
1. `evaluate` → verify data looks correct (catches syntax errors, missing sources, wrong columns)
56+
2. `create` → stores validated query in workbook
57+
58+
Skip evaluate only for trivial literal tables (`#table` with hardcoded values).
59+
60+
**IF CREATE/UPDATE FAILS**: Use `evaluate` to get detailed Power Query error message, fix code, retry.
61+
This avoids polluting the workbook with broken queries and gives better error messages than COM exceptions.
62+
63+
**Additional evaluate use cases**:
64+
65+
- Execute one-off queries without creating permanent queries
66+
- Ad-hoc data exploration or debugging M code transformations
67+
- Returns tabular data (columns, rows) in JSON - no cleanup needed
68+
5269
**List action and IsConnectionOnly**:
5370

5471
- `IsConnectionOnly=true` means query has NO data destination (not in worksheet, not in Data Model)

src/ExcelMcp.CLI/Commands/PowerQueryCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
5050
"load-to" => new { queryName = settings.QueryName, loadDestination = settings.LoadDestination },
5151
"get-load-config" => new { queryName = settings.QueryName },
5252
"unload" => new { queryName = settings.QueryName },
53+
"evaluate" => new { mCode },
5354
_ => new { queryName = settings.QueryName }
5455
};
5556

@@ -92,7 +93,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
9293
internal sealed class Settings : CommandSettings
9394
{
9495
[CommandArgument(0, "<ACTION>")]
95-
[Description("The action to perform (e.g., list, view, create, update, refresh)")]
96+
[Description("The action to perform (e.g., list, view, create, update, refresh, evaluate)")]
9697
public string Action { get; init; } = string.Empty;
9798

9899
[CommandOption("-s|--session <SESSION>")]

0 commit comments

Comments
 (0)