Skip to content

Conversation

@bokelley
Copy link
Contributor

Fix A2A Adapter by Using Official A2A SDK

Summary

Fixes the A2A protocol adapter implementation by using the official a2a-sdk client instead of a custom HTTP implementation. This resolves response parsing issues and ensures spec compliance.

Changes

1. Refactor to Official SDK (fix: refactor A2A adapter to use official a2a-sdk client)

Adapter Changes:

  • Use A2AClient and A2ACardResolver from a2a-sdk package
  • Replace manual HTTP requests with official SDK methods
  • Use A2A SDK types (Task, Message, TextPart, DataPart, Role, etc.)
  • Fix TaskState enum usage (lowercase strings: "completed", "failed")
  • Fix Pydantic attribute access (snake_case: task.context_id not task.contextId)
  • Handle Part union type wrapper (access via .root)

Test Updates:

  • Add helper functions for creating mock A2A responses
  • Update all 12 A2A tests to use SDK types
  • Mock _get_a2a_client instead of HTTP layer
  • All 27 protocol tests passing (12 A2A + 15 MCP)

Benefits:

  • ✅ Spec-compliant: Uses canonical A2A client implementation
  • ✅ Maintainable: No custom HTTP client code to maintain
  • ✅ Type-safe: Leverages A2A SDK Pydantic models
  • ✅ Future-proof: Auto-updates with A2A spec changes

2. CI Fixes (fix: resolve ruff and mypy errors in A2A adapter)

Fixed linting and type checking errors:

  • Removed unused imports (SendMessageSuccessResponse, TaskState)
  • Added missing imports (MessageSendParams, Part, Role)
  • Fixed Message construction to use snake_case field names
  • Wrapped TextPart in Part union type as required by SDK
  • Used Role.user enum instead of string literal
  • Fixed line length by extracting variable
  • Fixed AgentCard.extensions access with getattr for runtime safety
  • Fixed debug_request type annotation

All ruff and mypy checks pass ✓

3. Response Handling Fix (fix: unwrap SendMessageResponse RootModel in A2A adapter)

Critical Bug Fix:

The A2A SDK's SendMessageResponse is a Pydantic RootModel that wraps a union of JSONRPCSuccessResponse | JSONRPCErrorResponse. The adapter was trying to access .error or .result directly on the wrapper, which doesn't have these attributes.

Solution:

sdk_response = await a2a_client.send_message(request)
# Unwrap the RootModel to get the actual response object
response = sdk_response.root if hasattr(sdk_response, "root") else sdk_response

This enables proper response handling for both success and error cases.

Testing

Verified against test agent at https://test-agent.adcontextprotocol.org:

Without Authentication:

  • get_info() - Successfully retrieves agent metadata
  • list_tools() - Successfully lists 16 available tools
  • ⚠️ Tool calls return proper authentication errors

With Bearer Token Authentication:

  • get_products() - Successfully retrieved 2 products with full data
  • ✅ Authentication header correctly set: Authorization: Bearer <token>
  • ✅ Response time: ~247ms

CI Checks:

  • ✅ All ruff linting checks pass
  • ✅ All mypy type checks pass
  • ✅ All protocol tests pass (300/307 total tests pass, 7 CLI test failures are pre-existing)

Release Type

This is a patch release (2.12.1 → 2.12.2) containing bug fixes to make the A2A adapter work correctly with the official SDK.

bokelley and others added 4 commits November 29, 2025 07:13
Replace custom HTTP client implementation with official A2A SDK:

**Adapter Changes:**
- Use `A2AClient` and `A2ACardResolver` from a2a-sdk
- Replace manual HTTP requests with official SDK methods
- Use A2A SDK types (Task, Message, TextPart, DataPart, etc.)
- Fix TaskState enum usage (lowercase strings not attributes)
- Fix Pydantic attribute access (snake_case not camelCase)
- Handle Part union type wrapper (access via .root)

**Benefits:**
- Spec-compliant: Uses canonical A2A client implementation
- Maintainable: No custom HTTP client code to maintain
- Type-safe: Leverages A2A SDK Pydantic models
- Future-proof: Auto-updates with A2A spec changes

**Test Updates:**
- Add helper functions for creating mock A2A responses
- Update all 12 A2A tests to use SDK types
- Mock `_get_a2a_client` instead of HTTP layer
- All 27 protocol tests passing (12 A2A + 15 MCP)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Fixed linting and type checking issues:
- Removed unused imports (SendMessageSuccessResponse, TaskState)
- Added missing imports (MessageSendParams, Part, Role)
- Fixed Message construction to use snake_case field names
- Wrapped TextPart in Part union type as required by SDK
- Used Role.user enum instead of string literal
- Fixed line length by extracting agent_id to variable
- Fixed AgentCard.extensions access with getattr for runtime safety
- Fixed debug_request type annotation to avoid None issues

All ruff and mypy checks now pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
The A2A SDK's SendMessageResponse is a Pydantic RootModel that wraps
a union of JSONRPCSuccessResponse and JSONRPCErrorResponse. We need
to unwrap the .root attribute to access the actual response object
with .result or .error attributes.

This fix enables proper error handling and response processing when
communicating with A2A agents.

Verified against test-agent.adcontextprotocol.org:
- get_info() works ✓
- list_tools() works ✓
- Tool calls now properly return authentication errors instead of
  "Invalid response from A2A client"

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Implement explicit skill invocation (deterministic) by default for A2A
protocol, as recommended by the A2A specification. This uses DataPart
with skill name and parameters instead of natural language interpretation.

**Changes:**
- Add `use_explicit_skill` parameter to `_call_a2a_tool` (default: True)
- Explicit mode: Send DataPart with `{"skill": "...", "parameters": {...}}`
- Natural language mode: Send TextPart with formatted text (legacy)
- Import DataPart from a2a.types

**Benefits:**
- Works without authentication for discovery endpoints (test agent)
- Predictable, repeatable behavior
- Direct skill execution without AI interpretation overhead
- Aligns with A2A best practices

**Reference:**
https://docs.adcontextprotocol.org/docs/protocols/a2a-guide

**Testing:**
- ✅ list_authorized_properties works without auth
- ✅ list_creative_formats works without auth
- ✅ get_products validates parameters correctly

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@bokelley bokelley merged commit d1b55cf into main Nov 29, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants