-
Notifications
You must be signed in to change notification settings - Fork 313
feat: Add FastAPI Application #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/a2a/server/apps/default_app.py
Outdated
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class DefaultA2AApplication(ABC): |
There was a problem hiding this comment.
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
src/a2a/server/apps/default_app.py
Outdated
There was a problem hiding this comment.
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'
a49473e to
765a226
Compare
765a226 to
536e4a1
Compare
|
@pstephengoogle Accidentally closed this PR — I'll be opening another one soon with the comments addressed :) |
# 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 . ```   3. Run the test client ```bash uv run test_client.py ```   Continues #26 Fixes #21 🦕
Description
Summary
This PR introduces a
A2AFastAPIApplicationclass 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 thebuild(...)method used to register routes and return the appropriate application instance.Added
A2AFastAPIApplication, a concrete implementation ofDefaultA2AApplicationthat 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_requestsand_handle_get_agent_card.SSE streaming support is preserved for
SendStreamingMessageRequestandTaskResubscriptionRequest.Added
fastapito the dependencies inpyproject.toml.Usage Example
Hello World Example (
examples/helloword/)Change
A2AStarletteApplicationtoA2AFastAPIApplicationStart the server
uv run .CONTRIBUTINGGuide.nox -s formatfrom the repository root to format)Fixes #21 🦕