Skip to content

Commit 53fb802

Browse files
committed
refactor: adjust code after merge
Signed-off-by: Shingo OKAWA <[email protected]>
1 parent d5ff7d4 commit 53fb802

File tree

7 files changed

+265
-42
lines changed

7 files changed

+265
-42
lines changed

examples/apicatalog/__main__.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import logging
2-
import os
32
import sys
43
import traceback
54

65
import click
76
import uvicorn
8-
from dotenv import load_dotenv
97

108
from agent_executors import ( # type: ignore[import-untyped]
119
EchoAgentExecutor,
1210
HelloWorldAgentExecutor,
1311
)
12+
from dotenv import load_dotenv
1413

1514
from a2a.server.apps import A2AStarletteBuilder, A2AStarletteRouteBuilder
1615
from a2a.server.request_handlers import DefaultRequestHandler
@@ -54,19 +53,19 @@ def main(host: str, port: int):
5453
)
5554

5655
echo_skill = AgentSkill(
57-
id="echo",
58-
name="Echo input",
59-
description="Returns the input text as is",
60-
tags=["echo"],
61-
examples=["Hello!", "Repeat after me"],
56+
id='echo',
57+
name='Echo input',
58+
description='Returns the input text as is',
59+
tags=['echo'],
60+
examples=['Hello!', 'Repeat after me'],
6261
)
6362
echo_card = AgentCard(
64-
name="Echo Agent",
65-
description="An agent that echoes back your input.",
66-
url=f"http://{host}:{port}/a2a/echo",
67-
version="1.0.0",
68-
defaultInputModes=["text"],
69-
defaultOutputModes=["text"],
63+
name='Echo Agent',
64+
description='An agent that echoes back your input.',
65+
url=f'http://{host}:{port}/a2a/echo',
66+
version='1.0.0',
67+
defaultInputModes=['text'],
68+
defaultOutputModes=['text'],
7069
capabilities=AgentCapabilities(streaming=True),
7170
skills=[echo_skill],
7271
supportsAuthenticatedExtendedCard=False,
@@ -80,16 +79,11 @@ def main(host: str, port: int):
8079
http_handler=echo_handler,
8180
)
8281

83-
server = (
84-
A2AStarletteBuilder()
85-
.mount(hello_agent)
86-
.mount(echo_agent)
87-
.build()
88-
)
82+
server = A2AStarletteBuilder().mount(hello_agent).mount(echo_agent).build()
8983
uvicorn.run(server, host=host, port=port)
9084

9185

92-
if __name__ == "__main__":
86+
if __name__ == '__main__':
9387
try:
9488
main()
9589
except Exception as _:

examples/apicatalog/test_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import asyncio
22
import json
33
import logging
4-
import os
54
import sys
65
import traceback
76

87
import click
98
import httpx
9+
1010
from dotenv import load_dotenv
1111

1212

@@ -31,7 +31,7 @@ def main(host: str, port: int):
3131
print(json.dumps(catalog))
3232

3333

34-
if __name__ == "__main__":
34+
if __name__ == '__main__':
3535
try:
3636
main()
3737
except Exception as _:

src/a2a/server/apps/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from a2a.server.apps.jsonrpc import (
44
A2AFastAPIApplication,
55
A2AStarletteApplication,
6-
A2AStarletteRouteBuilder,
76
A2AStarletteBuilder,
7+
A2AStarletteRouteBuilder,
88
CallContextBuilder,
99
JSONRPCApplication,
1010
)
@@ -13,8 +13,8 @@
1313
__all__ = [
1414
'A2AFastAPIApplication',
1515
'A2AStarletteApplication',
16-
'A2AStarletteRouteBuilder',
1716
'A2AStarletteBuilder',
17+
'A2AStarletteRouteBuilder',
1818
'CallContextBuilder',
1919
'JSONRPCApplication',
2020
]

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
CallContextBuilder,
66
JSONRPCApplication,
77
)
8-
from a2a.server.apps.jsonrpc.starlette_app import A2AStarletteApplication
8+
from a2a.server.apps.jsonrpc.starlette_app import (
9+
A2AStarletteApplication,
10+
A2AStarletteBuilder,
11+
A2AStarletteRouteBuilder,
12+
)
913

1014

1115
__all__ = [
1216
'A2AFastAPIApplication',
1317
'A2AStarletteApplication',
18+
'A2AStarletteBuilder',
19+
'A2AStarletteRouteBuilder',
1420
'CallContextBuilder',
1521
'JSONRPCApplication',
1622
]

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

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

77
from a2a.server.apps.jsonrpc.jsonrpc_app import (
88
CallContextBuilder,
9-
JSONRPCApplication,
9+
JSONRPCApplicationAspect,
1010
)
1111
from a2a.server.request_handlers.jsonrpc_handler import RequestHandler
1212
from a2a.types import AgentCard
@@ -15,7 +15,7 @@
1515
logger = logging.getLogger(__name__)
1616

1717

18-
class A2AFastAPIApplication(JSONRPCApplication):
18+
class A2AFastAPIApplication(JSONRPCApplicationAspect):
1919
"""A FastAPI application implementing the A2A protocol server endpoints.
2020
2121
Handles incoming JSON-RPC requests, routes them to the appropriate

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import contextlib
22
import json
33
import logging
4-
import posixpath
54
import traceback
65

76
from abc import ABC, abstractmethod
87
from collections.abc import AsyncGenerator
9-
from typing import Any
10-
from urllib.parse import urlparse, urlunparse
8+
from typing import Any, Protocol, runtime_checkable
119

1210
from fastapi import FastAPI
1311
from pydantic import ValidationError
@@ -26,9 +24,6 @@
2624
A2AError,
2725
A2ARequest,
2826
AgentCard,
29-
AgentCatalog,
30-
AgentLinkContext,
31-
AgentLinkTarget,
3227
CancelTaskRequest,
3328
GetTaskPushNotificationConfigRequest,
3429
GetTaskRequest,
@@ -97,7 +92,7 @@ def build(self, request: Request) -> ServerCallContext:
9792
return ServerCallContext(user=user, state=state)
9893

9994

100-
class JSONRPCApplication(ABC):
95+
class JSONRPCApplicationAspect:
10196
"""Base class for A2A JSONRPC applications.
10297
10398
Handles incoming JSON-RPC requests, routes them to the appropriate
@@ -409,7 +404,15 @@ async def _handle_get_authenticated_extended_agent_card(
409404
status_code=404,
410405
)
411406

412-
@abstractmethod
407+
408+
@runtime_checkable
409+
class JSONRPCApplication(Protocol):
410+
"""Protocol for building a JSON-RPC application.
411+
412+
Implementers of this protocol must provide a `build` method that returns a FastAPI
413+
or Starlette application instance configured for A2A JSON-RPC communication.
414+
"""
415+
413416
def build(
414417
self,
415418
agent_card_url: str = '/.well-known/agent.json',
@@ -426,6 +429,4 @@ def build(
426429
Returns:
427430
A configured JSONRPC application instance.
428431
"""
429-
raise NotImplementedError(
430-
'Subclasses must implement the build method to create the application instance.'
431-
)
432+
...

0 commit comments

Comments
 (0)