Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >一文看懂Google的A2A协议以及与MCP的区别

一文看懂Google的A2A协议以及与MCP的区别

作者头像
AgenticAI
发布于 2025-04-18 09:58:58
发布于 2025-04-18 09:58:58
36000
代码可运行
举报
文章被收录于专栏:AgenticAIAgenticAI
运行总次数:0
代码可运行

谷歌最近发布了 Agent-to-Agent(A2A)开放协议,旨在实现智能体之间的互操作性,弥合不透明智能体系统之间的差距。此前,模型上下文协议(MCP)已经成为连接大型语言模型(LLM)与数据、资源和工具的新标准,简化了不同模型和框架之间的“功能调用”,并创建了一个工具服务生态系统,降低了智能体与工具之间连接的复杂性。随着越来越多的平台采用 MCP,这一趋势有望继续发展。如果还不了解MCP协议的同学,可查看我们上一篇文章一文读懂 MCP!大模型如何用它连接世界,打造更智能的 AI Agent?

然而,A2A 解决的是另一个问题。作为一个应用层协议,A2A 使智能体能够以自然的方式协作,允许它们像用户一样进行沟通,而不是仅仅作为工具。我们期望 A2A 能作为 MCP 的补充,推动智能体生态系统的进一步发展。通过 A2A,智能体能够在不共享记忆、思维或工具的情况下为终端用户完成任务。它们通过各自的原生模态交换上下文、状态、指令和数据。

A2A 的关键原则

  • 简单:复用现有标准
  • 企业级准备:认证、安全、隐私、追踪、监控
  • 异步优先:支持(非常)长时间运行的任务和人类参与流程
  • 模态无关:支持文本、音频/视频、表单、iframe 等多种形式
  • 不透明执行:智能体无需共享思维、计划或工具

A2A 概览

参与者(Actor)

A2A 协议涉及三个角色:

  • 用户(User) 最终用户(可以是人类或服务),使用智能体系统完成任务。
  • 客户端(Client) 代表用户向远程智能体发起操作请求的实体(可以是服务、智能体或应用程序)。
  • 远程智能体(Remote Agent / Server) 不透明的“黑箱”智能体,作为 A2A 协议中的服务器端。

传输层(Transport)

A2A 协议使用 HTTP 作为客户端与远程智能体之间的传输方式。 根据客户端与远程智能体的能力,它们可以使用 SSE(Server-Sent Events)支持从服务器接收流式更新。

A2A 使用 JSON-RPC 2.0[1] 作为客户端与远程智能体之间通信的数据交换格式。

异步通信(async)

A2A 客户端与服务器可以使用标准的请求/响应模式,并通过轮询获取更新。 此外,A2A 还支持使用 SSE 进行流式更新(连接期间),以及在断开连接时接收推送通知[2]

身份验证与授权(authentication-and-authorization)

A2A 将智能体建模为企业级应用程序(因为智能体是黑箱的,不共享工具和资源),从而使智能体互操作快速具备企业级能力。

A2A 遵循 OpenAPI 的认证规范[3]。重要的是,A2A 协议中智能体不交换身份信息。 相反,它们在 A2A 协议之外获取身份材料(如令牌),并通过 HTTP 头部传递这些材料,而非放入 A2A 消息体中。

虽然身份信息不在 A2A 协议中传输,但服务器会在 A2A 的响应消息中声明认证要求。最基本的做法是,服务器在其 Agent Card[4] 中发布认证要求。关于如何发现 Agent Card,请参见智能体发现[5]

客户端应使用服务器提供的认证机制之一,来验证身份并获取认证材料。A2A 服务器应对每一个请求执行身份验证,对于无效请求应通过标准 HTTP 响应码(如 401、403)进行拒绝或挑战,并附带相应的认证头信息或响应体(如使用 WWW-Authenticate[6] 头,或在特定路径下提供 OIDC 发现文档)。更多细节见 企业级准备[7]

注意:如果某个智能体在任务执行过程中需要客户端/用户提供额外认证信息(比如使用某个特定工具),智能体应返回 Input-Required 状态,并附带一个认证结构。此时客户端应再次通过 A2A 协议之外的方式获取认证材料。

智能体卡(Agent Card)

