11import logging
22import posixpath
33
4+ from dataclasses import dataclass
45from typing import Any
56from urllib .parse import urlparse , urlunparse
67
@@ -128,6 +129,21 @@ def build(
128129 return Starlette (** kwargs )
129130
130131
132+ @dataclass
133+ class StarletteRouteConfig :
134+ """Configuration for route paths used in the Starlette-based JSON-RPC application.
135+
136+ Attributes:
137+ agent_card_path: The URL path for the agent card endpoint.
138+ extended_agent_card_path: The URL path for the authenticated extended agent card endpoint.
139+ rpc_path: The URL path for the A2A JSON-RPC endpoint (POST requests).
140+ """
141+
142+ agent_card_path : str = '/agent.json'
143+ extended_agent_card_path : str = '/agent/authenticatedExtendedCard'
144+ rpc_path : str = '/'
145+
146+
131147class StarletteRouteBuilder (JSONRPCApplicationAspect ):
132148 """Configurable builder for Starlette routes that serve A2A protocol endpoints.
133149
@@ -141,10 +157,8 @@ def __init__(
141157 agent_card : AgentCard ,
142158 http_handler : RequestHandler ,
143159 extended_agent_card : AgentCard | None = None ,
144- agent_card_path : str = '/agent.json' ,
145- extended_agent_card_path : str = '/agent/authenticatedExtendedCard' ,
146- rpc_path : str = '/' ,
147160 context_builder : CallContextBuilder | None = None ,
161+ config : StarletteRouteConfig | None = None ,
148162 ):
149163 """Initializes the A2AStarletteRouter.
150164
@@ -154,22 +168,19 @@ def __init__(
154168 requests via http.
155169 extended_agent_card: An optional, distinct AgentCard to be served
156170 at the authenticated extended card endpoint.
157- agent_card_path: The URL path for the agent card endpoint.
158- rpc_path: The URL path for the A2A JSON-RPC endpoint (POST requests).
159- extended_agent_card_path: The URL path for the authenticated extended agent card endpoint.
160171 context_builder: The CallContextBuilder used to construct the
161172 ServerCallContext passed to the http_handler. If None, no
162173 ServerCallContext is passed.
174+ config: Optional route configuration including the paths for the agent card,
175+ extended agent card, and A2A JSON-RPC endpoint. If None, defaults are used.
163176 """
164177 super ().__init__ (
165178 agent_card = agent_card ,
166179 http_handler = http_handler ,
167180 extended_agent_card = extended_agent_card ,
168181 context_builder = context_builder ,
169182 )
170- self .agent_card_path = agent_card_path
171- self .extended_agent_card_path = extended_agent_card_path
172- self .rpc_path = rpc_path
183+ self .config = config or StarletteRouteConfig ()
173184
174185 def build (self ) -> list [Route ]:
175186 """Returns the Starlette Routes for handling A2A requests.
@@ -179,13 +190,13 @@ def build(self) -> list[Route]:
179190 """
180191 routes = [
181192 Route (
182- self .rpc_path ,
193+ self .config . rpc_path ,
183194 self ._handle_requests ,
184195 methods = ['POST' ],
185196 name = 'a2a_handler' ,
186197 ),
187198 Route (
188- self .agent_card_path ,
199+ self .config . agent_card_path ,
189200 self ._handle_get_agent_card ,
190201 methods = ['GET' ],
191202 name = 'agent_card' ,
@@ -194,7 +205,7 @@ def build(self) -> list[Route]:
194205 if self .agent_card .supportsAuthenticatedExtendedCard :
195206 routes .append (
196207 Route (
197- self .extended_agent_card_path ,
208+ self .config . extended_agent_card_path ,
198209 self ._handle_get_authenticated_extended_agent_card ,
199210 methods = ['GET' ],
200211 name = 'authenticated_extended_agent_card' ,
@@ -247,7 +258,7 @@ class StarletteBuilder:
247258 document at the standard path /.well-known/api-catalog.json.
248259 """
249260
250- def __init__ (self ):
261+ def __init__ (self ) -> None :
251262 """Initializes an empty A2AStarletteBuilder instance.
252263
253264 This sets up the internal structure to hold multiple mounted A2A route groups
@@ -300,12 +311,14 @@ def mount(
300311 )
301312 routes = route_builder .build ()
302313 self ._mounts .append (Mount (path , routes = routes ))
303- anchor = _join_url (route_builder .agent_card .url , route_builder .rpc_path )
314+ anchor = _join_url (
315+ route_builder .agent_card .url , route_builder .config .rpc_path
316+ )
304317 describedby = [
305318 AgentLinkTarget (
306319 href = _join_url (
307320 route_builder .agent_card .url ,
308- route_builder .agent_card_path ,
321+ route_builder .config . agent_card_path ,
309322 )
310323 )
311324 ]
@@ -317,7 +330,7 @@ def mount(
317330 AgentLinkTarget (
318331 href = _join_url (
319332 route_builder .extended_agent_card .url ,
320- route_builder .extended_agent_card_path ,
333+ route_builder .config . extended_agent_card_path ,
321334 )
322335 )
323336 )
0 commit comments