ILSpy MCP服务器
一个生产级MCP服务器,将所有主要的ILSpy CLI功能作为单独的反编译工具公开。NET程序集。
特性
- 10个MCP工具:分解、导出项目、提取IL、列出类型/方法、解析引用等
- 工作流程编排:自动化端到端处理。NET程序集
- 结构化输出:包含元数据、类型、方法和日志的JSON报告
- 向前兼容:原始ilspycmd工具不支持的功能
- 交叉平台的:适用于Windows、Linux和macOS
先决条件
- Python 3.10+
- ILSpy CLI(ilspycmd)已安装并在PATH中可用
- 下载地址:https://github.com/icsharpcode/ILSpy/releases - 或通过以下方式安装: dotnet tool install -g ilspycmd
安装
pip install -r requirements.txt用法
选项1:MCP服务器模式
启动MCP服务器,并将其与任何兼容MCP的客户端一起使用:
python server.py服务器使用JSON-RPC通过stdio进行通信。使用以下命令在MCP客户端中配置它 config.json.
选项2:直接使用编排器
直接使用编排器处理程序集:
# Single assembly
python orchestrator.py assembly.dll
# Multiple assemblies
python orchestrator.py assembly1.dll assembly2.dll assembly3.exe
# With options
python orchestrator.py assembly.dll --langver:11 --nullable --no-records选项3:Python API
以编程方式使用编排器:
from orchestrator import process_assemblies, save_report
from pathlib import Path
report = process_assemblies(
["assembly.dll"],
language_version="11",
nullable_enabled=True,
handle_records=True,
)
save_report(report, Path("work/report.json"))工具
- 反编译文件 -将程序集解压缩为C#文件
- 支持: --langver, --nullable, --with-records等等。
- 出口工程 -导出可编译的C#项目
- 包括:引用、元数据、项目文件
- get_il -提取IL代码
- 查看中间语言而不是反编译的C#
- list_types -列出装配中的所有类型
- 返回:命名空间限定类型名称
- list_方法 -列出类型的方法
- 输入:完整类型名称(例如。, Namespace.ClassName)
- resolve_references -分析外部参考
- 摘录:包引用、程序集引用
- set_language_version -控制C#语言版本
- 版本:9、10、11、12等。
- enable_nullable_context -启用/禁用可为null的引用类型
- 选项: true 或 false
- handle_records_init_only -正确处理记录类型
- 确保:记录和仅初始化设置程序正确反编译
- raw_ilspycmd -运行任意ilspycmd命令
- 向前兼容所有未来的ILSpy功能
输出结构
work/
input/ # Input assemblies (optional)
output/
decompile/ # Decompiled C# files
/
*.cs files
project/ # Exported projects
/
*.csproj
*.cs files
il/ # IL code output
/
*.il files
report.json # Final structured JSON reportJSON报告格式
编排器生成结构化的JSON报告:
{
"assemblies": [
{
"assembly": "example.dll",
"status": "success",
"paths": {
"decompiled": "work/output/decompile/example/",
"project": "work/output/project/example/",
"il": "work/output/il/example/"
},
"types": [
{
"name": "Namespace.MyClass",
"methods": ["Main", "DoSomething"],
"il_size": {"Main": 123, "DoSomething": 42}
}
],
"references": ["System", "System.Linq"],
"flags_used": ["--langver:11", "--nullable"],
"logs": {
"stdout": "...",
"stderr": "..."
}
}
]
}工作流程
编排器对每个程序集都遵循以下工作流程:
- 反编译 → 生成C#源文件
- 导出项目 → 创建带有引用的可编译项目
- 提取IL → 获取中间语言代码
- 列表类型 → 发现装配中的所有类型
- 列出方法 → 对于每种类型,提取方法
- 解析引用 → 分析依赖关系
- 应用设置 → 语言版本、可空上下文、记录
- 生成报告 → 将所有元数据收集到JSON中
错误处理
- 所有工具返回结构化JSON
returncode,stdout,以及stderr - 失败的操作标记为
status: "error"在报告中 - 调试报告中包含完整的错误日志
- 工具是幂等的,可以多次安全运行
例子
看 example_usage.py 查看以下完整示例:
- 处理单个组件
- 处理多个组件
- 使用不同的语言版本
- 启用/禁用可为null的上下文
- 自定义标志和选项
许可证
此项目封装了ILSpy CLI。有关使用条款,请参阅ILSpy的许可证。
