콘텐츠로 이동

A2A 어댑터

선언형 Agent를 A2A(Agent-to-Agent) AgentCard와 task transport로 노출하고, 원격 A2A Agent를 teammate delegation 경로로 호출하는 어댑터 가이드입니다.

spakky-a2a는 공식 a2a-sdk 타입을 사용합니다. 서버 쪽은 AgentRunner.run_events()의 protocol-neutral AgentEvent stream을 A2A task/message/artifact update로 투영하고, 클라이언트 쪽은 원격 A2A stream을 child AgentEvent로 되돌려 parent run에 합류시킵니다.

설치

pip install spakky-a2a

spakky[agent] extra에도 spakky-a2a가 포함됩니다.

서버로 노출

@A2ACompatible는 @Agent class에 붙는 marker입니다. @Agent가 Pod 등록을 담당하고, A2A marker는 AgentCard에 광고할 public transport URL/version과 optional ASGI/gRPC exposure metadata를 기록합니다.

from spakky.agent import Agent, AgentExecutionSpec
from spakky.plugins.a2a import A2ACompatible


@A2ACompatible(
    base_url="https://agents.example.com/a2a/assistant",
    version="1.0.0",
    mount_path="/a2a/assistant",
    rest_mount_path="/a2a-rest/assistant",
    rest_base_url="https://agents.example.com/a2a-rest/assistant",
    grpc_enabled=True,
    grpc_base_url="grpc://agents.example.com:443",
)
@Agent(spec=AgentExecutionSpec(name="assistant", objective="answer with tools"))
class AssistantAgent:
    ...

plugin 초기화는 A2AConfig, A2AAgentRegistry, A2AAgentServerSpec, RegisterA2AAgentServersPostProcessor, ASGI mount post-processor, gRPC registration post-processor, remote delegate Pod를 등록합니다. 부트스트랩 후 @A2ACompatible와 @Agent가 모두 붙은 Pod는 registry에 agent name 기준으로 들어가고, Starlette/FastAPI host Pod가 있으면 자동으로 mount됩니다. spakky-grpc가 함께 로드되어 GrpcServerSpec가 있으면 grpc_enabled=True entry의 gRPC handler도 자동 등록됩니다.

A2AConfig는 SPAKKY_A2A_ 접두사의 환경변수를 읽습니다. default_base_url은 marker가 base_url을 생략할 때 실제 mount path와 결합되어 AgentCard interface URL을 유도합니다.

환경변수 의미 기본값
SPAKKY_A2A_DEFAULT_BASE_URL mount path와 결합할 public host URL http://localhost:8000
SPAKKY_A2A_DEFAULT_VERSION AgentCard에 광고할 semantic version 1.0.0
SPAKKY_A2A_DEFAULT_MOUNT_PATH_PREFIX 자동 mount path prefix /a2a
from starlette.applications import Starlette
from spakky.core.pod.annotations.pod import Pod


@Pod(name="asgi_host")
def asgi_host() -> Starlette:
    return Starlette()

mount_path를 생략하면 {default_mount_path_prefix}/{agent_name}을 사용합니다. Bootstrap 후 /a2a/assistant/.well-known/agent-card.json과 /a2a/assistant/ JSON-RPC route가 host app에 존재합니다. rest_mount_path를 지정하면 /a2a-rest/assistant/.well-known/agent-card.json과 REST operation route가 같은 host app에 추가됩니다.

Transport 선택

일반 애플리케이션은 transport별 builder 함수를 호출하지 않습니다. 노출할 transport를 @A2ACompatible metadata로 선언하고, plugin post-processor가 host에 연결합니다.

transport 선언 결과
JSON-RPC + AgentCard mount_path 또는 기본 prefix Starlette/FastAPI host에 mount
HTTP+JSON REST + AgentCard rest_mount_path Starlette/FastAPI host에 별도 mount
gRPC grpc_enabled=True, 선택적 grpc_base_url spakky-grpc GrpcServerSpec에 handler 등록

