多代理MCP聊天机器人

一个基于FastMCP构建的多代理聊天机器人,展示代理如何通过HTTP调用服务器端工具。

  • by mbiswas22
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 mbiswas22/MCP-Chatbot

使用说明

🤖 多代理MCP聊天机器人(FastMCP+Python)

一个简单的 多代理聊天机器人 使用 快速MCP 这演示了代理如何通过HTTP调用服务器端工具。该项目展示了如何:

  • 创建和注册MCP工具
  • 构建异步聊天代理
  • 将用户输入(如姓名)传递给工具
  • 从MCP服务器获取结构化响应

这是一个很好的起点 基于工具的代理, MCP架构,以及 代理到服务器通信.


🧱 架构概述

User Input
   ↓
ChatAgent (client)
   ↓  call_tool()
FastMCP Server (HTTP)
   ↓
Tool Function (get_user_info)
   ↓
Response back to Agent

📁 项目结构

.
├── agents/
│   ├── chat_agent.py   # Chat agent that calls MCP tools
│   ├── time_agent.py   # Time agent that calls MCP tools
│   └── orchestrator_agent.py  # It routes intent to the right agent
├── server.py        # FastMCP server with tool definitions
├── client.py       # CLI client to interact with the agent
├── .env             # Environment variables (optional)
├── requirements.txt
├── README.md        # Project documentation

⚙️ 先决条件

  • python 3.10+ (用3.12测试)
  • 点
  • 虚拟环境(推荐)

📦 安装

1.️⃣ 创建并激活虚拟环境

python -m venv venv
source venv/bin/activate   # macOS/Linux
venv\Scripts\activate      # Windows
pip install -r .\requirements.txt

2.️⃣ 安装依赖项

pip install fastmcp python-dotenv

🚀 运行项目

步骤1:启动MCP服务器

python server.py

预期产量:

Starting MCP server 'multi-agent-mcp' with transport 'http'

⚠️ 重要提示:每次添加或更改工具时,请重新启动服务器。


步骤2:运行客户端

python client.py

您应该看到:

🤖 Multi-Agent MCP Chatbot (type 'exit' to quit)

💬 交互示例

You: my name is Monika
Bot: Hello Monika! 👋 How can I help you today?
You: What time is it now?
Bot: 2026-02-02 10:21:51
You: Nice to meet you! Bye!
Bot: Nice to meet you! 😊
You: weather in chicago
Bot: Current weather in Chicago: overcast clouds, Temperature: -4.02°C, Humidity: 78%
You: tell me a joke
Bot: Sure! Here’s a joke for you:

Why did the scarecrow win an award?

Because he was outstanding in his field!

🌱 未来的增强功能

  • 🧠 添加内存(每个会话存储用户名)
  • 📄 使用Pydantic模式验证工具输入
  • 🤝 具有任务委派的多个代理
  • 🔐 经过身份验证的MCP服务器
  • 🌐 Web UI而不是CLI

📚 参考文献

  • FastMCP文档
  • MCP(模型上下文协议)概念
  • 异步Python(async / await)

截图