Skip to content

Commit 50fce23

Browse files
committed
formatting
1 parent 8e2352f commit 50fce23

File tree

4 files changed

+149
-140
lines changed

4 files changed

+149
-140
lines changed

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

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ async def event_generator(
487487
handler_result.root.model_dump(mode='json', exclude_none=True),
488488
headers=headers,
489489
)
490-
490+
491491
def _modify_rpc_url(self, agent_card: AgentCard, request: Request):
492492
"""Modifies Agent's RPC URL based on the AgentCard request.
493493
@@ -498,48 +498,45 @@ def _modify_rpc_url(self, agent_card: AgentCard, request: Request):
498498
rpc_url = URL(agent_card.url)
499499
rpc_path = rpc_url.path
500500
port = None
501-
if "X-Forwarded-Host" in request.headers:
502-
host = request.headers["X-Forwarded-Host"]
501+
if 'X-Forwarded-Host' in request.headers:
502+
host = request.headers['X-Forwarded-Host']
503503
else:
504504
host = request.url.hostname
505505
port = request.url.port
506-
507-
if "X-Forwarded-Proto" in request.headers:
508-
scheme = request.headers["X-Forwarded-Proto"]
506+
507+
if 'X-Forwarded-Proto' in request.headers:
508+
scheme = request.headers['X-Forwarded-Proto']
509509
port = None
510510
else:
511511
scheme = request.url.scheme
512512
if not scheme:
513-
scheme = "http"
514-
if ":" in host: # type: ignore
515-
comps = host.rsplit(":", 1) # type: ignore
513+
scheme = 'http'
514+
if ':' in host: # type: ignore
515+
comps = host.rsplit(':', 1) # type: ignore
516516
host = comps[0]
517517
port = comps[1]
518518

519519
# Handle URL maps,
520520
# e.g. "agents/my-agent/.well-known/agent-card.json"
521-
if "X-Forwarded-Path" in request.headers:
522-
forwarded_path = request.headers["X-Forwarded-Path"].strip()
521+
if 'X-Forwarded-Path' in request.headers:
522+
forwarded_path = request.headers['X-Forwarded-Path'].strip()
523523
if (
524-
forwarded_path and
525-
request.url.path != forwarded_path
524+
forwarded_path
525+
and request.url.path != forwarded_path
526526
and forwarded_path.endswith(request.url.path)
527527
):
528528
# "agents/my-agent" for "agents/my-agent/.well-known/agent-card.json"
529-
extra_path = forwarded_path[:-len(request.url.path)]
529+
extra_path = forwarded_path[: -len(request.url.path)]
530530
new_path = extra_path + rpc_path
531531
# If original path was just "/",
532532
# we remove trailing "/" in the the extended one
533-
if len(new_path) > 1 and rpc_path == "/":
534-
new_path = new_path.rstrip("/")
533+
if len(new_path) > 1 and rpc_path == '/':
534+
new_path = new_path.rstrip('/')
535535
rpc_path = new_path
536536

537537
agent_card.url = str(
538538
rpc_url.replace(
539-
hostname=host,
540-
port=port,
541-
scheme=scheme,
542-
path=rpc_path
539+
hostname=host, port=port, scheme=scheme, path=rpc_path
543540
)
544541
)
545542

src/a2a/server/apps/rest/rest_adapter.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ async def handle_authenticated_agent_card(
152152
return JSONResponse(
153153
self.agent_card.model_dump(mode='json', exclude_none=True)
154154
)
155-
155+
156156
def _modify_rpc_url(self, agent_card: AgentCard, request: Request):
157157
"""Modifies Agent's RPC URL based on the AgentCard request.
158158
@@ -163,48 +163,45 @@ def _modify_rpc_url(self, agent_card: AgentCard, request: Request):
163163
rpc_url = URL(agent_card.url)
164164
rpc_path = rpc_url.path
165165
port = None
166-
if "X-Forwarded-Host" in request.headers:
167-
host = request.headers["X-Forwarded-Host"]
166+
if 'X-Forwarded-Host' in request.headers:
167+
host = request.headers['X-Forwarded-Host']
168168
else:
169169
host = request.url.hostname
170170
port = request.url.port
171-
172-
if "X-Forwarded-Proto" in request.headers:
173-
scheme = request.headers["X-Forwarded-Proto"]
171+
172+
if 'X-Forwarded-Proto' in request.headers:
173+
scheme = request.headers['X-Forwarded-Proto']
174174
port = None
175175
else:
176176
scheme = request.url.scheme
177177
if not scheme:
178-
scheme = "http"
179-
if ":" in host: # type: ignore
180-
comps = host.rsplit(":", 1) # type: ignore
178+
scheme = 'http'
179+
if ':' in host: # type: ignore
180+
comps = host.rsplit(':', 1) # type: ignore
181181
host = comps[0]
182182
port = comps[1]
183183

