Skip to content

Commit 1c8e12e

Browse files
authored
feat: Add constants for Well-Known URIs (#271)
Prevents usage of "Magic Strings"
1 parent 7c46e70 commit 1c8e12e

File tree

6 files changed

+73
-35
lines changed

6 files changed

+73
-35
lines changed

src/a2a/client/client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
SetTaskPushNotificationConfigRequest,
2828
SetTaskPushNotificationConfigResponse,
2929
)
30+
from a2a.utils.constants import (
31+
AGENT_CARD_WELL_KNOWN_PATH,
32+
)
3033
from a2a.utils.telemetry import SpanKind, trace_class
3134

3235

@@ -40,8 +43,8 @@ def __init__(
4043
self,
4144
httpx_client: httpx.AsyncClient,
4245
base_url: str,
43-
agent_card_path: str = '/.well-known/agent.json',
44-
):
46+
agent_card_path: str = AGENT_CARD_WELL_KNOWN_PATH,
47+
) -> None:
4548
"""Initializes the A2ACardResolver.
4649
4750
Args:
@@ -184,7 +187,7 @@ async def _apply_interceptors(
184187
async def get_client_from_agent_card_url(
185188
httpx_client: httpx.AsyncClient,
186189
base_url: str,
187-
agent_card_path: str = '/.well-known/agent.json',
190+
agent_card_path: str = AGENT_CARD_WELL_KNOWN_PATH,
188191
http_kwargs: dict[str, Any] | None = None,
189192
) -> 'A2AClient':
190193
"""Fetches the public AgentCard and initializes an A2A client.

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
)
1111
from a2a.server.request_handlers.jsonrpc_handler import RequestHandler
1212
from a2a.types import AgentCard
13+
from a2a.utils.constants import (
14+
AGENT_CARD_WELL_KNOWN_PATH,
15+
DEFAULT_RPC_URL,
16+
EXTENDED_AGENT_CARD_PATH,
17+
)
1318

1419

1520
logger = logging.getLogger(__name__)
@@ -29,7 +34,7 @@ def __init__(
2934
http_handler: RequestHandler,
3035
extended_agent_card: AgentCard | None = None,
3136
context_builder: CallContextBuilder | None = None,
32-
):
37+
) -> None:
3338
"""Initializes the A2AStarletteApplication.
3439
3540
Args:
@@ -52,9 +57,9 @@ def __init__(
5257
def add_routes_to_app(
5358
self,
5459
app: FastAPI,
55-
agent_card_url: str = '/.well-known/agent.json',
56-
rpc_url: str = '/',
57-
extended_agent_card_url: str = '/agent/authenticatedExtendedCard',
60+
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
61+
rpc_url: str = DEFAULT_RPC_URL,
62+
extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH,
5863
) -> None:
5964
"""Adds the routes to the FastAPI application.
6065
@@ -83,9 +88,9 @@ async def get_extended_agent_card(request: Request) -> Response:
8388

