使用说明
用户注册聊天机器人与本地MCP服务器
一个完整的用户注册系统 模型上下文协议(MCP) 它通过对话式聊天机器人界面处理用户注册。该系统以CSV格式在本地存储数据,取代了对Google Sheets或Zapier等外部服务的需求。
🎯 项目概述
本项目演示了如何:
- 构建本地MCP服务器 Python从头开始
- 创建对话式AI界面 使用Streamlit
- 处理数据持久性 使用本地CSV存储
- 实施数据验证 以及错误处理
- 提供丰富的用户交互 通过自然语言
🏗️ 系统架构
┌─────────────────┐ HTTP/JSON-RPC ┌─────────────────┐ CSV Operations ┌─────────────────┐
│ Streamlit UI │ ◄─────────────────► │ MCP Server │ ◄─────────────────► │ user_registrations│
│ (Chat Interface)│ │ (Python Server) │ │ .csv │
└─────────────────┘ └─────────────────┘ └─────────────────┘
关键部件:
- MCP服务器 (
registration_mcp_server.py)-处理所有数据操作 - Streamlit聊天机器人 (
streamlit_chatbot.py)-提供用户界面 - CSV存储 (
user_registrations.csv)-本地数据持久性
✨ 特性
🤖 会话注册
- 自然语言交互
- 逐步收集数据(名称→ 电子邮件→ DOB)
- 实时验证和反馈
- 保存前确认
📊 数据管理
- 添加注册 -存储新用户数据
- 查看所有注册 -列出所有注册用户
- 搜索功能 -按姓名或电子邮件查找用户
- 统计仪表板 -查看注册分析
- 数据验证 -电子邮件格式、日期验证、防重复
🛠️ 技术特性
- 本地MCP服务器 -无外部依赖关系
- CSV存储 -简单、便携的数据格式
- 错误处理 -全面的错误消息
- 实时验证 -数据输入的即时反馈
- 响应式用户界面 -简洁直观的Streamlit界面
📋 先决条件
- Python 3.8+
- 点 包管理器
🚀 安装和设置
第一步:克隆或下载文件
创建一个新目录并保存这些文件:
registration_mcp_server.py-MCP服务器streamlit_chatbot.py-Streamlit界面
步骤2:安装依赖项
# Install MCP SDK for the server
pip install mcp
# Install Streamlit for the chatbot interface
pip install streamlit requests
步骤3:启动MCP服务器
打开终端并运行:
python registration_mcp_server.py
预期产量:
🚀 Starting Registration MCP Server...
📁 Data file: user_registrations.csv
🛠️ Available tools:
• add_registration - Add new user registration
• get_all_registrations - Retrieve all registrations
• search_registrations - Search by name or email
• get_registration_statistics - View registration stats
• validate_registration_data - Validate data before saving
📚 Available resources:
• file://user_registrations.csv - Direct CSV file access
✅ Server ready for connections!
Connect your AI model to use these tools...
步骤4:启动Streamlit聊天机器人
打开A 新终端 并运行:
streamlit run streamlit_chatbot.py
聊天机器人将在您的浏览器中打开 http://localhost:8501
💬 使用指南
注册进程
- 开始注册:类型
register或点击“注册”按钮 - 输入名称:提供您的全名(至少2个字符)
- 输入电子邮件:提供有效的电子邮件地址
- 输入出生日期:使用YYYY-MM-DD格式(例如,1990-05-15)
- 确认详细信息:审核并确认您的注册
可用命令
| 命令 | 描述 | 示例 |
|---|---|---|
register |
开始新注册 | register |
show registrations |
查看所有注册用户 | show registrations |
search [query] |
按姓名或电子邮件搜索 | search john 或 search @gmail |
statistics |
查看注册统计信息 | statistics |
help |
显示可用命令 | help |
对话流程示例
User: register
Bot: Great! Let's start your registration. 📝
What's your full name?
User: John Doe
Bot: Nice to meet you, John Doe! 👋
Now, please provide your email address:
User: john@example.com
Bot: Perfect! 📧
Now please enter your date of birth.
Format: YYYY-MM-DD (e.g., 1990-05-15)
User: 1990-05-15
Bot: 📋 Please confirm your registration details:
• Name: John Doe
• Email: john@example.com
• Date of Birth: 1990-05-15
✅ Everything looks good!
• Type 'confirm' to complete registration
User: confirm
Bot: 🎉 Registration Completed Successfully!
✅ Successfully registered John Doe
Registration Details:
• Name: John Doe
• Email: john@example.com
• Date of Birth: 1990-05-15
• Registered: 2025-01-15 14:30:25
🗂️ 数据存储
CSV结构
数据存储在 user_registrations.csv 使用这些列:
| 列 | 说明 | 示例 |
|---|---|---|
| 姓名 | 用户全名 | John Doe |
| 电子邮件 | 电子邮件地址 | john@example.com |
| 出生日期 | 出生日期(YYYY-MM-DD) | 1990-05-15 |
| 注册日期 | 注册时间 | 2025-01-15 14:30:25 |
CSV内容示例
Name,Email,Date_of_Birth,Registration_Date
John Doe,john@example.com,1990-05-15,2025-01-15 14:30:25
Jane Smith,jane@example.com,1985-03-20,2025-01-15 14:35:12
Mike Johnson,mike@example.com,1992-07-08,2025-01-15 14:40:33
🔧 MCP服务器详细信息
可用工具
-
add_registration- 目的:添加新用户注册
- 参数:
name,email,dob - 验证:名称长度、电子邮件格式、日期格式、重复电子邮件检查
-
get_all_registrations- 目的:检索所有注册用户
- 参数:无
- 退货:所有带有ID的注册列表
-
search_registrations- 目的:按姓名或电子邮件搜索用户
- 参数:
query(搜索词) - 退货:匹配注册
-
get_registration_statistics- 目的:获取注册分析
- 参数:无
- 退货:总计数、年龄人口统计、注册日期
-
validate_registration_data- 目的:验证数据而不保存
- 参数:
name,email,dob - 退货:每个字段的验证结果
数据验证规则
| 字段 | 验证规则 |
|---|---|
| 名字 | •最少2个字符 •最多100个字符 •不能为空 |
| 电子邮件 | •有效的电子邮件格式(user@domain.com) •必须是唯一的(没有重复项) •不能为空 |
| 出生日期: | •YYYY-MM-DD格式 •不可能在未来 •合理的年龄限制(0-150岁) |
📊 功能展示
🎯 注册统计数据
📊 Registration Statistics:
📋 Total Registrations: 25
🌐 Unique Email Domains: 12
📅 First Registration: 2025-01-10 09:15:30
🆕 Latest Registration: 2025-01-15 16:45:22
💾 File Size: 1,234 bytes
👥 Age Demographics:
Average Age: 32.4 years
Youngest User: 18 years
Oldest User: 67 years
📁 Data File: user_registrations.csv
🔍 搜索结果
🔍 Search Results for 'john' (3 matches):
1. John Doe
📧 Email: john@example.com
🎂 Date of Birth: 1990-05-15
📅 Registered: 2025-01-15 14:30:25
2. Johnny Smith
📧 Email: johnny.smith@gmail.com
🎂 Date of Birth: 1988-11-22
📅 Registered: 2025-01-14 11:20:15
``
### **Common Issues**
**1. "MCP Server: Disconnected"**
- **Cause**: MCP server not running
- **Solution**: Start the server with `python registration_mcp_server.py`
**2. "Invalid email format"**
- **Cause**: Incorrect email format
- **Solution**: Use format like `user@domain.com`
**3. "Invalid date format"**
- **Cause**: Wrong date format
- **Solution**: Use YYYY-MM-DD format (e.g., `1990-05-15`)
**4. "Email already registered"**
- **Cause**: Duplicate email address
- **Solution**: Use a different email address
**5. "Failed to call MCP tool"**
- **Cause**: Communication issue between client and server
- **Solution**: Restart both the MCP server and Streamlit app
### **File Permissions**
- Ensure the directory is writable (for CSV file creation)
- Check that `user_registrations.csv` isn't locked by another program
### **Port Conflicts**
- MCP server uses stdio communication (no port needed)
- Streamlit uses port 8501 (changeable with `--server.port` flag)
## 📁 **Project Structure**
注册聊天机器人/ ├── registration_mcp_server.py#mcp服务器及其所有工具 ├── streamlit_chatbot.py#streamlit聊天界面 ├── user_registers.csv#生成的数据文件 ├── README.md#本文档 └── requirements.txt#Python依赖项(可选)
## 🔄 **Extending the System**
### **Adding New Fields**
1. Update `REQUIRED_FIELDS` in the MCP server
2. Add validation in `RegistrationValidator` class
3. Extend the chatbot conversation flow
4. Update CSV operations
### **Database Integration**
Replace CSV operations with database calls:
```python
# Instead of CSV operations
def add_registration(self, name, email, dob):
# Database insert logic here
cursor.execute("INSERT INTO users VALUES (?, ?, ?)", (name, email, dob))
API集成
将外部API调用添加到MCP服务器:
@app.list_tools()
async def list_tools():
return [
Tool(name="send_email", description="Send welcome email"),
Tool(name="sync_to_crm", description="Sync to CRM system")
]
🎓 学习成果
完成此项目后,您将了解:
- MCP架构 -如何构建和构造MCP服务器
- 数据验证 -实施稳健的输入验证
- 对话式AI -创建自然语言界面
- 文件操作 -CSV读取、写入和操作
- 错误处理 -全面的错误管理
- UI/UX设计 -构建用户友好的聊天界面
📚 额外资源
- MCP文件: https://modelcontextprotocol.io/
- 简化文档: https://docs.streamlit.io/
- Python CSV模块: https://docs.python.org/3/library/csv.html
快乐编码! 🎉
内置于❤️ 使用 模型上下文协议, 溪流,以及 python
