cc代理
](https://www.npmjs.com/package/@gonzih/cc-agent)
MCP服务器,用于在GitHub存储库中生成Claude Code代理。赋予Claude Code以下能力 分支本身 --克隆一个仓库,并启动一个子代理来自主处理它,在MCP重启时保持状态不变。
建造于 @贡齐赫.
快速入门
claude mcp add cc-agent -- npx @gonzih/cc-agent设置以下选项之一:
CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-... # OAuth token (recommended)
ANTHROPIC_API_KEY=sk-ant-api03-... # API key重新启动克劳德代码。您现在有13个新的MCP工具。
MCP工具
| 工具 | 说明 |
|---|---|
spawn_agent | 克隆一个仓库,可以选择创建一个分支,在任务上运行Claude Code |
get_job_status | 检查特定作业的状态 |
get_job_output | 从作业流输出行(支持尾部偏移) |
list_jobs | 列出所有作业的状态、最近的工具调用和退出信息 |
cancel_job | 终止正在运行的作业 |
send_message | 向正在运行的代理的stdin任务中写入消息 |
cost_summary | 所有工作的总美元成本,按回购细分 |
get_version | 返回正在运行的cc代理版本 |
create_plan | 在一次调用中生成代理作业的依赖关系图 |
create_profile | 保存一个命名的spawn配置以供重复使用 {{variable}} 模板 |
list_profiles | 列出所有已保存的配置文件 |
delete_profile | 删除已命名的配置文件 |
spawn_from_profile | 使用变量插值从保存的配置文件生成作业 |
spawn_agent参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
repo_url | string | yes | 要克隆的Git仓库(HTTPS或SSH) |
task | string | yes | 克劳德代码的任务提示 |
branch | string | no | 克隆后要签出的现有分支 |
create_branch | string | no | 要创建的新分支(例如。 feat/my-feature) |
claude_token | string | no | 每个作业令牌覆盖 |
continue_session | boolean | 否 | 通过 --continue 在workdir中恢复上次Claude会话 |
max_budget_usd | number | no | 美元支出上限(默认值:20) |
session_id | string | no | 来自先前作业的会话ID sessionIdAfter --恢复该会话 |
depends_on | string\[\] | no | 必须为的作业ID done 在此作业开始之前(排队为 pending) |
create_plan参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
goal | string | yes | 对该计划所实现目标的高级描述 |
steps | array | yes | 要执行的步骤的有序列表 |
每一步 steps:
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
id | string | yes | 逻辑步骤ID(用于 depends_on 计划中的参考) |
repo_url | string | yes | 要克隆的Git仓库 |
task | string | yes | 克劳德代码的任务提示 |
create_branch | string | no | 运行前要创建的新分支 |
depends_on | string\[\] | no | 此计划中必须首先完成的步骤ID |
create_profile参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
name | string | yes | 配置文件名称(字母数字、破折号、下划线) |
repo_url | string | yes | 要克隆的Git仓库 |
task_template | string | yes | 任务模板--使用 {{varName}} 替代品 |
default_budget_usd | number | no | 此配置文件中作业的默认美元预算 |
branch | string | no | 克隆后要签出的分支 |
description | string | no | 人类可读的配置文件描述 |
spawn_from_profile参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
profile_name | string | yes | 要使用的已保存配置文件的名称 |
vars | object | no | 要插入任务模板的变量 |
task_override | string | no | 使用此任务而不是配置文件模板 |
branch_override | string | no | 覆盖配置文件的分支 |
budget_override | number | no | 覆盖配置文件的默认预算 |
使用示例
基本代理
spawn_agent({
repo_url: "https://github.com/yourorg/yourrepo",
task: "Add error handling to all API endpoints. Open a PR when done.",
create_branch: "feat/error-handling",
max_budget_usd: 5
})
// → { job_id: "abc-123", status: "started" }
list_jobs()
// → [{ id: "abc-123", status: "running", recentTools: ["Read", "Edit", "Bash", ...] }]
get_job_output({ job_id: "abc-123", offset: 0 })
// → { lines: ["[cc-agent] Cloning...", "Reading src/api.ts...", ...], done: false }
send_message({ job_id: "abc-123", message: "Also update the tests." })
// → { sent: true }
cost_summary()
// → { totalJobs: 1, totalCostUsd: 1.23, byRepo: { "https://github.com/...": 1.23 } }具有依赖关系的多步骤计划
create_plan({
goal: "Refactor auth and update docs",
steps: [
{
id: "refactor",
repo_url: "https://github.com/yourorg/app",
task: "Refactor auth middleware to use JWT. Open a PR.",
create_branch: "feat/jwt-auth"
},
{
id: "docs",
repo_url: "https://github.com/yourorg/app",
task: "Update README to document the new JWT auth flow.",
create_branch: "docs/jwt-auth",
depends_on: ["refactor"]
}
]
})
// → { goal: "...", totalSteps: 2, steps: [{ stepId: "refactor", jobId: "abc-1", status: "cloning" }, { stepId: "docs", jobId: "abc-2", status: "pending" }] }重复任务的配置文件
// Save once:
create_profile({
name: "fix-issue",
repo_url: "https://github.com/yourorg/app",
task_template: "Fix issue #{{issue}}: {{title}}. Open a PR when done.",
default_budget_usd: 5
})
// Use many times:
spawn_from_profile({
profile_name: "fix-issue",
vars: { issue: "42", title: "Login broken on mobile" }
})继续上一节课
// Get session ID from a completed job:
get_job_status({ job_id: "abc-123" })
// → { ..., session_id_after: "ses_xyz" }
// Resume it:
spawn_agent({
repo_url: "https://github.com/yourorg/app",
task: "Continue where you left off — finish the tests.",
session_id: "ses_xyz"
})坚持
cc代理v0.3.0+将所有作业状态存储在 瑞迪斯,它在启动时自动配置——无需配置。
自动配置
启动时,cc代理尝试连接到Redis localhost:6379.如果不可用:
- 码头工人 --跑步
docker run -d --name cc-agent-redis -p 6379:6379 --restart=unless-stopped redis:alpine - redis服务器 --如果
redis-server位于PATH中,将其作为守护进程生成 - 内存回退 --记录警告并继续;作业在重新启动后不会持久化
一旦Redis可用,所有作业状态、输出、配置文件和计划都会在MCP服务器重启后继续存在,并且 在所有Claude Code会话中共享 指向同一Redis实例。
密钥架构
| 键 | 类型 | TTL | 内容 |
|---|---|---|---|
cca:job: | 字符串(JSON) | 7天 | 完整作业记录 |
cca:jobs:index | 列表 | -- | 作业ID,最新优先(最多500个) |
cca:job::output | 列表 | 7天 | 输出行(每行一个条目) |
cca:plan: | 字符串(JSON) | 30天 | 带步骤的计划记录→工作映射 |
cca:profile: | 字符串(JSON) | 永久 | 配置文件配置 |
cca:profiles:index | 设置 | 永久 | 所有配置文件名称 |
磁盘回退
当Redis不可用时,cc代理会回退到原始的基于磁盘的存储:
.cc-agent/jobs.json--作业元数据.cc-agent/jobs/.log--每个作业输出日志~/.cc-agent/profiles.json--配置文件
在Redis可用的情况下,现有的磁盘配置文件在首次启动时会自动迁移到Redis。
作业状态
| 状态 | 含义 |
|---|---|
pending | 等待 depends_on 要完成的作业 |
cloning | 克隆仓库 |
running | Claude代码正在运行 |
done | 已成功完成 |
failed | 退出时出错(检查 error 现场) |
cancelled | 已取消 cancel_job |
工具调用可见性
list_jobs 回报 recentTools --Claude每次作业调用的最后10个工具名称(例如。 ["Read", "Edit", "Bash", "Glob"]). get_job_output 返回完整 tool_calls 阵列。这让我们了解了代理在静默期实际上在做什么。
预算控制
集 max_budget_usd 每个工作岗位都要限制支出。默认值为20美元。当预算用尽时,Claude Code会被SIGTERM杀死(退出代码143)。
spawn_agent({ ..., max_budget_usd: 10 }) // up to $10 for this task
spawn_agent({ ..., max_budget_usd: 2 }) // quick/cheap task代理委托模式
推荐的思维模式: 你是技术主管,特工是你的团队.
- 为任何涉及代码库的任务(多个文件、运行测试、打开PR)生成代理
- 自己做研究、快速编辑和编排
- 始终以终端步骤提示终端代理:
gh pr create → gh pr merge → npm publish(或任何运送工作的东西) - 监视器
list_jobs+get_job_output,如果预算用完,则重新启动
# Standard agent task prompt ending:
gh pr create --title "feat: ..." --body "..." --base main
gh pr merge --squash --auto
npm version patch && npm publish # if it's a libraryMCP配置(claude.json)
{
"cc-agent": {
"command": "npx",
"args": ["@gonzih/cc-agent"],
"env": {
"CLAUDE_CODE_OAUTH_TOKEN": "sk-ant-oat01-..."
}
}
}运作原理
spawn_agent创建作业记录(持久化到磁盘),并立即返回作业ID- 背景:
git clone --depth 1进入临时目录 - 可选择签出现有分支或创建新分支
- 跑
claude --print --output-format stream-json --verbose --dangerously-skip-permissions --max-budget-usd - 将stdout/stderr流式传输到作业的输出日志(内存+磁盘)中
- 工具调用从JSON流中捕获并存储在
tool_calls[] - 退出时:作业标记为已完成/失败,10分钟后清理工作目录
- 作业在1小时后从内存中过期(日志文件保留在磁盘上)
- 待处理的作业在其依赖关系完成时每3秒自动升级一次
环境变量
| 变量 | 描述 |
|---|---|
CLAUDE_CODE_TOKEN | Claude OAuth令牌或Anthropic API密钥 |
CLAUDE_CODE_OAUTH_TOKEN | Claude OAuth令牌(备选) |
ANTHROPIC_API_KEY | 无烟煤API密钥(备选) |
需求
- Node.js 18+
claudeCLI:npm install -g @anthropic-ai/claude-code- Git
