-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic_usage.py
More file actions
59 lines (45 loc) · 1.84 KB
/
basic_usage.py
File metadata and controls
59 lines (45 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Basic usage example for AdCP Python client.
This example shows how to:
1. Configure an AdCP client
2. Call get_products
3. Handle sync vs async responses
"""
import asyncio
from adcp import ADCPClient, GetProductsRequest
from adcp.types import AgentConfig, Protocol
async def main():
"""Basic usage example."""
# Configure agent
config = AgentConfig(
id="test_agent",
agent_uri="https://test-agent.adcontextprotocol.org",
protocol=Protocol.A2A,
auth_token="your-token-here", # Optional
)
# Use context manager for automatic resource cleanup
async with ADCPClient(
config,
webhook_url_template="https://myapp.com/webhook/{task_type}/{agent_id}/{operation_id}",
on_activity=lambda activity: print(f"[{activity.type}] {activity.task_type}"),
) as client:
# Call get_products
print("Fetching products...")
result = await client.get_products(
GetProductsRequest(brief="Coffee brands targeting millennials", buying_mode="brief")
)
# Handle result
if result.status == "completed":
print(f"✅ Sync completion: Got {len(result.data.get('products', []))} products")
for product in result.data.get("products", []):
print(f" - {product.get('name')}: {product.get('description')}")
elif result.status == "submitted":
print(f"⏳ Async: Webhook will be sent to {result.submitted.webhook_url}")
print(f" Operation ID: {result.submitted.operation_id}")
elif result.status == "needs_input":
print(f"❓ Agent needs clarification: {result.needs_input.message}")
elif result.status == "failed":
print(f"❌ Error: {result.error}")
# Connection automatically closed here
if __name__ == "__main__":
asyncio.run(main())