콘텐츠로 이동

Python Strands Agent에서 A2A Agent로

connection 생성기는 Python Strands Agent를 원격 A2A 에이전트(TypeScript 또는 Python)에 연결하여, 에이전트가 다른 에이전트를 도구로 위임할 수 있도록 합니다.

생성기는 에이전트가 원격 A2A 에이전트를 발견하고 호출할 수 있도록 필요한 모든 배선을 설정하며, AWS에 배포된 경우(Bedrock AgentCore를 통해)와 로컬에서 실행되는 경우 모두 작동합니다.

이 생성기를 사용하기 전에 다음을 준비해야 합니다:

  1. Strands Agent 컴포넌트가 있는 Python 프로젝트 (모든 프로토콜)
  2. --protocol=A2A--auth=IAM으로 생성된 Strands Agent 컴포넌트가 있는 프로젝트 (ts#strands-agent 또는 py#strands-agent)
  3. computeType: BedrockAgentCoreRuntime으로 생성된 두 컴포넌트
  1. 설치 Nx Console VSCode Plugin 아직 설치하지 않았다면
  2. VSCode에서 Nx 콘솔 열기
  3. 클릭 Generate (UI) "Common Nx Commands" 섹션에서
  4. 검색 @aws/nx-plugin - connection
  5. 필수 매개변수 입력
    • 클릭 Generate

    호스트 에이전트 프로젝트를 소스로 선택하고 A2A 에이전트 프로젝트를 대상으로 선택합니다. 프로젝트에 여러 컴포넌트가 포함된 경우 sourceComponenttargetComponent 옵션을 지정하여 명확히 구분합니다.

    매개변수 타입 기본값 설명
    sourceProject 필수 string - 소스 프로젝트
    targetProject 필수 string - 연결할 대상 프로젝트
    sourceComponent string - 연결을 시작할 소스 컴포넌트 (컴포넌트 이름, 소스 프로젝트 루트 기준 상대 경로, 또는 generator id). 프로젝트를 소스로 명시적으로 선택하려면 '.'을 사용하세요.
    targetComponent string - 연결할 대상 컴포넌트 (컴포넌트 이름, 대상 프로젝트 루트 기준 상대 경로, 또는 generator id). 프로젝트를 대상으로 명시적으로 선택하려면 '.'을 사용하세요.

    생성기는 packages/common/agent_connection/에 공유 agent_connection Python 프로젝트를 생성합니다(아직 존재하지 않는 경우). 연결별 클라이언트 모듈이 이 공유 프로젝트에 생성됩니다:

    • 디렉터리packages/common/agent_connection
      • 디렉터리<scope>_agent_connection
        • __init__.py 연결별 클라이언트 재내보내기
        • 디렉터리core
          • agentcore_a2a_client.py SigV4 인증을 사용하는 코어 AgentCore A2A 클라이언트
        • 디렉터리app
          • <target_agent_name>_client.py 각 A2A 에이전트에 대한 연결별 클라이언트

    또한 생성기는:

    • 에이전트의 agent.py를 변환하여 원격 A2A 에이전트를 @tool을 사용하여 도구로 등록합니다
    • agent_connection 프로젝트를 에이전트 프로젝트의 워크스페이스 의존성으로 추가합니다
    • 에이전트의 serve-local 대상을 대상 에이전트의 serve-local 대상에 의존하도록 업데이트합니다

    생성기는 에이전트의 agent.py를 변환하여 원격 A2A 에이전트를 도구로 래핑합니다:

    packages/my-project/my_module/agent/agent.py
    from contextlib import contextmanager
    from strands import Agent, tool
    from my_scope_agent_connection import RemoteAgentClient
    @contextmanager
    def get_agent(session_id: str):
    remote_agent = RemoteAgentClient.create(session_id=session_id)
    @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],
    )

    session_id 매개변수는 호출자로부터 전달되어 Bedrock AgentCore Observability의 일관성을 보장합니다.

    내부적으로 RemoteAgentClient.create(session_id=...)는 AWS에 배포된 경우 SigV4로 요청에 서명하는 httpx.AsyncClient로 구성된 Strands A2AAgent를 반환하고, SERVE_LOCAL=true인 경우 일반 http://localhost:<port>/ 엔드포인트를 사용합니다.

    연결 생성기를 실행한 후, 호스트 에이전트에게 원격 A2A 에이전트를 호출할 수 있는 권한을 부여해야 합니다.

    packages/infra/src/stacks/application-stack.ts
    const remoteAgent = new RemoteAgent(this, 'RemoteAgent');
    const myAgent = new MyAgent(this, 'MyAgent');
    // Grant the host agent permission to invoke the remote A2A agent
    remoteAgent.grantInvokeAccess(myAgent);

    A2A 에이전트의 grantInvokeAccessbedrock-agentcore:InvokeAgentRuntimebedrock-agentcore:GetAgentCard 모두를 연결합니다 — A2A 클라이언트는 에이전트 카드를 가져오고 메시지를 전송하기 위해 두 가지 모두가 필요합니다.

    원격 에이전트의 AgentCore 런타임 ARN은 생성된 CDK 구성에 의해 Runtime Configurationagentcore 네임스페이스에 자동으로 등록되므로, 호스트 에이전트가 런타임에 이를 검색할 수 있습니다.

    생성기는 호스트 에이전트의 serve-local 대상을 다음과 같이 구성합니다:

    1. 연결된 A2A 에이전트를 자동으로 시작
    2. SERVE_LOCAL=true를 설정하여 생성된 클라이언트가 AgentCore 대신 http://localhost:<port>/에 직접 연결

    다음 명령으로 에이전트를 로컬에서 실행합니다:

    Terminal window
    pnpm nx <agent-name>-serve-local <project-name>

    이렇게 하면 호스트 에이전트와 연결된 모든 A2A 에이전트가 시작되며, 호스트 에이전트는 할당된 로컬 포트에서 일반 HTTP를 통해 원격 에이전트를 호출합니다.