-
Notifications
You must be signed in to change notification settings - Fork 2k
Python: feat: Add Agent Framework to A2A bridge support #2403
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
Merged
moonbox3
merged 53 commits into
microsoft:main
from
Shubham-Kumar-2000:agent_framework_to_a2a
Apr 24, 2026
Merged
Changes from 8 commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
298a443
feat: Add Agent Framework to A2A bridge support
Shubham-Kumar-2000 828b733
fix: Update references from agent_thread_storage to _agent_thread_sto…
Shubham-Kumar-2000 255ee71
Refactor A2A agent framework and improve code structure
Shubham-Kumar-2000 1e600dc
fix: Lint fix new line added
Shubham-Kumar-2000 c7a8b80
test: Add unit tests for AgentThreadStorage and InMemoryAgentThreadSt…
Shubham-Kumar-2000 db5bdf3
refactor: Update type hints to use new syntax for Union and List
Shubham-Kumar-2000 a3a5c48
fix: Validate RequestContext for context_id and message before execution
Shubham-Kumar-2000 4c43248
Refactor tests and remove A2aExecutionContext references
Shubham-Kumar-2000 cc2df5c
Refactor A2AExecutor tests and remove event adapter
Shubham-Kumar-2000 c732a05
refactor: Remove AgentThreadStorage and InMemoryAgentThreadStorage cl…
Shubham-Kumar-2000 64c1cfb
feat: A2AExecutor to have its own override able save and get threads …
Shubham-Kumar-2000 1f8a002
fix: linter bugs
Shubham-Kumar-2000 293f25d
Merge branch 'main' of https://github.com/Shubham-Kumar-2000/agent-fr…
Shubham-Kumar-2000 e3bb889
removed unnecessary changes form core package
Shubham-Kumar-2000 960bb22
new line added
Shubham-Kumar-2000 ed01679
Merge branch 'main' of https://github.com/microsoft/agent-framework i…
Shubham-Kumar-2000 f8ee82b
Refactor A2AExecutor tests and update imports
Shubham-Kumar-2000 f14e7c4
Update A2A documentation: enhance usage examples for A2AAgent and A2A…
Shubham-Kumar-2000 9d84791
Updated uv lock
Shubham-Kumar-2000 6e61d13
Fix metadata assertion in TestA2AExecutorHandleEvents and reorder loa…
Shubham-Kumar-2000 14b17d8
Update agent card configuration: add default input and output modes, …
Shubham-Kumar-2000 ae34dcf
Fix assertion for metadata in TestA2AExecutorHandleEvents
Shubham-Kumar-2000 4674494
Fix formatting issues in TestA2AExecutorExecute and TestA2AExecutorIn…
Shubham-Kumar-2000 7022f7f
Enhance A2AExecutor documentation with examples and clarify agent exe…
Shubham-Kumar-2000 1c7a196
Merge branch 'main' into agent_framework_to_a2a
Shubham-Kumar-2000 25c5528
Merge branch 'main' of https://github.com/microsoft/agent-framework i…
Shubham-Kumar-2000 556f1d3
Merge branch 'agent_framework_to_a2a' of https://github.com/Shubham-K…
Shubham-Kumar-2000 8c9feba
Merge branch 'main' of https://github.com/microsoft/agent-framework i…
Shubham-Kumar-2000 568fbd5
Revert uv lock to main
Shubham-Kumar-2000 c2f09c3
Refactor A2AExecutor: Improve formatting and streamline constructor p…
Shubham-Kumar-2000 46c6dcf
Merge branch 'main' into agent_framework_to_a2a
Shubham-Kumar-2000 9791458
Merge branch 'main' into agent_framework_to_a2a
Shubham-Kumar-2000 4d3cf2b
Merge branch 'main' into agent_framework_to_a2a
Shubham-Kumar-2000 50f8e83
Apply suggestions from code review
Shubham-Kumar-2000 8e31511
Refactor A2AExecutor to use SupportsAgentRun and enhance logging; upd…
Shubham-Kumar-2000 0297c6f
Merge branch 'main' into agent_framework_to_a2a
eavanvalkenburg 327352f
Enhance A2AExecutor with streaming support and custom run arguments; …
Shubham-Kumar-2000 e5b42a9
Merge branch 'agent_framework_to_a2a' of https://github.com/Shubham-K…
Shubham-Kumar-2000 e857572
Enhance A2AExecutor event handling with streamed artifact tracking; u…
Shubham-Kumar-2000 81032f8
Merge branch 'main' into agent_framework_to_a2a
Shubham-Kumar-2000 59dea33
Refactor A2AExecutor to enforce type hints for stream and run_kwargs …
Shubham-Kumar-2000 c1340d3
Merge branch 'agent_framework_to_a2a' of https://github.com/Shubham-K…
Shubham-Kumar-2000 fad2e82
Merge branch 'main' into agent_framework_to_a2a
Shubham-Kumar-2000 dae1c19
Refactor A2AExecutor and tests: replace AsyncMock with MagicMock for …
Shubham-Kumar-2000 877cefd
Merge branch 'agent_framework_to_a2a' of https://github.com/Shubham-K…
Shubham-Kumar-2000 2ce08eb
Merge branch 'main' into agent_framework_to_a2a
Shubham-Kumar-2000 39b1491
refactor: streamline imports and improve code readability across mult…
Shubham-Kumar-2000 6a5f29d
Merge branch 'main' into agent_framework_to_a2a
Shubham-Kumar-2000 9bb1fae
feat: enhance A2AExecutor cancel method with context validation and f…
Shubham-Kumar-2000 0257d01
feat: implement get_uri_data utility function for extracting base64 d…
Shubham-Kumar-2000 1e5766c
fix: update import path for get_uri_data utility function in A2AExecu…
Shubham-Kumar-2000 9889e68
fix: correct error message handling in A2AExecutor and update test as…
Shubham-Kumar-2000 0f46d52
Merge branch 'main' into agent_framework_to_a2a
Shubham-Kumar-2000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
422 changes: 422 additions & 0 deletions
422
python/packages/a2a/agent_framework_a2a/_a2a_event_adapter.py
Large diffs are not rendered by default.
Oops, something went wrong.
164 changes: 164 additions & 0 deletions
164
python/packages/a2a/agent_framework_a2a/_a2a_executor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| from asyncio import CancelledError | ||
|
|
||
| from a2a.server.agent_execution import AgentExecutor, RequestContext | ||
| from a2a.server.events import EventQueue | ||
| from a2a.server.tasks import TaskUpdater | ||
| from a2a.types import Part, Task, TaskState, TextPart | ||
| from a2a.utils import new_task | ||
| from agent_framework import ( | ||
| AgentThread, | ||
| AgentThreadStorage, | ||
| ChatAgent, | ||
| ChatMessage, | ||
| InMemoryAgentThreadStorage, | ||
| Role, | ||
| WorkflowAgent, | ||
| ) | ||
|
|
||
| from ._a2a_event_adapter import A2aEventAdapter, BaseA2aEventAdapter | ||
|
|
||
|
|
||
| class A2aExecutor(AgentExecutor): | ||
| """Execute AI agents using the A2A (Agent-to-Agent) protocol. | ||
|
|
||
| The A2aExecutor bridges AI agents built with the agent_framework library and the A2A protocol, | ||
| enabling structured agent execution with event-driven communication. It manages agent threads, | ||
| handles execution contexts, and converts agent responses into A2A protocol events. | ||
|
|
||
| The executor supports both ChatAgent and WorkflowAgent types, persists conversation threads | ||
| using configurable storage, and provides comprehensive error handling with task status updates. | ||
|
|
||
| Args: | ||
| agent_thread_storage (AgentThreadStorage): Storage backend for persisting agent conversation threads. | ||
| Defaults to InMemoryAgentThreadStorage if not provided. | ||
| agent (Union[ChatAgent, WorkflowAgent]): The AI agent to execute. | ||
| event_adapter (A2aEventAdapter): Adapter for converting agent responses to A2A protocol events. | ||
| Defaults to BaseA2aEventAdapter if not provided. | ||
|
|
||
| Example: | ||
| Basic usage with a ChatAgent: | ||
|
|
||
| >>> from agent_framework import ChatAgent | ||
| >>> from agent_framework.openai import OpenAIResponsesClient | ||
| >>> from a2a.server.events import EventQueue | ||
| >>> from a2a.server.agent_execution import RequestContext | ||
| >>> | ||
| >>> # Initialize the AI client and agent | ||
| >>> agent = OpenAIResponsesClient().create_agent( | ||
| ... name="WeatherBot", | ||
| ... instructions="You are a helpful weather assistant.", | ||
| ... ) | ||
| >>> | ||
| >>> # Create the executor with default event adapter and storage | ||
| >>> executor = A2aExecutor(agent=agent) | ||
| >>> | ||
| >>> # Execute an agent within an async context | ||
| >>> async def run_example(): | ||
| ... event_queue = EventQueue() | ||
| ... request_context = RequestContext(user_input="What is the weather today?", context_id="user-123") | ||
| ... await executor.execute(request_context, event_queue) | ||
| ... # Events are queued in event_queue for processing | ||
|
|
||
| Custom event adapter and persistent storage: | ||
|
|
||
| >>> from agent_framework import InMemoryAgentThreadStorage | ||
| >>> from agent_framework.a2a import BaseA2aEventAdapter | ||
| >>> | ||
| >>> custom_adapter = BaseA2aEventAdapter() | ||
| >>> storage = InMemoryAgentThreadStorage() | ||
| >>> | ||
| >>> executor = A2aExecutor(agent=agent, event_adapter=custom_adapter, agent_thread_storage=storage) | ||
| >>> | ||
| >>> # The same context_id will reuse the same conversation thread | ||
| >>> async def run_multi_turn(): | ||
| ... event_queue = EventQueue() | ||
| ... context_id = "conversation-session-1" | ||
| ... | ||
| ... # First turn | ||
| ... request1 = RequestContext(user_input="Hello, who are you?", context_id=context_id) | ||
| ... await executor.execute(request1, event_queue) | ||
| ... | ||
| ... # Second turn (same conversation thread) | ||
| ... request2 = RequestContext(user_input="What did I just ask?", context_id=context_id) | ||
| ... await executor.execute(request2, event_queue) | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| agent: ChatAgent | WorkflowAgent, | ||
| event_adapter: A2aEventAdapter | None = None, | ||
| agent_thread_storage: AgentThreadStorage | None = None, | ||
| ): | ||
| """Initialize the A2aExecutor. | ||
|
|
||
| Args: | ||
| agent: The AI agent to execute. Can be a ChatAgent for conversational tasks | ||
| or a WorkflowAgent for multistep agent workflows. | ||
| event_adapter: Optional adapter for converting agent responses to A2A protocol events. | ||
| If not provided, BaseA2aEventAdapter is used. | ||
| agent_thread_storage: Optional storage backend for persisting agent conversation threads. | ||
| If not provided, InMemoryAgentThreadStorage is used. For production systems, | ||
| consider using persistent storage to maintain conversation state across restarts. | ||
| """ | ||
| super().__init__() | ||
| self._agent_thread_storage: AgentThreadStorage = ( | ||
| agent_thread_storage if agent_thread_storage else InMemoryAgentThreadStorage() | ||
| ) | ||
| self._agent: ChatAgent | WorkflowAgent = agent | ||
| self._event_adapter: A2aEventAdapter = event_adapter if event_adapter else BaseA2aEventAdapter() | ||
|
|
||
| async def cancel(self, context: RequestContext, event_queue: EventQueue) -> None: | ||
|
Shubham-Kumar-2000 marked this conversation as resolved.
|
||
| # Cancellation handled at A2A protocol level | ||
| pass | ||
|
|
||
|
moonbox3 marked this conversation as resolved.
Outdated
|
||
| async def get_agent_thread(self, task: Task) -> AgentThread: | ||
| """Get or create the agent thread for the given context.""" | ||
| thread = await self._agent_thread_storage.load_thread(task.context_id) | ||
| if not thread: | ||
| thread = self._agent.get_new_thread() | ||
| await self._agent_thread_storage.save_thread(task.context_id, thread) | ||
| return thread | ||
|
|
||
| async def execute(self, context: RequestContext, event_queue: EventQueue) -> None: | ||
| """Execute the agent with the given context and event queue.""" | ||
| if context.context_id is None: | ||
| raise ValueError("Context ID must be provided in the RequestContext") | ||
| if context.message is None: | ||
| raise ValueError("Message must be provided in the RequestContext") | ||
|
|
||
| query = context.get_user_input() | ||
| task = context.current_task | ||
|
|
||
| if not task: | ||
| task = new_task(context.message) | ||
| await event_queue.enqueue_event(task) | ||
|
|
||
| updater = TaskUpdater(event_queue, task.id, context.context_id) | ||
| await updater.submit() | ||
|
|
||
| agent_thread = await self.get_agent_thread(task) | ||
| agent = self._agent | ||
| try: | ||
| await updater.start_work() | ||
| # Create a ChatMessage from the query | ||
| user_message = ChatMessage(role=Role.USER, text=query) | ||
|
|
||
| # Run the agent with the message | ||
| response = await agent.run(user_message, thread=agent_thread) | ||
| response_messages = response.messages | ||
| if not isinstance(response_messages, list): | ||
| response_messages = [response_messages] | ||
| for message in response_messages: | ||
| await self._event_adapter.handle_events(message, updater) | ||
| # Mark as complete | ||
| await updater.complete() | ||
| except CancelledError: | ||
| await updater.update_status(state=TaskState.canceled, final=True) | ||
| except Exception as e: | ||
| await updater.update_status( | ||
| state=TaskState.failed, | ||
| final=True, | ||
| message=updater.new_agent_message([Part(root=TextPart(text=str(e.args)))]), | ||
|
Shubham-Kumar-2000 marked this conversation as resolved.
Outdated
|
||
| ) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.