支持 A2A 协议的远程智能体必须以 JSON 格式发布一个Agent Card(智能体卡),用于描述该智能体的能力/技能以及其认证机制。 客户端会使用智能体卡中的信息来识别最适合执行特定任务的智能体,并通过 A2A 协议与其进行通信。

发现机制(Discovery)

建议智能体将其智能体卡托管在地址: https://<base url>/.well-known/agent.json

这一做法兼容 DNS 模式:客户端可以通过 DNS 查找服务器 IP,然后发送 HTTP GET 请求获取智能体卡。 同时也考虑到系统可能会维护私有注册表(如“智能体目录”或私有市场等)。 更多关于智能体发现的讨论见本节文档[8]

表示形式(Representation)

以下是智能体卡的推荐表示方式(JSON 结构):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// An AgentCard conveys key information:
// - Overall details (version, name, description, uses)
// - Skills: A set of capabilities the agent can perform
// - Default modalities/content types supported by the agent.
// - Authentication requirements
interface AgentCard {
// Human readable name of the agent.
// (e.g. "Recipe Agent")
  name: string;
// A human-readable description of the agent. Used to assist users and
// other agents in understanding what the agent can do.
// (e.g. "Agent that helps users with recipes and cooking.")
  description: string;
// A URL to the address the agent is hosted at.
  url: string;
// The service provider of the agent
  provider?: {
    organization: string;
    url: string;
  };
// The version of the agent - format is up to the provider. (e.g. "1.0.0")
  version: string;
// A URL to documentation for the agent.
  documentationUrl?: string;
// Optional capabilities supported by the agent.
  capabilities: {
    streaming?: boolean; // true if the agent supports SSE
    pushNotifications?: boolean; // true if the agent can notify updates to client
    stateTransitionHistory?: boolean; //true if the agent exposes status change history for tasks
  };
// Authentication requirements for the agent.
// Intended to match OpenAPI authentication structure.
  authentication: {
    schemes: string[]; // e.g. Basic, Bearer
    credentials?: string; //credentials a client should use for private cards
  };
// The set of interaction modes that the agent
// supports across all skills. This can be overridden per-skill.
  defaultInputModes: string[]; // supported mime types for input
  defaultOutputModes: string[]; // supported mime types for output
// Skills are a unit of capability that an agent can perform.
  skills: {
    id: string; // unique identifier for the agent's skill
    name: string; //human readable name of the skill
    // description of the skill - will be used by the client or a human
    // as a hint to understand what the skill does.
    description: string;
    // Set of tagwords describing classes of capabilities for this specific
    // skill (e.g. "cooking", "customer support", "billing")
    tags: string[];
    // The set of example scenarios that the skill can perform.
    // Will be used by the client as a hint to understand how the skill can be
    // used. (e.g. "I need a recipe for bread")
    examples?: string[]; // example prompts for tasks
    // The set of interaction modes that the skill supports
    // (if different than the default)
    inputModes?: string[]; // supported mime types for input
    outputModes?: string[]; // supported mime types for output
  }[];
}

智能体间通信(Agent-to-Agent Communication)

客户端与远程智能体之间的通信以**任务完成(task completion)**为导向,智能体通过协作来完成终端用户的请求。 任务对象(Task)允许客户端与远程智能体围绕提交的任务进行协作。

任务可以由远程智能体立即完成,也可能是一个长时间运行的任务。对于长任务,客户端可以通过轮询的方式获取最新状态。 如果处于连接状态,智能体也可以通过 SSE 推送更新;断开连接时,则可通过外部通知服务发送更新。


核心对象(Core Objects)

任务(Task)

任务是一个有状态的实体,用于让客户端和远程智能体协同实现特定目标并生成结果。 在任务上下文中,客户端与远程智能体可以互换消息(Messages),而远程智能体会以**工件(Artifacts)**形式生成结果。

任务总是由客户端创建,而其状态始终由远程智能体决定。 如果客户端有需要,多个任务可以归属在同一个会话中(通过可选的 sessionId 实现)。客户端在创建任务时设置该 sessionId

智能体在接收到任务后可以:

  • 立即完成请求
  • 将任务调度到稍后执行
  • 拒绝请求
  • 协商更合适的交互模态
  • 请求客户端提供更多信息
  • 委派任务给其他智能体或系统执行

即使在目标已完成后,客户端仍可以在该任务上下文中请求更多信息或修改内容。 (例如:客户端:“画一只兔子”,智能体:“<图片>”,客户端:“把它变成红色的”)

