> This is a page from the ElevenLabs documentation. For an index of the documentation, fetch https://elevenlabs.io/docs/llms.txt.

# A2A 服务器

## 概述

每个 ElevenLabs 智能体都可通过 [Agent2Agent（A2A）协议](https://a2a-protocol.org/)访问。这是一项开放标准，可让不同平台构建的智能体协同工作。使用 Google ADK、Microsoft Foundry、LangGraph 或其他任意 A2A 客户端构建的编排智能体，无需自定义集成即可发现你的智能体、向其发送消息并获取回复。

智能体会使用与其他场景相同的配置进行回复：提示词、知识库、工具、workflow 和安全护栏。通过 A2A 进行的对话以文本形式运行，并会像其他对话一样显示在智能体的对话历史中。

> **Note**
>
> A2A 服务器目前处于 Alpha 阶段，按工作区启用。请联系客户团队启用。
> 在此之前，请求会返回错误，提示该工作区未启用 A2A 服务器。

## 端点

每个智能体都有自己的智能体卡片和 JSON-RPC 端点。将 `{agent_id}` 替换为智能体 ID：

| 用途       | URL                                                                              |
| -------- | -------------------------------------------------------------------------------- |
| 智能体卡片    | `https://api.elevenlabs.io/v1/convai/a2a/{agent_id}/.well-known/agent-card.json` |
| JSON-RPC | `https://api.elevenlabs.io/v1/convai/a2a/{agent_id}`                             |

位于隔离[data 驻留](/docs/zh/overview/administration/data-residency)环境中的工作区，应使用所在区域的主机而非 `api.elevenlabs.io`，例如 `api.eu.residency.elevenlabs.io`。

智能体卡片会告知 A2A 客户端智能体名称、JSON-RPC URL、回复以流式传输，以及请求需要 API 密钥。

## 身份验证

在智能体卡片和 JSON-RPC 请求中，都需通过 `xi-api-key` 请求头发送 ElevenLabs API 密钥。在与智能体相同的工作区创建专用密钥，并将其范围设为具备写入权限的 ElevenAgents。

> **Warning**
>
> 持有该密钥的任何人，都可以与该密钥有权访问的工作区中的所有智能体通信。请将其存储在
> A2A 客户端的密钥存储中，不要放入客户端代码。

## 发送消息

`SendMessage` 会发送一条用户消息，并以 `Message` 形式返回智能体回复：

```bash
curl https://api.elevenlabs.io/v1/convai/a2a/$AGENT_ID \
  -H "xi-api-key: $ELEVENLABS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": "1",
    "method": "SendMessage",
    "params": {
      "message": {
        "messageId": "msg-1",
        "role": "ROLE_USER",
        "parts": [{ "text": "What are your opening hours?" }]
      }
    }
  }'
```

```json
{
  "jsonrpc": "2.0",
  "id": "1",
  "result": {
    "message": {
      "messageId": "9f1c…",
      "role": "ROLE_AGENT",
      "contextId": "6b0e…",
      "parts": [
        { "text": "We're open from 9am to 6pm, Monday to Saturday.", "mediaType": "text/plain" }
      ]
    }
  }
}
```

不含 `contextId` 的请求会开启新对话，回复会携带其 `contextId`。要继续该对话，请在下一条消息中发送该 `contextId`。随后，智能体会记住其中此前的所有内容。

## 流式获取回复

`SendStreamingMessage` 接受相同参数，并返回服务器发送事件，每个事件都是一个 JSON-RPC 响应：

1. 处于 `TASK_STATE_WORKING` 状态的 `task`。
2. 智能体生成回复时携带回复文本的 `artifactUpdate` 事件，每条回复对应一个 artifact。
3. 包含最终状态的 `statusUpdate`：`TASK_STATE_COMPLETED`，或包含 `status.message` 中原因的 `TASK_STATE_FAILED`。

按 `artifactId` 跟踪文本：当 `append` 为 `true` 时追加；当 `append` 为 `false` 时替换已存储的文本。结束更新会包含完整回复，其中 `append: false` 和 `lastChunk: true`，因此应使用该最终回复替换累积的文本。

每个任务对应一轮对话：智能体回答后即结束。请使用 `contextId` 而非任务 ID 继续对话。

## 从其他智能体使用智能体

A2A 客户端只需要智能体卡片 URL 和 API 密钥请求头。

### Google ADK

使用 [Google ADK](https://adk.dev/a2a/quickstart-consuming/) 时，将智能体封装为 `RemoteA2aAgent`，并作为子智能体交给编排器。编排器决定何时委派，ADK 会在同一对话中发送后续消息：

```python maxLines=0
# pip install "google-adk[a2a]"
import os

import httpx
from google.adk.agents import Agent
from google.adk.agents.remote_a2a_agent import RemoteA2aAgent

agent_id = os.environ["ELEVENLABS_AGENT_ID"]

support_agent = RemoteA2aAgent(
    name="support_agent",
    description="Acme's support agent. Answers questions about orders, shipping and returns.",
    agent_card=f"https://api.elevenlabs.io/v1/convai/a2a/{agent_id}/.well-known/agent-card.json",
    httpx_client=httpx.AsyncClient(
        headers={"xi-api-key": os.environ["ELEVENLABS_API_KEY"]}, timeout=120
    ),
)

root_agent = Agent(
    model="gemini-2.5-flash",
    name="assistant",
    instruction="You are a personal assistant. Hand any question about an Acme order to support_agent.",
    sub_agents=[support_agent],
)
```

使用 `adk web` 运行，并向助手询问订单相关问题。助手会将对话转交给 ElevenLabs 智能体，该智能体通过 A2A 回答，并会记住之前的对话轮次：

![ADK 网页界面：助手转交给 ElevenLabs 智能体 support\_agent，后者回答问题，并在下一轮记住用户的姓名](/docs/_fern-files/_img/678d0f3a7280b491e1bdffa74aa649f1eba66c6c5a63dcd38b0d6d47e9eafa66.webp)

### A2A Python SDK

使用 [A2A Python SDK](https://github.com/a2aproject/a2a-python) 1.x 时，从智能体的基础 URL 创建客户端。客户端会自动发现智能体卡片：

```python maxLines=0
# uv add "a2a-sdk>=1.3.1,<2"
import asyncio
import os

import httpx
from a2a.client import ClientConfig, create_client
from a2a.helpers import new_text_message
from a2a.types import Role, SendMessageRequest

agent_id = os.environ["ELEVENLABS_AGENT_ID"]


async def main() -> None:
    async with httpx.AsyncClient(
        headers={"xi-api-key": os.environ["ELEVENLABS_API_KEY"]}, timeout=120
    ) as http:
        client = await create_client(
            f"https://api.elevenlabs.io/v1/convai/a2a/{agent_id}",
            client_config=ClientConfig(httpx_client=http),
        )
        message = new_text_message("What are your opening hours?", role=Role.ROLE_USER)
        async for event in client.send_message(SendMessageRequest(message=message)):
            print(event)


asyncio.run(main())
```

## 协议支持

服务器通过同一 JSON-RPC 端点支持 A2A v1.0 和 v0.3。以上示例使用 v1.0；v0.3 客户端使用不同的方法名称和消息格式：

| 契约      | A2A v1.0                                                      | A2A v0.3                                               |
| ------- | ------------------------------------------------------------- | ------------------------------------------------------ |
| 发送／流式方法 | `SendMessage` / `SendStreamingMessage`                        | `message/send` / `message/stream`                      |
| 用户消息    | `role: "ROLE_USER"`                                           | `role: "user"`, `kind: "message"`                      |
| 文本部分    | `{ "text": "Hello" }`                                         | `{ "kind": "text", "text": "Hello" }`                  |
| 回复位置    | `result.message`                                              | `result`，带有 `kind: "message"`                          |
| 流式事件    | `result.task`, `result.artifactUpdate`, `result.statusUpdate` | `result.kind`：`task`、`artifact-update`、`status-update` |
| 最终任务状态  | `TASK_STATE_COMPLETED` / `TASK_STATE_FAILED`                  | `completed` / `failed`，状态更新中带有 `final: true`           |

两个版本都会在回复消息中携带 `contextId`。下一轮在 `params.message.contextId` 中复用它，并省略 `taskId`。

无法通过任务方法检索、取消或恢复任务。`GetTask`、`CancelTask` 和 `SubscribeToTask`（v0.3：`tasks/get`、`tasks/cancel` 和 `tasks/resubscribe`）会返回 `TaskNotFoundError`。v1.0 中的 `ListTasks` 会返回空列表。

格式错误的消息参数会返回代码为 `-32602` 且包含请求 `id` 的 JSON-RPC 错误。例如，`role` 必须是字符串；数组或对象无效。这同样适用于流式请求，它们会在流开始前返回 JSON 错误响应。

对于在该路径查找智能体卡片的客户端，也会在 `/v1/convai/a2a/{agent_id}/.well-known/agent.json` 提供智能体卡片。两个卡片 URL 都要求使用与 JSON-RPC 端点相同的 `xi-api-key` 请求头。

### 限制

> **Warning**
>
> 以下情况会返回错误，而非回复：
>
> * **仅限文本。** 消息只能包含文本部分。文件和结构化数据会被拒绝，并返回 `ContentTypeNotSupportedError`。
> * **语音智能体**仅会在其[安全设置](/docs/zh/eleven-agents/customization/personalization/overrides)允许客户端覆盖纯文本模式时以文本模式运行。否则，请将智能体设为纯文本模式。
> * **零保留模式**智能体无法通过 A2A 访问。

* **不支持客户端工具。** A2A 客户端无法响应[客户端工具](/docs/zh/eleven-agents/customization/tools/client-tools)调用。如果智能体调用此类工具，工具会向智能体返回错误，对话轮次将继续。
* **不支持推送通知。** 请使用流式传输，在生成回复时获取内容。
* **对话 Webhook。** A2A 文本对话不会触发语音对话启动 Webhook，也不会自动运行通话后分析和 Webhook。