Skip to content

[Feat]: ephemeral port support #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
codefromthecrypt opened this issue May 14, 2025 · 1 comment
Open
1 task done

[Feat]: ephemeral port support #4

codefromthecrypt opened this issue May 14, 2025 · 1 comment

Comments

@codefromthecrypt
Copy link

Is your feature request related to a problem? Please describe.

Right now, we need to supply a URL in the agent card, which means you already know its host and port. This is fine for static ports, but especially in small demos or e2e tests is less elegant.

Describe the solution you'd like

it would be nice to use port=0 and resolve AgentCard.url based on the bound port. This could be automatic, for example, to backfill it when absent with the uvicorn address

Describe alternatives you've considered

search for an open port and use that, or patch A2AStarletteApplication or JSONRPCHandler

Additional context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@codefromthecrypt
Copy link
Author

For now, I use an explicit port like this, so that I can pass a value in the agent card which is coherent with the later port actually used.

# TODO: remove after https://github.com/google/a2a-python/issues/4
default_port = 10000

--snip--

        return AgentCard(
            name="ElasticsearchVersionAgent",
            description="Answers questions about Elasticsearch versions.",
            version="1.0.0",
            url=f"http://localhost:{default_port}/",
--snip--

    app = A2AStarletteApplication(agent_card=agent.card(), http_handler=request_handler).build()

    # For convenience, launch the server in the same process as the client.
    async with serve_on_default_port(app):
--snip--

@asynccontextmanager
async def serve_on_default_port(app: Callable[..., Any]) -> AsyncIterator[None]:
    """Registers the app and serves it on the default port."""

    config = uvicorn.Config(app, host="127.0.0.1", port=default_port, log_level="error")
    server = uvicorn.Server(config)
    task = asyncio.create_task(server.serve())

    while not (server and server.started):
        await asyncio.sleep(0.01)
    try:
        yield
    finally:
        server.should_exit = True
        await server.shutdown()
        task.cancel()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant