๐ Java Class Analyzer MCP Server
A Java class analysis service based on the Model Context Protocol (MCP). It can scan Maven project dependencies, decompile Java class files, obtain detailed information such as class method lists, and provide them to LLMs for code analysis.
๐ฏ Applicable Scenarios
AI tools like Cursor can directly generate code to call second-party (internal) and third-party (external) package interfaces. However, since AIs cannot read the source code of dependencies that are not opened in the current project, the generated code often contains errors and may even exhibit hallucinatory coding.
To address this issue, developers usually copy the source code content directly and feed it to the LLM, or place the source code files in the current project and then reference them in the conversation.
Using the local decompilation MCP solution is the most effective approach. It can accurately parse classes and methods in JAR packages, significantly improving the accuracy and usability of code generation.
โจ Features
- ๐ Easy to Use: The MCP service is implemented in TypeScript and packaged with npm, making it easy to distribute and install with minimal environmental dependencies.
- ๐ Dependency Scanning: Automatically scans all dependency JAR packages in a Maven project.
- ๐ฆ Class Indexing: Creates an index mapping the full class names to the JAR package paths.
- ๐ Decompilation: Uses the CFR tool (built-in) to decompile .class files into Java source code in real-time.
- ๐ Class Analysis: Analyzes the structure, methods, fields, inheritance relationships, etc., of Java classes.
- ๐พ Intelligent Caching: Caches decompilation results according to the package name structure and supports cache control.
- ๐ Automatic Indexing: Automatically checks and creates an index before performing analysis.
- โ๏ธ Flexible Configuration: Supports specifying the path to the CFR tool externally.
- ๐ค LLM Integration: Provides Java code analysis capabilities to LLMs through the MCP protocol.
๐ป Usage Examples
Register the MCP service in the IDE
Use MCP in the agent conversation
๐ Documentation
Installation of the MCP Service
Global Installation (Recommended)
npm install -g java-class-analyzer-mcp-server
After installation, you can directly use the java-class-analyzer-mcp command.
Local Installation
npm install java-class-analyzer-mcp-server
Installation from Source Code
git clone https://github.com/handsomestWei/java-class-analyzer-mcp-server.git
cd java-class-analyzer-mcp-server
npm install
npm run build
Configuration of the MCP Service
Method 1: Use the Generated Configuration (Recommended)
Run the following command to generate a configuration template:
java-class-analyzer-mcp config -o mcp-client-config.json
Then add the generated configuration content to your MCP client configuration file.
Method 2: Manual Configuration
Refer to the following configuration examples and add them to your MCP client configuration file.
Configuration after global installation:
{
"mcpServers": {
"java-class-analyzer": {
"command": "java-class-analyzer-mcp",
"args": ["start"],
"env": {
"NODE_ENV": "production",
"MAVEN_REPO": "D:/maven/repository",
"JAVA_HOME": "C:/Program Files/Java/jdk-11"
}
}
}
}
Configuration after local installation:
{
"mcpServers": {
"java-class-analyzer": {
"command": "node",
"args": [
"node_modules/java-class-analyzer-mcp-server/dist/index.js"
],
"env": {
"NODE_ENV": "production",
"MAVEN_REPO": "D:/maven/repository",
"JAVA_HOME": "C:/Program Files/Java/jdk-11"
}
}
}
}
Parameter Explanation
command: The command to run the MCP server, here using node.
args: The parameters passed to Node.js, pointing to the file in the dist folder compiled by npm run build.
env: Environment variable settings.
Environment Variable Explanation
NODE_ENV: Identifier for the running environment.
production: Production environment, reduces log output and enables performance optimization.
development: Development environment, outputs detailed debugging information.
test: Test environment.
MAVEN_REPO: Path to the local Maven repository (optional).
- If set, the program will use the specified repository path to scan JAR packages.
- If not set, the program will use the default
~/.m2/repository path.
JAVA_HOME: Path to the Java installation (optional).
- If set, the program will use
${JAVA_HOME}/bin/java to execute Java commands (for CFR decompilation).
- If not set, the program will use the
java command in the PATH.
CFR_PATH: Path to the CFR decompilation tool (optional, the program will search for it automatically).
Available Tools
1. scan_dependencies
Scans all dependencies in a Maven project and creates an index mapping class names to JAR packages.
Parameters:
projectPath (string): The root directory path of the Maven project.
forceRefresh (boolean, optional): Whether to forcefully refresh the index, default is false.
Example:
{
"name": "scan_dependencies",
"arguments": {
"projectPath": "/path/to/your/maven/project",
"forceRefresh": false
}
}
2. decompile_class
Decompiles a specified Java class file and returns the Java source code.
Parameters:
className (string): The full name of the Java class to be decompiled, e.g., com.example.QueryBizOrderDO.
projectPath (string): The root directory path of the Maven project.
useCache (boolean, optional): Whether to use the cache, default is true. This avoids repeated generation each time.
cfrPath (string, optional): The path to the JAR package of the CFR decompilation tool. It is built-in, and you can specify an additional version.
Example:
{
"name": "decompile_class",
"arguments": {
"className": "com.example.QueryBizOrderDO",
"projectPath": "/path/to/your/maven/project",
"useCache": true,
"cfrPath": "/path/to/cfr-0.152.jar"
}
}
3. analyze_class
Analyzes the structure, methods, fields, etc., of a Java class.
Parameters:
className (string): The full name of the Java class to be analyzed.
projectPath (string): The root directory path of the Maven project.
Example:
{
"name": "analyze_class",
"arguments": {
"className": "com.example.QueryBizOrderDO",
"projectPath": "/path/to/your/maven/project"
}
}
Cache Files
The following cache directories and files will be generated in the current project:
.mcp-class-index.json: Class index cache file.
.mcp-decompile-cache/: Cache directory for decompilation results (structured by package name).
.mcp-class-temp/: Temporary file directory (structured by package name).
๐ Workflow
- Automatic Indexing: When
analyze_class or decompile_class is called for the first time, the index is automatically checked and created.
- Intelligent Caching: Decompilation results are cached according to the package name structure, and cache control is supported.
- Class Analysis: Use
analyze_class or decompile_class to obtain detailed information about the class.
- LLM Analysis: Provide the decompiled source code to the LLM for code analysis.
๐ง Technical Architecture
Core Components
- DependencyScanner: Responsible for scanning Maven dependencies and creating a class index.
- DecompilerService: Responsible for decompiling .class files.
- JavaClassAnalyzer: Responsible for analyzing the structure of Java classes.
- MCP Server: Provides a standardized MCP interface.
Dependency Scanning Process
- Execute
mvn dependency:tree to obtain the dependency tree.
- Parse each JAR package and extract all .class files.
- Create an index mapping the "full class name -> JAR package path".
- Cache the index in the
.mcp-class-index.json file.
Decompilation Process
- Find the corresponding JAR package path based on the class name.
- Check the cache. If it exists and caching is enabled, return directly.
- Extract the .class file from the JAR package to the
.mcp-class-temp directory (structured by package name).
- Use the CFR tool to decompile the .class file.
- Save the decompilation result in the
.mcp-decompile-cache directory (structured by package name).
- Return the Java source code.
๐ ๏ธ Troubleshooting
Common Issues
- Maven Command Failure
- Ensure that Maven is installed and added to the PATH.
- Check if the project has a valid pom.xml file.
- CFR Decompilation Failure
- Ensure that the CFR JAR package has been downloaded (any version number is supported).
- Check if the Java environment is correctly configured.
- You can specify the CFR path through the
cfrPath parameter.
- Class Not Found
- The program will automatically check and create an index.
- Check if the class name is correct.
- Ensure that the project dependencies are correctly resolved.
๐งช Testing Instructions
Build the Project
npm install
npm run build
Use the Testing Tools
The project provides independent testing tools that can directly test the various functions of the MCP service without going through the MCP client.
node test-tools.js
node test-tools.js --tool decompile_class --class com.alibaba.excel.EasyExcelFactory --project /path/to/project
node test-tools.js --tool decompile_class --no-cache
node test-tools.js --tool decompile_class --cfr-path /path/to/cfr.jar
Testing Tool Parameters
-t, --tool <tool name>: Specify the tool to be tested (scan|decompile|analyze|all).
-p, --project <path>: The project path.
-c, --class <class name>: The name of the class to be analyzed.
--no-refresh: Do not forcefully refresh the dependency index.
--no-cache: Do not use the decompilation cache.
--cfr-path <path>: Specify the path to the JAR package of the CFR decompilation tool.
-h, --help: Display help information.
Log Level Control
Control the log output through the NODE_ENV environment variable:
development: Output detailed debugging information.
production: Only output key information.