任务还承担着**结果(Artifacts)消息(Messages)**的传递通道,并维护一个状态记录和可选的消息/状态历史记录。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
interface Task {
  id: string; // unique identifier for the task
  sessionId: string; // client-generated id for the session holding the task.
  status: TaskStatus; // current status of the task
  history?: Message[];
  artifacts?: Artifact[]; // collection of artifacts created by the agent.
  metadata?: Record<string, any>; // extension metadata
}
// TaskState and accompanying message.
interface TaskStatus {
  state: TaskState;
  message?: Message; //additional status updates for client
  timestamp?: string; // ISO datetime value
}
// sent by server during sendSubscribe or subscribe requests
interface TaskStatusUpdateEvent {
  id: string;
  status: TaskStatus;
  final: boolean; //indicates the end of the event stream
  metadata?: Record<string, any>;
}
// sent by server during sendSubscribe or subscribe requests
interface TaskArtifactUpdateEvent {
  id: string;
  artifact: Artifact;
  metadata?: Record<string, any>;
}
// Sent by the client to the agent to create, continue, or restart a task.
interface TaskSendParams {
  id: string;
  sessionId?: string; //server creates a new sessionId for new tasks if not set
  message: Message;
  historyLength?: number; //number of recent messages to be retrieved
// where the server should send notifications when disconnected.
  pushNotification?: PushNotificationConfig;
  metadata?: Record<string, any>; // extension metadata
}
type TaskState =
  | "submitted"
  | "working"
  | "input-required"
  | "completed"
  | "canceled"
  | "failed"
  | "unknown";

工件(Artifact)

智能体会生成工件(Artifacts)作为任务的最终结果。工件是不可变的,可以命名,并且可以包含多个部分。流式响应可以将新部分附加到现有的工件上。

一个任务可以生成多个工件。例如,“创建一个网页”任务可能会生成单独的 HTML 和图片工件。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
interface Artifact {
  name?: string;
  description?: string;
  parts: Part[];
  metadata?: Record<string, any>;
  index: number;
  append?: boolean;
  lastChunk?: boolean;
}

消息(Message)

消息(Message)包含任何非工件(Artifact)的内容。这些内容可以包括智能体的思维、用户上下文、指令、错误、状态或元数据等。

所有来自客户端的内容都是以消息的形式发送的。智能体通过发送消息来传递状态或提供指令(而生成的结果则以工件的形式发送)。

一条消息可以包含多个部分,用以表示不同的内容。例如,用户请求可能包括一段来自用户的文本描述,接着是多个作为上下文的文件。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
interface Message {
  role: "user" | "agent";
  parts: Part[];
  metadata?: Record<string, any>;
}

部分(Part)

部分(Part)是客户端与远程智能体之间交换的完整内容单元,它是消息(Message)或工件(Artifact)的一部分。每个部分都有自己的内容类型和元数据。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
interface TextPart {
type: "text";
  text: string;
}
interface FilePart {
type: "file";
  file: {
    name?: string;
    mimeType?: string;
    // oneof {
    bytes?: string; //base64 encoded content
    uri?: string;
    //}
  };
}
interface DataPart {
type: "data";
  data: Record<string, any>;
}
type Part = (TextPart | FilePart | DataPart) & {
  metadata: Record<string, any>;
};

推送通知(Push Notifications)

A2A 支持一种安全的通知机制,智能体可以通过推送通知服务(PushNotificationService)在未连接会话的情况下通知客户端更新。在企业内部或跨企业的环境中,智能体必须验证通知服务的身份、对其进行认证,并提供一个标识符,将通知与正在执行的任务关联起来。

推送通知服务的目标服务器应被视为一个独立的服务,并且不保证(也不期望)它是直接的客户端。此推送通知服务负责认证和授权智能体,并将验证后的通知代理发送到适当的端点(该端点可以是发布/订阅队列、电子邮件收件箱或其他服务等)。

对于具有隔离的客户端-智能体配对的情境(例如,封闭虚拟私有网络中的本地服务网格等)或没有企业安全需求的隔离环境,客户端可以选择直接打开端口并充当自己的推送通知服务。任何企业级实现通常会有一个集中式服务,用于使用受信任的通知凭证认证远程智能体,并能够处理在线/离线场景。(这应类似于移动推送通知服务)。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
interface PushNotificationConfig {
  url: string;
  token?: string; // token unique to this task/session
  authentication?: {
    schemes: string[];
    credentials?: string;
  };
}
interface TaskPushNotificationConfig {
  id: string; //task id
  pushNotificationConfig: PushNotificationConfig;
}