184184
# Handle URL maps,
185185
# e.g. "agents/my-agent/.well-known/agent-card.json"
186-
if "X-Forwarded-Path" in request.headers:
187-
forwarded_path = request.headers["X-Forwarded-Path"].strip()
186+
if 'X-Forwarded-Path' in request.headers:
187+
forwarded_path = request.headers['X-Forwarded-Path'].strip()
188188
if (
189-
forwarded_path and
190-
request.url.path != forwarded_path
189+
forwarded_path
190+
and request.url.path != forwarded_path
191191
and forwarded_path.endswith(request.url.path)
192192
):
193193
# "agents/my-agent" for "agents/my-agent/.well-known/agent-card.json"
194-
extra_path = forwarded_path[:-len(request.url.path)]
194+
extra_path = forwarded_path[: -len(request.url.path)]
195195
new_path = extra_path + rpc_path
196196
# If original path was just "/",
197197
# we remove trailing "/" in the the extended one
198-
if len(new_path) > 1 and rpc_path == "/":
199-
new_path = new_path.rstrip("/")
198+
if len(new_path) > 1 and rpc_path == '/':
199+
new_path = new_path.rstrip('/')
200200
rpc_path = new_path
201201

202202
agent_card.url = str(
203203
rpc_url.replace(
204-
hostname=host,
205-
port=port,
206-
scheme=scheme,
207-
path=rpc_path
204+
hostname=host, port=port, scheme=scheme, path=rpc_path
208205
)
209206
)
210207

tests/server/apps/jsonrpc/test_jsonrpc_app.py

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
)
4040
from a2a.utils.constants import AGENT_CARD_WELL_KNOWN_PATH
4141

42+
4243
# --- StarletteUserProxy Tests ---
4344

4445

@@ -358,56 +359,62 @@ def side_effect(request, context: ServerCallContext):
358359

359360

360361
class TestAgentCardHandler:
361-
@pytest.fixture
362-
def agent_card(self):
363-
return AgentCard(
364-
name='APIKeyAgent',
365-
description='An agent that uses API Key auth.',
366-
url='http://localhost:8000',
367-
version='1.0.0',
368-
capabilities=AgentCapabilities(),
369-
default_input_modes=['text/plain'],
370-
default_output_modes=['text/plain'],
371-
skills=[],
372-
)
362+
@pytest.fixture
363+
def agent_card(self):
364+
return AgentCard(
365+
name='APIKeyAgent',
366+
description='An agent that uses API Key auth.',
367+
url='http://localhost:8000',
368+
version='1.0.0',
369+
capabilities=AgentCapabilities(),
370+
default_input_modes=['text/plain'],
371+
default_output_modes=['text/plain'],
372+
skills=[],
373+
)
373374

