Python Agent đến A2A Agent
Trình tạo connection có thể kết nối Python Agent của bạn với một A2A agent từ xa — có thể là TypeScript hoặc Python — để agent của bạn có thể ủy quyền cho một agent khác như một công cụ.
Trình tạo thiết lập tất cả các kết nối cần thiết để agent của bạn có thể khám phá và gọi A2A agent từ xa, cả khi triển khai lên AWS (thông qua Bedrock AgentCore) và khi chạy cục bộ.
Điều kiện tiên quyết
Phần tiêu đề “Điều kiện tiên quyết”Trước khi sử dụng trình tạo này, hãy đảm bảo bạn có:
- Một dự án Python với thành phần Python Agent (Strands hoặc LangChain)
- Một dự án với thành phần Agent được tạo với
--protocol=a2avà--auth=iam(có thể làts#agenthoặcpy#agent) - Cả hai thành phần được tạo với
infra: agentcore
Cách sử dụng
Phần tiêu đề “Cách sử dụng”Chạy Trình tạo
Phần tiêu đề “Chạy Trình tạo”pnpm nx g @aws/nx-plugin:connectionyarn nx g @aws/nx-plugin:connectionnpx nx g @aws/nx-plugin:connectionbunx nx g @aws/nx-plugin:connectionBạn cũng có thể thực hiện chạy thử để xem những tệp nào sẽ bị thay đổi
pnpm nx g @aws/nx-plugin:connection --dry-runyarn nx g @aws/nx-plugin:connection --dry-runnpx nx g @aws/nx-plugin:connection --dry-runbunx nx g @aws/nx-plugin:connection --dry-run- Cài đặt Nx Console VSCode Plugin nếu bạn chưa cài đặt
- Mở Nx Console trong VSCode
- Nhấp
Generate (UI)trong phần "Common Nx Commands" - Tìm kiếm
@aws/nx-plugin - connection - Điền các tham số bắt buộc
- Nhấp
Generate
Chọn dự án agent chủ của bạn làm nguồn và dự án A2A agent của bạn làm đích. Nếu các dự án của bạn chứa nhiều thành phần, hãy chỉ định các tùy chọn sourceComponent và targetComponent để phân biệt.
Tùy chọn
Phần tiêu đề “Tùy chọn”| Tham số | Kiểu | Mặc định | Mô tả |
|---|---|---|---|
| sourceProject Bắt buộc | string | - | Dự án nguồn |
| targetProject Bắt buộc | string | - | Dự án đích để kết nối tới |
| sourceComponent | string | - | Component nguồn để kết nối từ đó (tên component, đường dẫn tương đối so với thư mục gốc của dự án nguồn, hoặc generator id). Sử dụng '.' để chọn rõ ràng dự án làm nguồn. |
| targetComponent | string | - | Component đích để kết nối tới (tên component, đường dẫn tương đối so với thư mục gốc của dự án đích, hoặc generator id). Sử dụng '.' để chọn rõ ràng dự án làm đích. |
| preferInstallDependencies | boolean | true | Có nên cài đặt các dependencies sau khi generator chạy hay không. Đặt thành false để trì hoãn việc cài đặt khi chạy nhiều generator liên tiếp (việc cài đặt vẫn sẽ chạy nếu cần thiết để các generator tiếp theo có thể tính toán Nx project graph); cài đặt một lần vào cuối. |
Kết quả của Trình tạo
Phần tiêu đề “Kết quả của Trình tạo”Trình tạo tạo một dự án Python agent_connection được chia sẻ tại packages/common/agent_connection/ (nếu nó chưa tồn tại). Các mô-đun client theo từng kết nối được tạo vào dự án được chia sẻ này:
Thư mụcpackages/common/agent_connection
Thư mục<scope>_agent_connection
- __init__.py Re-exports per-connection clients
Thư mụccore
- agentcore_endpoints.py Framework-agnostic ARN/URL resolution
- agentcore_a2a_client_config.py Framework-agnostic A2A client config (signed
ClientConfig) - agentcore_a2a_client_<framework>.py A2A client wrapping the config for your agent’s framework
Thư mụcauth/ Framework-agnostic SigV4 / session-forwarding
httpx.Auth- …
Thư mụcapp
- <target_agent_name>_client_<framework>.py Per-connection A2A client for each A2A agent
Hậu tố client khớp với framework của agent của bạn (_strands hoặc _langchain). Cả hai đều bao bọc cùng một ClientConfig đã ký không phụ thuộc vào framework: client Strands bao bọc một A2AAgent của Strands, trong khi client LangChain điều khiển a2a SDK trực tiếp.
Ngoài ra, trình tạo:
- Chuyển đổi
agent.pycủa agent của bạn để đăng ký A2A agent từ xa như một công cụ sử dụng@tool - Thêm dự án
agent_connectionlàm phụ thuộc workspace của dự án agent của bạn - Cập nhật target
devcủa agent để phụ thuộc vào targetdevcủa agent đích
Sử dụng A2A Agent đã Kết nối
Phần tiêu đề “Sử dụng A2A Agent đã Kết nối”Trình tạo chuyển đổi agent.py của agent của bạn để bao bọc A2A agent từ xa như một công cụ. Agent từ xa được đăng ký với một delegate được trang trí bằng @tool — import của decorator và constructor của agent khác nhau theo framework:
from contextlib import contextmanagerfrom strands import Agent, tool
from my_scope_agent_connection import RemoteAgentClientStrands
@contextmanagerdef get_agent(): remote_agent = RemoteAgentClientStrands.create()
@tool def ask_remote_agent(prompt: str) -> str: """Delegate a question to the remote RemoteAgent A2A agent and return its reply.""" return str(remote_agent(prompt))
yield Agent( system_prompt="...", tools=[ask_remote_agent], )from langchain.agents import create_agentfrom langchain_aws import ChatBedrockConversefrom langchain_core.tools import tool
from my_scope_agent_connection import RemoteAgentClientLangChain
def get_agent(): remote_agent = RemoteAgentClientLangChain.create()
@tool def ask_remote_agent(prompt: str) -> str: """Delegate a question to the remote RemoteAgent A2A agent and return its reply.""" return str(remote_agent(prompt))
return create_agent( model=ChatBedrockConverse(model=MODEL_ID, region_name=REGION), system_prompt="...", tools=[ask_remote_agent], )Cả hai client đều có thể gọi trực tiếp, trả về phản hồi của agent từ xa, và bao bọc một httpx.AsyncClient ký các yêu cầu bằng SigV4 khi triển khai lên AWS và sử dụng endpoint http://localhost:<port>/ đơn giản khi LOCAL_DEV=true. Client Strands bao bọc một A2AAgent của Strands; client LangChain điều khiển a2a SDK trực tiếp.
ID phiên AgentCore được truyền tự động đến agent từ xa thông qua header X-Amzn-Bedrock-AgentCore-Runtime-Session-Id, bất kể framework: máy chủ agent liên kết phiên của yêu cầu đến vào một ngữ cảnh async, và httpx.Auth đã ký của client kết nối đóng dấu nó trên mọi cuộc gọi đi — đảm bảo tính nhất quán cho Bedrock AgentCore Observability.
Cơ sở hạ tầng
Phần tiêu đề “Cơ sở hạ tầng”Sau khi chạy trình tạo kết nối, bạn cần cấp quyền cho host agent để gọi remote A2A agent.
const remoteAgent = new RemoteAgent(this, 'RemoteAgent');const myAgent = new MyAgent(this, 'MyAgent');
// Grant the host agent permission to invoke the remote A2A agentremoteAgent.grantInvokeAccess(myAgent);grantInvokeAccess trên một A2A agent sẽ thiết lập cả bedrock-agentcore:InvokeAgentRuntime và bedrock-agentcore:GetAgentCard — A2A client cần cả hai để lấy agent card và gửi tin nhắn.
ARN của AgentCore runtime của remote agent được tự động đăng ký trong namespace agentcore của Runtime Configuration bởi CDK construct được tạo, để host agent có thể khám phá nó tại runtime.
module "remote_agent" { source = "../../common/terraform/src/app/agents/remote-agent"
appconfig_application_id = module.runtime_config_appconfig.application_id appconfig_application_arn = module.runtime_config_appconfig.application_arn}
module "my_agent" { source = "../../common/terraform/src/app/agents/my-agent"
appconfig_application_id = module.runtime_config_appconfig.application_id appconfig_application_arn = module.runtime_config_appconfig.application_arn}
# Grant the host agent permission to invoke the remote A2A agentresource "aws_iam_policy" "agent_invoke_a2a" { name = "AgentInvokeA2aPolicy" policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow" Action = [ "bedrock-agentcore:InvokeAgentRuntime", "bedrock-agentcore:GetAgentCard", ] Resource = module.remote_agent.agent_core_runtime_arn }] })}
resource "aws_iam_role_policy_attachment" "agent_invoke_a2a" { role = module.my_agent.agent_core_runtime_role_arn policy_arn = aws_iam_policy.agent_invoke_a2a.arn}ARN của AgentCore runtime của remote agent được tự động đăng ký trong namespace agentcore của Runtime Configuration bởi Terraform module được tạo, để host agent có thể khám phá nó tại runtime.
Phát triển Cục bộ
Phần tiêu đề “Phát triển Cục bộ”Trình tạo cấu hình target dev của agent chủ để:
- Tự động khởi động (các) A2A agent đã kết nối
- Đặt
LOCAL_DEV=trueđể client được tạo kết nối trực tiếp vớihttp://localhost:<port>/thay vì AgentCore
Chạy agent cục bộ với:
pnpm nx <agent-name>-dev <project-name>yarn nx <agent-name>-dev <project-name>npx nx <agent-name>-dev <project-name>bunx nx <agent-name>-dev <project-name>Điều này sẽ khởi động cả agent chủ và tất cả các A2A agent đã kết nối, với agent chủ gọi các agent từ xa qua HTTP đơn giản trên các cổng cục bộ được chỉ định của chúng.