Skip to content

Python: Add A2A server sample#4528

Open
giles17 wants to merge 2 commits intomicrosoft:mainfrom
giles17:python-a2a-server
Open

Python: Add A2A server sample#4528
giles17 wants to merge 2 commits intomicrosoft:mainfrom
giles17:python-a2a-server

Conversation

@giles17
Copy link
Contributor

@giles17 giles17 commented Mar 6, 2026

Summary

Adds a pure Python A2A server sample so testing the A2A client no longer requires running the .NET server. Also fixes a streaming bug in the existing client sample.

New Files

  • a2a_server.py — Main server entry point using a2a-sdk's A2AStarletteApplication + uvicorn, with CLI args for --agent-type (invoice/policy/logistics) and --port
  • agent_executor.py — Bridges the a2a-sdk AgentExecutor interface to Agent Framework agents
  • agent_definitions.py — Agent factory functions and AgentCard definitions for three agent types
  • invoice_data.py — Mock invoice data (10 invoices) and 3 @tool query functions
  • a2a_server.http — REST Client requests for testing the server from VS Code

Bug Fix

Fixed agent_with_a2a.py streaming: ResponseStream doesn't implement the async context manager protocol, so async with was replaced with async for (matching all other samples).

Verified

  • Syntax check passes (0 errors, 0 warnings)
  • End-to-end test: server starts, serves AgentCard at /.well-known/agent.json, client connects with both non-streaming and streaming responses
  • No new dependencies added

Closes #4045

Add a pure Python A2A server sample so testing the A2A client no longer
requires running the .NET server. The server uses the a2a-sdk's
A2AStarletteApplication with uvicorn and supports three agent types
(invoice, policy, logistics) backed by AzureOpenAIResponsesClient.

New files:
- a2a_server.py: Main server entry point with CLI args
- agent_executor.py: Bridges a2a-sdk AgentExecutor to Agent Framework
- agent_definitions.py: Agent and AgentCard factory definitions
- invoice_data.py: Mock invoice data and query tool functions
- a2a_server.http: REST Client requests for testing

Also fixes a streaming bug in agent_with_a2a.py where async with was
used on ResponseStream which does not support the async context manager
protocol. Changed to async for to match all other samples.

Closes microsoft#4045

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 6, 2026 17:05
@markwallace-microsoft markwallace-microsoft added documentation Improvements or additions to documentation python labels Mar 6, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Python A2A server sample under python/samples/04-hosting/a2a to enable testing the Python A2A client without requiring the .NET server, and fixes the streaming usage in the existing Python A2A client sample.

Changes:

  • Introduces a runnable A2A server entry point (a2a_server.py) backed by a2a-sdk + uvicorn, with agent-type selection.
  • Adds supporting server modules: an AgentExecutor bridge, Agent/AgentCard factories, and mock invoice data + tool functions.
  • Fixes the client streaming sample by iterating the ResponseStream with async for instead of using async with.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
python/samples/04-hosting/a2a/a2a_server.py New Python A2A server entry point (uvicorn + A2AStarletteApplication) wiring AgentCard + request handler.
python/samples/04-hosting/a2a/agent_executor.py Implements an AgentExecutor bridge from a2a-sdk requests to Agent Framework agent.run().
python/samples/04-hosting/a2a/agent_definitions.py Defines invoice/policy/logistics agent factories and AgentCard factories.
python/samples/04-hosting/a2a/invoice_data.py Adds mock invoice dataset and query tools used by the invoice agent.
python/samples/04-hosting/a2a/agent_with_a2a.py Fixes streaming consumption pattern for ResponseStream.
python/samples/04-hosting/a2a/a2a_server.http Adds REST Client requests for manual testing of the server endpoints.
python/samples/04-hosting/a2a/README.md Updates documentation to include server + client quickstart and file descriptions.

You can also share your feedback on Copilot code review. Take the survey.

- Re-raise asyncio.CancelledError before the broad exception handler
  so cooperative cancellation is not swallowed.
- Make end_date filter inclusive of the full day by comparing with
  < end + timedelta(days=1) instead of <= midnight.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Feature]: Python sample for creating A2A server

3 participants