374-
def test_agent_card_url_rewriting(
375-
self, agent_card: AgentCard,
376-
):
377-
"""
378-
Tests that the A2AStarletteApplication endpoint correctly handles Agent URL rewriting.
379-
"""
380-
handler = AsyncMock()
381-
app_instance = A2AStarletteApplication(agent_card, handler)
382-
client = TestClient(
383-
app_instance.build(),
384-
base_url="https://my-agents.com:5000"
385-
)
375+
def test_agent_card_url_rewriting(
376+
self,
377+
agent_card: AgentCard,
378+
):
379+
"""
380+
Tests that the A2AStarletteApplication endpoint correctly handles Agent URL rewriting.
381+
"""
382+
handler = AsyncMock()
383+
app_instance = A2AStarletteApplication(agent_card, handler)
384+
client = TestClient(
385+
app_instance.build(), base_url='https://my-agents.com:5000'
386+
)
386387

387-
response = client.get(AGENT_CARD_WELL_KNOWN_PATH)
388-
response.raise_for_status()
389-
assert response.json()["url"] == "https://my-agents.com:5000"
390-
391-
response = client.get(
392-
AGENT_CARD_WELL_KNOWN_PATH,
393-
headers={
394-
"X-Forwarded-Host": "my-great-agents.com:5678",
395-
"X-Forwarded-Proto": "http",
396-
"X-Forwarded-Path":
397-
"/agents/my-agent" + AGENT_CARD_WELL_KNOWN_PATH
398-
}
399-
)
400-
assert response.json()["url"] == "http://my-great-agents.com:5678/agents/my-agent"
388+
response = client.get(AGENT_CARD_WELL_KNOWN_PATH)
389+
response.raise_for_status()
390+
assert response.json()['url'] == 'https://my-agents.com:5000'
391+
392+
response = client.get(
393+
AGENT_CARD_WELL_KNOWN_PATH,
394+
headers={
395+
'X-Forwarded-Host': 'my-great-agents.com:5678',
396+
'X-Forwarded-Proto': 'http',
397+
'X-Forwarded-Path': '/agents/my-agent'
398+
+ AGENT_CARD_WELL_KNOWN_PATH,
399+
},
400+
)
401+
assert (
402+
response.json()['url']
403+
== 'http://my-great-agents.com:5678/agents/my-agent'
404+
)
401405

402-
client = TestClient(
403-
app_instance.build(
404-
agent_card_url="/agents/my-agent" + AGENT_CARD_WELL_KNOWN_PATH
405-
),
406-
base_url="https://my-mighty-agents.com"
407-
)
406+
client = TestClient(
407+
app_instance.build(
408+
agent_card_url='/agents/my-agent' + AGENT_CARD_WELL_KNOWN_PATH
409+
),
410+
base_url='https://my-mighty-agents.com',
411+
)
408412

409-
response = client.get("/agents/my-agent" + AGENT_CARD_WELL_KNOWN_PATH)
410-
assert response.json()["url"] == "https://my-mighty-agents.com/agents/my-agent"
413+
response = client.get('/agents/my-agent' + AGENT_CARD_WELL_KNOWN_PATH)
414+
assert (
415+
response.json()['url']
416+
== 'https://my-mighty-agents.com/agents/my-agent'
417+
)
411418

412419

413420
if __name__ == '__main__':

tests/server/apps/rest/test_rest_fastapi_app.py

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
)
2828
from a2a.utils.constants import AGENT_CARD_WELL_KNOWN_PATH
2929

30+
3031
logger = logging.getLogger(__name__)
3132

3233

