A comprehensive Python library for Google's Agent-to-Agent (A2A) protocol
Project description
Python A2A
The Definitive Python Implementation of Google's Agent-to-Agent (A2A) Protocol with Model Context Protocol (MCP) Integration
🌟 Overview
Python A2A is a comprehensive, production-ready library for implementing Google's Agent-to-Agent (A2A) protocol with full support for the Model Context Protocol (MCP). It provides everything you need to build interoperable AI agent ecosystems that can collaborate seamlessly to solve complex problems.
The A2A protocol establishes a standard communication format that enables AI agents to interact regardless of their underlying implementation, while MCP extends this capability by providing a standardized way for agents to access external tools and data sources. Python A2A makes these protocols accessible with an intuitive API that developers of all skill levels can use to build sophisticated multi-agent systems.
📋 What's New in v0.5.X
- LangChain Integration: Seamless integration with LangChain's tools and agents
- Expanded Tool Ecosystem: Use tools from both LangChain and MCP in any agent
- Enhanced Agent Interoperability: Convert between A2A agents and LangChain agents
- Mixed Workflow Engine: Build workflows combining both ecosystems
- Simplified Agent Development: Access thousands of pre-built tools instantly
- Advanced Streaming Architecture: Enhanced streaming with Server-Sent Events (SSE), better error handling, and robust fallback mechanisms
- Task-Based Streaming: New
tasks_send_subscribemethod for streaming task updates in real-time - Streaming Chunks API: Improved chunk processing with the
StreamingChunkclass for structured streaming data - Multi-Endpoint Support: Automatic discovery and fallback across multiple streaming endpoints
📋 What's New in v0.4.X
- Agent Network System: Manage and discover multiple agents with the new
AgentNetworkclass - Real-time Streaming: Implement streaming responses with
StreamingClientfor responsive UIs - Workflow Engine: Define complex multi-agent workflows using the new fluent API with conditional branching and parallel execution
- AI-Powered Router: Automatically route queries to the most appropriate agent with the
AIAgentRouter - Command Line Interface: Control your agents from the terminal with the new CLI tool
- Enhanced Asynchronous Support: Better async/await support throughout the library
- New Connection Options: Improved error handling and retry logic for more robust agent communication
✨ Why Choose Python A2A?
- Complete Implementation: Fully implements the official A2A specification with zero compromises
- MCP Integration: First-class support for Model Context Protocol for powerful tool-using agents
- Enterprise Ready: Built for production environments with robust error handling and validation
- Framework Agnostic: Works with any Python framework (Flask, FastAPI, Django, etc.)
- LLM Provider Flexibility: Native integrations with OpenAI, Anthropic, AWS Bedrock, and more
- Minimal Dependencies: Core functionality requires only the
requestslibrary - Excellent Developer Experience: Comprehensive documentation, type hints, and examples
📦 Installation
Using pip (traditional)
Install the base package with all dependencies:
pip install python-a2a # Includes LangChain, MCP, and other integrations
Or install with specific components based on your needs:
# For Flask-based server support
pip install "python-a2a[server]"
# For OpenAI integration
pip install "python-a2a[openai]"
# For Anthropic Claude integration
pip install "python-a2a[anthropic]"
# For AWS-Bedrock integration
pip install "python-a2a[bedrock]"
# For MCP support (Model Context Protocol)
pip install "python-a2a[mcp]"
# For all optional dependencies
pip install "python-a2a[all]"
Using UV (recommended)
UV is a modern Python package management tool that's faster and more reliable than pip. To install with UV:
# Install UV if you don't have it already
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install the base package
uv install python-a2a
Development Installation
For development, UV is recommended for its speed:
# Clone the repository
git clone https://github.com/themanojdesai/python-a2a.git
cd python-a2a
# Create a virtual environment and install development dependencies
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
uv pip install -e ".[dev]"
💡 Tip: Click the code blocks to copy them to your clipboard.
🚀 Quick Start Examples
1. Create a Simple A2A Agent with Skills
from python_a2a import A2AServer, skill, agent, run_server, TaskStatus, TaskState
@agent(
name="Weather Agent",
description="Provides weather information",
version="1.0.0"
)
class WeatherAgent(A2AServer):
@skill(
name="Get Weather",
description="Get current weather for a location",
tags=["weather", "forecast"]
)
def get_weather(self, location):
"""Get weather for a location."""
# Mock implementation
return f"It's sunny and 75°F in {location}"
def handle_task(self, task):
# Extract location from message
message_data = task.message or {}
content = message_data.get("content", {})
text = content.get("text", "") if isinstance(content, dict) else ""
if "weather" in text.lower() and "in" in text.lower():
location = text.split("in", 1)[1].strip().rstrip("?.")
# Get weather and create response
weather_text = self.get_weather(location)
task.artifacts = [{
"parts": [{"type": "text", "text": weather_text}]
}]
task.status = TaskStatus(state=TaskState.COMPLETED)
else:
task.status = TaskStatus(
state=TaskState.INPUT_REQUIRED,
message={"role": "agent", "content": {"type": "text",
"text": "Please ask about weather in a specific location."}}
)
return task
# Run the server
if __name__ == "__main__":
agent = WeatherAgent()
run_server(agent, port=5000)
2. Build an Agent Network with Multiple Agents
from python_a2a import AgentNetwork, A2AClient, AIAgentRouter
# Create an agent network
network = AgentNetwork(name="Travel Assistant Network")
# Add agents to the network
network.add("weather", "http://localhost:5001")
network.add("hotels", "http://localhost:5002")
network.add("attractions", "http://localhost:5003")
# Create a router to intelligently direct queries to the best agent
router = AIAgentRouter(
llm_client=A2AClient("http://localhost:5000/openai"), # LLM for making routing decisions
agent_network=network
)
# Route a query to the appropriate agent
agent_name, confidence = router.route_query("What's the weather like in Paris?")
print(f"Routing to {agent_name} with {confidence:.2f} confidence")
# Get the selected agent and ask the question
agent = network.get_agent(agent_name)
response = agent.ask("What's the weather like in Paris?")
print(f"Response: {response}")
# List all available agents
print("\nAvailable Agents:")
for agent_info in network.list_agents():
print(f"- {agent_info['name']}: {agent_info['description']}")
Real-time Streaming
Get real-time responses from agents with comprehensive streaming support:
import asyncio
from python_a2a import StreamingClient, Message, TextContent, MessageRole
async def main():
client = StreamingClient("http://localhost:5000")
# Create a message with required role parameter
message = Message(
content=TextContent(text="Tell me about A2A streaming"),
role=MessageRole.USER
)
# Stream the response and process chunks in real-time
try:
async for chunk in client.stream_response(message):
# Handle different chunk formats (string or dictionary)
if isinstance(chunk, dict):
if "content" in chunk:
print(chunk["content"], end="", flush=True)
elif "text" in chunk:
print(chunk["text"], end="", flush=True)
else:
print(str(chunk), end="", flush=True)
else:
print(str(chunk), end="", flush=True)
except Exception as e:
print(f"Streaming error: {e}")
Check out the examples/streaming/ directory for complete streaming examples:
- basic_streaming.py: Minimal streaming implementation (start here!)
- 01_basic_streaming.py: Comprehensive introduction to streaming basics
- 02_advanced_streaming.py: Advanced streaming with different chunking strategies
- 03_streaming_llm_integration.py: Integrating streaming with LLM providers
- 04_task_based_streaming.py: Task-based streaming with progress tracking
- 05_streaming_ui_integration.py: Streaming UI integration (CLI and web)
- 06_distributed_streaming.py: Distributed streaming architecture
3. Workflow Engine
The new workflow engine allows you to define complex agent interactions:
from python_a2a import AgentNetwork, Flow
import asyncio
async def main():
# Set up agent network
network = AgentNetwork()
network.add("research", "http://localhost:5001")
network.add("summarizer", "http://localhost:5002")
network.add("factchecker", "http://localhost:5003")
# Define a workflow for research report generation
flow = Flow(agent_network=network, name="Research Report Workflow")
# First, gather initial research
flow.ask("research", "Research the latest developments in {topic}")
# Then process the results in parallel
parallel_results = (flow.parallel()
# Branch 1: Create a summary
.ask("summarizer", "Summarize this research: {latest_result}")
# Branch 2: Verify key facts
.branch()
.ask("factchecker", "Verify these key facts: {latest_result}")
# End parallel processing and collect results
.end_parallel(max_concurrency=2))
# Extract insights based on verification results
flow.execute_function(
lambda results, context: f"Summary: {results['1']}\nVerified Facts: {results['2']}",
parallel_results
)
# Execute the workflow
result = await flow.run({
"topic": "quantum computing advancements in the last year"
})
print(result)
if __name__ == "__main__":
asyncio.run(main())
4. AI-Powered Router
Intelligent routing to select the best agent for each query:
from python_a2a import AgentNetwork, AIAgentRouter, A2AClient
import asyncio
async def main():
# Create a network with specialized agents
network = AgentNetwork()
network.add("math", "http://localhost:5001")
network.add("history", "http://localhost:5002")
network.add("science", "http://localhost:5003")
network.add("literature", "http://localhost:5004")
# Create a router using an LLM for decision making
router = AIAgentRouter(
llm_client=A2AClient("http://localhost:5000/openai"),
agent_network=network
)
# Sample queries to route
queries = [
"What is the formula for the area of a circle?",
"Who wrote The Great Gatsby?",
"When did World War II end?",
"How does photosynthesis work?",
"What are Newton's laws of motion?"
]
# Route each query to the best agent
for query in queries:
agent_name, confidence = router.route_query(query)
agent = network.get_agent(agent_name)
print(f"Query: {query}")
print(f"Routed to: {agent_name} (confidence: {confidence:.2f})")
# Get response from the selected agent
response = agent.ask(query)
print(f"Response: {response[:100]}...\n")
if __name__ == "__main__":
asyncio.run(main())
5. Define Complex Workflows with Multiple Agents
from python_a2a import AgentNetwork, Flow, AIAgentRouter
import asyncio
async def main():
# Create an agent network
network = AgentNetwork()
network.add("weather", "http://localhost:5001")
network.add("recommendations", "http://localhost:5002")
network.add("booking", "http://localhost:5003")
# Create a router
router = AIAgentRouter(
llm_client=network.get_agent("weather"), # Using one agent as LLM for routing
agent_network=network
)
# Define a workflow with conditional logic
flow = Flow(agent_network=network, router=router, name="Travel Planning Workflow")
# Start by getting the weather
flow.ask("weather", "What's the weather in {destination}?")
# Conditionally branch based on weather
flow.if_contains("sunny")
# If sunny, recommend outdoor activities
flow.ask("recommendations", "Recommend outdoor activities in {destination}")
# End the condition and add an else branch
flow.else_branch()
# If not sunny, recommend indoor activities
flow.ask("recommendations", "Recommend indoor activities in {destination}")
# End the if-else block
flow.end_if()
# Add parallel processing steps
(flow.parallel()
.ask("booking", "Find hotels in {destination}")
.branch()
.ask("booking", "Find restaurants in {destination}")
.end_parallel())
# Execute the workflow with initial context
result = await flow.run({
"destination": "Paris",
"travel_dates": "June 12-20"
})
print("Workflow result:")
print(result)
if __name__ == "__main__":
asyncio.run(main())
6. Use the Command Line Interface
# Send a message to an agent
a2a send http://localhost:5000 "What is artificial intelligence?"
# Stream a response in real-time
a2a stream http://localhost:5000 "Generate a step-by-step tutorial for making pasta"
# Start an OpenAI-powered A2A server
a2a openai --model gpt-4 --system-prompt "You are a helpful coding assistant"
# Start an Anthropic-powered A2A server
a2a anthropic --model claude-3-opus-20240229 --system-prompt "You are a friendly AI teacher"
# Start an MCP server with tools
a2a mcp-serve --name "Data Analysis MCP" --port 5001 --script analysis_tools.py
# Start an MCP-enabled A2A agent
a2a mcp-agent --servers data=http://localhost:5001 calc=http://localhost:5002
# Call an MCP tool directly
a2a mcp-call http://localhost:5001 analyze_csv --params file=data.csv columns=price,date
# Manage agent networks
a2a network --add weather=http://localhost:5001 travel=http://localhost:5002 --save network.json
# Run a workflow from a script
a2a workflow --script research_workflow.py --context initial_data.json
🔄 LangChain Integration (New in v0.5.X)
Python A2A includes built-in LangChain integration, making it easy to combine the best of both ecosystems:
1. Converting MCP Tools to LangChain
from python_a2a.mcp import FastMCP, text_response
from python_a2a.langchain import to_langchain_tool
# Create MCP server with a tool
mcp_server = FastMCP(name="Basic Tools", description="Simple utility tools")
@mcp_server.tool(
name="calculator",
description="Calculate a mathematical expression"
)
def calculator(input):
"""Simple calculator that evaluates an expression."""
try:
result = eval(input)
return text_response(f"Result: {result}")
except Exception as e:
return text_response(f"Error: {e}")
# Start the server
import threading, time
def run_server(server, port):
server.run(host="0.0.0.0", port=port)
server_thread = threading.Thread(target=run_server, args=(mcp_server, 5000), daemon=True)
server_thread.start()
time.sleep(2) # Allow server to start
# Convert MCP tool to LangChain
calculator_tool = to_langchain_tool("http://localhost:5000", "calculator")
# Use the tool in LangChain
result = calculator_tool.run("5 * 9 + 3")
print(f"Result: {result}")
2. Converting LangChain Tools to MCP Server
from langchain.tools import Tool
from langchain_core.tools import BaseTool
from python_a2a.langchain import to_mcp_server
# Create LangChain tools
def calculator(expression: str) -> str:
"""Evaluate a mathematical expression"""
try:
result = eval(expression)
return f"Result: {expression} = {result}"
except Exception as e:
return f"Error: {e}"
calculator_tool = Tool(
name="calculator",
description="Evaluate a mathematical expression",
func=calculator
)
# Convert to MCP server
mcp_server = to_mcp_server(calculator_tool)
# Run the server
mcp_server.run(port=5000)
3. Converting LangChain Components to A2A Servers
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from python_a2a import A2AClient, run_server
from python_a2a.langchain import to_a2a_server
# Create a LangChain LLM
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
# Convert LLM to A2A server
llm_server = to_a2a_server(llm)
# Create a simple chain
template = "You are a helpful travel guide.\n\nQuestion: {query}\n\nAnswer:"
prompt = PromptTemplate.from_template(template)
travel_chain = prompt | llm | StrOutputParser()
# Convert chain to A2A server
travel_server = to_a2a_server(travel_chain)
# Run servers in background threads
import threading
llm_thread = threading.Thread(
target=lambda: run_server(llm_server, port=5001),
daemon=True
)
llm_thread.start()
travel_thread = threading.Thread(
target=lambda: run_server(travel_server, port=5002),
daemon=True
)
travel_thread.start()
# Test the servers
llm_client = A2AClient("http://localhost:5001")
travel_client = A2AClient("http://localhost:5002")
llm_result = llm_client.ask("What is the capital of France?")
travel_result = travel_client.ask('{"query": "What are some must-see attractions in Paris?"}')
4. Converting A2A Agents to LangChain Agents
from python_a2a.langchain import to_langchain_agent
# Convert A2A agent to LangChain agent
langchain_agent = to_langchain_agent("http://localhost:5000")
# Use the agent in LangChain
result = langchain_agent.invoke("What are some famous landmarks in Paris?")
print(result.get('output', ''))
# Use in a LangChain pipeline
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(temperature=0)
prompt = ChatPromptTemplate.from_template(
"Generate a specific, detailed travel question about {destination}."
)
# Create a pipeline with the converted agent
chain = (
prompt |
llm |
StrOutputParser() |
langchain_agent |
(lambda x: f"Travel Info: {x.get('output', '')}")
)
result = chain.invoke({"destination": "Japan"})
print(result)
LangChain is automatically installed as a dependency with python-a2a, so everything works right out of the box:
pip install python-a2a
# That's it! LangChain is included automatically
🧩 Core Features
Agent Networks
Python A2A now includes a powerful system for managing multiple agents:
from python_a2a import AgentNetwork, A2AClient
# Create a network of agents
network = AgentNetwork(name="Medical Assistant Network")
# Add agents in different ways
network.add("diagnosis", "http://localhost:5001") # From URL
network.add("medications", A2AClient("http://localhost:5002")) # From client instance
# Discover agents from a list of URLs
discovered_count = network.discover_agents([
"http://localhost:5003",
"http://localhost:5004",
"http://localhost:5005"
])
print(f"Discovered {discovered_count} new agents")
# List all agents in the network
for agent_info in network.list_agents():
print(f"Agent: {agent_info['name']}")
print(f"URL: {agent_info['url']}")
if 'description' in agent_info:
print(f"Description: {agent_info['description']}")
print()
# Get a specific agent
agent = network.get_agent("diagnosis")
response = agent.ask("What are the symptoms of the flu?")
📖 Architecture & Design Principles
Python A2A is built on three core design principles:
-
Protocol First: Adheres strictly to the A2A and MCP protocol specifications for maximum interoperability
-
Modularity: All components are designed to be composable and replaceable
-
Progressive Enhancement: Start simple and add complexity only as needed
The architecture consists of seven main components:
- Models: Data structures representing A2A messages, tasks, and agent cards
- Client: Components for sending messages to A2A agents and managing agent networks
- Server: Components for building A2A-compatible agents
- MCP: Tools for implementing Model Context Protocol servers and clients
- LangChain: Bridge components for LangChain integration
- Workflow: Engine for orchestrating complex agent interactions
- Utils: Helper functions for common tasks
- CLI: Command-line interface for interacting with agents
🗺️ Use Cases
Python A2A can be used to build a wide range of AI systems:
Research & Development
- Experimentation Framework: Easily swap out different LLM backends while keeping the same agent interface
- Benchmark Suite: Compare performance of different agent implementations on standardized tasks
- Streaming Research Assistants: Create responsive research tools with real-time output using streaming
Enterprise Systems
- AI Orchestration: Coordinate multiple AI agents across different departments using agent networks
- Legacy System Integration: Wrap legacy systems with A2A interfaces for AI accessibility
- Complex Workflows: Create sophisticated business processes with multi-agent workflows and conditional branching
Customer-Facing Applications
- Multi-Stage Assistants: Break complex user queries into subtasks handled by specialized agents
- Tool-Using Agents: Connect LLMs to database agents, calculation agents, and more using MCP
- Real-time Chat Interfaces: Build responsive chat applications with streaming response support
Education & Training
- AI Education: Create educational systems that demonstrate agent collaboration
- Simulation Environments: Build simulated environments where multiple agents interact
- Educational Workflows: Design step-by-step learning processes with feedback loops
🛠️ Real-World Examples
Check out the examples/ directory for real-world examples, including:
- Multi-agent customer support systems
- LLM-powered research assistants with tool access
- Real-time streaming implementations
- LangChain integration examples
- MCP server implementations for various tools
- Workflow orchestration examples
- Agent network management
🔄 Related Projects
Here are some related projects in the AI agent and interoperability space:
- Google A2A - The official Google A2A protocol specification
- LangChain - Framework for building applications with LLMs
- AutoGen - Microsoft's framework for multi-agent conversations
- CrewAI - Framework for orchestrating role-playing agents
- MCP - The Model Context Protocol for tool-using agents
👥 Contributors
Thanks to all our contributors!
Want to contribute? Check out our contributing guide.
🤝 Community & Support
- GitHub Issues: Report bugs or request features
- GitHub Discussions: Ask questions and share ideas
- Contributing Guide: Learn how to contribute to the project
- ReadTheDocs: Visit our documentation site
📝 Citing this Project
If you use Python A2A in your research or academic work, please cite it as:
@software{desai2025pythona2a,
author = {Desai, Manoj},
title = {Python A2A: A Comprehensive Implementation of the Agent-to-Agent Protocol},
url = {https://github.com/themanojdesai/python-a2a},
version = {0.5.0},
year = {2025},
}
⭐ Star This Repository
If you find this library useful, please consider giving it a star on GitHub! It helps others discover the project and motivates further development.
Star History
🙏 Acknowledgements
- The Google A2A team for creating the A2A protocol
- The Contextual AI team for the Model Context Protocol
- The LangChain team for their powerful LLM framework
- All our contributors for their valuable input
👨💻 Author
Manoj Desai
- GitHub: themanojdesai
- LinkedIn: themanojdesai
- Medium: @the_manoj_desai
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by Manoj Desai
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_a2a-0.5.2.tar.gz.
File metadata
- Download URL: python_a2a-0.5.2.tar.gz
- Upload date:
- Size: 174.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35bf866205b7ecdcd3e5c7fe62c9644cb0dfd62e6c9e338484cbdb0abf5e111a
|
|
| MD5 |
ee42470a01fcb8a685b14853160b9fe6
|
|
| BLAKE2b-256 |
1cdbe2549bc1006940f780a56287ac04da99768dd2bacfb574cae3e47800d6f5
|
File details
Details for the file python_a2a-0.5.2-py3-none-any.whl.
File metadata
- Download URL: python_a2a-0.5.2-py3-none-any.whl
- Upload date:
- Size: 146.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e808207d2aa8e0952c8230ecf18d701aa187c6de724768e4a010e008f606e8bd
|
|
| MD5 |
6058c3cffc59ffb1d447b7cc60b85e50
|
|
| BLAKE2b-256 |
b47a62ae1a1025bdd972b7469916474c31bfdd8bbd2d8527bd4f4530da1d1a8c
|