Java Decompiler MCP Server
# Java Decompiler MCP Server
一个基于 MCP (Model Context Protocol) 的 Java 反编译工具,使用 CFR 反编译器对 `.class` 和 `.jar` 文件进行反编译。
## 功能特性
- ✅ 单个文件反编译(.class / .jar)
- ✅ 多个文件批量反编译
- ✅ 目录递归扫描反编译
- ✅ 自定义输出目录
- ✅ 自动下载 CFR 反编译器
- ✅ 直接保存到文件系统(避免 token 限制)
- ✅ 实时进度显示
- ✅ 详细统计信息
- ✅ **多线程并发处理(大幅提升速度)**
## 改进亮点
### 🚀 解决大文件/大量文件反编译问题
**问题:** 当反编译大量文件时,如果将所有结果作为字符串返回,可能超过 MCP 的 token 限制(例如 413,072 字符)。
**解决方案:**
1. **直接保存到文件系统**(推荐)
- 新增 `save_to_file` 参数(默认 `true`)
- 反编译结果直接写入指定目录
- 仅返回摘要信息(成功数、失败数、输出路径等)
- 避免通过 MCP 传输大量内容
2. **实时进度显示**
- 新增 `show_progress` 参数(默认 `true`)
- 显示当前处理进度:`[1/46] 处理中: Controller.class`
- 实时反馈处理状态
3. **详细统计报告**
- 成功/失败/跳过文件数
- 生成的 .java 文件总数
- 输出目录路径
- 清晰的格式化输出
### 使用示例
**场景 1:反编译大量文件(推荐方式)**
```python
# 使用 8 个线程并发处理,大幅提升速度
decompile_directory(
directory_path="/path/to/classes",
output_dir="/path/to/output",
save_to_file=True, # 默认值
show_progress=True, # 显示进度
max_workers=8 # 8 线程并发
)
```
**场景 2:反编译少量文件并查看内容**
```python
# 返回反编译内容(仅适用于小文件)
decompile_file(
file_path="/path/to/MyClass.class",
save_to_file=False # 返回内容而不是保存
)
```
**场景 3:静默批量处理**
```python
# 不显示详细进度,仅返回统计,单线程处理
decompile_files(
file_paths=[...],
show_progress=False, # 仅显示统计信息
max_workers=1 # 单线程
)
```
## 环境要求
- Python >= 3.10
- Java Runtime Environment (JRE)
- uv (Python 包管理器)
## 安装
### 方式一:通过 uvx 直接使用(推荐)
无需安装,直接在 MCP 配置中使用(可让ai自动下载cfr-0.152.jar然后你手动配置路径):
```json
{
"mcpServers": {
"java-decompiler": {
"type": "stdio",
"command": "uvx",
"args": ["java-decompile-mcp"],
"env": {
"CFR_PATH": "/你的路径/cfr-0.152.jar"
},
"disabled": false
}
}
}
```
### 方式二:本地开发
```bash
# 克隆项目
git clone <repository-url>
cd java-decompile-mcp
# 创建虚拟环境并安装依赖
uv venv
source .venv/bin/activate # macOS/Linux
# 或 .venv\Scripts\activate # Windows
uv pip install "mcp>=1.0.0"
```
## MCP 配置
### 方式一:使用 uvx(推荐,已发布到 PyPI)
在 `.kiro/settings/mcp.json` 或 `claude_desktop_config.json` 中添加:
```json
{
"mcpServers": {
"java-decompiler": {
"command": "uvx",
"args": ["java-decompile-mcp"],
"disabled": false
}
}
}
```
### 方式二:本地开发模式
在 `.kiro/settings/mcp.json` 中添加:
```json
{
"mcpServers": {
"java-decompiler": {
"command": "uv",
"args": [
"--directory",
"/项目路径",
"run",
"main.py"
],
"disabled": false,
"autoApprove": []
}
}
}
```
> ⚠️ 本地开发模式需要将路径替换为实际的项目路径
## 项目地址
GitHub: https://github.com/RuoJi6/java-decompile-mcp
## 可用工具
### 1. decompile_file
反编译单个文件
**参数:**
- `file_path` (必需): 要反编译的文件路径
- `output_dir` (可选): 输出目录,默认为文件所在目录下的 `decompiled` 文件夹
- `save_to_file` (可选): 是否直接保存到文件系统,默认 `true`(推荐)
**示例:**
```
反编译 /path/to/MyClass.class 到 /output/dir
```
**返回结果:**
```
✅ 反编译成功
源文件: /path/to/MyClass.class
输出目录: /output/dir
生成文件数: 1
提示: 反编译结果已保存到文件系统
```
### 2. decompile_files
批量反编译多个文件(支持多线程)
**参数:**
- `file_paths` (必需): 文件路径列表
- `output_dir` (可选): 输出目录
- `save_to_file` (可选): 是否直接保存到文件系统,默认 `true`
- `show_progress` (可选): 是否显示详细进度,默认 `true`
- `max_workers` (可选): 最大并发线程数,默认 `4`(设为 `1` 则单线程)
**示例:**
```
反编译以下文件:
- /path/to/Class1.class
- /path/to/Class2.class
- /path/to/app.jar
使用 8 个线程并发处理
```
**返回结果:**
```
✅ [1/3] 成功: Class1.class
✅ [2/3] 成功: Class2.class
✅ [3/3] 成功: app.jar
============================================================
📊 反编译完成统计
============================================================
✅ 成功: 3
❌ 失败: 0
⏭️ 跳过: 0
📁 总计: 3 个文件
📄 生成: 46 个 .java 文件
📂 输出目录: /output/dir
🔧 并发线程: 4
============================================================
💾 反编译结果已保存到文件系统
```
### 3. decompile_directory
反编译目录下所有 .class 和 .jar 文件(支持多线程)
**参数:**
- `directory_path` (必需): 目录路径
- `output_dir` (可选): 输出目录
- `recursive` (可选): 是否递归子目录,默认 `true`
- `save_to_file` (可选): 是否直接保存到文件系统,默认 `true`
- `show_progress` (可选): 是否显示详细进度,默认 `true`
- `max_workers` (可选): 最大并发线程数,默认 `4`
**示例:**
```
反编译 /path/to/classes 目录下的所有 class 文件,使用 8 个线程
```
**返回结果:**
```
📂 扫描目录: /path/to/classes
🔍 找到 46 个文件待反编译
📤 输出目录: /path/to/classes/decompiled
🔧 并发线程: 4
✅ [1/46] 成功: Controller1.class
✅ [2/46] 成功: Controller2.class
...
✅ [46/46] 成功: Utils.class
============================================================
📊 反编译完成统计
============================================================
✅ 成功: 46
❌ 失败: 0
⏭️ 跳过: 0
📁 总计: 46 个文件
📄 生成: 46 个 .java 文件
📂 输出目录: /path/to/classes/decompiled
🔧 并发线程: 4
============================================================
💾 反编译结果已保存到文件系统
```
### 4. download_cfr_tool
下载 CFR 反编译器
**参数:**
- `target_dir` (可选): 下载目标目录,默认当前工作目录
### 5. check_cfr_status
检查 CFR 反编译器状态
### 6. get_java_version
获取 Java 版本信息
## CFR 配置
CFR 反编译器查找顺序:
1. 环境变量 `CFR_PATH`
2. 项目目录下的 `cfr-*.jar`
3. 自动下载(首次调用反编译工具时)
### 方式一:MCP 配置中指定(推荐)
在 `mcp.json` 的 `env` 中设置:
```json
{
"mcpServers": {
"java-decompiler": {
"command": "uv",
"args": ["--directory", "/项目路径", "run", "main.py"],
"env": {
"CFR_PATH": "/你的路径/cfr-0.152.jar"
}
}
}
}
```
### 方式二:放到项目目录
将 `cfr-*.jar` 文件放到项目根目录,会自动识别。
### 方式三:自动下载
调用 `download_cfr_tool` 工具,会自动从镜像下载到项目目录。
## 手动运行测试
```bash
# 激活虚拟环境
source .venv/bin/activate
# 运行 MCP 服务器
uv run main.py
```
## 许可证
MIT License
TDQS
Scored across 6 tools
The tools have clear distinctions between checking status, downloading, decompiling files/directories, and getting Java version, but decompile_file and decompile_files have overlapping purposes that could cause confusion. The directory vs. file distinction is clear, but the single vs. multiple file tools are functionally similar with minor parameter differences.
All tools follow a consistent snake_case pattern with clear verb_noun structure (check_cfr_status, decompile_directory, decompile_file, decompile_files, download_cfr_tool, get_java_version). The naming is predictable and follows the same convention throughout.
With 6 tools, this is well-scoped for a Java decompiler server. Each tool serves a distinct purpose in the decompilation workflow, from setup (download, check status) to core operations (decompile files/directories) to system information (Java version).
The toolset covers the essential decompilation workflow comprehensively: tool setup (download, status check), decompilation operations at different granularities (file, files, directory), and system verification (Java version). A minor gap is the lack of configuration tools for CFR options or cleanup operations for decompiled output.