使用说明
ILSpy MCP服务器
建立在其上的模型上下文协议(MCP)服务器。NET 9提供内存。NET汇编反编译和AI助手的结构分析功能 ILSpy(ICSharpCode.Decompiler).
💡 运作原理
ILSpy MCP服务器充当AI助手(如Claude Code、Cursor、Antigravity)和编译之间的桥梁。NET二进制文件(.dll, .exe).它使LLM能够检查、反编译和分析已编译的文件。NET代码直接通过自然语言编写。
┌─────────────────────────────────────────────────────────────────────────────┐
│ AI Client / Host │
│ (Claude Code, Cursor, Antigravity) │
└───────────────────────────────────┬─────────────────────────────────────────┘
│ stdio (JSON-RPC 2.0)
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ ILSpy MCP Server │
│ │
│ ┌───────────────────────────────────────────────────────────────────────┐ │
│ │ Transport Layer (MCP Tools) │ │
│ │ Mappings: Tool Request ➔ Parameters Validation ➔ Response Formatting │ │
│ └──────────────────────────────────┬────────────────────────────────────┘ │
│ │ │
│ ┌──────────────────────────────────▼────────────────────────────────────┐ │
│ │ Application Use Cases │ │
│ │ Single-responsibility orchestrators (Timeouts, Cancellation, Limits) │ │
│ └──────────────────────────────────┬────────────────────────────────────┘ │
│ │ │
│ ┌──────────────────────────────────▼────────────────────────────────────┐ │
│ │ Infrastructure (ILSpy Engine Adapter) │ │
│ │ Wraps ICSharpCode.Decompiler CSharpDecompiler & PEFile Metadata │ │
│ └──────────────────────────────────┬────────────────────────────────────┘ │
└─────────────────────────────────────┼───────────────────────────────────────┘
│ In-Memory Inspection
▼
┌───────────────────────────────┐
│ Target .NET Assemblies │
│ (.dll / .exe binaries) │
└───────────────────────────────┘
请求生命周期和处理管道
- JSON-RPC通信:MCP服务器作为后台进程运行,通过标准输入/输出进行通信(
stdio)使用JSON-RPC 2.0。日志记录被重定向到stderr以保留协议有效载荷的标准输出。 - 刀具选择和参数映射:当AI客户端调用工具(例如。,
decompile_type或analyze_assembly),the 传输层 接收调用,使用强类型域模型验证输入(AssemblyPath,TypeName),并尽早防止路径遍历或丢失文件错误。 - 用例执行:The 应用层 调用一个专用的、单一责任的用例。它设置
CancellationToken与可配置的操作超时和尺寸安全限制相关联。 - ILSpy引擎处理:The 基础设施层 将文件路径传递给
ICSharpCode.Decompiler.CSharp.CSharpDecompiler和PEFile:- 解析PE元数据表和TypeDefs/MethodDefs。
- 重建C#抽象语法树(AST)。
- 在内存中生成格式化的、人类可读的C#源代码或结构化元数据。
- 响应格式化和截断:检查输出大小
MaxDecompilationSize以保护LLM上下文窗口。格式化的输出被发送回LLM。
🎯 推荐的LLM工作流程
在分析不熟悉的内容时获得最佳结果。NET库或程序集,AI助手遵循3步工作流程:
Step 1: DISCOVERY Step 2: STRUCTURAL INSPECTION Step 3: DECOMPILATION
┌─────────────────────────┐ ┌───────────────────────────────┐ ┌───────────────────────┐
│ `analyze_assembly` │ ───► │ `get_type_members` │ ──► │ `decompile_type` │
│ `list_assembly_types` │ │ `find_type_hierarchy` │ │ `decompile_method` │
└─────────────────────────┘ │ `search_members_by_name` │ └───────────────────────┘
│ `find_extension_methods` │
└───────────────────────────────┘
- 发现:使用以下方法了解总体结构和命名空间
analyze_assembly或使用过滤器类型list_assembly_types. - 结构检查:使用以下方法理解类型签名、公共成员和继承图
get_type_members,find_type_hierarchy,或find_extension_methods而无需加载完整的实施机构。 - 定向反编译:使用以下命令分解精确的类逻辑或特定的方法算法
decompile_type或decompile_method.
🛠️ 可用的MCP工具
| 工具 | 目的 | 描述 | 关键参数 |
|---|---|---|---|
analyze_assembly |
高级体系结构 | 命名空间、公共/内部类型计数和关键入口点的高级摘要。 | assemblyPath, query |
list_assembly_types |
类型发现 | 列出按命名空间分组的所有类型。支持命名空间过滤 | assemblyPath, namespaceFilter |
get_type_members |
API表面检查 | 列出类型的方法、属性、字段和事件,而不反编译代码体。 | assemblyPath, typeName |
find_type_hierarchy |
继承映射 | 显示基类、实现的接口和派生类型。 | assemblyPath, typeName |
decompile_type |
完整类型反编译 | 分解指定类、接口、结构或枚举的完整C#源代码。 | assemblyPath, typeName |
decompile_method |
方法反编译 | 仅分解特定方法的主体和签名。 | assemblyPath, typeName, methodName |
search_members_by_name |
成员搜索 | 在程序集中搜索与子字符串或模式匹配的成员。 | assemblyPath, searchTerm |
find_extension_methods |
扩展发现 | 查找程序集中定义的所有针对特定类型的C#扩展方法。 | assemblyPath, targetTypeName |
🚀 快速开始
先决条件
- .NET 9.0 SDK 或更高
- MCP兼容客户端(克劳德代码、光标、反重力、克劳德桌面、VS代码等)
选项A:从NuGet安装(推荐)
安装 ILSpyMcp.Server 全局通过NuGet:
dotnet tool install -g ILSpyMcp.Server
要更新到NuGet的最新版本,请执行以下操作:
dotnet tool update -g ILSpyMcp.Server
选项B:从源代码构建和运行
克隆存储库并构建项目:
git clone https://github.com/gentledepp/ILSpy-Mcp.git
cd ILSpy-Mcp
dotnet build -c Release
然后,您可以直接在MCP客户端设置中运行构建的DLL:
{
"mcpServers": {
"ilspy-mcp": {
"command": "dotnet",
"args": ["/path/to/ILSpy-Mcp/bin/Release/net9.0/ILSpy.Mcp.dll"]
}
}
}
📦 打包和发布到NuGet
该项目已预先配置为。NET全局工具包(ILSpyMcp.Server).
构建和打包
创造一个 .nupkg 处于发布模式的包:
dotnet pack -c Release -o ./nupkg
发布到NuGet.org
将生成的包推送到NuGet.org:
dotnet nuget push ./nupkg/ILSpyMcp.Server.*.nupkg \
--api-key YOUR_NUGET_API_KEY \
--source https://api.nuget.org/v3/index.json
安装本地构建(测试)
在发布之前测试本地构建的包:
dotnet tool install -g --add-source ./nupkg ILSpyMcp.Server
⚙️ MCP客户端配置
Claude 代码
直接在CLI中注册:
claude mcp add ilspy-mcp --command "ilspy-mcp" --scope user
或添加到 .mcp.json 在您的工作空间中:
{
"mcpServers": {
"ilspy-mcp": {
"type": "stdio",
"command": "ilspy-mcp",
"args": []
}
}
}
光标和反重力
添加到MCP服务器配置中:
{
"mcpServers": {
"ilspy-mcp": {
"command": "ilspy-mcp",
"args": []
}
}
}
Claude 桌面版
增添 claude_desktop_config.json:
{
"mcpServers": {
"ilspy-mcp": {
"command": "ilspy-mcp",
"args": []
}
}
}
💬 自然语言提示示例
- 分析装配结构:
“分析架构
/path/to/MyService.dll并列出关键入口点。" - 探索类型:
“列出命名空间中的所有公共类型
MyService.Core里面/path/to/MyService.dll." - 检查API表面:
“显示的所有公共方法和属性
UserManager在/path/to/MyService.dll." - 反编译类实现:
“分解
OrderProcessor阶级固化/path/to/ECommerce.dll并解释如何处理付款。" - 分解特定方法:
“分解
ValidateToken方法JwtHandler从/path/to/Auth.dll."
🔧 环境配置
通过环境变量自定义执行行为:
| 变量 | 描述 | 默认值 |
|---|---|---|
ILSpy__MaxDecompilationSize |
允许的最大输出大小(以字节为单位)(保护上下文窗口) | 1048576 (1 MB) |
ILSpy__DefaultTimeoutSeconds |
操作超时限制(秒) | 30 |
ILSpy__MaxConcurrentOperations |
最大并发反编译线程数 | 10 |
示例设置 .mcp.json:
{
"mcpServers": {
"ilspy-mcp": {
"command": "ilspy-mcp",
"env": {
"ILSpy__MaxDecompilationSize": "2097152",
"ILSpy__DefaultTimeoutSeconds": "60"
}
}
}
}
🏗️ 架构与安全
- 干净的六边形架构:严格解耦为
Domain,Application,Infrastructure,以及Transport. - 只读操作:服务器执行纯只读的反汇编和反射;它从不写入或修改磁盘上的文件。
- 路径验证:检查输入程序集路径是否存在非法字符、路径遍历尝试和文件是否存在。
- 上下文保护:反编译输出受可配置字节大小边界的限制,以避免超出LLM上下文预算。
- Stderr日志记录:所有诊断日志记录都会转到
stderr,离开stdout严格用于MCP协议通信。
📜 许可证
分布在 MIT 许可证.