示例方法和 JSON 响应

智能体卡

此部分提供了关于如何获取和解析智能体卡(Agent Card)的示例方法和相应的 JSON 响应格式。这将帮助客户端理解如何通过智能体卡获得智能体的能力、认证信息等内容。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//agent card
{
"name": "Google Maps Agent",
"description": "Plan routes, remember places, and generate directions",
"url": "https://maps-agent.google.com",
"provider": {
    "organization": "Google",
    "url": "https://google.com"
  },
"version": "1.0.0",
"authentication": {
    "schemes": "OAuth2"
  },
"defaultInputModes": ["text/plain"],
"defaultOutputModes": ["text/plain", "application/html"],
"capabilities": {
    "streaming": true,
    "pushNotifications": false
  },
"skills": [
    {
      "id": "route-planner",
      "name": "Route planning",
      "description": "Helps plan routing between two locations",
      "tags": ["maps", "routing", "navigation"],
      "examples": [
        "plan my route from Sunnyvale to Mountain View",
        "what's the commute time from Sunnyvale to San Francisco at 9AM",
        "create turn by turn directions from Sunnyvale to Mountain View"
      ],
      // can return a video of the route
      "outputModes": ["application/html", "video/mp4"]
    },
    {
      "id": "custom-map",
      "name": "My Map",
      "description": "Manage a custom map with your own saved places",
      "tags": ["custom-map", "saved-places"],
      "examples": [
        "show me my favorite restaurants on the map",
        "create a visual of all places I've visited in the past year"
      ],
      "outputModes": ["application/html"]
    }
  ]
}

发送任务

此方法允许客户端向远程智能体发送内容,以启动新任务、恢复中断的任务或重新开启已完成的任务。任务中断可能是由于智能体需要额外的用户输入或发生了运行时错误。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Request
{
"jsonrpc": "2.0",
"id": 1,
"method":"tasks/send",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "message": {
      "role":"user",
      "parts": [{
        "type":"text",
        "text": "tell me a joke"
      }]
    },
    "metadata": {}
  }
}
//Response
{
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "status": {
      "state": "completed",
    },
    "artifacts": [{
      "name":"joke",
      "parts": [{
          "type":"text",
          "text":"Why did the chicken cross the road? To get to the other side!"
        }]
      }],
    "metadata": {}
  }
}

获取任务

客户端可以使用此方法检索任务生成的工件(Artifacts)。智能体会根据其设定的保留时间窗口来处理之前提交的任务。对于已超出保留时间窗口的任务,或者那些生命周期较短且未由智能体持久化的任务,智能体可能会返回错误代码。

客户端还可以请求任务的最后 N 条历史记录,其中包含按顺序排列的所有客户端和智能体之间的消息。默认情况下,这个历史记录是 0(即不包含历史记录)。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Request
{
"jsonrpc": "2.0",
"id": 1,
"method":"tasks/get",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "historyLength": 10,
    "metadata": {}
  }
}
//Response
{
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "status": {
      "state": "completed"
    },
    "artifacts": [{
      "parts": [{
        "type":"text",
        "text":"Why did the chicken cross the road? To get to the other side!"
      }]
    }],
    "history":[
      {
        "role": "user",
        "parts": [
          {
            "type": "text",
            "text": "tell me a joke"
          }
        ]
      }
    ],
    "metadata": {}
  }
}

取消任务

客户端可以选择取消之前提交的任务,具体方法如下所示。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Request
{
"jsonrpc": "2.0",
"id": 1,
"method":"tasks/cancel",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "metadata": {}
  }
}
//Response
{
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": 1,
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "status": {
      "state": "canceled"
    },
    "metadata": {}
  }
}

设置任务推送通知

客户端可以配置一个推送通知 URL,以接收任务状态变化的更新。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Request
{
"jsonrpc": "2.0",
"id": 1,
"method":"tasks/pushNotification/set",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "pushNotificationConfig": {
      "url": "https://example.com/callback",
      "authentication": {
        "schemes": ["jwt"]
      }
    }
  }
}
//Response
{
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "pushNotificationConfig": {
      "url": "https://example.com/callback",
      "authentication": {
        "schemes": ["jwt"]
      }
    }
  }
}

