MCP重写
模型上下文协议(MCP)服务器,使Claude Code等AI编码助手能够管理长时间运行的开发过程。没有这个工具,Claude Code就无法运行以下命令 npm run dev 因为它会阻止等待进程完成,并最终超时而看不到任何输出。
问题
在使用Claude Code进行开发时,您遇到了一个根本的限制:
# What happens when Claude Code tries to run a dev server:
$ npm run dev
> my-app@1.0.0 dev
> next dev
▲ Next.js 14.0.0
- Local: http://localhost:3000
[Claude Code is now stuck here, waiting for the process to exit]
[After ~2 minutes, it times out without seeing any output]
[Claude Code never sees compilation errors, success messages, or any logs]克劳德代码不能:
- ❌ 查看长时间运行的流程的任何输出
- ❌ 了解开发服务器是否已成功启动
- ❌ 检查编译错误
- ❌ 更改后重新启动服务器
- ❌ 同时运行多个开发进程
这使得使用Claude Code进行有效开发几乎是不可能的,因为您需要手动运行所有开发服务器,并在更改后重新启动它们。
解决方案
MCP Rewatch充当Claude Code和您的开发流程之间的桥梁:
- 在后台运行进程 -Claude Code不会阻止
- 捕获所有输出 -stdout/stderr保存在内存缓冲区中
- 提供异步访问 -Claude Code可以随时检查日志
- 允许重新启动 -Claude Code可以在更改后重新启动服务器
- 管理多个流程 -同时运行前端、后端和数据库服务器
运作原理
MCP Rewatch充当Claude Code和您的开发流程之间的中介:
- 作为单独的服务运行 Claude Code可以通过MCP进行通信
- 独立管理流程 -将开发服务器作为子进程启动
- 非阻塞操作 -Claude Code可以启动/重新启动进程并立即继续
- 异步日志检索 -Claude Code可以稍后检查日志而不会阻塞
- 正确处理生命周期 -优雅关机,无孤立进程
这种架构允许Claude Code有效地管理长时间运行的流程,尽管其固有的局限性是不能直接运行它们。
安装
通过npm全局安装:
npm install -g mcp-rewatch或者直接与npx一起使用(无需安装):
npx mcp-rewatch配置
创建一个 rewatch.config.json 项目根目录中的文件(您将在其中运行Claude Code):
这 startupDelay 应根据您的具体流程进行调整:
- 快速工具(脚本、小型服务器):1000-2000ms
- Next.js/RReact开发服务器:3000-5000ms
- 重型建造过程:5000-10000ms
- 具有依赖关系的服务:8000-15000ms
{
"processes": {
"convex": {
"command": "pnpm",
"args": ["dlx", "convex", "dev"],
"cwd": "./",
"startupDelay": 5000
},
"nextjs": {
"command": "pnpm",
"args": ["dev"],
"cwd": "./",
"env": {
"PORT": "3000"
},
"startupDelay": 4000
},
"backend": {
"command": "npm",
"args": ["run", "dev"],
"cwd": "./backend",
"env": {
"NODE_ENV": "development",
"PORT": "8080"
},
"startupDelay": 2000
}
}
}配置选项
- 命令:要运行的可执行文件(例如。,
npm,pnpm,node) - 参数:命令参数数组
- 当前工作目录:进程的工作目录(相对于MCP服务器运行的位置,即您的项目根目录)
- 环境:其他环境变量(可选)
- 启动延迟:启动后检查状态前等待的时间(毫秒)(默认值:3000)
- ready图案:(尚未实施-见路线图)
使用Claude代码
快速启动(单个项目)
- 将MCP Rewatch添加到Claude代码中:
# Using npx (no installation needed)
claude mcp add rewatch npx -- mcp-rewatch
# Or if installed globally
claude mcp add rewatch mcp-rewatch
# Or for local development
claude mcp add rewatch node -- /path/to/mcp-rewatch/dist/index.js- 创建
rewatch.config.json在项目根目录中
- 从项目目录启动Claude Code-MCP Rewatch将在当前工作目录中查找配置
用户范围设置(全局访问)
要使MCP Rewatch在所有Claude Code会话中可用,请执行以下操作:
claude mcp add -s user rewatch npx -- mcp-rewatch重要:服务器正在查找 rewatch.config.json 在运行Claude Code的当前工作目录中。每个项目都需要自己的配置文件。
管理多个项目
运作原理:MCP Rewatch寻找 rewatch.config.json 在运行Claude Code的当前工作目录中。
最佳实践:
- 保持配置项目特定:每个项目都应该有自己的
rewatch.config.json - 使用相对路径:在配置中,使用relative
cwd路径像"./backend"或"./frontend" - 从项目根启动Claude Code:始终从项目目录中启动Claude Code
多服务配置示例:
{
"processes": {
"frontend": {
"command": "npm",
"args": ["run", "dev"],
"cwd": "./frontend"
},
"backend": {
"command": "npm",
"args": ["run", "dev"],
"cwd": "./backend"
},
"database": {
"command": "docker",
"args": ["compose", "up", "postgres"],
"cwd": "./"
}
}
}可用工具
配置后,Claude Code可以使用这些工具:
restart_process
按名称停止并重新启动开发过程。等待配置 startupDelay (默认情况下为3秒),然后返回初始日志。
await restart_process({ name: "nextjs" })
// Output:
// Process 'nextjs' started successfully
//
// Initial logs:
// [2024-01-07T10:00:01.123Z] [stdout] > my-app@1.0.0 dev
// [2024-01-07T10:00:01.456Z] [stdout] > next dev
// [2024-01-07T10:00:02.789Z] [stdout] ▲ Next.js 14.0.0
// [2024-01-07T10:00:03.012Z] [stdout] - Local: http://localhost:3000get_process_logs
从进程中检索日志,可以选择限制行数。
await get_process_logs({ name: "nextjs", lines: 50 })
// Returns last 50 lines of logs from the Next.js process
await get_process_logs({ name: "convex" })
// Returns all available logs from the Convex processlist_processes
列出所有已配置的进程及其当前状态。
await list_processes()
// Output:
// nextjs: running (PID: 12345)
// convex: stoppedstop_all
优雅地停止所有正在运行的进程。
await stop_all()
// Output: "All processes stopped"典型工作流程
以下是Claude Code在开发过程中如何使用MCP Rewatch:
- 初始设置 (由您完成一次):
- 创建 rewatch.config.json 在您的项目中 - 启动Claude Code-服务器可以按需启动
- 在开发过程中 克劳德代码将:
- 对文件进行代码更改 - 呼叫 restart_process({ name: "nextjs" }) 重新启动服务器 - 自动接收初始日志 启动延迟3秒后 - 检查日志中的成功指标或错误 - 根据结果继续进行更多更改 - 呼叫 get_process_logs({ name: "nextjs" }) 如果需要,稍后
- 主要优势:
- Claude Code永远不会被长时间运行的进程阻塞 - 您不需要在每次更改后手动重新启动服务器 - Claude Code可以通过检查日志来验证所做的更改 - 可以并行管理多台服务器
运作原理
当 restart_process 被称为:
- 停止 任何具有该名称的现有进程
- 开始 新工艺
- 等待 对于已配置的
startupDelay(默认值:3秒) - 退货 启动状态和初始日志
这为Claude Code提供了关于以下方面的即时反馈:
- 进程已成功启动
- 出现了即时错误(端口冲突、缺少deps)
- 服务器开始编译/构建
对于持续监控,Claude Code可以使用 get_process_logs 稍后检查进度。
为什么这很重要
没有MCP Rewatch,使用Claude Code的开发流程令人沮丧:
- ❌ 克劳德代码尝试
npm run dev→ 阻塞和超时 - ❌ 您进行了更改→ 服务器中断→ 需要手动重启
- ❌ 无法检查更改是否已成功编译
使用MCP Rewatch:
- ✅ 克劳德代码使用
restart_process→ 立即返回 - ✅ 更改后服务器会自动重新启动
- ✅ Claude Code可以检查日志以验证成功
故障排除
- 进程未启动:检查一下
rewatch.config.json存在于您的项目根目录中 - 权限错误:确保配置中的命令具有适当的执行权限
- 找不到工具:验证MCP Rewatch是否出现在Claude Code的MCP菜单中
- 日志未显示:进程可能正在缓冲输出;一些服务器需要特定的标志来禁用缓冲
发展
为MCP Rewatch做出贡献:
git clone https://github.com/brennancheung/mcp-rewatch.git
cd mcp-rewatch
pnpm install
pnpm build对于开发,您可以将Claude Code直接指向构建的输出:
# Build the project
pnpm build
# Add to Claude Code
claude mcp add rewatch-dev node -- /path/to/mcp-rewatch/dist/index.js然后创建一个 rewatch.config.json 无论您从哪个目录进行测试。
