java-class-analyzer-mcp-server 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +285 -0
- package/dist/analyzer/JavaClassAnalyzer.d.ts +61 -0
- package/dist/analyzer/JavaClassAnalyzer.d.ts.map +1 -0
- package/dist/analyzer/JavaClassAnalyzer.js +314 -0
- package/dist/analyzer/JavaClassAnalyzer.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +73 -0
- package/dist/cli.js.map +1 -0
- package/dist/decompiler/DecompilerService.d.ts +35 -0
- package/dist/decompiler/DecompilerService.d.ts.map +1 -0
- package/dist/decompiler/DecompilerService.js +238 -0
- package/dist/decompiler/DecompilerService.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +267 -0
- package/dist/index.js.map +1 -0
- package/dist/scanner/DependencyScanner.d.ts +52 -0
- package/dist/scanner/DependencyScanner.d.ts.map +1 -0
- package/dist/scanner/DependencyScanner.js +235 -0
- package/dist/scanner/DependencyScanner.js.map +1 -0
- package/lib/cfr-0.152.jar +0 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
# Java Class Analyzer MCP Server
|
|
2
|
+
|
|
3
|
+
一个基于Model Context Protocol (MCP)的Java类分析服务,可以扫描Maven项目依赖、反编译Java类文件、获取class方法列表等详细信息,并提供给LLM进行代码分析。
|
|
4
|
+
|
|
5
|
+
## 适用场景
|
|
6
|
+
Cursor等AI工具直接生成调用二方(内部调用)、三方包(外部调用)接口的代码,但因AI无法读取未在当前工程中打开的依赖源码,导致生成的代码错误频出,甚至出现幻觉式编码。
|
|
7
|
+
|
|
8
|
+
为解决此问题,一般会直接拷贝源码内容喂给LLM;或者先将源码文件放到当前工程内,再在对话中引用。
|
|
9
|
+
|
|
10
|
+
而使用本地反编译MCP方案最有效,能精准解析jar包中的类与方法,显著提升代码生成的准确性和可用性。
|
|
11
|
+
|
|
12
|
+
## 功能特性
|
|
13
|
+
|
|
14
|
+
- **🚀使用方便**:mcp服务基于TypeScript实现,使用npm打包,方便分发和安装,弱环境依赖。
|
|
15
|
+
- 🔍 **依赖扫描**: 自动扫描Maven项目的所有依赖JAR包
|
|
16
|
+
- 📦 **类索引**: 建立类全名到JAR包路径的映射索引
|
|
17
|
+
- 🔄 **反编译**: 使用CFR工具(已内置有)实时反编译.class文件为Java源码
|
|
18
|
+
- 📊 **类分析**: 分析Java类的结构、方法、字段、继承关系等
|
|
19
|
+
- 💾 **智能缓存**: 按包名结构缓存反编译结果,支持缓存控制
|
|
20
|
+
- 🚀 **自动索引**: 执行分析前自动检查并创建索引
|
|
21
|
+
- ⚙️ **灵活配置**: 支持外部指定CFR工具路径
|
|
22
|
+
- 🤖 **LLM集成**: 通过MCP协议为LLM提供Java代码分析能力
|
|
23
|
+
|
|
24
|
+
## 使用示例
|
|
25
|
+
### 在IDE中注册mcp服务
|
|
26
|
+

|
|
27
|
+
|
|
28
|
+
### 在智能体对话中使用mcp
|
|
29
|
+

