Skip to content

Commit cb08973

Browse files
refactor: refactor FastAPI route setup and docs (#280)
# Description ## Summary This PR improves the current implementation of `A2AFastAPIApplication` class that enables serving A2A endpoints using a FastAPI application. (#104) ### Implementation Details Refactors the `add_routes_to_app` method in `src/a2a/server/apps/jsonrpc/fastapi_app.py` to simplify route definitions by: * Directly associating handler methods with FastAPI route decorators * Removing unnecessary nested async function definitions by directly passing handler methods (`self._handle_requests`, `self._handle_get_agent_card`, and `self._handle_get_authenticated_extended_agent_card`) to the FastAPI decorators (`app.post` and `app.get`) * Enabling method descriptions to appear in Swagger documentation, improving developer experience These changes make the code more readable, maintainable, and better integrated with FastAPI's documentation features. ### Screenshots #### Before ![image](https://github.com/user-attachments/assets/6b9487fe-010f-492d-98cf-9f28d0ad194e) ![image](https://github.com/user-attachments/assets/7fd5edd9-5849-427d-aa74-6cb392ad370b) #### After ![image](https://github.com/user-attachments/assets/00e7265b-8f5f-4841-a76a-8445be8a3ee6) ![image](https://github.com/user-attachments/assets/6ffd81e7-646c-42fb-ab96-13807cbc00bd) --- Continues #104 🦕
1 parent 1c8e12e commit cb08973

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

src/a2a/server/apps/jsonrpc/fastapi_app.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import Any
44

5-
from fastapi import FastAPI, Request, Response
5+
from fastapi import FastAPI
66

77
from a2a.server.apps.jsonrpc.jsonrpc_app import (
88
CallContextBuilder,
@@ -69,22 +69,13 @@ def add_routes_to_app(
6969
rpc_url: The URL for the A2A JSON-RPC endpoint.
7070
extended_agent_card_url: The URL for the authenticated extended agent card endpoint.
7171
"""
72-
73-
@app.post(rpc_url)
74-
async def handle_a2a_request(request: Request) -> Response:
75-
return await self._handle_requests(request)
76-
77-
@app.get(agent_card_url)
78-
async def get_agent_card(request: Request) -> Response:
79-
return await self._handle_get_agent_card(request)
72+
app.post(rpc_url)(self._handle_requests)
73+
app.get(agent_card_url)(self._handle_get_agent_card)
8074

8175
if self.agent_card.supportsAuthenticatedExtendedCard:
82-
83-
@app.get(extended_agent_card_url)
84-
async def get_extended_agent_card(request: Request) -> Response:
85-
return await self._handle_get_authenticated_extended_agent_card(
86-
request
87-
)
76+
app.get(extended_agent_card_url)(
77+
self._handle_get_authenticated_extended_agent_card
78+
)
8879

8980
def build(
9081
self,

0 commit comments

Comments
 (0)