base_url, rest_base_url, grpc_base_url은 AgentCard에 광고되는 public operation endpoint입니다. ASGI mount path나 reverse proxy prefix가 외부 URL에 보이면 포함해야 합니다. AgentCard discovery path인 /.well-known/agent-card.json 자체는 포함하지 않습니다. 값을 생략하면 JSON-RPC는 default_base_url + mount_path, REST는 default_base_url + rest_mount_path를 사용합니다. gRPC는 HTTP path 기반 mount가 아니므로 실제 listener/scheme이 다르면 grpc_base_url을 명시합니다.

A2AAgentServerSpec.build_app_for(), build_rest_app_for(), build_grpc_handler_for()와 transport builder 함수들은 custom host와 테스트를 위한 lower-level API입니다.

AgentCard derivation

AgentCardFactory는 @Agent spec, tool catalog, teammate 선언으로 AgentCard를 만듭니다.

입력 AgentCard 반영
AgentExecutionSpec.name card name. 없으면 class name
objective 또는 instructions card description
streaming_exposure_mode NO_STREAM_UNTIL_FINAL_GUARDED가 아니면 streaming true
@agent_tool descriptor JSON input/output skill
AgentTeammate delegation tag가 붙은 teammate skill

Task 저장소

서버 transport는 container에서 optional IA2ATaskRepository Pod를 찾습니다. 등록된 repository가 없으면 InMemoryA2ATaskRepository가 사용됩니다. 운영에서 durable task state가 필요하면 repository 구현을 Pod로 등록합니다.

from collections.abc import Sequence
from a2a.types import Task
from spakky.core.pod.annotations.pod import Pod
from spakky.plugins.a2a.store.interfaces import IA2ATaskRepository


@Pod()
class PostgresA2ATaskRepository(IA2ATaskRepository):
    def get_or_none(self, task_id: str) -> Task | None:
        ...

    def save(self, task: Task) -> None:
        ...

    def delete(self, task_id: str) -> None:
        ...

    def list_all(self) -> Sequence[Task]:
        ...

SpakkyA2ATaskStore는 synchronous repository를 a2a-sdk의 async TaskStore로 감싸는 bridge입니다. 이 저장소는 A2A protocol Task snapshot을 보존합니다. Agent 대화 transcript를 conversation_id로 재생하는 core ITaskStore와는 별도 책임입니다.

Event projection

A2A executor는 inbound task id를 core RunAgentInput.state_id로 사용하고, A2A context_id를 RunAgentInput.conversation_id로 넘깁니다. 그 뒤 AgentRunner.run_events()를 순회해 task update로 투영합니다.

AgentEvent A2A 투영
RUN_STARTED task working
MESSAGE_DELTA, REASONING_DELTA working status message
TOOL_CALL_* working metadata 또는 artifact
RUN_PAUSED input-required 또는 auth-required
RUN_FINISHED executor가 stream drain 후 complete 또는 failed로 reconcile
STATE_SNAPSHOT, STATE_DELTA, ARTIFACT data part 또는 artifact
CUSTOM working status message의 data part, message metadata custom_event에 이벤트 이름

이 표는 semantic mapping이지 event-by-event 무손실 복제가 아닙니다. 여러 neutral event가 같은 working status 형태로 축약될 수 있고 tool result는 artifact로 바뀌며, RUN_FINISHED는 projector가 즉시 terminal update를 내지 않고 executor가 stream을 모두 drain한 뒤 한 번만 complete/failed로 reconcile합니다.

Executor는 run_events()를 AgentCustomEvents.merge()로 감쌉니다. 도구가 AgentCustomEvents.emit("myapp.turn_part", {"index": 0})를 호출하면 emit한 시점에 working status update가 하나 나갑니다. Agent message의 data part가 {"index": 0}이고 message metadata가 {"custom_event": "myapp.turn_part"}입니다. A2A에는 예약된 이벤트 이름이 없습니다. 사용법은 AI Agent 심화를 봅니다.

Iterative run의 model-1, tool-1, model-2 같은 step boundary는 순서대로 A2A working update에 들어갑니다. 첫 model이 tool을 요청하면 result artifact를 추가한 뒤 다음 model step의 message/status update가 이어지고, 전체 loop가 final에 도달한 뒤에만 executor가 task를 한 번 completed로 바꿉니다. 중간 model step의 DONE은 A2A task terminal이 아닙니다.

