Skip to content

Conversation

@martimfasantos
Copy link
Contributor

@martimfasantos martimfasantos commented May 15, 2025

Description

Summary

This PR introduces a A2AFastAPIApplication class that enables serving A2A endpoints using a FastAPI application, while preserving compatibility with the existing Starlette-based architecture.

Motivation

While the SDK currently provides a Starlette-based server app for A2A agent communication, many production Python APIs are built with FastAPI due to its support for extensibility, automatic OpenAPI schema generation, and strong async capabilities. Providing native FastAPI support enables seamless integration into such environments without requiring users to manually wrap the existing Starlette app.

This addition does not introduce any breaking changes. FastAPI is treated as an optional integration, and the core A2A logic continues to rely on shared base classes and request handlers.

Implementation Details

  • Introduced DefaultA2AApplication, an abstract base class that encapsulates the shared request-handling logic for both Starlette and FastAPI applications. This isolates the common behavior, with the only required customization being the build(...) method used to register routes and return the appropriate application instance.

  • Added A2AFastAPIApplication, a concrete implementation of DefaultA2AApplication that constructs a FastAPI app with the appropriate routes:

    • POST / (or custom RPC endpoint) for handling A2A JSON-RPC messages.
    • GET /.well-known/agent.json (or custom) for serving the agent card.
  • All request processing is shared via _handle_requests and _handle_get_agent_card.

  • SSE streaming support is preserved for SendStreamingMessageRequest and TaskResubscriptionRequest.

  • Added fastapi to the dependencies in pyproject.toml.

Usage Example

from a2a.server.apps.fastapi_app import A2AFastAPIApplication

app = A2AFastAPIApplication(agent_card=my_agent_card, http_handler=my_handler).build()

Hello World Example (examples/helloword/)

  1. Change A2AStarletteApplication to A2AFastAPIApplication

  2. Start the server

    uv run .

fastapi_app_running
fastapi_app_docs

  1. Run the test client
 uv run test_client.py

fastapi_results_server
fastapi_results_client


Fixes #21 🦕

@martimfasantos martimfasantos requested a review from a team as a code owner May 15, 2025 16:08
logger = logging.getLogger(__name__)


class DefaultA2AApplication(ABC):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall this is good but can we name this more like DefaultJSONRPCApplication? We are introducing different transport solutions soon and don't want to bias that one transport is dominant over another (also remove A2A from class names to avoid duplication - they will already be importing as from a2a... import DefaultJSONRPCApplication so the scope is clear

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comment on the file. scope it to the transport and not a general 'default'

@martimfasantos martimfasantos force-pushed the add-fastapi-app branch 2 times, most recently from a49473e to 765a226 Compare May 23, 2025 20:38
@martimfasantos
Copy link
Contributor Author

@pstephengoogle Accidentally closed this PR — I'll be opening another one soon with the comments addressed :)

holtskinner pushed a commit that referenced this pull request Jun 4, 2025
# Description

## Summary

This PR introduces a `A2AFastAPIApplication` class that enables serving
A2A endpoints using a FastAPI application, while preserving
compatibility with the existing JSONRPC Starlette-based architecture.

### Motivation

While the SDK currently provides a Starlette-based server app for A2A
agent communication using JSONRPC, many production Python APIs are built
with FastAPI due to its support for performance, extensibility,
automatic OpenAPI schema generation, and strong async capabilities.
Providing native FastAPI support enables seamless integration into such
environments without requiring users to manually wrap the existing
Starlette app.

This addition does **not** introduce any breaking changes. FastAPI is
treated as an optional integration, and the core A2A logic continues to
rely on shared base classes and request handlers.

### Implementation Details

* Introduced `JSONRPCApplication`, an abstract base class that
encapsulates the shared request-handling logic for both Starlette and
FastAPI JSONRPC applications. This isolates the common behavior, with
the only required customization being the `build(...)` method used to
register routes and return the appropriate application instance.

* Added `A2AFastAPIApplication`, a concrete implementation of
`JSONRPCApplication` that constructs a FastAPI app with the appropriate
routes:

* `POST /` (or custom RPC endpoint) for handling A2A JSON-RPC messages.
* `GET /.well-known/agent.json` (or custom) for serving the agent card.
* All request processing is shared via `_handle_requests` and
`_handle_get_agent_card`.
* SSE streaming support is preserved for `SendStreamingMessageRequest`
and `TaskResubscriptionRequest`.
* Added `fastapi` to the dependencies in `pyproject.toml`.

### Usage Example

```python
from a2a.server.apps.jsonrpc import A2AFastAPIApplication

app = A2AFastAPIApplication(agent_card=my_agent_card, http_handler=my_handler).build()
```

### Hello World Example (`examples/helloword/`)

1. Change `A2AStarletteApplication` to `A2AFastAPIApplication`

2. Start the server
	```bash
   uv run .
   ```

![fastapi_app_running](https://github.com/user-attachments/assets/cb31d08e-5aba-4e08-ad92-ef1c8e0d3c69)

![fastapi_app_docs](https://github.com/user-attachments/assets/a0aa2dbe-abea-4b8f-9c5c-448569f44bc5)

3. Run the test client
  ```bash
   uv run test_client.py
   ```

![fastapi_results_server](https://github.com/user-attachments/assets/372566ca-9b8c-4cb8-b9d1-7331bdcb05c2)

![fastapi_results_client](https://github.com/user-attachments/assets/cfbe944c-92b3-4abc-9fe7-4afca8b2bd4e)


Continues #26 
Fixes #21 🦕
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.

[Feat]: A2A FastAPI Application

3 participants