子代理任务分发服务器

MCP子代理服务器,允许将任务分发给子代理(如Claude Code、Q或Aider),适用于需要任务委派和双向通信的多代理协作场景。

  • by dvcrn
  • TypeScript
  • 本地部署
  • 15 星标
  • 更新于
在 GitHub 查看 dvcrn/mcp-server-subagent

使用说明

MCP子代理服务器

这是一个模型上下文协议(MCP)服务器,允许将任务分派给子代理(如Claude Code、Q或Aider)

此MCP的目的是允许“计划”代理将任务委托给“执行者”代理

特性

  • 通过MCP工具配置和运行子代理
  • 每个子代理都公开了三个工具:
    • run_subagent_<n>:使用提供的输入运行子代理
    • check_subagent_status:允许代理检查子代理的状态
    • get_subagent_logs:检索子代理运行的日志
    • update_subagent_status:更新状态并添加上次运行的摘要
  • 双向通信:子代理可以在执行过程中使用以下命令向父代理提问 ask_parent,家长可以使用 reply_subagent,子代理可以使用以下命令检查回复 check_message_status
  • 目前支持“q”子代理(Amazon q CLI)和“claude”子代理
  • 用于监控子代理执行的实时流式日志

安装

将其添加到MCP配置文件中(~/.aws/amazonq/mcp.json):

{
  "mcpServers": {
    "subagent": {
      "command": "npx",
      "args": ["-y", "mcp-server-subagent"]
    }
  }
}

或者,如果您在本地安装了它:

{
  "mcpServers": {
    "subagent": {
      "command": "node",
      "args": ["/ABSOLUTE/PATH/TO/mcp-server-subagent/build/index.js"]
    }
  }
}

可用工具

子代理执行工具

  • run_subagent_q:通过Amazon Q CLI运行查询

    • 参数: input (string)-要发送到Amazon Q的查询
    • 返回:可用于检查状态或获取日志的运行ID
  • run_subagent_claude:通过Claude CLI运行查询

    • 参数: input (string)-要发送给Claude的查询
    • 返回:可用于检查状态或获取日志的运行ID
  • check_subagent_status:检查上次运行的状态

    • 参数: runId (string)-要检查的运行的UUID
    • 返回:运行的状态和元数据
  • get_subagent_logs:获取上次运行的日志

    • 参数: runId (string)-用于获取日志的运行UUID
    • 返回:运行的完整日志
  • update_subagent_status:更新状态并添加上次运行的摘要

    • 参数:
      • runId (string)-要更新的运行的UUID
      • status (string)-要设置的新状态(“成功”、“错误”、“正在运行”、“已完成”之一)
      • summary (字符串,可选)-状态更新中包含的摘要或结果消息
    • 返回:运行的更新状态和元数据

双向通信工具

  • ask_parent:允许子代理向父代理提问

    • 参数: runId (字符串), question (字符串)
    • 返回:消息ID和轮询指令
  • reply_subagent:使家长能够回答子代理的问题

    • 参数: runId (字符串), messageId (字符串), answer (字符串)
    • 返回:确认回复
  • check_message_status:检查邮件状态并检索回复

    • 参数: runId (字符串), messageId (字符串)
    • 返回:消息详细信息和答案(如果可用)

双向通信

MCP子代理服务器通过消息传递系统支持父代理和子代理之间的双向通信。这允许子代理在执行过程中提问,并从父代理那里获得指导。

传播工具

  • ask_parent:允许子代理向父代理提问

    • 参数:
      • runId (string)-子代理的当前运行ID
      • question (string)-问题/消息内容
    • 返回:消息ID和轮询答案的说明
  • reply_subagent:使家长能够回答子代理的具体问题

    • 参数:
      • runId (string)-子代理的运行ID
      • messageId (string)-正在回复的消息的ID
      • answer (string)-家长的回复内容
    • 返回:确认回复
  • check_message_status:检查特定邮件的状态并检索回复

    • 参数:
      • runId (string)-运行ID以检查消息状态
      • messageId (string)-用于检查状态的消息ID
    • 返回:消息详细信息和答案(如果可用)(确认消息)

工作流示例

以下是双向通信在实践中的工作原理:

1.分代理人提问

子代理使用 ask_parent 执行过程中的工具:

{
  "tool": "ask_parent",
  "arguments": {
    "runId": "abc-123-def",
    "question": "I found multiple config files. Which one should I modify: config.json or settings.yaml?"
  }
}

答复:

{
  "messageId": "msg-456-789",
  "instructions": "Poll for the answer using the 'check_message_status' tool with your runId and messageId. Since this could take a while for the parent to respond, use 'sleep 60' between calls to avoid spamming."
}

2.家长检查子代理状态

当父级检查子代理状态时,他们将看到:

Status: waiting_parent_reply

Question awaiting reply (Message ID: msg-456-789):
  I found multiple config files. Which one should I modify: config.json or settings.yaml?
  (Asked at: 2025-01-15T10:30:00.000Z)
  To reply, use the 'reply_subagent' tool.

Note: This may take a while for the parent to respond. Use 'sleep 60' between status checks to avoid spamming.

3.家长提供答案

家长使用 reply_subagent 工具:

{
  "tool": "reply_subagent",
  "arguments": {
    "runId": "abc-123-def",
    "messageId": "msg-456-789",
    "answer": "Please modify config.json - it's the main configuration file. The settings.yaml is just for development overrides."
  }
}

4.子代理检索答案

子代理使用以下方式轮询答案 check_message_status:

{
  "tool": "check_message_status",
  "arguments": {
    "runId": "abc-123-def",
    "messageId": "msg-456-789"
  }
}

答案可用时的响应:

{
  "messageId": "msg-456-789",
  "questionContent": "I found multiple config files. Which one should I modify: config.json or settings.yaml?",
  "answerContent": "Please modify config.json - it's the main configuration file. The settings.yaml is just for development overrides.",
  "messageStatus": "acknowledged_by_subagent",
  "hasAnswer": true
}

然后,子代理可以在父代理的指导下继续执行。

最佳实践

  • 轮询频率:使用 sleep 30 在状态检查和消息轮询之间,以避免系统不堪重负
  • 问题清晰度:提出有助于指导任务执行的具体、可操作的问题
  • 及时响应家长应定期监控次级代理的状态,以便及时提供指导
  • 消息确认:The check_message_status 检索到答案时,工具会自动确认消息

添加新的子代理

要添加新的子代理,请修改 SUBAGENTS 对象在 src/index.ts:

const SUBAGENTS = {
  q: {
    name: "q",
    command: "q",
    getArgs: () => ["chat", "--trust-all-tools", "--no-interactive"],
    description: "Run a query through the Amazon Q CLI",
  },
  claude: {
    name: "claude",
    command: "claude",
    getArgs: () => [
      "--print",
      "--allowedTools",
      "Bash(git*) Bash(sleep*) Edit Write mcp__subagent__update_subagent_status",
      "--mcp-config",
      JSON.stringify(mcpConfig),
    ],
    description: "Run a query through the Claude CLI",
  },
  // Add your new sub-agent here
  newagent: {
    name: "newagent",
    command: "your-command",
    getArgs: () => ["--some-flag", "--other-flags"],
    description: "Description of your new agent",
  },
};

日志

所有子代理运行都记录到 logs 每次运行包含两个文件的目录:

  • <run-id>.log:包含实时输出日志
  • <run-id>.prompt.md:包含传递给代理的提示
  • <run-id>.meta.json:包含有关运行的元数据(包括通信消息)