DAX格式化服务

一个提供DAX(数据分析表达式)格式化功能的MCP服务器,适用于AI助手和其他MCP客户端,支持单表达式和批量处理。

  • by EmanueleMeazzo
  • C#
  • 本地部署
  • 33 星标
  • 更新于
在 GitHub 查看 EmanueleMeazzo/dax-formatter-mcp

使用说明

DAX格式化程序MCP服务器

一种模型上下文协议(MCP)服务器,为AI助手和其他MCP客户端提供DAX(数据分析表达式)格式化功能。此服务器与官方 DAX格式化程序 SQLBI提供专业级DAX代码格式化服务。

🚀 特性

  • 单个DAX表达式格式:按照专业标准格式化单个DAX表达式
  • 批处理:在单个高效请求中格式化多个DAX表达式
  • 全面的格式化选项:控制线长度、间距、分隔符等
  • 错误处理:具有详细错误消息的稳健错误处理
  • 回退机制:如果批处理失败,则自动回退到单个格式
  • 数据库上下文:支持数据库和服务器上下文(匿名保护隐私)

📋 先决条件

  • .NET 8.0运行时 或更高
  • 互联网连接(与daxformatter.com通信)
  • MCP兼容客户端(Claude Desktop、Continue或自定义MCP客户端)

🛠️ 安装

选项1:下载版本(推荐)

  1. 从下载最新版本 发布 页
  2. 将存档解压缩到您的首选位置
  3. 记下可执行文件的路径以进行配置

选项2:从源代码构建

# Clone the repository
git clone https://github.com/EmanueleMeazzo/dax-formatter-mcp.git
cd dax-formatter-mcp

# Build the project
dotnet build --configuration Release

# The executable will be in bin/Release/net8.0/

⚙️ 配置

克劳德桌面

将以下内容添加到您的Claude Desktop配置文件中:

视窗: %APPDATA%\Claude\claude_desktop_config.json
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Linux: ~/.config/claude/claude_desktop_config.json

{
  "mcpServers": {
    "dax-formatter": {
      "command": "path/to/dax-formatter-mcp.exe",
      "args": []
    }
  }
}

继续IDE扩展

添加到“继续”配置中:

{
  "mcpServers": [
    {
      "name": "dax-formatter",
      "command": "path/to/dax-formatter-mcp.exe",
      "args": []
    }
  ]
}

通用MCP客户端

对于支持MCP 2024-11-05协议的任何MCP客户端:

{
  "servers": [
    {
      "name": "dax-formatter",
      "transport": {
        "type": "stdio",
        "command": "path/to/dax-formatter-mcp.exe",
        "args": []
      }
    }
  ]
}

与一起使用。NET运行时

如果您更喜欢将DLL与一起使用。NET运行时:

{
  "mcpServers": {
    "dax-formatter": {
      "command": "dotnet",
      "args": ["path/to/dax-formatter-mcp.dll"]
    }
  }
}

📖 用法

配置后,您可以通过MCP客户端使用DAX格式化程序:

基本用法

Format this DAX expression:
CALCULATE(SUM(Sales[Amount]),FILTER(Products,Products[Category]="Electronics"))

多个表达式

Format these DAX measures:
1. [Total Sales] := SUM(Sales[Amount])
2. [Electronics Sales] := CALCULATE(SUM(Sales[Amount]),Products[Category]="Electronics")
3. [Sales YTD] := TOTALYTD(SUM(Sales[Amount]),'Calendar'[Date])

使用格式选项

Format this DAX with short lines and specific separators:
CALCULATE(SUM(Sales[Amount]),FILTER(Products,Products[Category]="Electronics"))

Options:
- Line length: ShortLine
- List separator: ;
- Decimal separator: ,

🔧 可用工具

format_dax

格式化单个DAX表达式。

参数:

  • dax (必需):要格式化的DAX表达式
  • options (可选):格式化选项对象

例子:

{
  "dax": "CALCULATE(SUM(Sales[Amount]),FILTER(Products,Products[Category]=\"Electronics\"))",
  "options": {
    "maxLineLength": "LongLine",
    "skipSpaceAfterFunctionName": "BestPractice"
  }
}