8489
def build(
8590
self,
86-
agent_card_url: str = '/.well-known/agent.json',
87-
rpc_url: str = '/',
88-
extended_agent_card_url: str = '/agent/authenticatedExtendedCard',
91+
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
92+
rpc_url: str = DEFAULT_RPC_URL,
93+
extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH,
8994
**kwargs: Any,
9095
) -> FastAPI:
9196
"""Builds and returns the FastAPI application instance.

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
TaskResubscriptionRequest,
4343
UnsupportedOperationError,
4444
)
45+
from a2a.utils.constants import (
46+
AGENT_CARD_WELL_KNOWN_PATH,
47+
DEFAULT_RPC_URL,
48+
)
4549
from a2a.utils.errors import MethodNotImplementedError
4650

4751

@@ -109,7 +113,7 @@ def __init__(
109113
http_handler: RequestHandler,
110114
extended_agent_card: AgentCard | None = None,
111115
context_builder: CallContextBuilder | None = None,
112-
):
116+
) -> None:
113117
"""Initializes the A2AStarletteApplication.
114118
115119
Args:
@@ -299,24 +303,32 @@ async def _process_non_streaming_request(
299303
request_obj, context
300304
)
301305
case SetTaskPushNotificationConfigRequest():
302-
handler_result = await self.handler.set_push_notification_config(
303-
request_obj,
304-
context,
306+
handler_result = (
307+
await self.handler.set_push_notification_config(
308+
request_obj,
309+
context,
310+
)
305311
)
306312
case GetTaskPushNotificationConfigRequest():
307-
handler_result = await self.handler.get_push_notification_config(
308-
request_obj,
309-
context,
313+
handler_result = (
314+
await self.handler.get_push_notification_config(
315+
request_obj,
316+
context,
317+
)
310318
)
311319
case ListTaskPushNotificationConfigRequest():
312-
handler_result = await self.handler.list_push_notification_config(
313-
request_obj,
314-
context,
320+
handler_result = (
321+
await self.handler.list_push_notification_config(
322+
request_obj,
323+
context,
324+
)
315325
)
316326
case DeleteTaskPushNotificationConfigRequest():
317-
handler_result = await self.handler.delete_push_notification_config(
318-
request_obj,
319-
context,
327+
handler_result = (
328+
await self.handler.delete_push_notification_config(
329+
request_obj,
330+
context,
331+
)
320332
)
321333
case _:
322334
logger.error(
@@ -424,8 +436,8 @@ async def _handle_get_authenticated_extended_agent_card(
424436
@abstractmethod
425437
def build(
426438
self,
427-
agent_card_url: str = '/.well-known/agent.json',
428-
rpc_url: str = '/',
439+
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
440+
rpc_url: str = DEFAULT_RPC_URL,
429441
**kwargs: Any,
430442
) -> FastAPI | Starlette:
431443
"""Builds and returns the JSONRPC application instance.

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
)
1212
from a2a.server.request_handlers.jsonrpc_handler import RequestHandler
1313
from a2a.types import AgentCard
14+
from a2a.utils.constants import (
15+
AGENT_CARD_WELL_KNOWN_PATH,
16+
DEFAULT_RPC_URL,
17+
EXTENDED_AGENT_CARD_PATH,
18+
)
1419

1520

1621
logger = logging.getLogger(__name__)
@@ -30,7 +35,7 @@ def __init__(
3035
http_handler: RequestHandler,
3136
extended_agent_card: AgentCard | None = None,
3237
context_builder: CallContextBuilder | None = None,
33-
):
38+
) -> None:
3439
"""Initializes the A2AStarletteApplication.
3540
3641
Args:
@@ -52,9 +57,9 @@ def __init__(
5257

5358
def routes(
5459
self,
55-
agent_card_url: str = '/.well-known/agent.json',
56-
rpc_url: str = '/',
57-
extended_agent_card_url: str = '/agent/authenticatedExtendedCard',
60+
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
61+
rpc_url: str = DEFAULT_RPC_URL,
62+
extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH,
5863
) -> list[Route]:
5964
"""Returns the Starlette Routes for handling A2A requests.
6065
@@ -95,9 +100,9 @@ def routes(
95100
def add_routes_to_app(
96101
self,
97102
app: Starlette,
98-
agent_card_url: str = '/.well-known/agent.json',
99-
rpc_url: str = '/',
100-
extended_agent_card_url: str = '/agent/authenticatedExtendedCard',
103+
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
104+
rpc_url: str = DEFAULT_RPC_URL,
105+
extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH,
101106
) -> None:
102107
"""Adds the routes to the Starlette application.
103108
@@ -116,9 +121,9 @@ def add_routes_to_app(
116121

117122
def build(
118123
self,
119-
agent_card_url: str = '/.well-known/agent.json',
120-
rpc_url: str = '/',
121-
extended_agent_card_url: str = '/agent/authenticatedExtendedCard',
124+
agent_card_url: str = AGENT_CARD_WELL_KNOWN_PATH,
125+
rpc_url: str = DEFAULT_RPC_URL,
126+
extended_agent_card_url: str = EXTENDED_AGENT_CARD_PATH,
122127
**kwargs: Any,
123128
) -> Starlette:
124129
"""Builds and returns the Starlette application instance.

src/a2a/utils/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
new_data_artifact,
66
new_text_artifact,
77
)
8+
from a2a.utils.constants import (
9+
AGENT_CARD_WELL_KNOWN_PATH,
10+
DEFAULT_RPC_URL,
11+
EXTENDED_AGENT_CARD_PATH,
12+
)
813
from a2a.utils.helpers import (
914
append_artifact_to_task,
1015
are_modalities_compatible,
@@ -24,6 +29,9 @@
2429

2530

2631
__all__ = [
32+
'AGENT_CARD_WELL_KNOWN_PATH',
33+
'DEFAULT_RPC_URL',
34+
'EXTENDED_AGENT_CARD_PATH',
2735
'append_artifact_to_task',
2836
'are_modalities_compatible',
2937
'build_text_artifact',

src/a2a/utils/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Constants for well-known URIs used throughout the A2A Python SDK."""
2+
3+
AGENT_CARD_WELL_KNOWN_PATH = '/.well-known/agent.json'
4+
EXTENDED_AGENT_CARD_PATH = '/agent/authenticatedExtendedCard'
5+
DEFAULT_RPC_URL = '/'

0 commit comments

Comments
 (0)