多代理通信2026:Claude Code、Codex 与 Cursor 的 MCP 协作实战

·阅读约15分钟·Evergreen Tools Team
Multi Agent

💡 工具推荐调试 MCP 接口与消息格式时,试试 Evergreen Tools 的 API测试工具JSON验证工具UUID生成工具,全部免费!

2026年8月27日,Hacker News 上出现了有趣的一幕:Concord——让 Claude Code、Codex 和 Cursor 互相通信的 MCP 项目;Open Session——开源云端代理编排器;Agent Mesh——多代理共享内存;Open Agent View——所有编码代理的统一 dashboard。一天之内四个项目指向同一方向:编码代理不再是孤岛,它们开始组队。本文用完整代码实现跨代理消息总线、共享内存与编排协议。

1. 为什么代理需要互相通信

每个编码代理都有独特的强项:Claude Code 擅长长上下文理解与规划,Codex 擅长大规模重构,Cursor 在前端迭代上反应最快。过去你只能选一个,或者人工在它们之间搬运上下文。2026 年的答案是让它们通过 MCP 协议连接起来——MCP 已经成了代理的 USB-C,任何支持 MCP 的工具都能接入同一个消息总线。经济账也算得过来:与其付一个工具干所有事,不如把每块工作路由给真正最擅长的工具,让代理之间自己完成交接。这就是「团队」和「一堆独立贡献者」的区别。

// The problem: Claude Code, Codex, and Cursor all live in
// separate worlds. Concord (Aug 27, 2026) lets them talk.
// Agents register on a shared MCP message bus:

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";

const server = new McpServer({
  name: "agent-bus",
  version: "1.0.0",
});

server.tool(
  "send_message",
  { to: z.string(), body: z.string(), thread: z.string().optional() },
  async ({ to, body, thread }) => {
    await bus.publish({ to, body, thread: thread ?? "general" });
    return { ok: true, delivered: to };
  }
);

server.tool(
  "read_thread",
  { thread: z.string(), since: z.number().optional() },
  async ({ thread, since }) => {
    return { messages: await bus.read(thread, since ?? 0) };
  }
);

// Now any MCP-capable agent (Claude Code, Codex, Cursor)
// can message any other agent through the same bus.
Message Bus

2. 用 MCP 搭消息总线

Concord 的核心思路很简单:把「发消息」「读线程」暴露成 MCP 工具,任何 MCP 客户端都能调用。Claude Code 说「Codex,请重构这个模块」,消息通过总线进入 Codex 的收件箱;Codex 完成后把结果写回线程,Claude Code 读到再继续。关键是消息总线必须是持久的——代理们是异步的,谁也不知道对方什么时候空闲。

// A durable message bus for cross-agent communication.
// Based on the Open Session pattern: cloud agent-orchestrator.
import { Redis } from "ioredis";

const redis = new Redis(process.env.REDIS_URL!);

export const bus = {
  async publish(msg: { to: string; from: string; body: string; thread: string }) {
    const id = crypto.randomUUID();
    await redis.xadd(
      `thread:${msg.thread}`,
      "*",
      "id", id,
      "to", msg.to,
      "from", msg.from,
      "body", msg.body,
      "ts", Date.now()
    );
    await redis.publish(`agent:${msg.to}`, JSON.stringify({ ...msg, id }));
    return id;
  },

  async read(thread: string, since = 0): Promise<any[]> {
    const rows = await redis.xrange(`thread:${thread}`, since, "+");
    return rows.map(([, fields]) => Object.fromEntries(fields));
  },
};

// Agents subscribe to their own channel and act asynchronously:
redis.subscribe("agent:claude-code", (err) => { /* ... */ });
redis.on("message", async (channel, raw) => {
  const msg = JSON.parse(raw);
  await claudeCode.handleMessage(msg);
});

3. 持久化消息总线实现

Redis Streams 是跨代理消息的天然载体:XADD 写入、XRANGE 按序读取、PUBLISH/SUBSCRIBE 做实时通知。每条消息带 thread、from、to 和 id,让多个代理围绕一个主题异步协作。Open Session 展示的云端编排器模式更进一步:把总线、状态和调度全部放到云上,代理可以随时加入和离开。

// Agent Mesh style shared memory: agents coordinate through
// a durable memory layer instead of fragile direct calls.
import { Database } from "@libsql/client";

const db = new Database({ url: process.env.TURSO_URL! });

export async function remember(agent: string, key: string, value: unknown) {
  await db.execute({
    sql: `INSERT INTO shared_memory (agent, key, value, updated_at)
         VALUES (?, ?, ?, ?)
         ON CONFLICT(agent, key) DO UPDATE SET
           value = excluded.value, updated_at = excluded.updated_at`,
    args: [agent, key, JSON.stringify(value), Date.now()],
  });
}

export async function recall(agent: string, key: string) {
  const row = await db.execute({
    sql: "SELECT value FROM shared_memory WHERE agent = ? AND key = ?",
    args: [agent, key],
  });
  return row.rows[0] ? JSON.parse(row.rows[0].value as string) : null;
}