获取任务推送通知

客户端可以使用此方法检索当前为任务配置的推送通知设置。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Request
{
"jsonrpc": "2.0",
"id": 1,
"method":"tasks/pushNotification/get",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64"
  }
}
//Response
{
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "pushNotificationConfig": {
      "url": "https://example.com/callback",
      "authentication": {
        "schemes": ["jwt"]
      }
    }
  }
}

多轮对话

如果任务需要额外的用户输入,它可能会暂停并在远程智能体上执行。当任务处于 input-required 状态时,客户端需要提供额外的输入,以便任务可以继续在远程智能体上处理。

input-required 状态下的消息必须包含指示客户端需要执行的操作的详细信息。例如,“填写表单”或“登录到 SaaS 服务 foo”。如果这些操作涉及结构化数据,指令应作为一个 Part 发送,结构化数据则作为第二个 Part 发送。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Request - seq 1
{
"jsonrpc": "2.0",
"id": 1,
"method":"tasks/send",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "message": {
      "role":"user",
      "parts": [{
        "type":"text",
        "text": "request a new phone for me"
      }]
    },
    "metadata": {}
  }
}
//Response - seq 2
{
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "status": {
      "state": "input-required",
      "message": {
        "parts": [{
          "type":"text",
          "text":"Select a phone type (iPhone/Android)"
        }]
      }
    },
    "metadata": {}
  }
}
//Request - seq 3
{
"jsonrpc": "2.0",
"id": 2,
"method":"tasks/send",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "message": {
      "role":"user",
      "parts": [{
        "type":"text",
        "text": "Android"
      }]
    },
    "metadata": {}
  }
}
//Response - seq 4
{
"jsonrpc": "2.0",
"id": 2,
"result": {
    "id": 1,
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "status": {
      "state": "completed"
    },
    "artifacts": [{
      "name": "order-confirmation",
      "parts": [{
          "type":"text",
          "text":"I have ordered a new Android device for you. Your request number is R12443"
        }],
      "metadata": {}
    }],
    "metadata": {}
  }
}

流式支持

对于支持通过 HTTP 和 SSE 通信的客户端和远程智能体,客户端可以在创建新任务时使用 tasks/sendSubscribe 方法发送 RPC 请求。远程智能体可以通过一系列任务状态更新事件(TaskStatusUpdateEvents,传递状态变化或指令/请求)和任务工件更新事件(TaskArtifactUpdateEvents,流式传输生成的结果)进行响应。请注意,任务工件更新事件可以将新部分附加到现有的工件中。客户端可以使用 task/get 来检索整个工件,而不依赖于流式传输。

智能体必须在流的末尾设置 final: true 属性,或者在智能体中断并需要额外用户输入时设置该属性。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Request
{
"method":"tasks/sendSubscribe",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "message": {
      "role":"user",
      "parts": [{
        "type":"text",
        "text": "write a long paper describing the attached pictures"
      },{
        "type":"file",
        "file": {
           "mimeType": "image/png",
           "data":"<base64-encoded-content>"
        }
      }]
    },
    "metadata": {}
  }
}

//Response
data: {
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": 1,
    "status": {
      "state": "working",
      "timestamp":"2025-04-02T16:59:25.331844"
    },
    "final": false
  }
}

data: {
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": 1,
    "artifact": [
      "parts": [
        {"type":"text", "text": "<section 1...>"}
      ],
      "index": 0,
      "append": false,
      "lastChunk": false
    ]
  }
}
data: {
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": 1,
    "artifact": [
      "parts": [
        {"type":"text", "text": "<section 2...>"}
      ],
      "index": 0,
      "append": true,
      "lastChunk": false
    ]
  }
}
data: {
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": 1,
    "artifact": [
      "parts": [
        {"type":"text", "text": "<section 3...>"}
      ],
      "index": 0,
      "append": true,
      "lastChunk": true
    ]
  }
}

data: {
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": 1,
    "status": {
      "state": "completed",
      "timestamp":"2025-04-02T16:59:35.331844"
    },
    "final": true
  }
}

重新订阅任务

