1- from a2a . client import A2AClient
1+ import logging # Import the logging module
22from typing import Any
3- import httpx
43from uuid import uuid4
5- from a2a .types import (
6- SendMessageRequest ,
7- MessageSendParams ,
8- SendStreamingMessageRequest ,
9- )
4+
5+ import httpx
6+
7+ from a2a .client import A2ACardResolver , A2AClient
8+ from a2a .types import (AgentCard , MessageSendParams , SendMessageRequest ,
9+ SendStreamingMessageRequest )
1010
1111
1212async def main () -> None :
13+ PUBLIC_AGENT_CARD_PATH = "/.well-known/agent.json"
14+ EXTENDED_AGENT_CARD_PATH = "/agent/authenticatedExtendedCard"
15+
16+ # Configure logging to show INFO level messages
17+ logging .basicConfig (level = logging .INFO )
18+ logger = logging .getLogger (__name__ ) # Get a logger instance
19+
20+ base_url = 'http://localhost:9999'
21+
1322 async with httpx .AsyncClient () as httpx_client :
14- client = await A2AClient .get_client_from_agent_card_url (
15- httpx_client , 'http://localhost:9999'
23+ # Initialize A2ACardResolver
24+ resolver = A2ACardResolver (
25+ httpx_client = httpx_client ,
26+ base_url = base_url ,
27+ # agent_card_path uses default, extended_agent_card_path also uses default
28+ )
29+
30+ # Fetch Public Agent Card and Initialize Client
31+ final_agent_card_to_use : AgentCard | None = None
32+
33+ try :
34+ logger .info (f"Attempting to fetch public agent card from: { base_url } { PUBLIC_AGENT_CARD_PATH } " )
35+ _public_card = await resolver .get_agent_card () # Fetches from default public path
36+ logger .info ("Successfully fetched public agent card:" )
37+ logger .info (_public_card .model_dump_json (indent = 2 , exclude_none = True ))
38+ final_agent_card_to_use = _public_card
39+ logger .info ("\n Using PUBLIC agent card for client initialization (default)." )
40+
41+ if _public_card .supportsAuthenticatedExtendedCard :
42+ try :
43+ logger .info (f"\n Public card supports authenticated extended card. Attempting to fetch from: { base_url } { EXTENDED_AGENT_CARD_PATH } " )
44+ auth_headers_dict = {"Authorization" : "Bearer dummy-token-for-extended-card" }
45+ _extended_card = await resolver .get_agent_card (
46+ relative_card_path = EXTENDED_AGENT_CARD_PATH ,
47+ http_kwargs = {"headers" : auth_headers_dict }
48+ )
49+ logger .info ("Successfully fetched authenticated extended agent card:" )
50+ logger .info (_extended_card .model_dump_json (indent = 2 , exclude_none = True ))
51+ final_agent_card_to_use = _extended_card # Update to use the extended card
52+ logger .info ("\n Using AUTHENTICATED EXTENDED agent card for client initialization." )
53+ except Exception as e_extended :
54+ logger .warning (f"Failed to fetch extended agent card: { e_extended } . Will proceed with public card." , exc_info = True )
55+ elif _public_card : # supportsAuthenticatedExtendedCard is False or None
56+ logger .info ("\n Public card does not indicate support for an extended card. Using public card." )
57+
58+ except Exception as e :
59+ logger .error (f"Critical error fetching public agent card: { e } " , exc_info = True )
60+ raise RuntimeError ("Failed to fetch the public agent card. Cannot continue." ) from e
61+
62+
63+ client = A2AClient (
64+ httpx_client = httpx_client , agent_card = final_agent_card_to_use
1665 )
66+ logger .info ("A2AClient initialized." )
67+
1768 send_message_payload : dict [str , Any ] = {
1869 'message' : {
1970 'role' : 'user' ,
@@ -29,14 +80,19 @@ async def main() -> None:
2980
3081 response = await client .send_message (request )
3182 print (response .model_dump (mode = 'json' , exclude_none = True ))
83+ # The HelloWorld agent doesn't support streaming requests. Validate
84+ # this throws an error.
85+ try :
86+ streaming_request = SendStreamingMessageRequest (
87+ params = MessageSendParams (** send_message_payload )
88+ )
3289
33- streaming_request = SendStreamingMessageRequest (
34- params = MessageSendParams (** send_message_payload )
35- )
36-
37- stream_response = client .send_message_streaming (streaming_request )
38- async for chunk in stream_response :
39- print (chunk .model_dump (mode = 'json' , exclude_none = True ))
90+ stream_response = client .send_message_streaming (streaming_request )
91+ print ("Got an unexpected response:" )
92+ async for chunk in stream_response :
93+ print (chunk .model_dump (mode = 'json' , exclude_none = True ))
94+ except Exception :
95+ pass
4096
4197
4298if __name__ == '__main__' :
0 commit comments