Power BI MCP服务器
工具
| 工具 | 说明 | 参数 |
|---|---|---|
list_workspaces | 列出经过身份验证的用户可访问的所有Power BI工作区 | -- |
list_datasets | 列出工作区中的数据集 | workspaceId |
list_tables | 列出数据集中的表和列(通过DAX COLUMNSTATISTICS()) | workspaceId, datasetId |
execute_dax | 通过REST API执行DAX查询 | workspaceId, datasetId, daxQuery |
先决条件
- .NET 8 SDK
- ASP。NET Core 8运行时 (
aspnetcore-runtime-8.0) - Azure命令行界面(
az login)或用于身份验证的服务主体 - Power BI Pro/PPU/Premium工作空间
快速开始
# 1. Clone and enter the repo
git clone && cd pbi-mcp
# 2. Log in to Azure (use your Power BI tenant)
az login --tenant
# 3. Update appsettings.json with your tenant ID
# (already configured if you cloned this repo)
# 4. Build and run
dotnet run服务器启动于 http://localhost:3001/mcp.
配置
应用程序参数
{
"PowerBi": {
"TenantId": "your-tenant-id-here"
}
}这 TenantId 传递给 DefaultAzureCredential 以确保为正确的租户获取令牌。
认证
服务器使用 DefaultAzureCredential,它按顺序尝试这些来源:
| 优先级 | 来源 | 用例 |
|---|---|---|
| 1 | AZURE_TENANT_ID / AZURE_CLIENT_ID / AZURE_CLIENT_SECRET env变量 | 服务主体/CI |
| 2 | 工作负载标识 | Kubernetes |
| 3 | 托管身份 | Azure虚拟机、应用服务、ACA |
| 4 | Azure命令行界面(az login) | 地方发展 |
本地开发(az登录)
az login --tenant 16b3c013-d300-468d-ac64-7eda0820b6d3
dotnet run服务主体(环境变量)
export AZURE_TENANT_ID="your-tenant-id"
export AZURE_CLIENT_ID="your-sp-app-id"
export AZURE_CLIENT_SECRET="your-sp-secret"
dotnet run无需更改代码-- DefaultAzureCredential 自动拾取env变量。
连接MCP客户端
VS代码(副本)
回购包括 .vscode/mcp.json:
{
"servers": {
"pbi-mcp": {
"type": "http",
"url": "http://localhost:3001/mcp"
}
}
}使用启动服务器 dotnet run,则Copilot将自动发现工具。
MCP检查员
npx @modelcontextprotocol/inspector连接到 http://localhost:3001/mcp 使用流式HTTP传输。
项目结构
PbiMcpServer.csproj # ASP.NET Core Web project
Program.cs # Entry point — DI, MCP server, HTTP transport
PowerBiService.cs # Power BI REST API + XMLA service layer
PowerBiTools.cs # MCP tool definitions (4 tools)
appsettings.json # Tenant ID + logging config
.vscode/mcp.json # VS Code MCP client config
tests/
PbiMcpServer.Tests/
PowerBiServiceUnitTests.cs # 11 offline tests (mocked HTTP)
PowerBiServiceIntegrationTests.cs # 5 live API tests
XunitLoggerProvider.cs # xUnit ↔ ILogger bridge测试
# Unit tests (offline, fast — no credentials needed)
dotnet test tests/PbiMcpServer.Tests/PbiMcpServer.Tests.csproj \
--filter "Category!=Integration"
# Integration tests (hits live Power BI API — requires az login)
dotnet test tests/PbiMcpServer.Tests/PbiMcpServer.Tests.csproj \
--filter "Category=Integration"
# All tests
dotnet test tests/PbiMcpServer.Tests/PbiMcpServer.Tests.csproj
# With detailed HTTP request/response logging
dotnet test tests/PbiMcpServer.Tests/PbiMcpServer.Tests.csproj \
--filter "Category=Integration" \
--logger "console;verbosity=detailed"架构说明
DAX执行
所有DAX查询都使用 Power BI REST API (POST /datasets/{id}/executeQueries).这适用于任何Pro/EPU/Premium工作区,不需要XMLA端点。
XMLA路径(通过ADOMD.NET)包含在代码库中,但被注释掉了。它需要租户管理员启用XMLA读取的高级/PPU容量。
表发现
这 list_tables 工具用途 EVALUATE COLUMNSTATISTICS() 通过REST DAX端点而不是REST /tables 端点,因为 /tables 仅适用于推送数据集。DAX方法适用于所有数据集类型(导入、DirectQuery、复合)。
.NET运行时
该项目需要:
Microsoft.NETCore.App--基地。NET运行时(控制台、I/O、网络)Microsoft.AspNetCore.App--web运行时(Kestrel、路由、中间件)
安装方式: sudo apt install aspnetcore-runtime-8.0 (包括两者)。
技术栈
- .NET 8 /ASP。NET核心
- 模型上下文协议。AspNetCore --官方MCP C#SDK,支持流式HTTP传输
- Azure。身份 —
DefaultAzureCredential用于代币获取 - 微软。分析服务。AdomdClient --XMLA/DAX连接
- 单元 + Moq --测试
许可证
该项目根据 MIT许可证.
