feat: Add FastAPI Application#26
Closed
martimfasantos wants to merge 0 commit intoa2aproject:mainfrom
Closed
Conversation
src/a2a/server/apps/default_app.py
Outdated
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class DefaultA2AApplication(ABC): |
Contributor
There was a problem hiding this comment.
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
Contributor
There was a problem hiding this comment.
some comment on the file. scope it to the transport and not a general 'default'
a49473e to
765a226
Compare
765a226 to
536e4a1
Compare
Contributor
Author
|
@pstephengoogle Accidentally closed this PR — I'll be opening another one soon with the comments addressed :) |
4 tasks
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 . ```   3. Run the test client ```bash uv run test_client.py ```   Continues #26 Fixes #21 🦕
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 🦕