// Workflow: Cursor writes the plan to shared memory, Codex reads it,
// Claude Code reviews the diff — no direct coupling needed.

4. 共享内存:比消息更松的耦合

消息是「推」,共享内存是「拉」。Agent Mesh 的思路是维护一张共享记忆表:Cursor 把计划写进去,Codex 读出来执行,Claude Code 拿到 diff 再审查。代理之间没有直接调用关系,各自按自己的节奏工作,靠记忆层协调。对复杂多步任务,这种松耦合比来回发消息稳定得多。

// Open Agent View: one dashboard for all your coding agents.
// Every agent reports events to a central sink; the dashboard
// aggregates them into a single timeline.
export type AgentEvent =
  | { kind: "step"; agent: string; ts: number; detail: string }
  | { kind: "tool"; agent: string; ts: number; tool: string; args: unknown }
  | { kind: "message"; agent: string; ts: number; to: string; body: string }
  | { kind: "checkpoint"; agent: string; ts: number; state: unknown };

export async function report(event: AgentEvent) {
  await fetch(process.env.OBSERVABILITY_URL!, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify(event),
  });
}

// A shared dashboard query: all messages between agents today
// grouped by thread, with latency between first and last event:
SELECT thread, COUNT(*) AS messages,
       MAX(ts) - MIN(ts) AS span_ms
FROM agent_events
WHERE kind = 'message' AND ts > now() - interval '1 day'
GROUP BY thread
ORDER BY span_ms DESC;
Agent Dashboard

5. 统一可见性:dashboard 聚合

代理一多,最大的痛点是「不知道它们在干什么」。Open Agent View 的做法是把所有代理的事件——步骤、工具调用、消息、checkpoint——汇聚到同一个观察层,用一个 dashboard 呈现完整时间线。可观测性不是锦上添花:当两个代理因为上下文不一致互相覆盖时,唯一的排查手段就是完整的事件日志。一条实用的经验法则:任何值得知道的状态变化都应该是一条事件——哪个代理、什么时间、用了什么工具、产出了什么。当 dashboard 能讲出一个连贯的故事时,你可以信任系统;当它出现缺口时,你恰好找到了协调将要失败的地方。

// Production orchestration pattern: supervisor + worker agents.
// The supervisor (Claude Code) decomposes work and delegates to
// specialists (Codex for refactors, Cursor for frontend).
supervisor:
  agent: claude-code
  strategy: decompose-and-delegate
  workers:
    - name: codex-refactor
      triggers: [refactor, migration]
      capabilities: [large-edit, test-update]
    - name: cursor-frontend
      triggers: [ui, component]
      capabilities: [tailwind, react]
  coordination:
    bus: redis-streams
    memory: shared_memory_table
    handoff: pull-request
    review: human-required-on-main

// Handoff protocol: worker completes -> writes summary to memory ->
// messages supervisor -> supervisor reviews diff -> human approves.
handoff:
  - worker: codex-refactor
    action: open_pr
    notify: [supervisor]
  - supervisor: claude-code
    action: review_pr
    on_pass: message_human_approval
  - human: approve
    action: merge

6. 生产级编排模式

把模式组合起来就是生产架构:supervisor 代理分解任务并委派给 specialist 代理;worker 完成后把摘要写进共享内存、发消息通知 supervisor;supervisor 审查 diff、请求人工批准;合并后事件全部进 dashboard。记住两条原则:总线持久化、协作靠协议而不是靠运气。多代理系统不是「更多代理」,而是「更少的直接耦合、更多的结构化通信」。从小处开始:挑两个能力互补的代理,用总线连起来,等交接协议变得枯燥可靠之后,再加第三个。2026年8月27日那一波工具——Concord、Open Session、Agent Mesh、Open Agent View——都还年轻,但它们指向同一个方向:未来是互相说话的代理,而协议已经在这里了。

📌 常见问题 FAQ

Claude Code、Codex 和 Cursor 真的能互相通信吗?

能。2026年8月 Concord 等项目把「发消息」「读线程」暴露成 MCP 工具,任何支持 MCP 的编码代理都能调用,从而实现跨代理消息传递。

MCP 在这里扮演什么角色?

MCP(Model Context Protocol)是代理的通用接口标准。它让不同厂商的代理通过同一个协议连接工具和服务,就像 USB-C 统一了外设接口一样。

消息总线和共享内存有什么区别?

消息总线是推模式:代理显式发送和接收消息;共享内存是拉模式:代理读写共享状态表。消息适合明确的任务交接,共享内存适合松耦合的并行协作。

多代理系统需要人工监督吗?

需要,尤其在生产环境。推荐模式是 supervisor 分解任务、worker 执行、人类批准合并。可观测性层(统一 dashboard)是排查多代理问题的必要条件。

什么时候该上多代理而不是单代理?

当任务可以清晰分解、不同步骤需要不同强项(规划、重构、前端)、或步骤之间可以异步并行时。简单任务用单代理更便宜、更可靠。