-
Notifications
You must be signed in to change notification settings - Fork 83
feat!: Add FastAPI JSONRPC Application #104
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
04a3626
add fastapi jsonrpc app
martimfasantos b2f8adc
add fastapi jsonrpc app
martimfasantos d91a595
small correction
martimfasantos 1d00446
added FastAPI dependency
martimfasantos ee2f29e
merged with main; linter run
martimfasantos d31964e
Merge branch 'main' into add-fastapi-app
holtskinner 16cbb71
Spelling
holtskinner de44166
Update uv.lock
holtskinner a402a3b
regenerate types
holtskinner 33be240
Merge branch 'main' into add-fastapi-app
holtskinner 57420d5
improved methods docstrings
martimfasantos 54747f8
fixed & improved tests for starlette and fastapi
martimfasantos d0bc883
Merge branch 'main' into add-fastapi-app
martimfasantos 8902bf7
fixed imports
martimfasantos 920f97f
added extended_agent_card route
martimfasantos d2cbb05
Merge branch 'main' into add-fastapi-app
martimfasantos ba24241
added exports
martimfasantos cbc09ad
Formatting
holtskinner 9764850
Merge branch 'main' into add-fastapi-app
holtskinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
ACard | ||
AClient | ||
AError | ||
AFast | ||
ARequest | ||
ARun | ||
AServer | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ authors = [{ name = "Google LLC", email = "[email protected]" }] | |
requires-python = ">=3.10" | ||
keywords = ["A2A", "A2A SDK", "A2A Protocol", "Agent2Agent"] | ||
dependencies = [ | ||
"fastapi>=0.115.12", | ||
"httpx>=0.28.1", | ||
"httpx-sse>=0.4.0", | ||
"opentelemetry-api>=1.33.0", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1 @@ | ||
"""HTTP application components for the A2A server.""" | ||
|
||
from a2a.server.apps.starlette_app import A2AStarletteApplication | ||
|
||
|
||
__all__ = ['A2AStarletteApplication'] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""A2A JSON-RPC Applications.""" | ||
|
||
from .jsonrpc_app import CallContextBuilder, JSONRPCApplication | ||
from .fastapi_app import A2AFastAPIApplication | ||
from .starlette_app import A2AStarletteApplication | ||
|
||
|
||
__all__ = [ | ||
'A2AFastAPIApplication', | ||
'A2AStarletteApplication', | ||
'CallContextBuilder', | ||
'JSONRPCApplication', | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import logging | ||
|
||
from typing import Any | ||
|
||
from fastapi import FastAPI, Request | ||
|
||
from .jsonrpc_app import CallContextBuilder, JSONRPCApplication | ||
from a2a.server.request_handlers.jsonrpc_handler import RequestHandler | ||
from a2a.types import AgentCard | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class A2AFastAPIApplication(JSONRPCApplication): | ||
"""A FastAPI application implementing the A2A protocol server endpoints. | ||
|
||
Handles incoming JSON-RPC requests, routes them to the appropriate | ||
handler methods, and manages response generation including Server-Sent Events | ||
(SSE). | ||
""" | ||
|
||
def __init__( | ||
self, | ||
agent_card: AgentCard, | ||
http_handler: RequestHandler, | ||
extended_agent_card: AgentCard | None = None, | ||
context_builder: CallContextBuilder | None = None, | ||
): | ||
"""Initializes the A2AStarletteApplication. | ||
|
||
Args: | ||
agent_card: The AgentCard describing the agent's capabilities. | ||
http_handler: The handler instance responsible for processing A2A | ||
requests via http. | ||
extended_agent_card: An optional, distinct AgentCard to be served | ||
at the authenticated extended card endpoint. | ||
context_builder: The CallContextBuilder used to construct the | ||
ServerCallContext passed to the http_handler. If None, no | ||
ServerCallContext is passed. | ||
""" | ||
super().__init__( | ||
agent_card=agent_card, | ||
http_handler=http_handler, | ||
extended_agent_card=extended_agent_card, | ||
context_builder=context_builder, | ||
) | ||
|
||
def build( | ||
self, | ||
agent_card_url: str = '/.well-known/agent.json', | ||
extended_agent_card_url: str = '/agent/authenticatedExtendedCard', | ||
rpc_url: str = '/', | ||
**kwargs: Any, | ||
) -> FastAPI: | ||
"""Builds and returns the FastAPI application instance. | ||
|
||
Args: | ||
agent_card_url: The URL for the agent card endpoint. | ||
rpc_url: The URL for the A2A JSON-RPC endpoint. | ||
extended_agent_card_url: The URL for the authenticated extended agent card endpoint. | ||
**kwargs: Additional keyword arguments to pass to the FastAPI constructor. | ||
|
||
Returns: | ||
A configured FastAPI application instance. | ||
""" | ||
app = FastAPI(**kwargs) | ||
|
||
@app.post(rpc_url) | ||
async def handle_a2a_request(request: Request): | ||
return await self._handle_requests(request) | ||
|
||
@app.get(agent_card_url) | ||
async def get_agent_card(request: Request): | ||
return await self._handle_get_agent_card(request) | ||
|
||
if self.agent_card.supportsAuthenticatedExtendedCard: | ||
@app.get(extended_agent_card_url) | ||
async def get_extended_agent_card(request: Request): | ||
return await self._handle_get_authenticated_extended_agent_card(request) | ||
|
||
return app |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import logging | ||
|
||
from typing import Any | ||
|
||
from starlette.applications import Starlette | ||
from starlette.routing import Route | ||
|
||
from .jsonrpc_app import CallContextBuilder, JSONRPCApplication | ||
from a2a.server.request_handlers.jsonrpc_handler import RequestHandler | ||
from a2a.types import AgentCard | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class A2AStarletteApplication(JSONRPCApplication): | ||
"""A Starlette application implementing the A2A protocol server endpoints. | ||
|
||
Handles incoming JSON-RPC requests, routes them to the appropriate | ||
handler methods, and manages response generation including Server-Sent Events | ||
(SSE). | ||
""" | ||
|
||
def __init__( | ||
self, | ||
agent_card: AgentCard, | ||
http_handler: RequestHandler, | ||
extended_agent_card: AgentCard | None = None, | ||
context_builder: CallContextBuilder | None = None, | ||
): | ||
"""Initializes the A2AStarletteApplication. | ||
|
||
Args: | ||
agent_card: The AgentCard describing the agent's capabilities. | ||
http_handler: The handler instance responsible for processing A2A | ||
requests via http. | ||
extended_agent_card: An optional, distinct AgentCard to be served | ||
at the authenticated extended card endpoint. | ||
context_builder: The CallContextBuilder used to construct the | ||
ServerCallContext passed to the http_handler. If None, no | ||
ServerCallContext is passed. | ||
""" | ||
super().__init__( | ||
agent_card=agent_card, | ||
http_handler=http_handler, | ||
extended_agent_card=extended_agent_card, | ||
context_builder=context_builder, | ||
) | ||
|
||
def routes( | ||
self, | ||
agent_card_url: str = '/.well-known/agent.json', | ||
extended_agent_card_url: str = '/agent/authenticatedExtendedCard', | ||
rpc_url: str = '/', | ||
) -> list[Route]: | ||
"""Returns the Starlette Routes for handling A2A requests. | ||
|
||
Args: | ||
agent_card_url: The URL path for the agent card endpoint. | ||
rpc_url: The URL path for the A2A JSON-RPC endpoint (POST requests). | ||
extended_agent_card_url: The URL for the authenticated extended agent card endpoint. | ||
|
||
Returns: | ||
A list of Starlette Route objects. | ||
""" | ||
app_routes = [ | ||
Route( | ||
rpc_url, | ||
self._handle_requests, | ||
methods=['POST'], | ||
name='a2a_handler', | ||
), | ||
Route( | ||
agent_card_url, | ||
self._handle_get_agent_card, | ||
methods=['GET'], | ||
name='agent_card', | ||
), | ||
] | ||
|
||
if self.agent_card.supportsAuthenticatedExtendedCard: | ||
app_routes.append( | ||
Route( | ||
extended_agent_card_url, | ||
self._handle_get_authenticated_extended_agent_card, | ||
methods=['GET'], | ||
name='authenticated_extended_agent_card', | ||
) | ||
) | ||
return app_routes | ||
|
||
def build( | ||
self, | ||
agent_card_url: str = '/.well-known/agent.json', | ||
extended_agent_card_url: str = '/agent/authenticatedExtendedCard', | ||
rpc_url: str = '/', | ||
**kwargs: Any, | ||
) -> Starlette: | ||
"""Builds and returns the Starlette application instance. | ||
|
||
Args: | ||
agent_card_url: The URL path for the agent card endpoint. | ||
rpc_url: The URL path for the A2A JSON-RPC endpoint (POST requests). | ||
extended_agent_card_url: The URL for the authenticated extended agent card endpoint. | ||
**kwargs: Additional keyword arguments to pass to the Starlette | ||
constructor. | ||
|
||
Returns: | ||
A configured Starlette application instance. | ||
""" | ||
app_routes = self.routes( | ||
agent_card_url, extended_agent_card_url, rpc_url | ||
) | ||
if 'routes' in kwargs: | ||
kwargs['routes'].extend(app_routes) | ||
else: | ||
kwargs['routes'] = app_routes | ||
|
||
return Starlette(**kwargs) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.