[docs]classWellKnownAgentCardResolver(AgentCardResolverBase):"""Agent card resolver that loads AgentCard from a well-known URL."""
[docs]def__init__(self,base_url:str,agent_card_path:str|None=None,)->None:"""Initialize the WellKnownAgentCardResolver. Args: base_url (`str`): The base URL to resolve the agent card from. agent_card_path (`str | None`, optional): The path to the agent card relative to the base URL. Defaults to AGENT_CARD_WELL_KNOWN_PATH from a2a.utils. """self._base_url=base_urlself._agent_card_path=agent_card_path
[docs]asyncdefget_agent_card(self)->AgentCard:"""Get the agent card from the well-known URL. Returns: `AgentCard`: The agent card loaded from the URL. """importhttpxfroma2a.clientimportA2ACardResolverfroma2a.utilsimportAGENT_CARD_WELL_KNOWN_PATHtry:parsed_url=urlparse(self._base_url)ifnotparsed_url.schemeornotparsed_url.netloc:logger.error("[%s] Invalid URL format: %s",self.__class__.__name__,self._base_url,)raiseValueError(f"Invalid URL format: {self._base_url}",)base_url=f"{parsed_url.scheme}://{parsed_url.netloc}"relative_card_path=parsed_url.path# Use default path if not specifiedagent_card_path=(self._agent_card_pathifself._agent_card_pathisnotNoneelseAGENT_CARD_WELL_KNOWN_PATH)# Use async context manager to ensure proper cleanupasyncwithhttpx.AsyncClient(timeout=httpx.Timeout(timeout=600),)as_http_client:resolver=A2ACardResolver(httpx_client=_http_client,base_url=base_url,agent_card_path=agent_card_path,)returnawaitresolver.get_agent_card(relative_card_path=relative_card_path,)exceptExceptionase:logger.error("[%s] Failed to resolve agent card from URL %s: %s",self.__class__.__name__,self._base_url,e,)raiseRuntimeError(f"Failed to resolve AgentCard from URL "f"{self._base_url}: {e}",)frome