断开连接的客户端可以重新订阅支持流式传输的远程智能体,以通过 SSE 接收任务更新。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Request
{
"method":"tasks/resubscribe",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "metadata": {}
  }
}
//Response
data: {
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "artifact":[
      "parts": [
        {"type":"text", "text": "<section 2...>"}
      ],
      "index": 0,
      "append": true,
      "lastChunk":false
    ]
  }
}
data: {
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "artifact":[
      "parts": [
        {"type":"text", "text": "<section 3...>"}
      ],
      "index": 0,
      "append": true,
      "lastChunk": true
    ]
  }
}

data: {
"jsonrpc": "2.0",
"id": 1,
"result": {
    "id": 1,
    "status": {
      "state": "completed",
      "timestamp":"2025-04-02T16:59:35.331844"
    },
    "final": true
  }
}

非文本媒体

以下是客户端与智能体之间交互的示例,涉及非文本数据。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Request - seq 1
{
"jsonrpc": "2.0",
"id": 9,
"method":"tasks/send",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "message": {
      "role":"user",
      "parts": [{
        "type":"text",
        "text": "Analyze the attached report and generate high level overview"
      },{
        "type":"file",
        "file": {
           "mimeType": "application/pdf",
           "data":"<base64-encoded-content>"
        }
      }]
    },
    "metadata": {}
  }
}
//Response - seq 2
{
"jsonrpc": "2.0",
"id": 9,
"result": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "status": {
      "state": "working",
      "message": {
        "role": "agent",
        "parts": [{
          "type":"text",
          "text":"analysis in progress, please wait"
        }],
        "metadata": {}
       }
     },
    "metadata": {}
  }
}
//Request - seq 3
{
"jsonrpc": "2.0",
"id": 10,
"method":"tasks/get",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "metadata": {}
  }
}
//Response - seq 4
{
"jsonrpc": "2.0",
"id": 9,
"result": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "status": {
      "state": "completed"
     },
    "artifacts": [{
      "parts": [{
        "type":"text",
        "text":"<generated analysis content>"
       }],
       "metadata": {}
     }],
    "metadata": {}
  }
}

结构化输出

客户端或智能体都可以要求对方提供结构化输出。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Request
{
"jsonrpc": "2.0",
"id": 9,
"method":"tasks/send",
"params": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "message": {
      "role":"user",
      "parts": [{
        "type":"text",
        "text": "Show me a list of my open IT tickets",
        "metadata": {
          "mimeType": "application/json",
          "schema": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "ticketNumber": { "type": "string" },
                "description": { "type": "string" }
              }
            }
          }
        }
      }]
    },
    "metadata": {}
  }
}
//Response
{
"jsonrpc": "2.0",
"id": 9,
"result": {
    "id": "de38c76d-d54c-436c-8b9f-4c2703648d64",
    "sessionId": "c295ea44-7543-4f78-b524-7a38915ad6e4",
    "status": {
      "state": "working",
      "message": {
        "role": "agent",
        "parts": [{
            "type":"text",
            "text":"[{\"ticketNumber\":\"REQ12312\",\"description\":\"request for VPN access\"},{\"ticketNumber\":\"REQ23422\",\"description\":\"Add to DL - team-gcp-onboarding\"}]"
        }],
        "metadata": {}
      }
    },
    "metadata": {}
  }
}

错误处理

以下是服务器在处理客户端请求时遇到错误时向客户端响应的错误消息格式(ErrorMessage)。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
interface ErrorMessage {
  code: number;
  message: string;
  data?: any;
}

以下是服务器在错误场景中可以响应的标准 JSON-RPC 错误代码:

错误代码

消息

描述

-32700

JSON 解析错误

发送的 JSON 无效

-32600

无效请求

请求负载验证错误

-32601

找不到方法

不是有效的方法

-32602

无效的参数

方法参数无效

-32603

内部错误

内部 JSON-RPC 错误

-32000 至 -32099

服务器错误

保留用于特定实现的错误代码

-32001

找不到任务

使用提供的 ID 未找到任务

-32002

任务无法取消

远程智能体无法取消任务

-32003

不支持推送通知

智能体不支持推送通知

-32004

不支持的操作

操作不被支持

-32005

不兼容的内容类型

客户端和智能体之间的内容类型不兼容

参考资料

[1]

JSON-RPC 2.0: https://www.jsonrpc.org/specification

