fix: update agent card URL when host/port overridden in A2AServer.ser… #1626
+164
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a bug where specifying
hostandportparameters inA2AServer.serve()would start the server on the correct port, but the agent card (returned at/.well-known/agent-card.json) would still contain the original URL from the constructor defaults. This caused A2A clients to fail with HTTP 503 errors when trying to send messages, as they would attempt to connect to the wrong host/port.Root Cause
The
serve()method was passing the overriddenhostandportto uvicorn, but it wasn't updating thehttp_urlandpublic_base_urlattributes that are used to generate the agent card. As a result, clients would:Solution
_http_url_explicitflag in__init__()to track whether the user explicitly provided anhttp_urlparameter (e.g., for load balancer scenarios where the public URL differs from the local binding)serve()to updateself.host,self.port,self.http_url, andself.public_base_urlwhen host/port are overridden, but only ifhttp_urlwasn't explicitly set in the constructorThis preserves backwards compatibility and ensures that:
serve()get the correct agent card URLhttp_urlfor load balancer scenarios don't have their public URL overwrittenRelated Issues
Fixes #1258
Documentation PR
N/A - This is a bug fix that restores expected behavior. No documentation changes are needed.
Type of Change
Bug fix
Testing
How have you tested the change?
Unit Tests:
Added 6 new unit tests to verify the fix:
test_serve_with_overridden_host_port_updates_agent_card_url- Verifies the main bug fix (issue [BUG] specifying host, port in A2AServer.serve(...) is broken #1258)test_serve_with_overridden_port_only_updates_url- Tests port-only overridetest_serve_with_overridden_host_only_updates_url- Tests host-only overridetest_serve_with_explicit_http_url_does_not_override_url- Ensures explicithttp_urlis preserved for load balancer scenariostest_serve_without_overrides_does_not_change_url- Verifies no changes when no overrides providedtest_http_url_explicit_flag_set_correctly- Tests the new flag initializationAll 60 tests in
tests/strands/multiagent/a2a/test_server.pypassManual testing:
Created a test server with default config and called
serve(host="localhost", port=9210)Verified the agent card at
/.well-known/agent.jsonreturns"url": "http://localhost:9210/"(correctly matching the overridden port)Verified the A2A client library (
a2a.client.A2AClient) successfully retrieves the agent card with the correct URLI ran
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.