Content
# A2A MCP AG2 Smart Agent Example
## Project Introduction
This project demonstrates how to build a smart agent that supports the MCP (Model Context Protocol) using the [AG2](https://github.com/ag2ai/ag2) framework (formerly known as AutoGen) and facilitates standardized communication through the A2A (Agent to Agent) protocol.
The core functionalities of this project include:
- **MCP Tool Integration**: Access various external tools and capabilities via the MCP protocol
- **YouTube Subtitle Processing**: A smart agent specifically designed to download and analyze YouTube video subtitles
- **A2A Protocol Support**: Provides a standardized communication interface between agents
- **Real-time Streaming Processing**: Supports real-time status updates during task execution
- **Cross-Framework Compatibility**: Demonstrates interoperability between different agent frameworks
## How to Run
### 1. Clone the Code
```bash
git clone https://github.com/sing1ee/a2a-mcp-ag2-sample.git
cd a2a-mcp-ag2-sample
```
### 2. Environment Configuration
Create a virtual environment and install dependencies using the uv package manager:
```bash
# Create a virtual environment
uv venv
# Activate the virtual environment and synchronize dependencies
uv sync
```
### 3. Set Environment Variables
Create a `.env` file and add your OpenAI API key:
```bash
echo "OPENAI_API_KEY=your_api_key_here" > .env
```
### 4. Install MCP YouTube Tool
```bash
uv tool install git+https://github.com/sparfenyuk/mcp-youtube
```
### 5. Run the Agent
```bash
# Run with default configuration
uv run .
# Customize host and port
uv run . --host 0.0.0.0 --port 8080
```
### 6. Debugging and Testing
Refer to the [A2A Inspector](https://a2aprotocol.ai/blog/a2a-inspector) for debugging. A2A Inspector is a powerful tool designed for debugging A2A applications, helping you to:
- Monitor communication between agents
- Inspect A2A protocol messages
- Debug task execution flows
- Validate agent response formats
### Example Usage
After starting the agent, you can send the following request to test the YouTube subtitle functionality:
```
Summarize this video: https://www.youtube.com/watch?v=kQmXtrmQ5Zg
```
## Project Flow Sequence Diagram
```mermaid
sequenceDiagram
participant Client as A2A Client
participant Server as A2A Server
participant Agent as AG2 Agent
participant MCP as MCP Server
participant YouTube as YouTube MCP Tool
Client->>Server: Send task request
Server->>Agent: Forward query to AG2 Agent
Note over Server,Agent: Real-time status updates (streaming processing)
Agent->>MCP: Request available tool list
MCP->>Agent: Return tool definitions
Agent->>Agent: LLM decides to use YouTube tool
Agent->>MCP: Send tool execution request
MCP->>YouTube: Call YouTube subtitle download tool
YouTube->>YouTube: Download video subtitles
YouTube->>MCP: Return subtitle data
MCP->>Agent: Return tool execution result
Agent->>Agent: LLM processes subtitle data and generates response
Agent->>Server: Return complete response
Server->>Client: Respond with task result
```
## Technical Architecture
### Core Components
- **YoutubeMCPAgent**: Core agent implementation based on AG2 AssistantAgent
- **AG2AgentExecutor**: A2A protocol adapter that handles task execution and event queue
- **MCP Tool Integration**: Connects to MCP server via stdio client
- **A2A Server**: Provides a standardized communication interface for agents
### Key Features
- **Response Model**: Uses Pydantic models to ensure structured output
- **Asynchronous Processing**: Supports concurrent task handling and streaming responses
- **Error Handling**: Comprehensive error capture and recovery mechanisms
- **Tool Registration**: Dynamic registration and management of MCP tools
## Conclusion
With the rapid development and proliferation of AI agent products, there are increasingly more agent frameworks and solutions available in the market, such as LangGraph, CrewAI, AG2, etc. Each framework has its unique advantages and applicable scenarios, but this also brings challenges in interoperability.
**Significance of the A2A Protocol**:
1. **Standardized Communication**: The A2A protocol serves as a universal language for communication between agents, eliminating barriers between different frameworks.
2. **Ecosystem Interconnectivity**: Enables agents from different tech stacks to collaborate seamlessly, forming a more robust AI ecosystem.
3. **Reduced Integration Costs**: Developers do not need to create adapters for each framework individually, significantly lowering the complexity of system integration.
4. **Fostering Innovation**: By standardizing protocols, developers can focus on enhancing agent capabilities rather than protocol adaptation.
5. **Future Scalability**: Lays a solid foundation for building complex multi-agent systems.
This project showcases the practical application value of the A2A protocol, proving that efficient communication can indeed be achieved between different agent frameworks through a unified protocol. As agent technology continues to mature, the A2A protocol will become an important bridge connecting the AI agent ecosystem, driving the entire industry towards a more open and interconnected future.