@@ -224,60 +225,67 @@ async def test_send_message_success_task(
224225

225226

226227
class TestAgentCardHandler:
227-
@pytest.fixture
228-
def agent_card(self):
229-
return AgentCard(
230-
name='APIKeyAgent',
231-
description='An agent that uses API Key auth.',
232-
url='http://localhost:8000',
233-
version='1.0.0',
234-
capabilities=AgentCapabilities(),
235-
default_input_modes=['text/plain'],
236-
default_output_modes=['text/plain'],
237-
skills=[],
238-
)
239-
240-
@pytest.mark.anyio
241-
async def test_agent_card_url_rewriting(
242-
self, agent_card: AgentCard,
243-
):
244-
"""
245-
Tests that the REST endpoint correctly handles Agent URL rewriting.
246-
"""
247-
app_instance = A2ARESTFastAPIApplication(agent_card, AsyncMock())
248-
app = app_instance.build(
249-
agent_card_url=AGENT_CARD_WELL_KNOWN_PATH
250-
)
251-
client = AsyncClient(
252-
transport=ASGITransport(app=app),
253-
base_url="https://my-agents.com:5000"
254-
)
255-
256-
response = await client.get(AGENT_CARD_WELL_KNOWN_PATH)
257-
response.raise_for_status()
258-
assert response.json()["url"] == "https://my-agents.com:5000"
259-
260-
response = await client.get(
261-
AGENT_CARD_WELL_KNOWN_PATH,
262-
headers={
263-
"X-Forwarded-Host": "my-great-agents.com:5678",
264-
"X-Forwarded-Proto": "http",
265-
"X-Forwarded-Path":
266-
"/agents/my-agent" + AGENT_CARD_WELL_KNOWN_PATH
267-
}
268-
)
269-
assert response.json()["url"] == "http://my-great-agents.com:5678/agents/my-agent"
270-
271-
app = app_instance.build(
272-
agent_card_url="/agents/my-agent" + AGENT_CARD_WELL_KNOWN_PATH
273-
)
274-
client = AsyncClient(
275-
transport=ASGITransport(app=app),
276-
base_url="https://my-mighty-agents.com"
277-
)
278-
279-
response = await client.get("/agents/my-agent" + AGENT_CARD_WELL_KNOWN_PATH)
280-
assert response.json()["url"] == "https://my-mighty-agents.com/agents/my-agent"
228+
@pytest.fixture
229+
def agent_card(self):
230+
return AgentCard(
231+
name='APIKeyAgent',
232+
description='An agent that uses API Key auth.',
233+
url='http://localhost:8000',
234+
version='1.0.0',
235+
capabilities=AgentCapabilities(),
236+
default_input_modes=['text/plain'],
237+
default_output_modes=['text/plain'],
238+
skills=[],
239+
)
240+
241+
@pytest.mark.anyio
242+
async def test_agent_card_url_rewriting(
243+
self,
244+
agent_card: AgentCard,
245+
):
246+
"""
247+
Tests that the REST endpoint correctly handles Agent URL rewriting.
248+
"""
249+
app_instance = A2ARESTFastAPIApplication(agent_card, AsyncMock())
250+
app = app_instance.build(agent_card_url=AGENT_CARD_WELL_KNOWN_PATH)
251+
client = AsyncClient(
252+
transport=ASGITransport(app=app),
253+
base_url='https://my-agents.com:5000',
254+
)
255+
256+
response = await client.get(AGENT_CARD_WELL_KNOWN_PATH)
257+
response.raise_for_status()
258+
assert response.json()['url'] == 'https://my-agents.com:5000'
259+
260+
response = await client.get(
261+
AGENT_CARD_WELL_KNOWN_PATH,
262+
headers={
263+
'X-Forwarded-Host': 'my-great-agents.com:5678',
264+
'X-Forwarded-Proto': 'http',
265+
'X-Forwarded-Path': '/agents/my-agent'
266+
+ AGENT_CARD_WELL_KNOWN_PATH,
267+
},
268+
)
269+
assert (
270+
response.json()['url']
271+
== 'http://my-great-agents.com:5678/agents/my-agent'
272+
)
273+
274+
app = app_instance.build(
275+
agent_card_url='/agents/my-agent' + AGENT_CARD_WELL_KNOWN_PATH
276+
)
277+
client = AsyncClient(
278+
transport=ASGITransport(app=app),
279+
base_url='https://my-mighty-agents.com',
280+
)
281+
282+
response = await client.get(
283+
'/agents/my-agent' + AGENT_CARD_WELL_KNOWN_PATH
284+
)
285+
assert (
286+
response.json()['url']
287+
== 'https://my-mighty-agents.com/agents/my-agent'
288+
)
281289

282290

283291
if __name__ == '__main__':

0 commit comments

Comments
 (0)