|
|
30
|
+
|
|
31
|
+
## 使用说明
|
|
32
|
+
|
|
33
|
+
### mcp服务安装
|
|
34
|
+
|
|
35
|
+
#### 全局安装(推荐)
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install -g java-class-analyzer-mcp-server
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
安装后可以直接使用 `java-class-analyzer-mcp` 命令。
|
|
42
|
+
|
|
43
|
+
#### 本地安装
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install java-class-analyzer-mcp-server
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
#### 从源码安装
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
git clone https://github.com/handsomestWei/java-class-analyzer-mcp-server.git
|
|
53
|
+
cd java-class-analyzer-mcp-server
|
|
54
|
+
npm install
|
|
55
|
+
npm run build
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### MCP服务配置
|
|
59
|
+
|
|
60
|
+
#### 方法1:使用生成的配置(推荐)
|
|
61
|
+
|
|
62
|
+
运行以下命令生成配置模板:
|
|
63
|
+
```bash
|
|
64
|
+
java-class-analyzer-mcp config -o mcp-client-config.json
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
然后将生成的配置内容添加到你的MCP客户端配置文件中。
|
|
68
|
+
|
|
69
|
+
#### 方法2:手动配置
|
|
70
|
+
|
|
71
|
+
参考以下配置示例,添加到MCP客户端配置文件中:
|
|
72
|
+
|
|
73
|
+
**全局安装后的配置:**
|
|
74
|
+
```json
|
|
75
|
+
{
|
|
76
|
+
"mcpServers": {
|
|
77
|
+
"java-class-analyzer": {
|
|
78
|
+
"command": "java-class-analyzer-mcp",
|
|
79
|
+
"args": ["start"],
|
|
80
|
+
"env": {
|
|
81
|
+
"NODE_ENV": "production",
|
|
82
|
+
"MAVEN_REPO": "D:/maven/repository",
|
|
83
|
+
"JAVA_HOME": "C:/Program Files/Java/jdk-11"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**本地安装后的配置:**
|
|
91
|
+
```json
|
|
92
|
+
{
|
|
93
|
+
"mcpServers": {
|
|
94
|
+
"java-class-analyzer": {
|
|
95
|
+
"command": "node",
|
|
96
|
+
"args": [
|
|
97
|
+
"node_modules/java-class-analyzer-mcp-server/dist/index.js"
|
|
98
|
+
],
|
|
99
|
+
"env": {
|
|
100
|
+
"NODE_ENV": "production",
|
|
101
|
+
"MAVEN_REPO": "D:/maven/repository",
|
|
102
|
+
"JAVA_HOME": "C:/Program Files/Java/jdk-11"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### 参数说明
|
|
110
|
+
- `command`: 运行MCP服务器的命令,这里使用 `node`
|
|
111
|
+
- `args`: 传递给Node.js的参数,指向`npm run build`编译后的dist文件夹内文件
|
|
112
|
+
- `env`: 环境变量设置
|
|
113
|
+
|
|
114
|
+
#### 环境变量说明
|
|
115
|
+
- `NODE_ENV`: 运行环境标识
|
|
116
|
+
- `production`: 生产环境,减少日志输出,启用性能优化
|
|
117
|
+
- `development`: 开发环境,输出详细调试信息
|
|
118
|
+
- `test`: 测试环境
|
|
119
|
+
- `MAVEN_REPO`: Maven本地仓库路径(可选)
|
|
120
|
+
- 如果设置,程序会使用指定的仓库路径扫描JAR包
|
|
121
|
+
- 如果未设置,程序会使用默认的 `~/.m2/repository` 路径
|
|
122
|
+
- `JAVA_HOME`: Java安装路径(可选)
|
|
123
|
+
- 如果设置,程序会使用 `${JAVA_HOME}/bin/java` 执行Java命令(用于CFR反编译)
|
|
124
|
+
- 如果未设置,程序会使用PATH中的 `java` 命令
|
|
125
|
+
- `CFR_PATH`: CFR反编译工具的路径(可选,程序会自动查找)
|
|
126
|
+
|
|
127
|
+
### 可用的工具
|
|
128
|
+
|
|
129
|
+
#### 1. scan_dependencies
|
|
130
|
+
扫描Maven项目的所有依赖,建立类名到JAR包的映射索引。
|
|
131
|
+
|
|
132
|
+
**参数:**
|
|
133
|
+
- `projectPath` (string): Maven项目根目录路径
|
|
134
|
+
- `forceRefresh` (boolean, 可选): 是否强制刷新索引,默认false
|
|
135
|
+
|
|
136
|
+
**示例:**
|
|
137
|
+
```json
|
|
138
|
+
{
|
|
139
|
+
"name": "scan_dependencies",
|
|
140
|
+
"arguments": {
|
|
141
|
+
"projectPath": "/path/to/your/maven/project",
|
|
142
|
+
"forceRefresh": false
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
#### 2. decompile_class
|
|
148
|
+
反编译指定的Java类文件,返回Java源码。
|
|
149
|
+
|
|
150
|
+
**参数:**
|
|
151
|
+
- `className` (string): 要反编译的Java类全名,如:com.example.QueryBizOrderDO
|
|
152
|
+
- `projectPath` (string): Maven项目根目录路径
|
|
153
|
+
- `useCache` (boolean, 可选): 是否使用缓存,默认true。避免每次都重复生成。
|
|
154
|
+
- `cfrPath` (string, 可选): CFR反编译工具的jar包路径。已内置有,可以额外指定版本。
|
|
155
|
+
|
|
156
|
+
**示例:**
|
|
157
|
+
```json
|
|
158
|
+
{
|
|
159
|
+
"name": "decompile_class",
|
|
160
|
+
"arguments": {
|
|
161
|
+
"className": "com.example.QueryBizOrderDO",
|
|
162
|
+
"projectPath": "/path/to/your/maven/project",
|
|
163
|
+
"useCache": true,
|
|
164
|
+
"cfrPath": "/path/to/cfr-0.152.jar"
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
#### 3. analyze_class
|
|
170
|
+
分析Java类的结构、方法、字段等信息。
|
|
171
|
+
|
|
172
|
+
**参数:**
|
|
173
|
+
- `className` (string): 要分析的Java类全名
|
|
174
|
+
- `projectPath` (string): Maven项目根目录路径
|
|
175
|
+
|
|
176
|
+
**示例:**
|
|
177
|
+
```json
|
|
178
|
+
{
|
|
179
|
+
"name": "analyze_class",
|
|
180
|
+
"arguments": {
|
|
181
|
+
"className": "com.example.QueryBizOrderDO",
|
|
182
|
+
"projectPath": "/path/to/your/maven/project",
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### 缓存文件
|
|
188
|
+
在当前工程,会生成以下缓存目录和文件。
|
|
189
|
+
- `.mcp-class-index.json`: 类索引缓存文件
|
|
190
|
+
- `.mcp-decompile-cache/`: 反编译结果缓存目录(按包名结构)
|
|
191
|
+
- `.mcp-class-temp/`: 临时文件目录(按包名结构)
|
|
192
|
+
|
|
193
|
+
## 工作流程
|
|
194
|
+
|
|
195
|
+
1. **自动索引**: 首次调用`analyze_class`或`decompile_class`时,自动检查并创建索引
|
|
196
|
+
2. **智能缓存**: 反编译结果按包名结构缓存,支持缓存控制
|
|
197
|
+
3. **分析类**: 使用`analyze_class`或`decompile_class`获取类的详细信息
|
|
198
|
+
4. **LLM分析**: 将反编译的源码提供给LLM进行代码分析
|
|
199
|
+
|
|
200
|
+
## 技术架构
|
|
201
|
+
|
|
202
|
+
### 核心组件
|
|
203
|
+
|
|
204
|
+
- **DependencyScanner**: 负责扫描Maven依赖和建立类索引
|
|
205
|
+
- **DecompilerService**: 负责反编译.class文件
|
|
206
|
+
- **JavaClassAnalyzer**: 负责分析Java类结构
|
|
207
|
+
- **MCP Server**: 提供标准化的MCP接口
|
|
208
|
+
|
|
209
|
+
### 依赖扫描流程
|
|
210
|
+
|
|
211
|
+
1. 执行`mvn dependency:tree`获取依赖树
|
|
212
|
+
2. 解析每个JAR包,提取所有.class文件
|
|
213
|
+
3. 建立"类全名 -> JAR包路径"的映射索引
|
|
214
|
+
4. 缓存索引到`.mcp-class-index.json`文件
|
|
215
|
+
|
|
216
|
+
### 反编译流程
|
|
217
|
+
|
|
218
|
+
1. 根据类名查找对应的JAR包路径
|
|
219
|
+
2. 检查缓存,如果存在且启用缓存则直接返回
|
|
220
|
+
3. 从JAR包中提取.class文件到`.mcp-class-temp`目录(按包名结构)
|
|
221
|
+
4. 使用CFR工具反编译.class文件
|
|
222
|
+
5. 保存反编译结果到缓存`.mcp-decompile-cache`目录(按包名结构)
|
|
223
|
+
6. 返回Java源码
|
|
224
|
+
|
|
225
|
+
## 故障排除
|
|
226
|
+
|
|
227
|
+
### 常见问题
|
|
228
|
+
|
|
229
|
+
1. **Maven命令失败**
|
|
230
|
+
- 确保Maven已安装并在PATH中
|
|
231
|
+
- 检查项目是否有有效的pom.xml文件
|
|
232
|
+
|
|
233
|
+
2. **CFR反编译失败**
|
|
234
|
+
- 确保CFR jar包已下载(支持任意版本号)
|
|
235
|
+
- 检查Java环境是否正确配置
|
|
236
|
+
- 可通过`cfrPath`参数指定CFR路径
|
|
237
|
+
|
|
238
|
+
3. **类未找到**
|
|
239
|
+
- 程序会自动检查并创建索引
|
|
240
|
+
- 检查类名是否正确
|
|
241
|
+
- 确保项目依赖已正确解析
|
|
242
|
+
|
|
243
|
+
## 测试说明
|
|
244
|
+
|
|
245
|
+
### 构建项目
|
|
246
|
+
|
|
247
|
+
```bash
|
|
248
|
+
npm install
|
|
249
|
+
npm run build
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### 测试工具使用
|
|
253
|
+
|
|
254
|
+
项目提供了独立的测试工具,可以直接测试MCP服务的各个功能,无需通过MCP客户端。
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
# 测试所有工具
|
|
258
|
+
node test-tools.js
|
|
259
|
+
|
|
260
|
+
# 测试特定工具
|
|
261
|
+
node test-tools.js --tool decompile_class --class com.alibaba.excel.EasyExcelFactory --project /path/to/project
|
|
262
|
+
|
|
263
|
+
# 不使用缓存
|
|
264
|
+
node test-tools.js --tool decompile_class --no-cache
|
|
265
|
+
|
|
266
|
+
# 指定CFR路径
|
|
267
|
+
node test-tools.js --tool decompile_class --cfr-path /path/to/cfr.jar
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### 测试工具参数
|
|
271
|
+
|
|
272
|
+
- `-t, --tool <工具名>`: 指定要测试的工具 (scan|decompile|analyze|all)
|
|
273
|
+
- `-p, --project <路径>`: 项目路径
|
|
274
|
+
- `-c, --class <类名>`: 要分析的类名
|
|
275
|
+
- `--no-refresh`: 不强制刷新依赖索引
|
|
276
|
+
- `--no-cache`: 不使用反编译缓存
|
|
277
|
+
- `--cfr-path <路径>`: 指定CFR反编译工具的jar包路径
|
|
278
|
+
- `-h, --help`: 显示帮助信息
|
|
279
|
+
|
|
280
|
+
### 日志级别控制
|
|
281
|
+
|
|
282
|
+
通过 `NODE_ENV` 环境变量控制日志输出:
|
|
283
|
+
|
|
284
|
+
- `development`: 输出详细调试信息
|
|
285
|
+
- `production`: 只输出关键信息
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export interface ClassField {
|
|
2
|
+
name: string;
|
|
3
|
+
type: string;
|
|
4
|
+
modifiers: string[];
|
|
5
|
+
}
|
|
6
|
+
export interface ClassMethod {
|
|
7
|
+
name: string;
|
|
8
|
+
returnType: string;
|
|
9
|
+
parameters: string[];
|
|
10
|
+
modifiers: string[];
|
|
11
|
+
}
|
|
12
|
+
export interface ClassAnalysis {
|
|
13
|
+
className: string;
|
|
14
|
+
packageName: string;
|
|
15
|
+
modifiers: string[];
|
|
16
|
+
superClass?: string;
|
|
17
|
+
interfaces: string[];
|
|
18
|
+
fields: ClassField[];
|
|
19
|
+
methods: ClassMethod[];
|
|
20
|
+
}
|
|
21
|
+
export declare class JavaClassAnalyzer {
|
|
22
|
+
private scanner;
|
|
23
|
+
constructor();
|
|
24
|
+
/**
|
|
25
|
+
* 分析Java类的结构信息
|
|
26
|
+
*/
|
|
27
|
+
analyzeClass(className: string, projectPath: string): Promise<ClassAnalysis>;
|
|
28
|
+
/**
|
|
29
|
+
* 使用 javap 工具分析JAR包中的类结构
|
|
30
|
+
*/
|
|
31
|
+
private analyzeClassWithJavap;
|
|
32
|
+
/**
|
|
33
|
+
* 解析 javap 输出
|
|
34
|
+
*/
|
|
35
|
+
private parseJavapOutput;
|
|
36
|
+
/**
|
|
37
|
+
* 解析类声明
|
|
38
|
+
*/
|
|
39
|
+
private parseClassDeclaration;
|
|
40
|
+
/**
|
|
41
|
+
* 从 javap 输出解析方法
|
|
42
|
+
*/
|
|
43
|
+
private parseMethodFromJavap;
|
|
44
|
+
/**
|
|
45
|
+
* 智能分割参数,处理泛型和嵌套类型
|
|
46
|
+
*/
|
|
47
|
+
private splitParameters;
|
|
48
|
+
/**
|
|
49
|
+
* 获取javap命令路径
|
|
50
|
+
*/
|
|
51
|
+
private getJavapCommand;
|
|
52
|
+
/**
|
|
53
|
+
* 获取类的继承层次结构
|
|
54
|
+
*/
|
|
55
|
+
getInheritanceHierarchy(className: string, projectPath: string): Promise<string[]>;
|
|
56
|
+
/**
|
|
57
|
+
* 查找类的所有子类
|
|
58
|
+
*/
|
|
59
|
+
findSubClasses(className: string, projectPath: string): Promise<string[]>;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=JavaClassAnalyzer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JavaClassAnalyzer.d.ts","sourceRoot":"","sources":["../../src/analyzer/JavaClassAnalyzer.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,UAAU;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,EAAE,CAAC;CACvB;AAED,MAAM,WAAW,WAAW;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,SAAS,EAAE,MAAM,EAAE,CAAC;CACvB;AAED,MAAM,WAAW,aAAa;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,MAAM,EAAE,UAAU,EAAE,CAAC;IACrB,OAAO,EAAE,WAAW,EAAE,CAAC;CAC1B;AAED,qBAAa,iBAAiB;IAC1B,OAAO,CAAC,OAAO,CAAoB;;IAMnC;;OAEG;IACG,YAAY,CAAC,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IAkBlF;;OAEG;YACW,qBAAqB;IAmBnC;;MAEE;IACF,OAAO,CAAC,gBAAgB;IAyGxB;;OAEG;IACH,OAAO,CAAC,qBAAqB;IAiC7B;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAqE5B;;OAEG;IACH,OAAO,CAAC,eAAe;IA6BvB;;OAEG;IACH,OAAO,CAAC,eAAe;IAQvB;;OAEG;IACG,uBAAuB,CAAC,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAiBxF;;OAEG;IACG,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAiBlF"}
|