format_dax_multiple

在单个请求中格式化多个DAX表达式。

参数:

  • expressions (必需):要格式化的DAX表达式数组
  • options (可选):格式化选项对象

例子:

{
  "expressions": [
    "CALCULATE(SUM(Sales[Amount]),FILTER(Products,Products[Category]=\"Electronics\"))",
    "[Total Sales] := SUM(Sales[Amount])"
  ],
  "options": {
    "maxLineLength": "LongLine",
    "databaseName": "SalesDB",
    "serverName": "ProductionServer"
  }
}

🎛️ 格式选项

所有格式选项都是可选的,并具有合理的默认值:

选项 值 默认值 描述
maxLineLength ShortLine, LongLine, VeryLongLine LongLine 格式化代码的最大行长
skipSpaceAfterFunctionName BestPractice, False BestPractice 函数名称后的间距
listSeparator 任何字符 , 用作列表分隔符的字符
decimalSeparator 任何字符 . 用作小数分隔符的字符
databaseName 字符串 - 上下文数据库名称(匿名)
serverName 字符串 - 上下文服务器名称(匿名)

📝 DAX表达式示例

以下是一些可以测试的DAX表达式示例:

基本措施

[Total Sales] := SUM(Sales[Amount])

复杂计算

CALCULATE(SUM(Sales[Amount]),FILTER(ALL(Products),Products[Category]="Electronics"),USERELATIONSHIP(Sales[OrderDate],'Calendar'[Date]))

时间智能

[Sales YTD] := TOTALYTD(SUM(Sales[Amount]),'Calendar'[Date])

表表达式

EVALUATE FILTER(Customer, Customer[Country] = "USA")

🔍 故障排除

常见问题

服务器启动失败:

  • 确保。NET 8.0运行时已安装
  • 检查配置中的可执行路径是否正确
  • 验证您是否具有互联网连接(DAX格式化程序服务所需)

格式化请求失败:

  • 检查您的互联网连接
  • 验证DAX语法是否有效
  • 先尝试使用更简单的表达式

MCP客户端无法识别服务器:

  • 添加配置后重新启动MCP客户端
  • 检查JSON配置语法
  • 验证可执行路径是否绝对正确

调试模式

要启用调试日志记录,您可以手动运行服务器以查看详细日志:

# Run the server directly to see debug output
./dax-formatter-mcp.exe

然后手动发送JSON-RPC消息以测试功能。

🏗️ 建筑

MCP服务器由以下组件构建:

  • .NET 9.0:现代、跨平台运行时
  • DAX格式化程序客户端:SQLBI的官方NuGet包
  • JSON-RPC 2.0:MCP通信的标准协议
  • 异步/等待:无阻塞I/O,性能更好

流程图

MCP Client → JSON-RPC → DAX Formatter MCP Server → DAX Formatter API → Formatted DAX

🤝 贡献

欢迎投稿!请按照以下步骤操作:

  1. 分叉存储库
  2. 创建要素分支(git checkout -b feature/amazing-feature)
  3. 进行更改
  4. 如果适用,添加测试
  5. 提交您的更改(git commit -m 'Add amazing feature')
  6. 推到分支(git push origin feature/amazing-feature)
  7. 打开拉取请求

开发设置

# Clone the repository
git clone https://github.com/EmanueleMeazzo/dax-formatter-mcp.git
cd dax-formatter-mcp

# Restore dependencies
dotnet restore

# Build the project
dotnet build

# Run tests
dotnet test

🔒 隐私和安全

  • 所有DAX表达式都会发送到daxformatter.com的官方DAX格式化服务
  • DAX格式化程序客户端对数据库和服务器名称进行匿名处理
  • 没有本地存储DAX表达式
  • 不收集或传输任何个人信息

📜 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

🙏 致谢

  • SQLBI 为了获得卓越的DAX格式化服务
  • Anthropic 用于模型上下文协议规范
  • 微软 DAX语言

📚 相关资源