multi-agent README

The project's README from github.com/phoenix-zhou/multi-agent. As one markdown file: README.md.

🤖 基于 A2A 协议的旅行智能助手

一个采用 Agent2Agent (A2A) 协议 + MCP (Model Context Protocol) 构建的多智能体旅行助手系统。系统通过总控中心进行意图识别与路由,由多个专业 Agent 协作完成天气查询、票务查询、票务预定、景点推荐等服务,支持 CLI 与 Streamlit Web 两种交互方式。

✨ 功能特性

🏗️ 系统架构

┌─────────────────────────────────────────────────────────────┐
│                    总控中心 (Orchestrator)                    │
│        app.py (Streamlit Web)  /  main.py (CLI)             │
│    - 意图识别 (Intent Agent, LLM)                            │
│    - 查询改写 (Query Rewriting)                              │
│    - 结果总结 (Summarization, LLM)                          │
└──────────────┬──────────────────────┬───────────────────────┘
               │ A2A 协议             │ A2A 协议
               ▼                      ▼
   ┌───────────────────────┐  ┌───────────────────────────┐
   │   A2A Agent 层         │  │                           │
   │  WeatherQueryAssistant│  │  TicketOrderAssistant      │
   │  (端口 5005)           │  │  (端口 5007)               │
   │  TicketQueryAssistant │  │   └─ 调用 TicketQuery...   │
   │  (端口 5006)           │  │      (Agent 间协作)        │
   └───────┬───────────┬───┘  └───────────┬───────────────┘
           │ MCP       │ MCP             │ MCP
           ▼           ▼                 ▼
   ┌─────────────┐ ┌────────────┐ ┌───────────────┐
   │ MCP Server 层│ │            │ │               │
   │WeatherTools │ │TicketTools │ │ OrderTools    │
   │ (端口 8002) │ │(端口 8001) │ │ (端口 8003)   │
   │query_weather│ │query_tickets│ │order_train/    │
   │             │ │            │ │order_flight/   │
   │             │ │            │ │order_concert   │
   └──────┬──────┘ └──────┬─────┘ └──────┬────────┘
          │               │              │
          ▼               ▼              ▼
        ┌────────────────────────────────────┐
        │           MySQL 数据库 travel_rag    │
        │  weather_data / train_tickets /    │
        │  flight_tickets / concert_tickets  │
        └────────────────────────────────────┘

处理流程

用户输入 → 意图识别(LLM) → 路由到对应 Agent
                              ├─ out_of_scope / 追问 → 直接回复
                              ├─ attraction → LLM 直接生成景点推荐
                              └─ weather/flight/train/concert/order
                                    → A2A Agent 生成 SQL (LLM)
                                    → 调用 MCP 工具查询数据库
                                    → 返回结果 → 总控中心 LLM 总结 → 用户

端口规划

服务 组件 端口
A2A Agent WeatherQueryAssistant(天气查询) 5005
A2A Agent TicketQueryAssistant(票务查询) 5006
A2A Agent TicketOrderAssistant(票务预定) 5007
MCP Server WeatherTools(天气工具) 8002
MCP Server TicketTools(票务工具) 8001
MCP Server OrderTools(预定工具) 8003
Web UI Streamlit 应用 8501(默认)

📁 目录结构

multi-agent/
├── app.py                     # Streamlit Web 入口(总控中心)
├── main.py                    # CLI 命令行入口(总控中心)
├── main_prompts.py            # 提示词模板(意图识别/结果总结/景点推荐)
├── config.py                  # 全局配置(LLM / 数据库 / 意图映射)
├── create_logger.py           # 日志配置
│
├── a2a_server/                # A2A 协议 Agent 服务层
│   ├── weather_server.py      # 天气查询 Agent(端口 5005)
│   ├── ticket_server.py       # 票务查询 Agent(端口 5006)
│   └── order_server.py        # 票务预定 Agent(端口 5007)
│
├── mcp_server/                # MCP Server 数据服务层
│   ├── mcp_weather_server.py  # 天气 MCP(端口 8002)
│   ├── mcp_ticket_server.py   # 票务 MCP(端口 8001)
│   └── mcp_order_server.py    # 预定 MCP(端口 8003)
│
├── query_data/
│   └── query_ticket.py        # 票务数据库查询服务
│
├── utils/
│   ├── format.py              # JSON 日期/Decimal 序列化工具
│   └── spider_weather.py      # 和风天气数据爬虫(定时更新天气库)
│
├── sql/
│   ├── sql_data.sql           # 建库建表 DDL
│   └── insert2.sql            # 票务示例数据
│
└── test/                      # 各服务测试脚本
    ├── test_weather_mcp_server.py
    ├── test_ticket_mcp_server.py
    ├── test_order_mcp_server.py
    ├── test_weather_agent_server.py
    ├── test_ticket_agent_server.py
    └── test_order_agent_server.py

🛠️ 技术栈

🚀 快速开始

1. 环境准备

# Python 3.9+
pip install streamlit langchain langchain-openai python-a2a mcp
pip install langchain-mcp-adapters mysql-connector-python requests schedule pytz

2. 初始化数据库

# 执行建库建表脚本
mysql -u root -p < sql/sql_data.sql

# 导入票务示例数据
mysql -u root -p travel_rag < sql/insert2.sql