Candidate-only model event는 core runner가 missing START/END frame만 합성하므로 A2A에는 call start/end working metadata가 순서대로 나타납니다. Signal Progress는 ArtifactEvent(name="signal_progress")를 거쳐 A2A artifact가 되며 unsupported hook yield는 agent_signal_projection_unsupported failed task입니다. Run 안에서 소비한 canonical cancel signal도 code="cancelled" error data와 함께 failed task로 끝납니다. 이것은 A2A protocol의 별도 cancel operation이 task를 canceled state로 바꾸는 경로와 구분합니다.

Declared output_type이 성공하면 A2A projector는 JSON-safe final output을 data part로 가진 artifact를 먼저 추가합니다. Artifact name은 declared type 이름이고 그 뒤 executor가 task를 complete합니다. output_type=None인 기존 Agent는 core terminal에 output key가 없으므로 final output artifact를 만들지 않습니다. Structured capability/missing/ambiguous/invalid terminal은 output artifact 없이 해당 error data를 가진 failed task입니다.

A2A inbound data part를 core AgentContext로 자동 승격하는 wire 계약은 현재 없습니다. Protocol-exposed Agent의 typed dynamic context는 constructor-injected IAgentContextProvider를 사용합니다.

Multimodal inbound message

A2A text와 raw/URL media parts는 한 message에서 함께 보낼 수 있습니다. Python SDK에서의 최소 shape는 다음과 같습니다.

from a2a.types import Part


parts = (
    Part(text="차트와 보고서를 비교해 주세요."),
    Part(
        url="https://cdn.example.com/chart.png",
        media_type="image/png",
    ),
    Part(
        raw=b"%PDF-1.4",
        media_type="application/pdf",
        filename="report.pdf",
    ),
)

Executor는 text를 instruction으로, raw/URL part를 ordered RunAgentInput.attachments로 매핑합니다. Data part는 model selection/metadata/MCP/approval 용도이며 attachment로 해석하지 않습니다. 일반 run에는 nonblank text가 필요하고 approval-only resume만 "resume" marker를 사용합니다.

MIME family가 image/audio/video/application-or-text 중 하나여야 하며 empty raw bytes, local URI, unknown part, media 16개 초과와 inline 합계 20 MiB 초과는 A2ARunResolutionError입니다. Document filename과 message-ID provenance는 보존됩니다. Generated non-text output의 공통 A2A artifact contract는 아직 없습니다.

승인 재개는 inbound A2A data part에 approval_id와 decision을 담아 보냅니다. executor는 이를 APPROVAL_DECISION signal로 append하고 RunAgentInput(resume=True)로 runner를 재개합니다. 현재 A2A ingress는 modified_payload를 signal로 전달하지 않으므로 argument-bearing MODIFY는 지원하지 않습니다. A2A client는 APPROVE, REJECT, DEFER, CANCEL을 사용하고, payload 수정 승인이 필요하면 modified_payload를 보존하는 다른 inbound adapter를 사용해야 합니다.

A2A client가 logical model이나 MCP 서버 선택을 함께 전달해야 하면 message data part를 사용합니다. Executor는 canonical modelSelection을 RunAgentInput.model_selection으로, metadata와 mcp object를 RunAgentInput.metadata로 변환합니다. Legacy model_selection outer key는 허용하지 않습니다.

Model selection object는 정확히 camelCase modelRef 하나만 가집니다. 이 ref는 operator가 LlmConfig.models에 미리 등록한 opaque key이며 provider, profile, physical model, credential, endpoint를 요청에서 선택하지 않습니다. Catalog 등록 형식은 LLM 모델 라우팅을 확인하세요.

{
  "modelSelection": {
    "modelRef": "coding/fast"
  },
  "mcp": {
    "servers": ["github"]
  },
  "metadata": {
    "tenant": "acme"
  }
}

