cervonwong/ILSpy-MCP

GitHub: cervonwong/ILSpy-MCP

这是一个基于 ILSpy 引擎的 MCP 服务器,赋予 AI 助手自动化反编译及深度分析 .NET 二进制文件的能力。

Stars: 14 | Forks: 1

ILSpy MCP Server Banner

License: MIT GitHub stars GitHub release .NET 10.0 C# Platform Build: passing

# ILSpy MCP Server MCP server,通过 [ILSpy](https://github.com/icsharpcode/ILSpy) 为 AI 助手提供 .NET 反编译和静态分析功能。适用于 Windows、Linux 和 macOS 上的任何 MCP 客户端(Claude Code、Codex、Opencode、Cursor 等)。 向你最喜欢的 AI Chatbot 询问如何使用 ILSpy MCP Server:[![ChatGPT](https://img.shields.io/badge/ChatGPT-Read%20README-74aa9c?logo=openai&logoColor=white)](https://chatgpt.com/?q=Read%20https%3A%2F%2Fgithub.com%2Fcervonwong%2FILSpy-MCP%2Fblob%2Fmain%2FREADME.md) [![Claude](https://img.shields.io/badge/Claude-Read%20README-d4a27f?logo=claude&logoColor=white)](https://claude.ai/new?q=Read%20https%3A%2F%2Fgithub.com%2Fcervonwong%2FILSpy-MCP%2Fblob%2Fmain%2FREADME.md) - [这是什么?](#what-is-this) - [功能](#features) - [支持的 MCP 客户端](#supported-mcp-clients) - [快速开始](#quick-start) - [步骤 1:获取二进制文件](#step-1-get-the-binary) - [步骤 2:选择你的传输方式](#step-2-choose-your-transport) - [步骤 3:配置你的 MCP 客户端](#step-3-configure-your-mcp-client) - [工作原理](#how-it-works) - [使用示例](#usage-examples) - [工具参考](#tool-reference) - [反编译](#decompilation) - [IL 反汇编](#il-disassembly) - [类型分析](#type-analysis) - [交叉引用](#cross-references) - [程序集检查](#assembly-inspection) - [搜索](#search) - [跨程序集](#cross-assembly) - [批量操作](#bulk-operations) - [HTTP 服务器参考](#http-server-reference) - [配置参考](#configuration-reference) - [与其他服务器的比较](#comparison-with-other-net-decompilation-mcp-servers) - [致谢](#acknowledgements) - [许可证](#license) ## 这是什么? ILSpy MCP Server 将 [ICSharpCode.Decompiler](https://github.com/icsharpcode/ILSpy/tree/master/ICSharpCode.Decompiler)(ILSpy 的引擎)封装为 [Model Context Protocol](https://modelcontextprotocol.io/) (MCP) 服务器。你的 AI 助手可以直接调用反编译器,并获得它可以推理的结构化结果。不需要 GUI。 ## 功能 **27 个工具**,分为 8 个类别: - **反编译** -- 将类型、方法和整个命名空间反编译为 C# 源代码 - **IL 反汇编** -- 类型或方法级别的原始 CIL/IL - **类型分析** -- 列出类型、检查成员、跟踪层次结构、查找实现者、发现扩展方法 - **交叉引用** -- 使用位置、依赖关系、实例化位置 - **程序集检查** -- 元数据、自定义特性(程序集/类型/成员)、嵌入资源、编译器生成的类型 - **搜索** -- IL 字节码中的字符串字面量和数值常量 - **跨程序集** -- 跨程序集解析类型、加载整个目录 - **批量操作** -- 将程序集导出为完整的 C# 项目 ## 支持的 MCP 客户端 适用于任何兼容 MCP 的客户端。我们推荐 **Claude Code**。 Amazon Q Developer CLI、Augment Code、Claude、**Claude Code**(推荐)、Cline、Codex、Copilot CLI、Crush、Cursor、Gemini CLI、Kilo Code、Kiro、LM Studio、Opencode、Qodo Gen、Qwen Coder、Roo Code、Trae、VS Code、VS Code Insiders、Warp、Windsurf、Zed 等。 ## 快速开始 ### 步骤 1:获取二进制文件 选择你想要安装的方式:
选项 A:预构建二进制文件(推荐 —— 不需要 .NET) 1. 从 [Releases](https://github.com/cervonwong/ILSpy-MCP/releases) 下载适合你平台的最新版本: | 平台 | 文件 | |----------|------| | Windows x64 | `ilspy-mcp-win-x64.zip` | | Linux x64 | `ilspy-mcp-linux-x64.tar.gz` | | Linux ARM64 | `ilspy-mcp-linux-arm64.tar.gz` | | macOS x64 | `ilspy-mcp-osx-x64.zip` | | macOS ARM64 | `ilspy-mcp-osx-arm64.zip` | 每个版本都包含用于验证下载完整性的 SHA256 校验和(`.sha256` 文件)。 2. 解压归档文件: **Windows (PowerShell):** Expand-Archive ilspy-mcp-win-x64.zip -DestinationPath ilspy-mcp **Linux:** tar -xzf ilspy-mcp-linux-x64.tar.gz -C ilspy-mcp chmod +x ilspy-mcp/ILSpy.Mcp **macOS:** unzip ilspy-mcp-osx-arm64.zip -d ilspy-mcp chmod +x ilspy-mcp/ILSpy.Mcp
选项 B:从源代码构建 1. 克隆仓库: git clone https://github.com/cervonwong/ILSpy-MCP.git cd ILSpy-Mcp 2. 构建项目: dotnet build 3. 二进制文件位于 `src/ILSpy.Mcp/bin/Debug/net10.0/ILSpy.Mcp`。在以下步骤中使用此路径。
### 步骤 2:选择你的传输方式 使用下方的流程图来决定如何设置服务器: ``` flowchart TD A[Where is the DLL you want to analyze?] --> B{Is the DLL on your\nlocal machine?} B -->|Yes| C[Use stdio transport\nDefault -- no extra config needed] B -->|No| D{Does the DLL need to stay\non a VM for security?\ne.g. malware analysis} D -->|Yes| E[Use HTTP transport\nRun the server on the VM] D -->|No| C C --> F[Go to Step 3:\nConfigure with stdio] E --> G[Start the HTTP server on the VM] G --> H[Go to Step 3:\nConfigure with HTTP URL] ``` **stdio(默认)** -- MCP 客户端自动启动服务器。无需额外设置。如果 DLL 位于你的本地计算机上,请使用此方式。 **HTTP** -- 你在远程计算机上手动启动服务器。适用于分析虚拟机、构建服务器或 Docker 容器。有关启动命令和配置,请参阅 [HTTP Server Reference](#http-server-reference)。 ### 步骤 3:配置你的 MCP 客户端 选择你的客户端并按照步骤操作。
Claude Code **stdio(本地):** ``` claude mcp add ilspy-mcp --command "/path/to/ilspy-mcp/ILSpy.Mcp" --scope user ``` **HTTP(远程):** ``` claude mcp add ilspy-mcp --transport http http://:3001/mcp --scope user ``` 重启 Claude Code。工具现已可用。
Cursor 添加到你的 MCP 设置 JSON 中: **stdio(本地):** ``` { "mcpServers": { "ilspy-mcp": { "command": "/path/to/ilspy-mcp/ILSpy.Mcp", "args": [] } } } ``` **HTTP(远程):** ``` { "mcpServers": { "ilspy-mcp": { "type": "http", "url": "http://:3001/mcp" } } } ``` 重启 Cursor。
Claude Desktop 添加到 `claude_desktop_config.json`: **stdio(本地):** ``` { "mcpServers": { "ilspy-mcp": { "command": "/path/to/ilspy-mcp/ILSpy.Mcp", "args": [] } } } ``` **HTTP(远程):** ``` { "mcpServers": { "ilspy-mcp": { "type": "http", "url": "http://:3001/mcp" } } } ``` 重启 Claude Desktop。
其他 MCP 客户端 对于任何兼容 MCP 的客户端,请按以下方式配置: - **stdio:** 指向 `ILSpy.Mcp` 二进制文件作为命令 - **HTTP:** 使用 `http://:3001/mcp` 作为 endpoint URL 查阅你客户端的文档以了解如何添加 MCP 服务器。
## 卸载 1. **移除 MCP 客户端注册:**
Claude Code claude mcp remove ilspy-mcp --scope user
Cursor / Claude Desktop / 其他 从你客户端的 MCP 设置 JSON(`claude_desktop_config.json`、Cursor MCP 设置等)中删除 `"ilspy-mcp"` 条目,并重启客户端。
2. **删除解压的目录:** **Windows (PowerShell):** Remove-Item -Recurse -Force C:\path\to\ilspy-mcp **Linux / macOS:** rm -rf /path/to/ilspy-mcp 3. **(仅限从源代码构建)** 删除克隆的仓库: rm -rf ILSpy-MCP 服务器在其自身目录之外不存储任何配置、缓存或状态,因此不需要其他清理工作。 ## 工作原理 你不会直接与 ILSpy MCP 交互。你的 MCP 客户端(Claude Code、Cursor 等)会在后台自动启动并与其通信。 要分析 .NET 程序集,请在聊天中提及 DLL 的完整路径。AI 助手会自动选择正确的工具。 ## 使用示例 将下面的路径替换为你计算机上的实际 DLL 路径: - **反编译类型** -- "Decompile the String class from `C:\Program Files\dotnet\shared\Microsoft.NETCore.App\10.0.0\System.Runtime.dll`" - **列出所有类型** -- "List all types in `C:\Users\me\projects\MyApp\bin\Debug\net10.0\MyLibrary.dll`" - **查找方法** -- "Find the CalculateTotal method in `D:\libs\Calculator.dll`" - **反编译构造函数** -- "Decompile the .ctor constructor of UserService in `C:\repos\MyApp\bin\Debug\net10.0\MyApp.dll`" - **分析类型层次结构** -- "Show me the type hierarchy for ProductService in `C:\repos\ECommerce\bin\Release\net10.0\ECommerce.dll`" - **搜索成员** -- "Search for members containing 'Authenticate' in `C:\repos\AuthService\bin\Debug\net10.0\Auth.dll`" - **查看 IL 反汇编** -- "Show me the raw IL for the Serialize method in `C:\repos\MyApp\bin\Debug\net10.0\MyApp.dll`" - **查找所有用法** -- "Where is the Save method of IRepository called in `C:\repos\MyApp\bin\Debug\net10.0\MyApp.dll`?" - **查找实现者** -- "What types implement ILogger in `C:\Program Files\dotnet\shared\Microsoft.NETCore.App\10.0.0\Microsoft.Extensions.Logging.dll`?" - **跟踪依赖关系** -- "What does the ProcessOrder method in OrderService depend on in `C:\repos\ECommerce\bin\Release\net10.0\ECommerce.dll`?" - **查找实例化** -- "Where is HttpClient instantiated in `C:\repos\MyApp\bin\Debug\net10.0\MyApp.dll`?" - **检查程序集元数据** -- "What target framework and PE bitness does `C:\repos\MyApp\bin\Release\net10.0\MyApp.dll` target?" - **读取自定义特性** -- "What custom attributes are on the OrderService class in `C:\repos\ECommerce\bin\Debug\net10.0\ECommerce.dll`?" - **提取嵌入资源** -- "List all embedded resources in `C:\repos\MyApp\bin\Debug\net10.0\MyApp.dll` and extract the SQL migration script" - **查找编译器生成的类型** -- "Show me the async state machines and closures in `C:\repos\MyApp\bin\Debug\net10.0\MyApp.dll`" - **导出项目** -- "Export `C:\repos\MyApp\bin\Debug\net10.0\MyApp.dll` as a C# project to `C:\temp\MyApp-decompiled`" ## 工具参考 ### 反编译 #### `decompile_type` 将 .NET 类型反编译为 C# 源代码,并提供关于使用模式的 AI 分析见解。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 要反编译的类型的全名(例如 `System.String`) | | `query` | string | 否 | 要查找的特定信息(例如 "method implementations"、"property definitions") |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "MyLibrary.Services.UserService", "query": "overall structure" } ``` **输出(截断):** ``` using System; using System.Threading.Tasks; namespace MyLibrary.Services { public class UserService : IUserService { private readonly IUserRepository _repository; public UserService(IUserRepository repository) { _repository = repository; } public async Task GetByIdAsync(int id) { ... } public async Task DeleteAsync(int id) { ... } ... } } ```
#### `decompile_method` 将特定方法或构造函数(`.ctor`/`.cctor`)反编译为 C# 源代码。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 包含该方法的类型的全名 | | `methodName` | string | 是 | 要反编译的方法的名称 | | `query` | string | 否 | 重点关注方法的哪个方面(例如 "algorithm logic"、"error handling") |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "MyLibrary.Services.UserService", "methodName": "GetByIdAsync" } ``` **输出(截断):** ``` public async Task GetByIdAsync(int id) { if (id <= 0) throw new ArgumentOutOfRangeException(nameof(id)); var user = await _repository.FindByIdAsync(id); if (user == null) throw new UserNotFoundException(id); return user; } ```
#### `list_namespace_types` 列出命名空间中的所有类型,包含完整签名、成员计数和公共方法签名。返回摘要 —— 使用 `decompile_type` 获取单个类型的完整源代码。 | 参数 | 类型 | 必需 | 描述 | |-----------||----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `namespaceName` | string | 是 | 完整的命名空间名称(例如 `System.Collections.Generic`) | | `maxResults` | int | 否 | 返回的最大结果数(默认:100) | | `offset` | int | 否 | 分页时要跳过的结果数(默认:0) |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "namespaceName": "MyLibrary.Services" } ``` **输出(截断):** ``` Namespace: MyLibrary.Services Types: 5 --- Interfaces --- IUserService (Interface, 3 members) Task GetByIdAsync(int id) Task> GetAllAsync() Task DeleteAsync(int id) --- Classes --- UserService : IUserService (Class, 4 members) .ctor(IUserRepository repository) Task GetByIdAsync(int id) Task> GetAllAsync() Task DeleteAsync(int id) OrderService : IOrderService (Class, 3 members) ... ```
### IL 反汇编 #### `disassemble_type` 获取 .NET 类型的原始 CIL/IL 反汇编,以 IL 格式显示方法签名、字段、属性和事件。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 类型的全名(例如 `System.String`) | | `showTokens` | bool | 否 | 显示元数据标记编号(例如 `/* 06000001 */`) | | `resolveDeep` | bool | 否 | 为 true 时,内联展开参数和泛型的完整类型签名(输出较大)。默认 false 保留缩写形式。 |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "MyLibrary.Models.User" } ``` **输出(截断):** ``` .class public auto ansi beforefieldinit MyLibrary.Models.User extends [System.Runtime]System.Object { .field private string 'k__BackingField' .property instance string Name() { .get instance string MyLibrary.Models.User::get_Name() .set instance void MyLibrary.Models.User::set_Name(string) } .method public hidebysig specialname instance string get_Name () cil managed { ... } } ```
#### `disassemble_method` 获取特定方法的原始 CIL/IL 反汇编,包含操作码、标签和堆栈信息。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 包含该方法的类型的全名 | | `methodName` | string | 是 | 要反汇编的方法的名称 | | `showBytes` | bool | 否 | 显示原始操作码字节序列 | | `showTokens` | bool | 否 | 显示元数据标记编号(例如 `/* 06000001 */`) | | `resolveDeep` | bool | 否 | 为 true 时,内联展开参数和泛型的完整类型签名(输出较大)。默认 false 保留缩写形式。 |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "MyLibrary.Services.UserService", "methodName": "GetByIdAsync" } ``` **输出(截断):** ``` .method public hidebysig newslot virtual instance class [System.Runtime]System.Threading.Tasks.Task`1 GetByIdAsync (int32 id) cil managed { .maxstack 2 .locals init ( [0] class MyLibrary.Models.User user ) IL_0000: ldarg.1 IL_0001: ldc.i4.0 IL_0002: bgt.s IL_000a IL_0004: ldstr "id" IL_0009: newobj instance void [System.Runtime]System.ArgumentOutOfRangeException::.ctor(string) IL_000e: throw ... } ```
### 类型分析 #### `list_assembly_types` 列出程序集中的所有类型,按命名空间组织。在不熟悉的库中探索时,从这里开始。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `namespaceFilter` | string | 否 | 按命名空间过滤类型(不区分大小写) |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "namespaceFilter": "MyLibrary.Services" } ``` **输出(截断):** ``` Assembly: MyLibrary Types in namespace 'MyLibrary.Services': 5 MyLibrary.Services.IUserService (Interface) MyLibrary.Services.UserService (Class) MyLibrary.Services.IOrderService (Interface) MyLibrary.Services.OrderService (Class) MyLibrary.Services.ServiceException (Class) ```
#### `get_type_members` 获取类型的完整 API 表面(构造函数、方法、属性、字段、事件),不包含实现细节。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 要检查的类型的全名 |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "MyLibrary.Services.UserService" } ``` **输出(截断):** ``` Type: MyLibrary.Services.UserService (Class) Base: System.Object Implements: MyLibrary.Services.IUserService Constructors: .ctor(IUserRepository repository) Methods: Task GetByIdAsync(int id) Task> GetAllAsync() Task DeleteAsync(int id) Properties: (none) Fields: IUserRepository _repository (private) ```
#### `find_type_hierarchy` 查找继承关系:基类、实现的接口和派生类型。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 要分析的类型的全名 |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "MyLibrary.Services.UserService" } ``` **输出(截断):** ``` Type: MyLibrary.Services.UserService Base types: System.Object Implements: MyLibrary.Services.IUserService Derived types: MyLibrary.Services.CachedUserService ```
#### `find_implementors` 查找程序集中实现给定接口或扩展给定基类的所有类型。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 接口或基类的全名(例如 `MyNamespace.IMyInterface`) |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "MyLibrary.Services.IUserService" } ``` **输出(截断):** ``` Implementors of MyLibrary.Services.IUserService: MyLibrary.Services.UserService (implements) MyLibrary.Services.CachedUserService (implements) ```
#### `find_extension_methods` 查找可用于特定类型的扩展方法。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `targetTypeName` | string | 是 | 要查找扩展的目标类型的全名(例如 `System.String`) |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "targetTypeName": "System.String" } ``` **输出(截断):** ``` Extension methods for System.String: MyLibrary.Extensions.StringExtensions.ToSlug(this string value) -> string MyLibrary.Extensions.StringExtensions.Truncate(this string value, int maxLength) -> string ```
#### `find_compiler_generated_types` 查找编译器生成的类型(异步状态机、显示类、闭包、迭代器)及其父方法和类型上下文。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll" } ``` **输出(截断):** ``` Compiler-generated types: 3 d__1 (AsyncStateMachine) Parent method: UserService.GetByIdAsync Parent type: MyLibrary.Services.UserService <>c__DisplayClass2_0 (DisplayClass) Parent method: OrderService.ProcessOrderAsync Parent type: MyLibrary.Services.OrderService ... ```
#### `search_members_by_name` 按名称在程序集中的所有类型中搜索成员(方法、属性、字段、事件)。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `searchTerm` | string | 是 | 要搜索的名称或部分名称(不区分大小写) | | `memberKind` | string | 否 | 按成员种类过滤:`method`、`property`、`field`、`event` |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "searchTerm": "Async", "memberKind": "method" } ``` **输出(截断):** ``` Members matching 'Async': 4 MyLibrary.Services.UserService.GetByIdAsync(int) -> Task MyLibrary.Services.UserService.GetAllAsync() -> Task> MyLibrary.Services.UserService.DeleteAsync(int) -> Task MyLibrary.Services.OrderService.ProcessOrderAsync(Order) -> Task ```
### 交叉引用 #### `find_usages` 查找程序集中特定成员的所有调用位置、字段访问和属性用法。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 包含该成员的类型的全名(例如 `MyNamespace.MyClass`) | | `memberName` | string | 是 | 成员的名称(方法、字段或属性) |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "MyLibrary.Data.IUserRepository", "memberName": "FindByIdAsync" } ``` **输出(截断):** ``` Usages of MyLibrary.Data.IUserRepository.FindByIdAsync: 3 MyLibrary.Services.UserService.GetByIdAsync (call) MyLibrary.Services.CachedUserService.GetByIdAsync (call) MyLibrary.Tests.UserServiceTests.GetById_ReturnsUser (call) ```
#### `find_dependencies` 查找类型或特定方法的所有外部依赖关系(方法调用、字段访问、类型引用)。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 要分析的类型的全名(例如 `MyNamespace.MyClass`) | | `methodName` | string | 否 | 方法名称,用于将分析范围缩小到特定方法 |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "MyLibrary.Services.UserService", "methodName": "GetByIdAsync" } ``` **输出(截断):** ``` Dependencies of MyLibrary.Services.UserService.GetByIdAsync: 4 MyLibrary.Data.IUserRepository.FindByIdAsync (call) System.ArgumentOutOfRangeException..ctor (call) MyLibrary.Exceptions.UserNotFoundException..ctor (call) MyLibrary.Data.IUserRepository (type reference) ```
#### `find_instantiations` 查找程序集中实例化给定类型(`newobj`)的所有位置。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 要查找实例化的类型的全名(例如 `MyNamespace.MyClass`) |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "System.Net.Http.HttpClient" } ``` **输出(截断):** ``` Instantiations of System.Net.Http.HttpClient: 2 MyLibrary.Services.ApiClient..ctor (newobj) MyLibrary.Services.WebhookSender.SendAsync (newobj) ```
### 程序集检查 #### `analyze_assembly` 获取程序集架构的概览:主要命名空间、公共类型和设计模式。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `query` | string | 否 | 要分析的方面(例如 "architecture overview"、"public API surface") |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "query": "architecture overview" } ``` **输出(截断):** ``` Assembly: MyLibrary, Version=1.0.0.0 Types: 42 | Namespaces: 6 Top namespaces: MyLibrary.Services (12 types) MyLibrary.Models (10 types) MyLibrary.Data (8 types) MyLibrary.Extensions (5 types) ... ```
#### `get_assembly_metadata` 获取程序集元数据,包括目标框架、运行时版本、PE 位数、强名称、入口点和所有引用的程序集。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll" } ``` **输出(截断):** ``` Assembly: MyLibrary Version: 1.0.0.0 Target Framework: .NETCoreApp,Version=v10.0 Runtime: v10.0.0 PE: PE32+ (64-bit) Strong Name: No Entry Point: None Referenced Assemblies: System.Runtime, Version=10.0.0.0 System.Collections, Version=10.0.0.0 Microsoft.Extensions.DependencyInjection.Abstractions, Version=10.0.0.0 ... ```
#### `get_assembly_attributes` 列出程序集上声明的所有自定义特性及其构造函数参数和命名属性。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll" } ``` **输出(截断):** ``` Assembly attributes: [AssemblyTitle("MyLibrary")] [AssemblyVersion("1.0.0.0")] [AssemblyFileVersion("1.0.0.0")] [TargetFramework(".NETCoreApp,Version=v10.0")] ... ```
#### `get_type_attributes` 列出类型上声明的所有自定义特性及其构造函数参数和命名属性。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 类型的全名(例如 `MyNamespace.MyClass`) |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "MyLibrary.Models.User" } ``` **输出(截断):** ``` Attributes on MyLibrary.Models.User: [Serializable] [DataContract(Name = "user")] ```
#### `get_member_attributes` 列出类型成员(方法、属性、字段、事件)上的所有自定义特性及其构造函数参数。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `typeName` | string | 是 | 类型的全名(例如 `MyNamespace.MyClass`) | | `memberName` | string | 是 | 成员的名称(方法、属性、字段或事件) |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "typeName": "MyLibrary.Controllers.UserController", "memberName": "GetUser" } ``` **输出(截断):** ``` Attributes on MyLibrary.Controllers.UserController.GetUser: [HttpGet("{id}")] [ProducesResponseType(typeof(User), 200)] [Authorize(Roles = "Admin")] ```
#### `list_embedded_resources` 列出程序集中的所有嵌入资源,包含名称、类型、大小和可见性。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll" } ``` **输出(截断):** ``` Embedded resources: 3 MyLibrary.Resources.schema.sql (7,234 bytes, public) MyLibrary.Resources.config.json (1,024 bytes, public) MyLibrary.Resources.logo.png (45,678 bytes, public) ```
#### `extract_resource` 提取嵌入资源内容。文本内联显示,二进制内容显示为 base64。支持偏移和限制以进行分页二进制提取。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `resourceName` | string | 是 | 嵌入资源的名称(例如 `MyNamespace.Resources.file.txt`) | | `offset` | int | 否 | 分页提取的字节偏移量 | | `limit` | int | 否 | 分页提取要返回的最大字节数 |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "resourceName": "MyLibrary.Resources.schema.sql" } ``` **输出(截断):** ``` Resource: MyLibrary.Resources.schema.sql Type: text Size: 7,234 bytes CREATE TABLE Users ( Id INT PRIMARY KEY IDENTITY(1,1), Name NVARCHAR(100) NOT NULL, Email NVARCHAR(255) NOT NULL UNIQUE, ... ); ```
### 搜索 #### `search_strings` 在程序集 IL 字节码中搜索匹配正则表达式模式的字符串字面量。扫描所有 `ldstr` 指令。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `pattern` | string | 是 | 要匹配的正则表达式模式(例如 `https?://`、`password`、`Error.*`) | | `maxResults` | int | 否 | 返回的最大结果数(默认:100) | | `offset` | int | 否 | 分页时要跳过的结果数(默认:0) |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "pattern": "https?://" } ``` **输出(截断):** ``` String search for 'https?://': 3 total matches (showing 1-3) "https://api.example.com/v1" in MyLibrary.Services.ApiClient..ctor (IL_0012) surrounding IL: IL_000b: ldarg.0 IL_000c: ldarg.1 IL_000d: stfld string MyLibrary.Services.ApiClient::_baseUrl IL_0012: ldstr "https://api.example.com/v1" IL_0017: stloc.0 IL_0018: ldloc.0 IL_0019: ret "http://localhost:5000" in MyLibrary.Tests.IntegrationTestBase.Setup (IL_0024) surrounding IL: ... "https://cdn.example.com/assets" in MyLibrary.Services.AssetService.GetBaseUrl (IL_0008) surrounding IL: ... [pagination:{total:3,returned:3,truncated:false,offset:0}] ```
#### `search_constants` 在程序集 IL 字节码中搜索数值整型常量。查找所有加载指定值的 `ldc.i4` 和 `ldc.i8` 指令。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `value` | | 是 | 要搜索的确切数值(整数) | | `maxResults` | int | 否 | 返回的最大结果数(默认:100) | | `offset` | int | 否 | 分页时要跳过的结果数(默认:0) |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "value": 404 } ``` **输出(截断):** ``` Constant search for 404: 2 total matches (showing 1-2) 404 (ldc.i4) in MyLibrary.Controllers.UserController.GetUser (IL_0014) 404 (ldc.i4) in MyLibrary.Controllers.OrderController.GetOrder (IL_0021) [pagination:{total:2,returned:2,truncated:false,offset:0}] ```
### 跨程序集 #### `resolve_type` 解析目录中哪个程序集定义了给定类型。支持部分类型名称匹配。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `directoryPath` | string | 是 | 包含 .NET 程序集的目录路径 | | `typeName` | string | 是 | 要搜索的类型名称(支持部分匹配,例如 `HttpClient`) | | `maxDepth` | int | 否 | 最大目录递归深度(默认:3) |
示例 **输入:** ``` { "directoryPath": "C:\\repos\\MyApp\\bin\\Debug\\net10.0", "typeName": "HttpClient" } ``` **输出(截断):** ``` Type 'HttpClient' found in: System.Net.Http.HttpClient Assembly: System.Net.Http, Version=10.0.0.0 Path: C:\repos\MyApp\bin\Debug\net10.0\System.Net.Http.dll ```
#### `load_assembly_directory` 加载并列出目录中找到的所有 .NET 程序集。递归扫描 `.dll` 和 `.exe` 文件,深度可达指定值。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `directoryPath` | string | 是 | 要扫描 .NET 程序集的目录路径 | | `maxDepth` | int | 否 | 最大目录递归深度(默认:3) |
示例 **输入:** ``` { "directoryPath": "C:\\repos\\MyApp\\bin\\Debug\\net10.0" } ``` **输出(截断):** ``` Assemblies loaded: 12 Skipped: 3 Loaded: MyApp, Version=1.0.0.0 MyLibrary, Version=1.0.0.0 Microsoft.Extensions.DependencyInjection, Version=10.0.0.0 ... Skipped: native.dll (not a .NET assembly) legacy.dll (unsupported metadata) ... ```
### 批量操作 #### `export_project` 将 .NET 程序集导出为完整的 C# 项目(`.csproj` + `.cs` 文件)到目标目录。该目录必须为空或不存在。 | 参数 | 类型 | 必需 | 描述 | |-----------|------|----------|-------------| | `assemblyPath` | string | 是 | .NET 程序集文件的路径 | | `outputDirectory` | string | 是 | 导出项目的目标目录(必须为空或不存在) | | `namespaceFilter` | string | 否 | 用于限制导出范围的命名空间过滤器 | | `maxTypes` | int | 否 | 要导出的最大类型数(默认:500) |
示例 **输入:** ``` { "assemblyPath": "C:\\libs\\MyLibrary.dll", "outputDirectory": "C:\\temp\\MyLibrary-decompiled", "namespaceFilter": "MyLibrary.Services" } ``` **输出(截断):** ``` Export complete. Output directory: C:\temp\MyLibrary-decompiled Project file: MyLibrary.csproj Total files: 8 Files: MyLibrary.csproj Services/IUserService.cs Services/UserService.cs Services/IOrderService.cs Services/OrderService.cs Services/ServiceException.cs ... ```
## 设计理念 本项目中的工具是为特定用户设计的:一个没有任何先验知识的大型 .NET 程序集的 AI 代理。这一假设推动了每一个接口决策,并将本项目与通用反编译库区分开来。 **AI 代理是懒惰的。** 它们不会追求来回往返。它们不会从工具名称中推断隐藏的功能。它们只会阅读一次工具的描述和参数列表,决定是否符合其当前意图,然后调用它或忽略它。如果描述是机械式的("列出所有 X 类型的 Y"),代理必须即时将其转化为用例 —— 而且它通常做得不好。如果输出是裸露的("在方法 'Process' 中找到匹配"),代理必须进行后续调用才能弄清楚是 *哪个* `Process` 方法 —— 经过几次这样的往返后,它会放弃并开始产生幻觉。设计的其余部分遵循这两个假设。 **描述表达的是场景,而不是机制。** 一个写得好的工具描述以 "Use this when…"(在……时使用此工具)开头,并命名用户所处的情境。"Use this when you just installed a NuGet library and need to understand its overall architecture"(当你刚刚安装了一个 NuGet 库并需要了解其整体架构时,使用此工具)比 "Analyzes an assembly's types and namespaces"(分析程序集的类型和命名空间)是更好的描述。场景框架让代理能够一次匹配意图与工具;机械框架迫使它推理动词。 **输出默认包含嵌套引用。** 当工具的结果引用了类型、方法或代理显然接下来需要的元数据标记时,本项目会内联解析它。IL 反汇编包含 `call` 和 `newobj` 指令的完全限定目标。匹配结果包含包含类型和程序集。特性列表包含构造函数参数和命名属性。这种权衡是值得的:在服务器上花费更多工作以节省代理一次往返。 **对于无界输出,分页是强制性的。** 任何结果数量取决于程序集内容的工具 —— 类型列表、用法列表、搜索匹配、依赖关系图 —— 都会暴露 `maxResults` 和 `offset` 参数,并在响应中报告 `truncated`/`total`。静默截断被视为错误,而不是功能。这是一条硬性规则。 **丰富,但不泛滥。** "包含嵌套上下文"和"不要淹没上下文窗口"之间的张力是真实的。该项目通过默认为摘要视图并为代理可以指向的特定项目提供可选的详细度来解决这个问题。默认响应应包含足够的信息,以便代理正确选择下一次调用 —— 而不是整个转储。 请参阅 `.claude/skills/mcp-tool-design/SKILL.md` 获取这些原则的面向开发人员的版本,包括新工具检查清单和 MCP 表面使用的动词-名词命名约定。 ## HTTP 服务器参考 对于远程访问(分析虚拟机、构建服务器、容器),请在 HTTP 模式下运行。有关客户端配置,请参阅 [步骤 3](#step-3-configure-your-mcp-client)。 ### CLI 帮助 使用帮助命令查看可用的启动标志: ``` ilspy-mcp help ilspy-mcp --help ilspy-mcp -h ``` ### 启动 HTTP 服务器
预构建二进制文件 ``` # 从您解压发布档案的目录 ./ILSpy.Mcp --transport http # Linux / macOS .\ILSpy.Mcp.exe --transport http # Windows # 仅绑定到 loopback(适用于 SSH 隧道) ./ILSpy.Mcp --transport http --host 127.0.0.1 # Linux / macOS ./ILSpy.Mcp --transport http --host 127.0.0.1 --port 8080 # Linux / macOS .\ILSpy.Mcp.exe --transport http --host 127.0.0.1 # Windows .\ILSpy.Mcp.exe --transport http --host 127.0.0.1 --port 8080 # Windows ```
从源代码 ``` # 从仓库根目录 dotnet run -- --transport http # 仅绑定到 loopback(适用于 SSH 隧道) dotnet run -- --transport http --host 127.0.0.1 # 自定义主机和端口 dotnet run -- --transport http --host 127.0.0.1 --port 8080 ```
服务器会打印 `ILSpy MCP server listening on http://0.0.0.0:3001` 并保持运行,直到你停止它(Ctrl+C)。 ### 更改端口和主机 | 设置 | 默认值 | CLI | 环境变量 | appsettings.json | |---------|---------|-----|--------------|------------------| | 传输方式 | stdio | `--transport http` | `ILSPY_TRANSPORT=http` | `Transport:Type` | | 端口 | 3001 | `--port ` | `Transport__Http__Port` | `Transport:Http:Port` | | 主机 | 0.0.0.0 | `--host ` | `Transport__Http__Host` | `Transport:Http:Host` | 这三个设置按优先级顺序解析:CLI 参数 > 环境变量 > appsettings.json > 默认值。`--host` 和 `--port` 标志仅在选择了 HTTP 传输方式时适用 —— 将它们与 stdio 传输方式一起使用是错误的,服务器将拒绝启动。
通过 appsettings.json 将 HTTP 设为永久默认值 编辑 `appsettings.json`(位于二进制文件旁边): ``` { "Transport": { "Type": "http", "Http": { "Port": 3001, "Host": "0.0.0.0" } } } ```
作为后台服务运行 **Linux (systemd):** ``` # /etc/systemd/system/ilspy-mcp.service [Unit] Description=ILSpy MCP Server After=network.target [Service] ExecStart=/path/to/ILSpy.Mcp --transport http Restart=on-failure User=youruser [Install] WantedBy=multi-user.target ``` ``` sudo systemctl enable --now ilspy-mcp ``` **Windows (Task Scheduler 或 sc.exe):** ``` Start-Process -NoNewWindow -FilePath .\ILSpy.Mcp.exe -ArgumentList "--transport http" ``` **Docker / tmux / screen** 也可以工作。该服务器是一个单一的自包含二进制文件,没有外部依赖。
### 安全性 未内置身份验证。服务器默认绑定到 `0.0.0.0`(所有接口)。请使用以下方式保护它: - 限制对受信任 IP 的防火墙规则 - 添加 TLS 和/或身份验证的反向代理(nginx、Caddy) - 客户端和服务器之间的 VPN 或 SSH 隧道 - 绑定到 `127.0.0.1` 并使用 SSH 端口转发 ## 配置参考 | 变量 | 默认值 | 描述 | |----------|---------|-------------| | `ILSpy__MaxDecompilationSize` | 1048576 (1 MB) | 反编译代码的最大大小(字节) | | `ILSpy__DefaultTimeoutSeconds` | 30 | 操作的默认超时时间(秒) | | `ILSpy__MaxConcurrentOperations` | 10 | 最大并发操作数 | | `ILSPY_TRANSPORT` | `stdio` | 传输模式:`stdio` 或 `http` | | `Transport__Http__Port` | 3001 | HTTP 服务器端口 | | `Transport__Http__Host` | 0.0.0.0 | HTTP 服务器绑定地址 | ## 与其他 .NET 反编译 MCP 服务器的比较 有几个项目通过 MCP 公开 .NET 反编译: | | **ILSpy MCP Server** | [DnSpy-MCPserver-Extension](https://github.com/AgentSmithers/DnSpy-MCPserver-Extension) | [DecompilerServer](https://github.com/pardeike/DecompilerServer) | [ilspy-mcp-server](https://github.com/Borealin/ilspy-mcp-server) | [@iffrce/mcp-dotnetdc](https://www.npmjs.com/package/@iffrce/mcp-dotnetdc) | |---|---|---|---|---|---| | **引擎** | ICSharpCode.Decompiler(积极维护) | dnSpyEx(已弃用的 dnSpy 的社区分支) | ICSharpCode.Decompiler | ILSpy CLI (`ilspycmd`) | ILSpy CLI (`ilspycmd`) | | **所需运行时** | 无(自包含二进制文件) | .NET + dnSpyEx 安装 | .NET 10 | Python 3.8+ 和 ILSpy CLI | Node.js 和 ILSpy CLI | | **安装选项** | 预构建二进制文件或源代码 | Clone + 构建 | Clone + 构建 | `pip install` | `npm install` | | **传输方式** | stdio + HTTP(远程/虚拟机支持) | SSE 或 stdio | stdio | stdio | stdio | | **跨平台** | Windows、Linux、macOS(x64 + ARM64) | 仅 Windows(dnSpyEx 依赖) | Windows、Linux、macOS | 取决于 ILSpy CLI 的可用性 | 取决于 ILSpy CLI 的可用性 | | **工具数量** | 27 | ~10 | ~5 | ~3 | ~3 | | **类型反编译** | 是 | 是 | 是 | 是 | 是 | | **方法反编译** | 是(包括 `.ctor`/`.cctor`) | 是 | 是(成员范围片段) | 否 | 否 | | **命名空间反编译** | 是 | 否 | 否 | 否 | 否 | | **类型层次结构分析** | 是 | 否 | 否 | 否 | 否 | | **扩展方法发现** | 是 | 否 | 否 | 否 | 否 | | **IL 反汇编输出** | 是(类型 + 方法级别) | 是 | 否 | 否 | 否 | | **交叉引用分析** | 是(用法、实现者、依赖关系、实例化) | 否 | 否 | 否 | 否 | | **程序集元数据检查** | 是(目标框架、PE 位数、强名称、入口点、引用) | 否 | 否 | 否 | 否 | | **自定义特性检查** | 是(程序集、类型和成员级别,带有解码参数) | 否 | 否 | 否 | 否 | | **嵌入资源提取** | 是(文本内联显示,二进制为 base64,支持分页) | 否 | 否 | 否 | 否 | | **编译器生成的类型发现** | 是(异步状态机、闭包、带有父上下文的显示类) | 否 | 否 | 否 | 否 | | **字符串/常量搜索** | 是(使用正则表达式扫描 IL 字节码) | 否 | 否 | 否 | 否 | | **项目导出** | 是(完整的 .csproj + .cs 文件) | 否 | 否 | 否 | 否 | | **程序集架构概览** | 是 | 是 | 否 | 是 | 否 | | **成员搜索** | 是 | 是 | 是 | 否 | 否 | | **设计为只读** | 是(从不修改文件) | 具有重命名/编辑功能 | 是 | 是 | 是 | ### 为什么选择这一个? 上面的比较表涵盖了功能差异。这里展示了它未显示的内容: 你只需下载一个二进制文件并运行它。不需要 .NET SDK、Python 或 Node.js。为 Windows、Linux 和 macOS 的 x64 和 ARM64 提供了预构建的二进制文件。 服务器在进程中调用 ICSharpCode.Decompiler,而不是通过 Shell 调用 CLI 工具,因此两者之间没有文本解析层。ICSharpCode.Decompiler 正在积极开发并定期发布,这与 2020 年被弃用的 dnSpy 不同。 HTTP 传输允许你在虚拟机或构建服务器上运行服务器并进行远程连接。表中的其他服务器都不支持这一点。 交叉引用分析(跟踪用法、查找实现者、映射依赖关系)和程序集检查(元数据、特性、嵌入资源、编译器生成的类型)在任何其他服务器中也都不可用。 每个操作都是只读的。服务器从不修改程序集或执行它分析的代码。唯一的例外是 `export_project`,它会将反编译的源文件写入你指定的目录。 ## 致谢 Fork 自 [bivex/ILSpy-Mcp](https://github.com/bivex/ILSpy-Mcp)。 ## 许可证 MIT —— 详见 [](LICENSE)。 ``` ________________________________________ < Built with love by a fellow analyst :) > ---------------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || ```
标签:AI助手, ChatGPT, Claude, CVE检测, DAST, DotNet, ILSpy, IL代码, MCP, Model Context Protocol, Promptflow, URL提取, 中间语言, 二进制分析, 云安全监控, 云安全运维, 云资产清单, 反编译, 多人体追踪, 恶意软件分析, 网络安全, 自动化分析, 跨站脚本, 逆向工程, 隐私保护, 静态分析