如需导入真实天气数据,运行 python utils/spider_weather.py(需配置和风天气 API Key)。

3. 修改配置

编辑 config.py,配置你的 LLM 与数据库信息:

# 大模型配置
self.base_url = 'https://api.siliconflow.cn/v1'
self.api_key = 'sk-xxx'               # 替换为你的 API Key
self.model_name = 'Qwen/Qwen2.5-72B-Instruct'

# 数据库配置
self.host = 'localhost'
self.user = 'root'
self.password = '123456'
self.database = 'travel_rag'

4. 启动服务(按顺序)

# ① 启动 MCP Server(数据层)
python mcp_server/mcp_weather_server.py   # 端口 8002
python mcp_server/mcp_ticket_server.py    # 端口 8001
python mcp_server/mcp_order_server.py     # 端口 8003

# ② 启动 A2A Agent(智能体层)
python a2a_server/weather_server.py       # 端口 5005
python a2a_server/ticket_server.py        # 端口 5006
python a2a_server/order_server.py         # 端口 5007

# ③ 启动总控中心(二选一)
python main.py                            # CLI 交互版
streamlit run app.py                      # Web 聊天版

💬 使用示例

> 成都明天天气怎么样
> 帮我查一下明天成都到西安的高铁票
> 7月15号从成都到西安的机票还有吗
> 帮我订一张明天成都到西安的二等座火车票
> 去西安有什么好玩的景点推荐
> 明天天气怎么样 + 顺便看看去广州的机票    # 多意图组合

CLI 版输入 cards 可查看所有 Agent 的卡片信息(技能/描述/地址),输入 quit 退出。

🔑 核心设计说明

  1. 意图识别与查询改写(main_prompts.py):总控中心将用户输入收敛为 weather / flight / train / concert / order / attraction / out_of_scope 等固定意图;结合对话历史将残缺查询(如"那明天呢?")改写为完整明确的句子,使下游 Agent 无需关心上下文。

  2. SQL 生成 Agent(a2a_server/):下游 Agent 通过 LLM 将自然语言转换为 SQL,并将数据库 Schema 作为提示词上下文。信息不足时返回 input_required 追问用户,无关提问则拒绝回答。

  3. Agent 间协作(order_server.py):票务预定 Agent 先调用票务查询 Agent 确认余票,再将余票信息拼接后交由 MCP 预定工具执行,体现了多 Agent 协作流程。

  4. MCP 解耦:数据库访问统一封装为 MCP 工具(query_weather / query_tickets / order_train 等),Agent 通过 HTTP 流式调用,数据层与智能体层彻底解耦,便于扩展。

🧪 测试

test/ 目录下提供了各 MCP Server 与 A2A Agent 的独立测试脚本,启动对应服务后即可运行:

python test/test_weather_mcp_server.py
python test/test_weather_agent_server.py
python test/test_ticket_mcp_server.py
python test/test_ticket_agent_server.py
python test/test_order_mcp_server.py
python test/test_order_agent_server.py

📄 License

MIT

对应的博客:[]

multi-agent (phoenix-zhou/multi-agent)

Searching Database…

What is this?This is a multi-agent travel assistant that uses the Agent2Agent (A2A) protocol and the Model Context Protocol (MCP) so specialized agents can work together on weather lookups, train/flight/concert ticket queries, ticket booking, and attraction recommendations.
Is this a library I import, or a running system?It is a self-hosted multi-agent system: a central dispatcher routes your request to specialized A2A agents (weather, ticket query, ticket order), which call MCP tool servers for data access. You run it as a set of services, not import it as a library.
How do I get this running?Prepare the environment, initialize the MySQL database, edit config.py with your LLM and database settings, then start each A2A agent and MCP server in order before launching the CLI or the Streamlit web app.
What do I need before I can run it, besides Python?You need a MySQL database and an OpenAI-compatible LLM API key — the project defaults to Qwen2.5-72B-Instruct via SiliconFlow through LangChain — plus optionally a 和风天气 (QWeather) API key to pull real 30-day weather forecasts.
What's the catch with running this locally?There are six separate services on different ports (three A2A agents, three MCP servers, plus the web UI) that must be started in the right order, and intent recognition is limited to a fixed set of categories (weather/flight/train/concert/order/attraction/outofscope).
Can I use this from a browser, or only the terminal?Both: it ships with a CLI client and a Streamlit web chat interface, which runs on port 8501 by default.
What is an A2A AgentCard?Each agent exposes a standard A2A AgentCard listing its name, description, and skills, so other agents and the central orchestrator can discover and call it over the A2A protocol.
How does the system handle follow-up questions like 'and tomorrow?'It keeps conversation history and uses an LLM to rewrite incomplete follow-up queries into complete standalone sentences before routing them to the right agent, so downstream agents don't need to track context themselves.
How does ticket booking actually work across agents?The booking agent first calls the ticket-query agent to confirm seat availability, then passes that result to an MCP booking tool to place the order - a concrete example of agent-to-agent collaboration.
Can I swap out the LLM or weather data source?Yes, the LLM defaults to Qwen2.5-72B-Instruct via SiliconFlow but any OpenAI-compatible endpoint works, and weather data can be refreshed by running the included spiderweather.py scraper with your own QWeather API key.
Size
0.1 MB
Stars
490
Updated
CommitAug 11
Age
2mo
Commits · 90 Days