바깥 key는 modelSelection만 사용합니다. 한 message의 전체 data part 중 selector가 두 번 이상 나타나거나, legacy model_selection이 있거나, 내부에 modelRef 외 field를 넣거나, model_ref처럼 내부 key를 snake_case로 보내거나, blank/non-string 값을 보내면 A2ARunResolutionError로 거부합니다. Well-formed하지만 catalog에 없는 ref는 protocol shape 오류가 아닙니다. Catalog-aware LlmAgentModel.stream()이 llm_model_selection_invalid model error를 내고 runner가 terminal error로 바꾸면 A2A executor가 error data part와 함께 task를 failed로 전이합니다. /는 provider 구분자로 parsing하지 않습니다. Selection이 없을 때 default_model을 쓰는 것도 활성 model이 LlmAgentModel인 경우에만 해당합니다.

metadata object는 얕은 update이고 별도 top-level mcp object는 같은 RunAgentInput.metadata["mcp"] slot에 기록됩니다. 같은 data part에서는 top-level mcp가 metadata.mcp를 덮어쓰며, 여러 data part에서는 뒤에 처리한 값이 앞 값을 덮어씁니다. Deep merge나 duplicate conflict 오류가 없으므로 두 경로를 섞지 말고 runtime MCP 선택은 top-level mcp 하나로 보내세요.

mcp.servers에는 McpConfig.servers에 선언된 서버 이름 또는 inline MCP server declaration을 넣습니다. 같은 run 안에서 같은 MCP server name을 두 번 선택하면 도구 prefix와 credential 선택이 모호하므로 McpServerConfigurationError로 실패합니다.

Teammate 위임

AgentExecutionSpec.teammates에 선언한 teammate는 runner가 model-callable delegation tool로 노출합니다. tool schema 이름은 teammate.<schema_token(name)>.delegate입니다. schema_token은 teammate name의 앞뒤 공백을 제거한 뒤 [a-zA-Z0-9_]가 아닌 연속 문자를 단일 _로 치환하고, 앞뒤 _를 제거한 다음 소문자화한 값입니다. 이 결과가 비면 agent definition 단계에서 거부됩니다.

로컬 teammate는 parent agent에 teammate Pod 인스턴스를 주입하면 in-process로 실행됩니다.

from spakky.agent import Agent, AgentExecutionSpec, AgentTeammate


@Agent(spec=AgentExecutionSpec(name="researcher"))
class ResearcherAgent:
    ...


@Agent(
    spec=AgentExecutionSpec(
        name="orchestrator",
        delegation_allowed=True,
        teammates=(AgentTeammate(name="researcher", pod=ResearcherAgent),),
    )
)
class OrchestratorAgent:
    def __init__(self, researcher: ResearcherAgent) -> None:
        self._researcher = researcher

원격 teammate는 AgentCard URL을 선언하고 A2AAgentDelegate를 parent agent에 주입합니다. delegate는 AgentCard를 fetch한 뒤 SDK client로 message stream을 수행하고, 지원하는 remote task/message/artifact update를 neutral child event로 되돌립니다.

from spakky.agent import Agent, AgentExecutionSpec, AgentTeammate
from spakky.plugins.a2a import A2AAgentDelegate


@Agent(
    spec=AgentExecutionSpec(
        name="orchestrator",
        delegation_allowed=True,
        teammates=(
            AgentTeammate(
                name="remote_reviewer",
                card_url="https://reviewer.example/.well-known/agent-card.json",
            ),
        ),
    )
)
class OrchestratorAgent:
    def __init__(self, delegate: A2AAgentDelegate) -> None:
        self._delegate = delegate

A2ARemoteAgentClient.resolve_card()는 URL path가 비어 있으면 /.well-known/agent-card.json을 사용합니다. RemoteA2AMessage는 text, optional task/context id, message id를 담는 송신 envelope입니다.

Remote delegation mapping도 무손실 변환은 아닙니다. Text message part만 child MESSAGE_DELTA가 되고 message의 data/url/raw part는 text projection에서 생략됩니다. Artifact part는 text/data/url/raw를 portable value로 보존합니다. Status update는 working, completed, failed만 child step/run event로 바꾸며 remote input-required, auth-required, canceled, rejected 등은 현재 first-class child pause/terminal event로 승격하지 않습니다. 마지막 status의 enum 이름은 delegation tool result의 output.state에는 남지만, parent가 이를 자동 resume 가능한 durable child state로 간주해서는 안 됩니다.

API Reference