使用python-a2a包调用本地A2A计算器Agent时通信失败问题求助
使用python-a2a包调用本地A2A计算器Agent时通信失败问题求助
大家好,我最近在用python-a2a包开发一个Agent-to-Agent的计算器示例,但不管怎么调整请求URL,都没法和本地运行的Agent服务建立通信,一直返回错误。我不确定是不是包版本不兼容的问题,想请大家帮忙看看哪里出了问题。
报错日志
我先尝试将客户端URL设为http://127.0.0.1:5001/a2a,得到如下报错:
A2ACalcClient initialized for URL: http://127.0.0.1:5001/a2a Sending calculation request: add(a=5, b=3) Unexpected response type from A2A agent: error Full response content: ErrorContent(message='Failed to communicate with agent at http://127.0.0.1:5001/a2a/a2a. Tried multiple endpoint variations.', type=<ContentType.ERROR: 'error'>)
后来又把URL简化为http://127.0.0.1:5001,还是收到类似的通信失败错误:
A2ACalcClient initialized for URL: http://127.0.0.1:5001 Sending calculation request: add(a=5, b=3) Unexpected response type from A2A agent: error Full response content: ErrorContent(message='Failed to communicate with agent at http://127.0.0.1:5001/tasks/send. Tried multiple endpoint variations.', type=<ContentType.ERROR: 'error'>)
服务端代码
我编写的Calculator Agent服务端代码如下:
import math from python_a2a import ( A2AServer, Message, TextContent, FunctionCallContent, FunctionResponseContent, FunctionParameter, MessageRole, run_server, Task ) class CalculatorAgent(A2AServer): def handle_message(self, message): if message.content.type == "text": return Message( content=TextContent( text="I'm a Python A2A calculator agent. You can call my functions:\n" "- calculate: Basic arithmetic (operation, a, b)\n" "- sqrt: Square root (value)" ), role=MessageRole.AGENT, parent_message_id=message.message_id, conversation_id=message.conversation_id ) elif message.content.type == "function_call": function_name = message.content.name params = {p.name: p.value for p in message.content.parameters} try: if function_name == "calculate": operation = params.get("operation", "add") a = float(params.get("a", 0)) b = float(params.get("b", 0)) if operation == "add": result = a + b elif operation == "subtract": result = a - b elif operation == "multiply": result = a * b elif operation == "divide": if b == 0: raise ValueError("Cannot divide by zero") result = a / b else: raise ValueError(f"Unknown operation: {operation}") return Message( content=FunctionResponseContent( name="calculate", response={"result": result} ), role=MessageRole.AGENT, parent_message_id=message.message_id, conversation_id=message.conversation_id ) elif function_name == "sqrt": value = float(params.get("value", 0)) if value < 0: raise ValueError("Cannot calculate square root of negative number") result = math.sqrt(value) return Message( content=FunctionResponseContent( name="sqrt", response={"result": result} ), role=MessageRole.AGENT, parent_message_id=message.message_id, conversation_id=message.conversation_id ) except Exception as e: return Message( content=FunctionResponseContent( name=function_name, response={"error": str(e)} ), role=MessageRole.AGENT, parent_message_id=message.message_id, conversation_id=message.conversation_id ) if __name__ == "__main__": agent = CalculatorAgent() run_server(agent, host="0.0.0.0", port=5001)
客户端代码
对应的客户端调用代码:
import python_a2a from python_a2a import ( A2AClient, Message, FunctionCallContent, FunctionParameter, MessageRole ) class A2ACalcClient: """ A class to encapsulate the communication with a Python A2A agent. """ def __init__(self, a2a_url: str): """ Initializes the A2ACalcClient with the A2A agent's URL. Args: a2a_url: The URL of the Python A2A agent (e.g., "http://127.0.0.1:5001"). """ self.client = A2AClient(a2a_url) print(f"A2ACalcClient initialized for URL: {a2a_url}") def send_calculation_request(self, operation: str, a: int, b: int) -> int | None: """ Sends a function call message to the A2A agent to perform a calculation. Args: operation: The mathematical operation (e.g., "add", "subtract", "multiply", "divide"). a: The first operand. b: The second operand. Returns: The result of the calculation if successful, otherwise None. """ print(f"Sending calculation request: {operation}(a={a}, b={b})") function_call_content = FunctionCallContent( name="calculate", parameters=[ FunctionParameter(name="operation", value=operation), FunctionParameter(name="a", value=a), FunctionParameter(name="b", value=b) ] ) function_call_message = Message( content=function_call_content, role=MessageRole.USER ) try: response = self.client.send_message(function_call_message) if response.content.type == "function_response": result = response.content.response.get("result") if result is not None: print(f"Received result from A2A agent: {result}") return result else: print("Function response received, but 'result' key is missing in the response.") else: print(f"Unexpected response type from A2A agent: {response.content.type}") print(f"Full response content: {response.content}") except Exception as e: print(f"Error sending calculation request: {str(e)}") return None # 调用示例 # client = A2ACalcClient("http://127.0.0.1:5001") # client.send_calculation_request("add", 5, 3)
我已经确认服务端正常启动,端口5001也没有被其他程序占用,但客户端始终无法正确命中服务端的处理接口。有没有用过这个包的朋友能指点下,是我URL路径写错了,还是服务端的实现有问题?
内容来源于stack exchange
相关产品推荐
相关产品推荐