[2]

推送通知: https://google.github.io/A2A/#/topics/push_notifications?id=remote-agent-to-client-updates

[3]

OpenAPI 的认证规范: https://swagger.io/docs/specification/v3_0/authentication/

[4]

Agent Card: https://google.github.io/A2A/#/documentation?id=agent-card

[5]

智能体发现: https://google.github.io/A2A/#/topics/agent_discovery?id=discovering-agent-cards

[6]

WWW-Authenticate: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate

[7]

企业级准备: https://google.github.io/A2A/#/topics/enterprise_ready

[8]

本节文档: https://google.github.io/A2A/#/topics/agent_discovery?id=discovering-agent-cards

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-04-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AgenticAI 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
前端工程师必知的 MCP 秘籍 包含渲染优化与性能提升等长尾知识要点
MCP即渲染优化三要素(Measure - Compute - Paint),是前端性能优化的核心方法论:
小焱
2025/05/28
1260
前端工程师必知的 MCP 秘籍 包含渲染优化与性能提升等长尾知识要点
​给前端开发者的 14 个 JavaScript 代码优化建议
JavaScript 已经成为当下最流行的编程语言之一。根据 W3Tech,全世界几乎 96% 的网站都在使用它。
王小婷
2020/11/10
9810
深入了解加快网站加载时间的 JavaScript 优化技术
在当今快节奏的数字世界中,网站性能在决定任何在线企业的成功方面起着至关重要的作用。
winty
2023/08/23
4510
深入了解加快网站加载时间的 JavaScript 优化技术
ECMAScript性能优化技巧与陷阱
随着Web应用变得越来越复杂,对性能的要求也在不断提高。ECMAScript(即JavaScript的标准形式)作为Web开发的核心语言,其性能优化成为每位前端开发者必须掌握的核心技能之一。本文将深入探讨ECMAScript性能优化的各种技巧,并揭示隐藏在日常编码中的性能陷阱,帮助开发者在追求高性能的道路上少走弯路,提升Web应用的流畅度和用户体验。
井九
2024/10/12
1030
ECMAScript性能优化技巧与陷阱
负责任地编写Javascript(二)
重构工作一开始非常简单,就是到处安装 npm,这其实就是在快速安装生产依赖项,就像一个大学生在做桶支架,而不关心第二天早上的情况一样。
WecTeam
2019/12/16
7100
负责任地编写Javascript(二)
14个 JavaScript 代码优化技巧
本文最初发布于 Medium 网站,经原作者授权由 InfoQ 中文站翻译并分享。
深度学习与Python
2020/07/30
9550
Webpack知识点速记
Webpack是一个模块打包工具,在Webpack里一切文件皆模块。通过loader转换文件,通过plugin注入钩子,最后输出由多个模块组合的文件。Webpack专注构建模块化项目。 Webpack可以看作是模块打包机:它做的事情是,分析你的项目结构,找到JavaScript模块以及其他一些不能被浏览器直接运行的扩展语音(如:Scss,TypeScript等),并将其打包为合适的格式以供浏览器使用。
Clearlove
2019/08/29
9540
Webpack知识点速记
ECMA Script 性能优化技巧与陷阱
ECMAScript(通常被称为JavaScript)是现代网页开发的核心语言。然而,由于其动态特性和多样化的运行环境,性能优化在实际开发中显得尤为重要。本文将深入探讨ECMAScript的性能优化技巧,并揭示一些常见的陷阱,帮助开发者写出高效、快速的代码。
繁依Fanyi
2024/08/20
1320
14个 JavaScript 代码优化技巧
这篇文章列举了一些技巧,可帮助你写出更好的 JavaScript 代码,从而提高性能。
ConardLi
2020/07/29
1K0
14个 JavaScript 代码优化技巧
前端性能优化:提升网站速度与用户体验的终极指南
在今天的数字时代,用户对网站速度和性能的要求越来越高。前端性能优化不仅可以改善用户体验,还可以提高搜索引擎排名,减少用户流失率。本文将为您提供一份终极指南,让您深入了解前端性能优化的关键概念,并提供带有实际代码示例的技术方法,以加速您的网站加载速度。
海拥
2023/09/17
7960
前端页面加载性能指标之LCP
之前已经介绍过 FCP,可以点击查阅前端页面加载性能指标之 FCP。本文介绍与之相对应的 LCP。通过上文得知,FCP 衡量的是页面首次渲染出有意义的内容的时间点,这通常包括文本、图像、非白色画布或 SVG 的渲染,可以让用户感知到网页正在加载。那么 LCP 又是什么?
用户8171976
2024/11/29
4790
Webpack配置与优化:提升前端项目构建效率与性能新探索
大家好,我是腾讯云开发者社区的 Front_Yue,本篇文章将带领大家探索前端开发必不可少的构建工具--Webpack,探讨Webpack的配置与优化,帮助大家提升前端项目的构建效率和性能。
Front_Yue
2024/04/20
7390
Webpack配置与优化:提升前端项目构建效率与性能新探索
前端性能优化
本来想写一篇实用而又全面的性能优化文章,很多大佬已经写了非常好,我就不再造轮子了。我这篇文章就归纳整理一下吧,方便后续我或他人学习使用。
Dawnzhang
2022/05/10
1.4K0
前端性能优化
极速 JavaScript 打包器:esbuild
esbuild是一个快速、可扩展的JavaScript打包器和压缩器,它的目标是成为最快的打包器。它使用Go编写,可以在几乎瞬间内完成大多数项目的构建。在本文中,我们将深入了解esbuild,并探讨其如何实现如此出色的性能。
can4hou6joeng4
2023/11/16
3950
我工作中用到的性能优化全面指南
在构建过程中,为了减少文件的大小和加载时间,通常会对JavaScript代码进行最小化和压缩处理。这包括移除不必要的空格、换行、注释,以及缩短变量和函数名。工具如UglifyJS和Terser等可以帮助我们完成这个任务。
linwu
2023/07/26
3650
Vue 性能革命:揭秘前端优化的终极技巧;Vue优化技巧,解决Vue项目卡顿问题
也就是使用v-for这样通过循环生成的列表,应给每个列表项一个稳定且唯一的key,这样有利于在列表变动时,尽量少的删除、新增、改动元素。
watermelo37
2025/01/22
1040
Vue 性能革命:揭秘前端优化的终极技巧;Vue优化技巧,解决Vue项目卡顿问题
JavaScript 中的前端代码压缩与混淆
在前端开发中,为了提高网站的性能和保护代码的知识产权,代码压缩与混淆是十分重要的环节。本文将深入探讨 JavaScript 中前端代码压缩与混淆的概念、作用、常见方法及工具。
iwhao
2024/08/08
5800
JavaScript 是如何工作的:模块的构建以及对应的打包工具
如果你是 JavaScript 的新手,一些像 “module bundlers vs module loaders”、“Webpack vs Browserify” 和 “AMD vs.CommonJS” 这样的术语,很快让你不堪重负。
前端小智@大迁世界
2019/03/15
1.5K0
JavaScript 是如何工作的:模块的构建以及对应的打包工具
《前端性能优化秘籍:打造极致用户体验》
在当下,网站和应用的性能表现直接关乎用户去留。快速加载、流畅交互的页面能让用户沉浸其中,反之,缓慢的响应速度则会让他们毫不犹豫地离开。对于前端开发者而言,性能优化不仅是技术追求,更是提升用户体验、增强产品竞争力的关键所在。接下来,就让我们深入探寻前端性能优化的宝藏秘籍。
程序员阿伟
2025/04/17
2190
《前端性能优化秘籍:打造极致用户体验》
从Npm Script到Webpack,6种常见的前端构建工具对比
小编说:历史上先后出现了一系列构建工具,它们各有优缺点。由于前端工程师很熟悉JavaScript,Node.js又可以胜任所有构建需求,所以大多数构建工具都是用Node.js开发的。本文就来一一介绍它们。 前端技术发展之快,各种可以提高开发效率的新思想和框架层出不穷。但是它们都有一个共同点:源代码无法直接运行,必须通过转换后才可以正常运行。 构建就是做这件事情,将源代码转换成可执行的JavaScript、CSS、HTML代码,包括如下内容。 代码转换:将TypeScript编译成JavaScript、将SC
企鹅号小编
2018/02/11
2.1K0
从Npm Script到Webpack,6种常见的前端构建工具对比
推荐阅读
相关推荐
前端工程师必知的 MCP 秘籍 包含渲染优化与性能提升等长尾知识要点
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验