Skip to content

Commit 4aac572

Browse files
committed
add google a2a
1 parent 6178f6f commit 4aac572

21 files changed

+2375
-9
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,7 @@ coverage/
9393
.eslintcache
9494

9595
# Optional REPL history
96-
.node_repl_history
96+
.node_repl_history
97+
98+
# LLM cache
99+
llm_cache.json

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ This is a tutorial project of [Pocket Flow](https://github.com/The-Pocket/Pocket
4444

4545
- [Flask](https://the-pocket.github.io/Tutorial-Codebase-Knowledge/Flask) - Craft web apps with minimal code that scales from prototype to production!
4646

47+
- [Google A2A](https://the-pocket.github.io/Tutorial-Codebase-Knowledge/Google%20A2A) - The universal language that lets AI agents collaborate across borders!
48+
4749
- [LangGraph](https://the-pocket.github.io/Tutorial-Codebase-Knowledge/LangGraph) - Design AI agents as flowcharts where each step remembers what happened before!
4850

4951
- [LevelDB](https://the-pocket.github.io/Tutorial-Codebase-Knowledge/LevelDB) - Store data at warp speed with Google's engine that powers blockchains!

docs/Google A2A/01_agent_card.md

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
layout: default
3+
title: "Agent Card"
4+
parent: "Google A2A"
5+
nav_order: 1
6+
---
7+
8+
# Chapter 1: Agent Card - The AI's Business Card
9+
10+
Welcome to the Google Agent-to-Agent (A2A) Protocol tutorial! Imagine a world full of helpful AI assistants, or "agents." Maybe one agent is great at translating languages, another excels at summarizing long documents, and a third can book appointments. How do these agents, potentially built by different companies using different technologies, find each other and figure out how to work together?
11+
12+
That's where the **Agent Card** comes in. It solves the problem of **discovery** – how one agent or application can learn about another agent's existence, capabilities, and how to communicate with it.
13+
14+
Think of it like this:
15+
16+
* **You want to hire a plumber.** How do you find one? You might look them up online, find their website, or get their business card. This tells you their name, what services they offer (fixing leaks, installing pipes), and how to contact them (phone number, address).
17+
* **An application (or another agent) wants to use an AI agent.** How does it find one? It looks for the agent's **Agent Card**.
18+
19+
## What is an Agent Card?
20+
21+
An **Agent Card** is a small, standardized file, usually named `agent.json`, that acts like a public profile or digital business card for an AI agent. It's typically hosted by the agent itself at a predictable web address.
22+
23+
This card contains essential information:
24+
25+
1. **Who is the agent?** (Name, description, version, who made it)
26+
2. **What can it do?** (List of skills, like "translate_text" or "summarize_document")
27+
3. **How do I talk to it?** (The agent's web address/URL, what kind of inputs it understands - text, files, structured data?)
28+
4. **Does it have special features?** (Like supporting real-time updates via streaming?)
29+
30+
By reading this card, other agents or applications can quickly understand if this agent is the right one for a job and exactly how to start a conversation (or, in technical terms, initiate a [Task](02_task.md)).
31+
32+
## Finding and Reading the Card (Discovery)
33+
34+
Just like many websites have a standard `robots.txt` file to tell search engines what to do, A2A agents typically make their Agent Card available at a standard path: `/.well-known/agent.json`.
35+
36+
So, if an agent lives at `http://my-translator-agent.com`, its Agent Card would likely be found at `http://my-translator-agent.com/.well-known/agent.json`.
37+
38+
Let's see how a client application might fetch this card using Python.
39+
40+
```python
41+
# File: demo/ui/utils/agent_card.py (simplified)
42+
import requests # A library to make web requests
43+
from common.types import AgentCard # A helper to understand the card's structure
44+
45+
def get_agent_card(remote_agent_address: str) -> AgentCard:
46+
"""Gets the agent card from the agent's address."""
47+
agent_card_url = f"{remote_agent_address}/.well-known/agent.json"
48+
print(f"Fetching card from: {agent_card_url}")
49+
# Make a web request to get the file
50+
response = requests.get(agent_card_url)
51+
response.raise_for_status() # Check if the request was successful
52+
# Parse the JSON file content into an AgentCard object
53+
return AgentCard(**response.json())
54+
55+
# Example Usage:
56+
agent_address = "http://example-agent.com" # Assume our agent is here
57+
try:
58+
card = get_agent_card(agent_address)
59+
print(f"Got card for agent: {card.name}")
60+
except requests.exceptions.RequestException as e:
61+
print(f"Could not fetch card: {e}")
62+
```
63+
64+
**Explanation:**
65+
66+
1. We define the `agent_address` where the agent lives.
67+
2. The function builds the full URL to the standard `agent.json` path.
68+
3. It uses the `requests` library to make an HTTP GET request, just like your web browser does when you visit a page.
69+
4. If the request is successful (HTTP status 200 OK), it takes the JSON text returned by the server and parses it into a structured `AgentCard` object that the program can easily use.
70+
71+
### Example `agent.json`
72+
73+
Here's a simplified example of what the `agent.json` file might look like:
74+
75+
```json
76+
// File: /.well-known/agent.json (Example)
77+
{
78+
"name": "Text Summarizer Bot",
79+
"description": "Summarizes long text documents.",
80+
"version": "1.0.0",
81+
"url": "http://example-agent.com/a2a", // Where to send tasks
82+
"capabilities": {
83+
"streaming": false // Doesn't support real-time updates
84+
},
85+
"defaultInputModes": ["text"], // Primarily accepts text
86+
"defaultOutputModes": ["text"], // Primarily outputs text
87+
"skills": [
88+
{
89+
"id": "summarize",
90+
"name": "Summarize Text",
91+
"description": "Provide text, get a short summary."
92+
}
93+
],
94+
"provider": {
95+
"organization": "AI Helpers Inc."
96+
}
97+
}
98+
```
99+
100+
**Explanation:**
101+
102+
* `name`, `description`, `version`, `provider`: Basic identification info.
103+
* `url`: The specific endpoint *within* the agent's server where A2A communication happens (we'll use this later when sending a [Task](02_task.md)).
104+
* `capabilities`: Tells us if it supports advanced features like `streaming`. This one doesn't.
105+
* `defaultInputModes`/`defaultOutputModes`: What kind of data it generally works with (here, just plain `text`).
106+
* `skills`: A list of specific things this agent can do. This one has a "summarize" skill.
107+
108+
## Under the Hood: The Discovery Flow
109+
110+
How does fetching the Agent Card actually work between the client and the agent (server)? It's a simple web request:
111+
112+
```mermaid
113+
sequenceDiagram
114+
participant C as Client App
115+
participant A as Agent Server
116+
C->>A: GET /.well-known/agent.json
117+
Note right of A: Agent looks for its agent.json file
118+
A-->>C: 200 OK (Returns content of agent.json)
119+
Note left of C: Client parses the JSON data
120+
```
121+
122+
**Steps:**
123+
124+
1. **Client Request:** The client application (e.g., our Python script) sends an HTTP GET request to the agent's base URL + `/.well-known/agent.json`.
125+
2. **Server Response:** The agent's server receives the request, finds its `agent.json` file, and sends its content back to the client with a success status (like `200 OK`).
126+
3. **Client Processing:** The client receives the JSON data and processes it to understand the agent's capabilities.
127+
128+
The provided sample code includes helper classes to make this easier:
129+
130+
* **Python:** The `A2ACardResolver` class (`samples/python/common/client/card_resolver.py`) handles fetching and parsing the card.
131+
* **JavaScript:** The `cli.ts` sample (`samples/js/src/cli.ts`) uses the standard `fetch` API to get the card directly.
132+
133+
```typescript
134+
// File: samples/js/src/cli.ts (Relevant Snippet)
135+
async function fetchAndDisplayAgentCard() {
136+
const wellKnownUrl = new URL("/.well-known/agent.json", serverUrl).toString();
137+
console.log(`Attempting to fetch agent card from: ${wellKnownUrl}`);
138+
try {
139+
// Use browser's fetch to get the card
140+
const response = await fetch(wellKnownUrl);
141+
if (response.ok) {
142+
const card: AgentCard = await response.json(); // Parse JSON
143+
agentName = card.name || "Agent";
144+
console.log(`✓ Agent Card Found: ${agentName}`);
145+
// ... display other card info ...
146+
} else {
147+
console.log(`⚠️ Could not fetch agent card (Status: ${response.status})`);
148+
}
149+
} catch (error: any) {
150+
console.log(`⚠️ Error fetching agent card: ${error.message}`);
151+
}
152+
}
153+
```
154+
155+
This JavaScript code does essentially the same thing as the Python example: builds the URL, fetches the content, and parses the JSON if successful.
156+
157+
## Conclusion
158+
159+
The Agent Card is the cornerstone of discovery in the A2A protocol. It's the agent's public announcement, telling the world who it is, what it can do, and how to interact with it. By fetching and reading this simple `agent.json` file, clients can dynamically discover and prepare to communicate with diverse AI agents.
160+
161+
Now that we understand how to *find* an agent and learn its basic properties using the Agent Card, we need to learn how to actually *give it work* to do. This brings us to the concept of a **Task**.
162+
163+
Ready to learn how to ask an agent to perform an action? Let's move on to the next chapter!
164+
165+
**Next:** [Chapter 2: Task](02_task.md)
166+
167+
---
168+
169+
Generated by [AI Codebase Knowledge Builder](https://github.com/The-Pocket/Tutorial-Codebase-Knowledge)

0 commit comments

Comments
 (0)