A2A Agent Card:结构、发现与签名
Agent Card 是 A2A 服务器描述自身的 JSON 文档:它是谁、暴露哪些接口、能做什么、客户端必须如何认证。A2A 服务器必须提供一份。客户端读取它,判断某个智能体是否适合当前任务,以及如何与之通信。代码里还会见到连写的 AgentCard——a2a.proto 中的消息名、各 SDK 里的类型名——指的是同一个对象;本文行文使用 Agent Card。
准备编写或修复自己的卡片?先按 Agent Card 实现指南 选择字段、运行校验程序并排查错误。已经拿到卡片?跟随 Task Runtime 快速开始 ,从读取卡片到收到并断言第一次回复。需要查询单个字段和发现规则时,继续阅读下文。
客户端从哪里获取
规范列出了获取 Agent Card 的三种方式:
- Well-Known URI:
https://{server_domain}/.well-known/agent-card.json - 注册表或目录:经过整理的智能体集合
- 直接配置:预先配置的 Agent Card 地址,或直接配置卡片内容
各种策略及取舍见 A2A 智能体发现 。
最小的卡片
这是规范用来解释规范化(canonicalization)的示例:
{
"name": "Example Agent",
"description": "An example agent that echoes messages.",
"supportedInterfaces": [
{
"url": "https://example.com/a2a/v1",
"protocolBinding": "JSONRPC",
"protocolVersion": "1.0"
}
],
"version": "1.0.0",
"capabilities": {
"streaming": false,
"pushNotifications": false,
"extensions": []
},
"defaultInputModes": ["text/plain"],
"defaultOutputModes": ["text/plain"],
"skills": [
{
"id": "echo",
"name": "Echo",
"description": "Echoes the input.",
"tags": ["echo"]
}
]
}v1.0 中的必填字段是 name、description、version、supportedInterfaces、capabilities、defaultInputModes、defaultOutputModes 和 skills。可选字段包括 provider、documentationUrl、iconUrl、securitySchemes、securityRequirements 和 signatures。
想自己生成并校验一张这样的卡片?Agent Card 实现指南
提供可本地运行的快速开始和 jq 校验程序。
声明接口
supportedInterfaces 按优先顺序列出智能体支持的所有「协议绑定 + URL」组合。第一项是首选接口;多种传输共用同一个端点时,URL 可以重复使用。下面的片段来自规范的示例卡片:
"supportedInterfaces": [
{"url": "https://georoute-agent.example.com/a2a/v1", "protocolBinding": "JSONRPC", "protocolVersion": "1.0"},
{"url": "https://georoute-agent.example.com/a2a/grpc", "protocolBinding": "GRPC", "protocolVersion": "1.0"},
{"url": "https://georoute-agent.example.com/a2a/json", "protocolBinding": "HTTP+JSON", "protocolVersion": "1.0"}
]客户端必须遵守以下规则:
- 解析
supportedInterfaces,选择客户端支持的第一个传输。 - 支持多个选项时,优先选择靠前的条目。
- 使用所选传输对应的 URL。
- 每条请求消息中的
tenant必须与所选条目中的值完全一致;条目没有该字段时则省略。见多租户与多智能体路由 。
根据 A2A 官方规范
,协议协商使用 Major.Minor(例如 1.0),不考虑补丁号。
能力、模式与技能
capabilities 声明可选特性:
streaming:智能体支持流式传输。流式操作要求它为true。pushNotifications:智能体可以发送 webhook 通知。推送通知相关操作要求它为true。extendedAgentCard:已认证的客户端可以调用GetExtendedAgentCard获取更完整的卡片,其中可能包含公开卡片中没有的技能或设置。客户端应当在已认证会话期间,用它替换缓存的公开卡片。extensions:智能体支持的协议扩展。见 A2A 扩展 。
defaultInputModes 和 defaultOutputModes 是智能体在所有技能上接受和返回的媒体类型,每个技能都可以覆盖它们。一个技能由 id、name、description 和 tags 描述,另有可选的 examples、inputModes、outputModes 和 securityRequirements;完整字段与规则见 AgentSkill
页面。下面是规范示例卡片中的一个技能(此处省略了第二条示例提示):
{
"id": "route-optimizer-traffic",
"name": "Traffic-Aware Route Optimizer",
"description": "Calculates the optimal driving route between two or more locations, taking into account real-time traffic conditions, road closures, and user preferences (e.g., avoid tolls, prefer highways).",
"tags": ["maps", "routing", "navigation", "directions", "traffic"],
"examples": [
"Plan a route from '1600 Amphitheatre Parkway, Mountain View, CA' to 'San Francisco International Airport' avoiding tolls."
],
"inputModes": ["application/json", "text/plain"],
"outputModes": ["application/json", "application/vnd.geo+json", "text/html"]
}安全
securitySchemes 是一个映射,键是您自定义的名称,值是安全方案定义;securityRequirements 说明客户端必须满足哪些方案以及哪些 scope。示例卡片使用 OpenID Connect:
"securitySchemes": {
"google": {
"openIdConnectSecurityScheme": {
"openIdConnectUrl": "https://accounts.google.com/.well-known/openid-configuration"
}
}
},
"securityRequirements": [{ "schemes": { "google": { "list": ["openid", "profile", "email"] } } }]五种方案类型分别是 API Key、HTTP 认证、OAuth 2.0、OpenID Connect 和双向 TLS;各类型的字段和 OAuth 2.0 流程见 SecurityScheme 页面。客户端从卡片读取要求,通过带外方式(例如 OAuth 2.0 流程)获取凭据,并在每个请求中通过协议约定的请求头或元数据发送。服务器必须认证每一个收到的请求。见 A2A 企业级特性 。
签名
Agent Card 可以签名,让客户端校验它是否真实、是否被篡改:
- 先用 JSON 规范化方案(RFC 8785)对卡片做规范化,再按 JSON Web Signature(RFC 7515)签名。规范化之前,持有默认值的字段会被移除,除非它们是必填字段或标记了
optional;对上面的最小卡片来说,被移除的是空的extensions数组。signatures字段本身不参与签名。 signatures中的每一项包含 base64url 编码的protected头、base64url 编码的signature,以及可选的未保护header。protected 头必须包含alg和kid,typ应为JOSE,还可以包含密钥集地址jku。- 客户端应当在信任卡片之前至少验证一个签名。可以同时存在多个签名以支持密钥轮换,已过期或已吊销的密钥不得使用。
完整流程见智能体发现与 Agent Card 。
缓存
Agent Card 很少变化,所以服务器应当发送带合适 max-age 的 Cache-Control 和 ETag。客户端应当遵循 HTTP 缓存规则(RFC 9111),缓存过期后使用条件请求,即 If-None-Match 或 If-Modified-Since。
发布前预检
一张通过结构校验的卡片,仍可能带着错误的发布版本或错误的服务端点发布出去。本站提供一份样本卡片和一个离线检查脚本,在发布前钉住这些事实:样本卡片
声明应用版本 2.4.0 和一个 JSONRPC 1.0 接口;check_agent_card_release.py
只读取本地文件断言三件事——不访问网络,不需要账户:
- 卡片的
version等于本次要发布的版本; - 存在
protocolBinding和protocolVersion都与目标客户端匹配的supportedInterfaces条目可选(第 8.3.1 节); - 选中了兼容接口时,该条目的
url必须是期望的服务端点;没有兼容接口时,只报告该 URL 仍处于声明状态。
curl -fsSL -o echo-agent-card.json https://a2acn.com/samples/agent-card/echo-agent-card.json
curl -fsSL -o check_agent_card_release.py https://a2acn.com/samples/agent-card/check_agent_card_release.py
python3 check_agent_card_release.py \
--binding JSONRPC --protocol 1.0 \
--expect-version 2.4.0 \
--expect-url https://agent.example.com/a2a/v1 \
echo-agent-card.json对未修改的样本运行时,脚本打印三行 OK 并以 0 退出。每条断言各占一行,所以发布版本写错和接口不可用一眼就能区分。下面的几组运行用 jq 改造样本后重跑同一条命令;把命令末尾的 echo-agent-card.json 换成各变体文件,「发布预期」一行还需同步修改 --expect-version:
# 只把协议版本改成 0.3:发布版本断言仍通过、URL 仍处于声明状态,
# 兼容接口检查失败,退出码 1
jq '.supportedInterfaces |= map(if .protocolBinding == "JSONRPC"
then .protocolVersion = "0.3" else . end)' echo-agent-card.json > card-protocol-03.json
# 只把应用版本改成 2.4.1:接口检查仍选中 supportedInterfaces[0],
# 发布版本断言失败,退出码 1
jq '.version = "2.4.1"' echo-agent-card.json > card-release-241.json
# 在最前面加一个不兼容的 JSONRPC 0.3 条目:选择器跳过它,
# 选中 supportedInterfaces[1],退出码 0
jq '.supportedInterfaces = [{"url": "https://agent.example.com/a2a/legacy",
"protocolBinding": "JSONRPC", "protocolVersion": "0.3"}] + .supportedInterfaces' \
echo-agent-card.json > card-legacy-first.json
# URL 漂移:兼容条目改到 /a2a/drift,前插的 0.3 条目持有旧 URL——
# 选择器选中条目 1,端点断言在它上面失败,退出码 1
jq '.supportedInterfaces |= map(.url |= sub("/a2a/v1$"; "/a2a/drift"))
| .supportedInterfaces = [{"url": "https://agent.example.com/a2a/v1",
"protocolBinding": "JSONRPC", "protocolVersion": "0.3"}] + .supportedInterfaces' \
echo-agent-card.json > card-url-drift.json| 运行 | 改动 | 结果 | 退出码 |
|---|---|---|---|
| 未修改的样本 | 无 | 三行 OK | 0 |
| 协议版本 | 所有 JSONRPC 条目改为 0.3 | 发布版本 OK,端点仅声明 OK,接口 FAIL | 1 |
| 应用版本 | version 改为 2.4.1 | 发布版本 FAIL,接口选中条目 0 | 1 |
| 发布预期 | 同一张卡改用 --expect-version 2.4.1 | 三行 OK | 0 |
| 不兼容条目在前 | 前插一个 JSONRPC 0.3 条目 | 选择器选中 supportedInterfaces[1] | 0 |
| URL 漂移 | 条目 0 为 0.3+旧 URL,条目 1 为 1.0+/a2a/drift | 接口选中条目 1,端点在它上面 FAIL | 1 |
端点断言始终针对选择器实际选中的条目,因此兼容接口上的 URL 漂移无法躲在仍声明旧 URL 的旧条目后面。没有兼容接口时,端点行只说明该 URL 仍处于声明状态——整个运行仍因接口检查而失败。0.3 变体只是接口选择的负例:它说明寻找 JSONRPC 1.0 的客户端会拒绝这张卡。旧版服务器是否仍解析 v0.3 时期的卡片、是否与 v1.0 客户端互通,需要独立的证据。在 CI 中,先把检查脚本放进您的仓库,再把它作为针对构建产物中卡片的步骤调用;它与实现指南
中的结构校验一起,从结构和发布事实两个层面把关同一张卡:
- name: Agent Card pre-flight
run: |
python3 check_agent_card_release.py \
--binding JSONRPC --protocol 1.0 \
--expect-version "${RELEASE_VERSION}" \
--expect-url "${A2A_ENDPOINT_URL}" \
public/.well-known/agent-card.json这条命令只需要 Python 3 和卡片文件,因此可以在任何 CI 平台、任何提供卡片的仓库中运行。
从 v0.3 升级?
| v0.3 字段 | v1.0 |
|---|---|
url、preferredTransport、additionalInterfaces | 合并为 supportedInterfaces[];主端点是 supportedInterfaces[0].url |
protocolVersion | 移到每个接口中 |
supportsAuthenticatedExtendedCard | capabilities.extendedAgentCard |
security | securityRequirements;每条要求的形状是 {"schemes": {"名称": {"list": [scope]}}} |
agent/getAuthenticatedExtendedCard | 重命名为 GetExtendedAgentCard |
| 签名 | 明确了签名时使用 RFC 8785 规范化 |
完整变更列表见 A2A v1.0 有哪些变化 。这些变化背后的 protobuf 字段编号,包括扩展 Agent Card 标志的去向,见 Agent Card Proto 字段编号 。
相关页面
- AgentSkill 与 SecurityScheme :占据卡片大部分内容的两个对象。
- Task 与 Message :客户端选定接口之后要发送的内容。
- 数据结构 :Agent Card 的全部字段和安全方案对象。
来源:A2A 官方规范:Agent Discovery Objects 与 Agent Discovery: The Agent Card