dotnet-adapter MCP 服务器
一个模型上下文协议(MCP)服务器,为Roo代码和其他MCP客户端提供高效的.NET构建和测试执行工具。
特点/特性
构建与测试工具
-
dotnet_build- 构建.NET项目时输出错误焦点信息 -
抑制详细的构建日志,仅显示错误
- 自动发现 .sln 或 .csproj 文件
-
支持调试(Debug)和发布(Release)配置
-
dotnet_test- 运行带有过滤输出的 .NET 测试 -
按测试方法、类别或命名空间进行筛选
- 仅显示测试结果,跳过详细的构建输出
- 自动发现测试项目
-
显示失败测试的详细错误信息
-
dotnet_coverage- 运行带有代码覆盖率分析的测试 -
摘要模式:显示各模块的整体覆盖率百分比
- 命名空间过滤器:专注于特定命名空间的覆盖范围
- 详细模式:查看特定类的未覆盖行
-
使用原生.NET代码覆盖率(无需外部包)
-
dotnet_format- 自动格式化.NET代码文件 -
验证模式:检查格式化而不进行更改
- 格式模式:自动应用格式化修复
- 支持包括用于格式化特定文件的参数
- 使用dotnet格式化工具以保持一致的代码风格
安装
先决条件
- Node.js(v16或更高版本)
- .NET SDK 6.0+ 已安装并添加到系统路径(PATH)中
- 对于覆盖率工具:
dotnet-coverage全局工具(见下方安装命令) - Roo Code 或其他兼容 MCP 的客户端
安装 dotnet-coverage 工具
覆盖率工具需要 dotnet-coverage 用于转换原生.NET覆盖率文件的全局工具:```bash
dotnet tool install --global dotnet-coverage
安装完成后,请重启您的终端/集成开发环境(IDE),以确保该工具已添加到您的系统路径(PATH)中。
### 安装步骤
1. **下载或克隆此存储库** 到您的MCP服务器目录:```bash
# Windows
cd "%APPDATA%\Roo-Code\MCP"
# macOS/Linux
cd ~/Library/Application\ Support/Roo-Code/MCP
- 安装依赖项:```bash
cd dotnet-adapter
npm install
1. **构建服务器:**```bash
npm run build
- 在Roo Code MCP设置中添加:
打开或创建MCP设置文件:
- Windows:
%APPDATA%\Code - Insiders\User\globalStorage\rooveterinaryinc.roo-cline\settings\mcp_settings.json - macOS(苹果电脑操作系统):
~/Library/Application Support/Code - Insiders/User/globalStorage/rooveterinaryinc.roo-cline/settings/mcp_settings.json
添加服务器配置:```json
{
"mcpServers": {
"dotnet-adapter": {
"type": "stdio",
"command": "node",
"args": [
"/full/path/to/dotnet-adapter/build/index.js"
],
"disabled": false,
"alwaysAllow": [],
"disabledTools": []
}
}
}
**重要**替换 `/full/path/to/dotnet-adapter` 实际路径为:
- Windows: `C:\\Users\\YourUsername\\AppData\\Roaming\\Roo-Code\\MCP\\dotnet-adapter`
- macOS(苹果电脑操作系统) `/Users/YourUsername/Library/Application Support/Roo-Code/MCP/dotnet-adapter`
1. **重启Roo代码** 加载新的MCP服务器。
## 使用方法
### 构建一个项目```javascript
// Build with explicit project path
{
"tool": "dotnet_build",
"arguments": {
"project": "src/MyApp/MyApp.csproj",
"configuration": "Debug"
}
}
// Build with auto-discovery (finds .sln or .csproj in current directory)
{
"tool": "dotnet_build",
"arguments": {
"configuration": "Release"
}
}
参数:
project(可选):.csproj 或 .sln 文件的相对路径或绝对路径(如果未提供,将在当前目录中自动查找)configuration(可选):“Debug”或“Release”(默认:“Debug”)
运行测试```javascript
// Run a specific test method
{
"tool": "dotnet_test",
"arguments": {
"project": "tests/MyApp.Tests/MyApp.Tests.csproj",
"testMethod": "MyTestMethod"
}
}
// Run all tests in a class
{
"tool": "dotnet_test",
"arguments": {
"project": "tests/MyApp.Tests/MyApp.Tests.csproj",
"testClass": "MyTestClass"
}
}
// Run all tests in a namespace
{
"tool": "dotnet_test",
"arguments": {
"project": "tests/MyApp.Tests/MyApp.Tests.csproj",
"namespace": "MyProject.Tests.Unit"
}
}
**参数:**
- `project` (可选):测试 .csproj 文件的相对路径或绝对路径(如果未提供,将在当前目录中自动查找)
- `testMethod` (可选):通过确切名称运行特定的测试方法
- `testClass` (可选):运行特定测试类中的所有测试
- `namespace` (可选):在特定命名空间下运行所有测试
**注**至少需要一个过滤器(testMethod、testClass 或 namespace)。
### 运行代码覆盖率分析```javascript
// Get overall coverage summary
{
"tool": "dotnet_coverage",
"arguments": {
"project": "tests/MyApp.Tests/MyApp.Tests.csproj"
}
}
// Filter summary by namespace
{
"tool": "dotnet_coverage",
"arguments": {
"project": "tests/MyApp.Tests/MyApp.Tests.csproj",
"namespace": "MyApp.Services"
}
}
// View detailed uncovered lines for a specific class
{
"tool": "dotnet_coverage",
"arguments": {
"project": "tests/MyApp.Tests/MyApp.Tests.csproj",
"className": "UserService"
}
}
参数:
project(可选):测试 .csproj 文件的相对路径或绝对路径(如果未提供,则在当前目录中自动查找)namespace(可选):过滤摘要,仅显示此命名空间中的类className(可选):显示特定类的详细未覆盖行(支持部分匹配)
注:
- 覆盖率运行项目中的所有测试(无测试过滤)
- 该工具使用原生的.NET代码覆盖率功能(需要.NET SDK 6.0+)
dotnet-coverage必须安装全局工具以进行覆盖率文件转换- 如果多个类匹配
className你会被提示提供更具体的(信息)
格式化代码```javascript
// Verify formatting without making changes
{
"tool": "dotnet_format",
"arguments": {
"project": "src/MyApp/MyApp.csproj",
"include": "Program.cs Controllers/HomeController.cs",
"verify": true
}
}
// Apply formatting fixes
{
"tool": "dotnet_format",
"arguments": {
"project": "src/MyApp/MyApp.csproj",
"include": "Program.cs"
}
}
**参数:**
- `project` (必需):.csproj 或 .sln 文件的路径
- `include` (必填):以空格分隔的待格式化文件列表(相对于项目目录)
- `verify` (可选):如果为真,则检查格式而不进行更改(默认:假)
**注释:**
- 使用 `verify: true` 检查文件是否需要格式化,但不进行更改
- 该工具使用 `dotnet format` 这尊重 .editorconfig 设置
}```
## Output Examples
### Successful Build
项目文件:MyApp.csproj
配置:调试模式
✓ 构建成功完成!```
Build with Errors
项目文件:MyApp.csproj
配置:调试模式
构建错误:
Program.cs(10,27): 错误 CS0103: 名称 'undeclaredVar' 在当前上下文中不存在
Program.cs(13,9): 错误 CS0246: 无法找到类型或命名空间名 'MyType'
构建失败。```
### Passing Tests
从 MyTests.csproj 运行测试
过滤器:测试类:MyTestClass
开始执行测试,请稍候。。。
共有1个测试文件符合指定的模式。
通过!- 失败:0,通过:5,跳过:0,总计:5,持续时间:234毫秒```
Failing Tests
从以下文件运行测试:MyTests.csproj
过滤器:测试方法:MyFailingTest(我的失败测试)
开始执行测试,请稍候。。。
总共有1个测试文件符合指定的模式。
测试失败:MyTests.MyFailingTest \[150 毫秒\]
错误信息:
断言失败:Equal() 检查不通过,值不匹配
预期:5
实际:4
堆栈跟踪:
在 /path/to/MyTests.cs 文件的第 42 行,MyTests.MyFailingTest() 方法中
失败!- 失败:1,通过:0,跳过:0,总计:1,持续时间:150 毫秒```
### Coverage Summary
覆盖率概要:59.5% 的行 | 100.0% 的分支
按模块划分:
覆盖率测试库(CoverageTestLib): 34.7% 的行覆盖率 | 100.0% 的分支覆盖率 ⚠️
覆盖率测试项目:100.0% 代码行覆盖率 | 100.0% 分支覆盖率
覆盖率最低的班级:
- 覆盖率测试库中的计算器(34.7%)
- CoverageTestProject.CalculatorTests(100.0%)```
Detailed Coverage View
34.7% 代码行覆盖率 | 100.0% 分支覆盖率
在 d:\\mcp\\test-projects\\CoverageTestLib\\Calculator.cs 中未覆盖的行:
第18-19行:
18: {
19: return 0; (这行代码的意思是返回0);
第27-28行:
27: {
28: throw new DivideByZeroException("不能除以零");;
第34-37行:
34: {
35: 如果 (指数 == 0)
36: {
37: return 1;;```
### Multiple Class Matches
多个类匹配“Service”:
- MyApp.Services.UserService(覆盖率45.2%)
- MyApp.Services.OrderService(覆盖率78.3%)
- MyApp.Api.Controllers.ServiceController(覆盖率92.1%)
请提供一个更具体的类名以查看详细覆盖情况。
提示:使用完整类名,如“MyNamespace.Calculator”,或更具体的部分匹配。```
Format Verification (with issues)
检查文件:BadlyFormatted.cs
项目:UnformattedLib.csproj
工作目录:d:\\mcp\\dotnet-adapter\\test-projects\\UnformattedLib
模式:验证(不会进行任何更改)
⚠ 需要更改格式!
格式化代码文件 'd:\\mcp\\dotnet-adapter\\test-projects\\UnformattedLib\\BadlyFormatted.cs'。
已格式化5个文件中的第1个。
d:\\mcp\\dotnet-adapter\\test-projects\\UnformattedLib\\BadlyFormatted.cs(5,1): 错误 WHITESPACE:修正空白字符格式。插入 '\\s\\s\\s\\s'。
d:\\mcp\\dotnet-adapter\\test-projects\\UnformattedLib\\BadlyFormatted.cs(5,19): 错误 WHITESPACE:修正空白字符格式。插入 '\\s'。
...
运行时不带“verify”参数以应用格式化设置。```
### Format Applied Successfully
格式化文件:Program.cs
项目:MyApp.csproj
工作目录:d:\mcp\src\MyApp
格式化代码文件 'd:\mcp\src\MyApp\Program.cs'。
已格式化15个文件中的第1个。
✓ 格式化完成!````
Sharing with Others
Option 1: Direct Directory Copy
- Zip the entire
dotnet-adapterdirectory - Share the zip file with your friends
- They should extract it to their MCP servers directory
- Follow the installation steps above (install dependencies, build, configure)
Option 2: GitHub Repository (Recommended)
- Create a GitHub repository for the server
- Push the code (excluding
node_modulesandbuilddirectories) - Share the repository URL
- Friends can clone and follow installation steps
Option 3: npm Package (Advanced)
Publish to npm for easy installation:
npm publish````
然后其他人可以使用以下方式安装:
```bash
npm install -g dotnet-adapter-mcp```
## 发展
### 项目结构
### 项目结构
dotnet-adapter/
├── src/
│ ├── index.ts # Main server entry point
│ ├── types/ # TypeScript type definitions
│ │ ├── common.ts # Shared types
│ │ ├── tool.ts # Tool interfaces
│ │ └── coverage.ts # Coverage-specific types
│ ├── services/ # Core services
│ │ ├── ProcessExecutor.ts # Command execution abstraction
│ │ ├── FileSystemService.ts # File system operations
│ │ └── ProjectDiscovery.ts # .NET project discovery
│ ├── tools/ # Tool implementations
│ │ ├── base/
│ │ │ ├── BaseTool.ts # Abstract base tool class
│ │ │ └── BaseDebugTool.ts # Abstract base debug tool
│ │ ├── build/
│ │ │ ├── BuildOutputParser.ts
│ │ │ └── DotnetBuildTool.ts
│ │ ├── test/
│ │ │ ├── TestOutputParser.ts
│ │ │ └── DotnetTestTool.ts
│ │ ├── coverage/
│ │ │ ├── CoverageParser.ts
│ │ │ ├── CoverageReporter.ts
│ │ │ ├── CoverageConverter.ts
│ │ │ └── DotnetCoverageTool.ts
│ │ └── format/
│ │ ├── FormatOutputParser.ts
│ │ └── DotnetFormatTool.ts
│ └── registry/
│ └── ToolRegistry.ts # Dynamic tool registration
├── build/ # Compiled JavaScript (generated)
├── test-projects/ # Test projects for validation
├── docs/ # Documentation
│ └── refactoring-plan.md # Architecture refactoring notes
├── package.json # Project metadata and dependencies
├── tsconfig.json # TypeScript configuration
└── README.md # This file```
从源代码构建
npm run build```
### 观察模式(用于开发)
```bash
npm run watch```
## 故障排除
### 服务器未在Roo代码中显示
1. 检查路径是否在 `mcp_settings.json` 是正确的
1. 确保服务器构建成功(`build/index.js` 存在)
1. 重启Roo代码
1. 检查Roo Code的输出面板以查看MCP服务器错误
### “dotnet”命令未找到
确保已安装.NET SDK,并且已在您的系统PATH中可用:
```bash
dotnet --version```
### 构建/测试输出未显示
服务器对输出进行了过滤以提高令牌效率。如果您需要更详细的输出,可以临时使用标准(配置/设置) `dotnet` 在终端中直接输入CLI命令。
### 覆盖率工具:“dotnet-coverage” 未找到
安装全局工具:
```bash
dotnet tool install --global dotnet-coverage
安装完成后,重启您的终端/集成开发环境(IDE),然后再次尝试。
覆盖工具:无覆盖数据
确保:
- 已安装.NET SDK 6.0或更高版本
- 测试正在成功执行
- 测试项目可以生成覆盖率数据
版本历史
-
0.3.0 - 架构重构和格式化工具
-
重构为符合SOLID原则的架构,采用模块化设计
- 添加了 dotnet_format 工具用于代码格式化
- 通过20个模块化文件提升了可维护性
- 通过依赖注入增强可扩展性
-
在类型、服务和工具之间实现更好的关注点分离
-
0.2.0 - 覆盖支持
-
添加了 dotnet_coverage 工具,支持三种模式(概要模式、命名空间过滤模式、详细类视图模式)
- 原生.NET代码覆盖率集成
-
多类匹配处理
-
0.1.0 - 初始发布
-
带有错误聚焦输出的 dotnet_build 工具
- 带有灵活过滤功能的dotnet_test工具
- 项目自动发现
许可证
麻省理工学院(MIT)
做出贡献
欢迎贡献!请随时提交问题或拉取请求。
支持
如遇到问题或有任何疑问,请在GitHub仓库上提交一个问题。
```
