@0x663/read_class
v1.1.5
Published
A Model Context Protocol (MCP) service that reads and analyzes Java class files to provide code context for AI models and tools.
Readme
MCP Class Reader Service
A Model Context Protocol (MCP) service that reads and analyzes Java class files to provide code context for AI models and tools.
As an MCP Tool
This service can be used as an MCP tool by calling the /mcp/analyze-class endpoint with Base64-encoded class file content.
Endpoint: POST /mcp/analyze-class
Content-Type: application/json
Request Body:
{
"fileContent": "Base64-encoded content of the .class file"
}Example Usage in MCP Configuration:
{
"tools": [
{
"name": "analyze-java-class",
"description": "Analyzes Java class files and extracts structural information",
"inputSchema": {
"type": "object",
"properties": {
"fileContent": {
"type": "string",
"description": "Base64-encoded content of the .class file"
}
},
"required": ["fileContent"]
}
}
]
}Features
- Parse Java class files and extract detailed class information
- Retrieve class metadata, fields, methods, and inheritance information
- REST API endpoint for class file analysis
- JSON-formatted responses for easy integration with AI systems
Project Structure
/src
/main
/java
/com/example/mcp
McpClassReaderApplication.java # Main application with SparkJava server
ClassFileAnalyzer.java # ASM-based class file parser
ClassInfo.java # Model for class metadata
FieldInfo.java # Model for field information
MethodInfo.java # Model for method information
/test
/pom.xml # Maven dependencies and build configurationDependencies
- ASM 9.5: For Java class file parsing
- SparkJava 2.9.4: Lightweight web framework for REST API
- Gson 2.10.1: JSON serialization/deserialization
- Java 11 or higher
Build and Run
Prerequisites
- Java Development Kit (JDK) 11 or higher
- Apache Maven 3.6+ or compatible build tool
Build the Project
Option 1: Using Maven directly
mvn clean packageOption 2: Using npm build script (for npm package preparation)
npm run buildRun the Service
java -jar target/mcp-class-reader-1.0-SNAPSHOT.jarThe service will start on port 4567 by default.
Run with NPX
You can also run the service directly with npx without manual building:
npx @0x663/read_class示例操作
我们提供了一个示例脚本,展示如何使用npx配置方式:
./example-npx-usage.sh该脚本将:
- 创建一个示例Java类文件
- 编译该类文件
- 展示如何使用Base64编码class文件内容
- 提供在AI Agent IDE中配置MCP工具的JSON示例
API Usage
Analyze Class File
Endpoint: POST /analyze-class
Content-Type: multipart/form-data
Request Parameters:
classFile: The Java .class file to analyze (form-data)
Example Request (curl):
curl -X POST http://localhost:4567/analyze-class \
-F "classFile=@/path/to/YourClass.class"Example Response:
{
"className": "com.example.YourClass",
"superClassName": "java.lang.Object",
"interfaces": [],
"modifiers": ["public"],
"fields": [
{
"name": "exampleField",
"type": "Ljava/lang/String;",
"modifiers": ["private", "final"]
}
],
"methods": [
{
"name": "<init>",
"descriptor": "()V",
"modifiers": ["public"],
"exceptions": []
},
{
"name": "exampleMethod",
"descriptor": "(ILjava/lang/String;)Z",
"modifiers": ["public"],
"exceptions": ["java/io/IOException"]
}
]
}Using as an MCP Tool
To use this service as an MCP tool:
Build and run the service:
mvn clean package java -jar target/mcp-class-reader-1.0-SNAPSHOT.jarConfigure your MCP client to call
http://localhost:4567/mcp/analyze-classwith Base64-encoded class file content.The service will return structured information about the class, which can be used by AI agents for code analysis and understanding.
For detailed instructions on integrating this tool with an AI Agent IDE, please refer to the MCP Tool Usage Guide.
Using with NPX
This tool can also be configured to run directly with npx. To use this configuration:
- Ensure Java 11+ is installed on your system (Maven is no longer required)
- Add the following configuration to your MCP client:
{
"ReadClass": {
"command": "npx",
"args": [
"-y",
"@0x663/read_class@latest"
],
"env": {}
}
}- When called, the tool will automatically start the service and analyze the provided Java class file.
Note: The first time you run this tool, it will download the pre-built JAR file which includes all dependencies. Subsequent runs will be faster as the JAR file is cached.
在AI Agent IDE中使用
要在支持MCP的AI Agent IDE中使用此工具,请按照以下步骤操作:
- 在IDE的MCP工具配置中添加上述JSON配置
- 确保您的项目中有Java class文件
- 右键点击任何.class文件并选择分析选项
- IDE将自动处理文件读取、Base64编码和结果展示
我们提供了一个完整的IDE配置示例文件 example-mcp-ide-config.json,您可以参考其中的配置说明和使用步骤。
License
MIT
