1818from a2a .server .context import ServerCallContext
1919from a2a .server .request_handlers .jsonrpc_handler import JSONRPCHandler
2020from a2a .server .request_handlers .request_handler import RequestHandler
21+ from a2a .server .request_utils import update_card_rpc_url_from_request
2122from a2a .types import (
2223 A2AError ,
2324 A2ARequest ,
5859 from sse_starlette .sse import EventSourceResponse
5960 from starlette .applications import Starlette
6061 from starlette .authentication import BaseUser
61- from starlette .datastructures import URL
6262 from starlette .exceptions import HTTPException
6363 from starlette .requests import Request
6464 from starlette .responses import JSONResponse , Response
7171 from sse_starlette .sse import EventSourceResponse
7272 from starlette .applications import Starlette
7373 from starlette .authentication import BaseUser
74- from starlette .datastructures import URL
7574 from starlette .exceptions import HTTPException
7675 from starlette .requests import Request
7776 from starlette .responses import JSONResponse , Response
8988 Request = Any
9089 JSONResponse = Any
9190 Response = Any
91+ URL = Any
9292 HTTP_413_REQUEST_ENTITY_TOO_LARGE = Any
9393
9494
@@ -492,58 +492,6 @@ async def event_generator(
492492 headers = headers ,
493493 )
494494
495- def _modify_rpc_url (self , agent_card : AgentCard , request : Request ) -> None :
496- """Modifies Agent's RPC URL based on the AgentCard request.
497-
498- Args:
499- agent_card (AgentCard): Original AgentCard
500- request (Request): AgentCard request
501- """
502- rpc_url = URL (agent_card .url )
503- rpc_path = rpc_url .path
504- port = None
505- if 'X-Forwarded-Host' in request .headers :
506- host = request .headers ['X-Forwarded-Host' ]
507- else :
508- host = request .url .hostname or rpc_url .hostname or 'localhost'
509- port = request .url .port
510-
511- if 'X-Forwarded-Proto' in request .headers :
512- scheme = request .headers ['X-Forwarded-Proto' ]
513- port = None
514- else :
515- scheme = request .url .scheme
516- if not scheme :
517- scheme = 'http'
518- if ':' in host : # type: ignore
519- comps = host .rsplit (':' , 1 ) # type: ignore
520- host = comps [0 ]
521- port = int (comps [1 ]) if comps [1 ] else port
522-
523- # Handle URL maps,
524- # e.g. "agents/my-agent/.well-known/agent-card.json"
525- if 'X-Forwarded-Path' in request .headers :
526- forwarded_path = request .headers ['X-Forwarded-Path' ].strip ()
527- if (
528- forwarded_path
529- and request .url .path != forwarded_path
530- and forwarded_path .endswith (request .url .path )
531- ):
532- # "agents/my-agent" for "agents/my-agent/.well-known/agent-card.json"
533- extra_path = forwarded_path [: - len (request .url .path )]
534- new_path = extra_path + rpc_path
535- # If original path was just "/",
536- # we remove trailing "/" in the extended one
537- if len (new_path ) > 1 and rpc_path == '/' :
538- new_path = new_path .rstrip ('/' )
539- rpc_path = new_path
540-
541- agent_card .url = str (
542- rpc_url .replace (
543- hostname = host , port = port , scheme = scheme , path = rpc_path
544- )
545- )
546-
547495 async def _handle_get_agent_card (self , request : Request ) -> JSONResponse :
548496 """Handles GET requests for the agent card endpoint.
549497
@@ -567,7 +515,7 @@ async def _handle_get_agent_card(self, request: Request) -> JSONResponse:
567515 card_to_serve = self .card_modifier (card_to_serve )
568516 # If agent's RPC URL was not modified, we build it dynamically.
569517 if rpc_url == card_to_serve .url :
570- self . _modify_rpc_url (card_to_serve , request )
518+ update_card_rpc_url_from_request (card_to_serve , request )
571519
572520 return JSONResponse (
573521 card_to_serve .model_dump (
@@ -602,7 +550,7 @@ async def _handle_get_authenticated_extended_agent_card(
602550 if card_to_serve :
603551 # If agent's RPC URL was not modified, we build it dynamically.
604552 if rpc_url == card_to_serve .url :
605- self . _modify_rpc_url (card_to_serve , request )
553+ update_card_rpc_url_from_request (card_to_serve , request )
606554 return JSONResponse (
607555 card_to_serve .model_dump (
608556 exclude_none = True ,
0 commit comments