代码分析工具

一个基于Roslyn API的双模式C#代码分析和重构工具,支持作为MCP服务器或CLI工具运行。

  • by pvasek
  • C#
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 pvasek/csharp-mcp

使用说明

C#MCP-Roslyn驱动的代码分析MCP服务器和CLI

一个使用Roslyn API进行C#代码分析和重构的双模工具。作为一个 MCP服务器 对于人工智能助手或作为传统 CLI工具.

安装

# Install as global tool (after published to NuGet)
dotnet tool install -g CSharpMcp

# Or build from source
git clone <repository-url>
cd csharp-mcp
dotnet build

MCP服务器模式(推荐)

解决方案在启动时加载一次,使后续的工具调用快速。

csharp-mcp                                    # Auto-discover solution
csharp-mcp --solution /path/to/Solution.sln   # Explicit path

MCP配置

{
  "mcpServers": {
    "csharp-mcp": {
      "command": "/path/to/csharp-mcp",
      "args": ["--solution", "/path/to/your/Solution.sln"]
    }
  }
}

可用的MCP工具

工具 说明
csharp_find_definition 查找符号的定义位置
csharp_find_references 查找符号的所有用法
csharp_rename 在整个解决方案中安全地重命名符号
csharp_signature 获取方法/类型签名
csharp_list_members 列出某一类型的成员
csharp_find_implementations 查找接口/抽象类的实现
csharp_inheritance_tree 显示继承层次结构
csharp_find_callers 查找调用特定方法的方法
csharp_find_callees 查找由特定方法调用的方法
csharp_diagnostics 获取编译错误/警告
csharp_check_symbol_exists 检查符号是否存在
csharp_dependencies 分析类型依赖关系
csharp_unused_code 查找可能未使用的代码
csharp_generate_interface 从类中提取接口
csharp_implement_interface 生成实施存根
csharp_list_types 列出命名空间/文件中的类型
csharp_namespace_tree 显示命名空间层次结构
csharp_analyze_file 全面的文件分析

命令行接口命令模式

csharp-mcp [command] [options]
csharp-mcp --solution MySolution.sln find-definition UserService --type class

全局选项

选项 描述
--solution, -s .sln文件的路径(如果未指定,则自动发现)
--output, -o 输出格式:json、text、markdown(默认为json)
--verbose, -v 启用详细日志记录

命令参考

命令 选项
find-definition <symbol> --type, -t (类/方法/属性/字段/接口/枚举), --in-file, -f, --in-namespace, -n
find-references <symbol> --type, -t, --in-namespace, -n
rename <old> <new> --type, -t, --in-namespace, -n, --preview, --rename-file
signature <symbol> --type, -t, --include-overloads, --include-docs
list-members <type> --kind, -k (方法/属性/字段/事件), --accessibility, -a, --include-inherited
find-implementations <symbol> -
inheritance-tree <type> --direction, -d (向上/向下/两者)
find-callers <method> -
find-callees <method> -
diagnostics --severity, -s (错误/警告/信息), --file, -f, --code, -c
check-symbol-exists <symbol> --type, -t, --in-namespace, -n
dependencies <target> -
unused-code -
generate-interface <class> -
implement-interface <interface> -
list-types --namespace
namespace-tree -
analyze-file <path> -

需求

  • .NET 10.0运行时 - https://dotnet.microsoft.com/download/dotnet/10.0
  • 要分析的解决方案或项目文件

技术细节

依赖关系: ModelContextProtocol(v0.7.0-preview.1),微软。代码分析。CSharp。工作区(v5.0.0),微软。建造。定位器(v1.8.1),系统。命令行(v2.0.0-beta4)

看 规格.md 完整规格。

许可证

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