Java Boilerplate Generator MCP
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@Java Boilerplate Generator MCP Generate getter, setter, equals, hashCode, toString, and all-args constructor for src/main/java/com/example/User.java"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
Java Boilerplate Generator MCP
一个 MCP(Model Context Protocol) 服务器,用于为 Java 实体类自动生成显式的样板方法(getter/setter/equals/hashCode/toString/构造函数),并写回原文件。
为什么需要它
AI 编码助手在为 Java 实体类生成样板代码时,倾向于直接加 Lombok 注解(@Data、@Getter 等)。这会引入对 Lombok 的隐式依赖,且生成的代码不可见、不可审查。本工具由 MCP 替代 AI 直接生成显式的、IDE 标准风格的 Java 方法,让实体类自带完整方法,无需 Lombok。
Related MCP server: Maven Project Generator MCP
工具:generate_boilerplate
解析指定 .java 文件中的实体类,生成缺失的样板方法并写回原文件。
参数
参数 | 类型 | 默认 | 说明 |
|
| 必填 | Java 实体类的绝对路径 |
|
|
| 要只补缺失的方法类型子集,仅接受 |
|
|
| setter 返回风格: |
|
|
| 为 |
|
| 后 5 类 | 要删旧重建的方法类型子集,可选值为全部 7 种: |
冲突:同一访问器类型(getter 或 setter)不能同时出现在
generate与regenerate中——"只补缺失"与"删旧重建"对同一访问器互斥。冲突时工具返回error="conflict"并给出说明,不写回文件,需重新调用二选一。同理,把 5 类放入generate也会触发该错误(它们只能经regenerate请求)。
行为规则
generate(仅 getter/setter):只补缺失——只为缺少访问器的字段生成,保留手写逻辑。regenerate(全部 7 种皆可):删旧重建——删除旧方法后重新生成。equals/hashCode/toString/构造器只能经此参数请求;getter/setter 列入则改为删旧重建。默认(不传参):生成全部 7 种——
generate默认[getter,setter]补缺失,regenerate默认后 5 类删旧重建。static/transient 字段:不参与生成(与 IDE 行为一致)。
风格:IDE 标准风格。equals 用
instanceof模式;对象字段用Objects.equals,基本类型用==;hashCode 用Objects.hash。Lombok:工具不解析 Lombok 语义,不处理与 Lombok 注解的冲突。
关于 include_super_fields 与继承
当 include_super_fields=true 时,子类的 equals/hashCode 会调用 super.equals(o) / super.hashCode()。由于 equals 使用 instanceof 模式(而非 getClass()),父类的 o instanceof Parent 对子类实例为 true,因此 super.equals(o) 能在父子类间正确传递——前提是父类也由本工具生成 equals/hashCode。
// 父类 Parent(由本工具生成)
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Parent)) return false; // Child 也是 Parent,通过
Parent other = (Parent) o;
return Objects.equals(this.id, other.id);
}
// 子类 Child(include_super_fields=true)
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Child)) return false;
Child other = (Child) o;
return super.equals(o) // 委托父类比较父类字段
&& Objects.equals(this.name, other.name); // 子类自己的字段
}注意:
instanceof模式 + 继承有一个固有的对称性副作用(parent.equals(child)可能为 true 而child.equals(parent)为 false),这是 IDE 生成代码的相同行为,实体类场景通常可接受。
安装与接入
前置要求
Python 3.14+
uv(用于管理依赖与运行)
接入 Claude Code
在 Claude Code 的 MCP 配置(~/.claude.json 或项目的 .mcp.json)中添加:
{
"mcpServers": {
"java-boilerplate": {
"command": "uvx",
"args": ["--from", "git+https://github.com/FlyDut/java-boilerplate-mcp", "python", "-m", "java_mcp.server"]
}
}
}接入 Claude Desktop
在 Claude Desktop 的配置文件(claude_desktop_config.json)中添加同样的 mcpServers 条目。
验证
# 运行测试
uv run pytest
# 启动服务器(stdio 模式,应显示 FastMCP 横幅后等待输入)
uv run python -m java_mcp.server使用示例
对 AI 说:
帮我为
src/main/java/com/example/User.java生成 getter、setter、equals、hashCode、toString 和构造函数,不要用 Lombok。
AI 会调用 generate_boilerplate 工具,工具读取文件、生成方法、写回原文件,并返回生成统计。
项目结构
src/java_mcp/
├── server.py # FastMCP 实例 + generate_boilerplate 工具 + 编排逻辑
├── parser.py # tree-sitter-java 解析 → ParsedFile/EntityClass/Field
├── model.py # 数据模型: Field, EntityClass, ParsedFile
├── detector.py # 检测已存在的 getter/setter/equals/hashCode/toString/构造
├── generator.py # 生成各方法的 Java 源码(IDE 标准风格)
└── writer.py # 插入方法 + import,替换旧 @Override 方法,写回文件测试
uv run pytest # 全部测试(含用 javac/java 的端到端验证)
uv run pytest tests/test_e2e.py -v # 端到端:编译运行生成的 Java 验证 equals 链端到端测试会用 javac 编译工具生成的 Java 文件并运行,验证父子类 equals/hashCode 链在运行时自洽正确。
Available Tools
1 toolgenerate_boilerplateA
为 Java 实体类生成显式的样板方法并写回原文件(替代 Lombok)。
解析指定 .java 文件中的实体类,生成 IDE 标准风格的 getter/setter/ equals/hashCode/toString/无参构造/全参构造。
两个参数表达如何生成方法,互为补充:
generate(仅 getter/setter):只补缺失——只为缺少访问器的字段生成, 保留手写逻辑(如带校验的 setter)。regenerate(全部 7 种皆可):删旧重建——删除旧方法后重新生成。这是 请求 equals/hashCode/toString/构造器的唯一途径(它们没有"只补缺失"模式), 也是把 getter/setter 从"补缺失"切换为"重建"的方式(适合改 setter 风格等)。
冲突:同一访问器类型(getter 或 setter)不能同时出现在 generate 与
regenerate 中——"只补缺失"与"删旧重建"对同一访问器是矛盾的。冲突时工具
返回 error="conflict" 并给出说明,不写回文件,需重新调用二选一。
不传任何方法参数 = 生成全部 7 种(generate 默认 getter/setter 补缺失,
regenerate 默认后 5 类删旧重建)。
Inputs: file_path (required), generate, regenerate, setter_style, +1 more. Returns: object
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 1 tool
Only one tool exists, so agents cannot confuse it with another. The internal generate/regenerate modes are clearly separated and described, avoiding ambiguity within the tool itself.
The single tool name follows a clear lowercase snake_case verb_noun pattern. With no other tools, there are no naming inconsistencies to evaluate.
One tool feels slightly thin for a boilerplate generator, but the tool is non-trivial and packs multiple operations into well-organized parameters. It sits at the borderline end of the appropriate range.
The tool covers generated getters/setters, equals/hashCode, toString, and both constructors, which covers the main entities boilerplate. It lacks Lombok-style extras like builder or with methods, leaving minor gaps for agents to work around.
Related MCP Connectors
Build Spring Boot applications, ready for coding agents, including best practices.
Design domain models and generate deterministic multi-stack code, driven by your coding agent.
Ship better Java with your coding agent.
30+ deterministic dev and delivery tools: linting, metrics, diagrams, model advice, AI-text checks.
Related MCP Servers
- -licenseNot gradedqualityNot gradedmaintenanceEnables generation of enterprise-grade software templates with built-in GDPR/Swedish compliance validation, workflow automation for platform migrations, and comprehensive template management through domain-driven design principles.-
- AlicenseAqualityDmaintenanceEnables automatic generation of complete Maven projects (applications, plugins, libraries) with intelligent package detection, customizable file structure, and integrated ZIP export functionality.81MIT
- AlicenseBqualityBmaintenanceEnables reverse engineering of database tables into Spring Boot projects with AI-enhanced naming and code generation, providing interactive visualizations and workflow orchestration.2941 npm36MIT
- AlicenseAqualityDmaintenanceGenerates complete, production-ready REST, GraphQL, and microservice APIs with built-in security, validation, and deployment configurations.5MIT