MCP IntelliJ服务器
连接到IntelliJ IDEA以利用其增量编译和测试执行功能的MCP(模型上下文协议)服务器。这提供了与您的开发环境的一致性(没有ECJ/Lombok问题),同时保持了快速的反馈循环。
建筑
Claude Code ──MCP(stdio)──► Node.js MCP Server ──HTTP──► IntelliJ Plugin
│
┌─────────┴─────────┐
│ IntelliJ APIs │
│ CompilerManager │
│ RunManager │
│ ActionManager │
└───────────────────┘组件
1.IntelliJ插件(intellij-plugin/)
一个Kotlin插件,为以下对象公开HTTP端点:
- 编译:利用IntelliJ的编译器管理器进行增量构建
- 测试执行:使用IntelliJ的JUnit运行器和Spring上下文缓存
- 诊断:报告当前错误和警告
2.MCP服务器(mcp-server/)
Claude Code可以用来与IntelliJ交互的TypeScript MCP服务器。
安装
构建IntelliJ插件
cd intellij-plugin
./gradlew buildPlugin插件将在 intellij-plugin/build/distributions/mcp-bridge-*.zip.
安装插件
- 打开IntelliJ IDEA
- 首选 设置→ 插件→ ⚙️ → 从磁盘安装插件
- 从以下位置选择ZIP文件
build/distributions/ - 重新启动IntelliJ IDEA
配置插件
- 首选 设置→ 构建、执行、部署→ MCP电桥
- 配置HTTP端口(默认值:10082)
- 启用/禁用IDE启动时的自动启动
构建MCP服务器
cd mcp-server
npm install
npm run build配置Claude代码
添加到您的 .mcp.json (或全局MCP设置):
{
"mcpServers": {
"intellij": {
"command": "node",
"args": ["/path/to/mcp-intellij-server/mcp-server/dist/index.js"],
"env": {
"INTELLIJ_PORT": "10082",
"INTELLIJ_HOST": "localhost"
}
}
}
}用法
可用工具
intellij_status
检查IntelliJ IDEA的连接状态。
Use this first to verify the MCP Bridge plugin is running.intellij_compile
IntelliJ IDEA中的触发器编译。
Arguments:
- incremental (boolean, default: true): Only compile changed files例子:
intellij_compile incremental=trueintellij_test
在IntelliJ IDEA中运行测试。
Arguments:
- pattern (string, required): Test pattern to run
- timeout (number, default: 300): Timeout in seconds
Pattern formats:
- "com.example.MyTest" - Run all tests in fully qualified class (RECOMMENDED)
- "com.example.*" - Run all tests in package
- "com.example.MyTest#testMethod" - Run specific test method
- "com.example.MyTest$NestedClass#test method name" - Run nested class test (Kotlin)例子:
intellij_test pattern="com.example.UserServiceTest#testCreateUser"
intellij_test pattern="com.example.MyTest$When doing something#it should work"已知限制:
- 简单的类名(例如。,
MyTest)可能无法正确解析。使用完全限定的名称。 - 使用回溯方法名称的Kotlin测试需要
$NestedClass#method name格式。
intellij_errors
获取当前编译错误和警告。
intellij_compile_status
获取上次编译的结果。
intellij_test_results
获取上次测试运行的详细结果。
intellij_run_start
无需等待完成即可启动运行配置。
Arguments:
- configName (string, required): Name of the run configuration to start
- projectPath (string, optional): Project path or name (if multiple projects open)
Returns a runId that can be used with intellij_run_output and intellij_run_stop.例子:
intellij_run_start configName="Tunnel"intellij_run_output
从正在运行的进程中获取当前输出。
Arguments:
- runId (string, required): The run ID from intellij_run_start
- clear (boolean, default: false): Clear output buffer after reading例子:
intellij_run_output runId="run-1"intellij_run_stop
通过运行ID停止正在运行的进程。
Arguments:
- runId (string, required): The run ID to stopintellij_run_list
列出所有活动和最近的跑步及其状态。
intellij_projects
列出IntelliJ IDEA中所有打开的项目。
intellij_plugin_reinstall
从内置的zip文件重新安装MCP Bridge插件。
Arguments:
- pluginPath (string, optional): Custom path to plugin zip file
Default: ~/zatlas_projects/mcp-intellij-server/intellij-plugin/build/distributions/intellij-plugin-1.0.0.zip
After reinstall, IntelliJ must be restarted for changes to take effect.intellij_plugin_restart
重新启动IntelliJ IDEA以应用插件更改。
intellij_plugin_info
获取有关已安装的MCP网桥插件的信息(版本、状态、路径)。
HTTP API(插件)
IntelliJ插件公开了以下HTTP端点:
| 端点 | 方法 | 描述 |
|---|---|---|
/health | GET | 健康检查 |
/compile | POST | 触发器编译({"incremental": true}) |
/compile/status | GET | 上次编译结果 |
/test | POST | 运行测试({"pattern": "...", "timeout": 300}) |
/test/results | GET | 上次测试结果 |
/diagnostics | GET | 当前错误/警告 |
/run/start | POST | 启动运行配置({"configName": "...", "projectPath": "..."}) |
/run/list | GET | 列出活动跑步记录 |
/run/projects | GET | 列出打开的项目 |
/run/{runId}/output | GET | 获取运行输出(查询: ?clear=true) |
/run/{runId}/stop | POST | 停止正在运行的进程 |
/plugin/reinstall | POST | 重新安装插件({"pluginPath": "..."}) |
/plugin/restart | POST | 重新启动IntelliJ IDE |
/plugin/info | GET | 获取插件信息 |
好处
- 一致性:使用与IDE相同的编译器和测试运行器
- 速度:增量编译仅重建更改的文件
- Spring上下文缓存:测试运行在执行过程中维护Spring上下文
- 没有龙目岛问题:使用IntelliJ的注释处理,而不是ECJ
- 熟悉的输出:错误消息与您在IntelliJ中看到的一致
故障排除
连接失败
- 验证IntelliJ IDEA是否正在运行
- 检查MCP网桥插件是否已安装并启用
- 验证服务器是否已启动(设置→ 工具→ MCP桥)
- 确保端口匹配(默认值:10082)
未找到测试
- 确保测试类已编译
- 对不明确的模式使用完全限定的类名
- 检查IntelliJ中的模块配置
首次试运行缓慢
这是意料之中的。第一次测试运行加载Spring上下文(如果适用)。由于上下文缓存,后续运行将很快(~100ms)。
环境变量
| 变量 | 默认值 | 描述 |
|---|---|---|
INTELLIJ_PORT | 10082 | IntelliJ插件监听的端口 |
INTELLIJ_HOST | localhost | 运行IntelliJ的主机 |
许可证
麻省理工学院
