.NET格式化服务

一个基于MCP stdio服务器的.NET项目及文件格式化工具,支持本地和容器化工作流,可集成到代理工具中。

  • by ghisguth
  • C#
  • 本地部署
  • 更新于
在 GitHub 查看 ghisguth/mcp-server-dotnet-format

使用说明

McpServerDotnetFormat

用于格式化的模型上下文协议(MCP)stdio服务器。NET项目和文件使用 dotnet format CLI。该项目使基于代理的工具(如GitHub Copilot)能够以编程方式调用格式化命令,支持本地和容器化工作流。

特性

  • 显示的格式化命令。NET项目和文件通过MCP stdio服务器。
  • 支持格式化整个项目、单个文件或目录。
  • 强制执行 .editorconfig、StyleCop和分析器规则。
  • 可以在本地运行,也可以作为Docker容器运行。

局限性

  • JetBrains Rider没有从输出中获得shell赞扬,它希望工具对其进行格式化

全部

  • 我们可以从返回工具改为实际运行工具。为了做到这一点,我们需要:
    • 运行工具,而不是向代理返回命令。
    • 对于docker场景,提供将存储库根挂载到docker的方法
    • 另外,在docker文件中安装netsformat,这样它就可以开箱即用了

入门指南

先决条件

  • .NET 9.0 SDK(请参阅 global.json 对于固定版本)
  • Docker(用于容器化使用)

建设项目

dotnet build

运行MCP服务器(Stdio)

您可以使用以下配置直接运行服务器 mcp.json:

"dotnet-format": {
    "type": "stdio",
    "command": "dotnet",
    "args": [
        "run",
        "--project",
        "/PATH/mcp-server-dotnet-format/src/McpServerDotnetFormat/McpServerDotnetFormat.csproj"
    ]
}

这将启动MCP stdio服务器,准备接受格式化请求。

发布Docker镜像

要为多个平台构建和发布Docker镜像:

dotnet publish /t:PublishContainer src/McpServerDotnetFormat/McpServerDotnetFormat.csproj

输出示例:

Restore complete (0.2s)
  McpServerDotnetFormat succeeded (6.6s) → src/McpServerDotnetFormat/bin/Release/net9.0/linux-arm64/publish/
  McpServerDotnetFormat succeeded (7.2s) → src/McpServerDotnetFormat/bin/Release/net9.0/linux-x64/publish/
  McpServerDotnetFormat succeeded (0.3s) → src/McpServerDotnetFormat/bin/Release/net9.0/publish/

Build succeeded in 9.1s

生成的Docker镜像可以推送到您的注册表(例如。, ghisguth/mcp-server-dotnet-format:latest).

通过Docker运行

您可以使用以下配置在容器中运行服务器:

"dotnet-format-docker": {
    "command": "docker",
    "args": [
        "run",
        "-i",
        "--rm",
        "ghisguth/mcp-server-dotnet-format:latest"
    ],
    "env": {}
}

这将启动Docker容器内的MCP服务器,暴露相同的stdio接口。

示例用法

  • 设置整个项目的格式:

    dotnet format /path/to/YourProject.csproj
    
  • 格式化特定文件:

    dotnet format /path/to/YourProject.csproj --include path/to/YourFile.cs
    

发展

  • 主代码: src/McpServerDotnetFormat/
  • 测验: src/McpServerDotnetFormat.Tests/
  • 样式和分析器规则通过以下方式执行 .editorconfig 以及StyleCop。

许可证

看 许可证 了解详情。


有关更多详细信息,请参阅存储库中的源代码和配置文件。