Skip to content

[Feat]: Expose close() and async context manager support on abstract Client #689

Description

@cchinchilla-dev

Is your feature request related to a problem? Please describe.

ClientTransport supports the async context manager protocol since #682, and BaseClient since #688. However, the abstract Client class (src/a2a/client/client.py) — which defines the public interface for all client implementations — does not declare close(), __aenter__, or __aexit__.

This means code that works with the Client type cannot use the async context manager or call close():

client = await ClientFactory.connect('https://agent.example.com')
# client is typed as Client, not BaseClient

async with client:
    ...
# TypeError: 'Client' object does not support the asynchronous context manager protocol

await client.close()
# AttributeError: 'Client' object has no attribute 'close'

The integration tests reflect this gap — they rely on hasattr checks instead of the type system:

if hasattr(transport, 'close'):
    await transport.close()

Describe the solution you'd like

Add close() as an abstract method and __aenter__/__aexit__ as concrete methods on Client in src/a2a/client/client.py:

@abstractmethod
async def close(self) -> None:
    """Closes the client and releases any underlying resources."""

async def __aenter__(self) -> 'Client':
    return self

async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
    await self.close()

BaseClient already implements close() delegating to self._transport.close(), so it satisfies the new abstract method with no changes needed.

This enables the idiomatic pattern at the Client abstraction level:

async with await ClientFactory.connect('https://agent.example.com') as client:
    async for event in client.send_message(msg):
        ...
    # close() called automatically, even on exceptions

Making close() abstract rather than a concrete no-op is consistent with how Client is designed — it already declares 7 abstract methods. Any implementation managing resources (transports, connections) should be required to handle cleanup explicitly. The SDK is pre-1.0, and the fix for any hypothetical external subclass would be a single-line async def close(self): pass.

Describe alternatives you've considered

  • Change ClientFactory.connect() return type to BaseClient: This would expose close() and async with to factory users, but narrows the return type to a concrete class. Code with Client type hints (e.g. function signatures, dependency injection) would still lack access.
  • Keep relying on hasattr(client, 'close'): This is what the integration tests currently do. It works but is fragile, bypasses static analysis, and doesn't guarantee that all implementations handle cleanup.
  • Make close() a concrete no-op instead of abstract: This avoids forcing subclasses to implement it, but loses the guarantee that resource-holding implementations explicitly handle cleanup. Given that Client already requires 7 abstract methods, one more is not a meaningful burden.

Additional context

After contributing #682 (ClientTransport) and #688 (BaseClient), I saw the opportunity to extend async context manager support to the abstract Client interface, making the pattern consistent across the entire client hierarchy — from transport to concrete client to the abstract interface that ClientFactory.connect() returns.

Adding an abstract method to Client is technically a breaking change for any external subclass, but given the SDK is pre-1.0 and Client already requires 7 abstract methods, the impact is minimal (a single-line async def close(self): pass fix). Existing code that calls close() manually or uses try/finally continues to work as before.

If there's interest, EventQueue on the server side could also benefit from the same pattern — it already has a sophisticated close() with graceful/immediate modes, child propagation, and cross-version handling, but doesn't support async with. Happy to open a separate issue and submit PRs for both.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    component: client
    on Feb 18, 2026
  2. self-assigned this
    on Feb 18, 2026
  3. rohityan commented on Feb 19, 2026

    @rohityan

    Hi @cchinchilla-dev , thanks for identifying this discrepancy. The solution you proposed sounds good. Would you be willing to contribute?

  4. cchinchilla-dev commented on Feb 19, 2026

    @cchinchilla-dev
    ContributorAuthor

    Hi @ryanaiagent,

    Yes, absolutely — I'd be happy to contribute.

    Thanks for the quick review — I’ll get started on it 👍

  5. added
    help wanted
    and removed
    help wanted
    on Feb 19, 2026
  6. added a commit that references this issue on Mar 2, 2026
    e25ba7b
  7. ishymko commented on Mar 2, 2026

    @ishymko
    Member

    Fixed via #719 in 1.0-dev branch as it's a breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

component: client

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions