使用说明
MCP良好查询
通过MCP从PostgreSQL生成加密交易摘要,并将其发送到Gmail。包括Streamlit web UI和可选的OpenAI驱动聊天,用于收集参数并触发报告。
🎯 MCP SDK实现
该项目使用 官方MCP Python SDK 以公开适当的MCP服务器,以及一个Streamlit UI(带有可选的OpenAI聊天)来运行报告。
主要特点:
- ✅ LLM兼容(与Claude Desktop兼容)
- ✅ 遵循MCP标准协议
- ✅ 动态工具发现
- ✅ MCP生态系统的一部分
- 📚 看 MCP_SDK_GUIDE.md 获取完整指南
- 🌐 Streamlit web应用程序,用于配置和发送报告
- 🧠 可选的LLM聊天(OpenAI)以交互方式收集参数
📋 建筑
[Claude/LLM/User] → [agent_mcp.py (MCP Client)]
↓
├─→ [mcp_server_postgres.py (MCP Server)] → PostgreSQL
└─→ [mcp_server_gmail.py (MCP Server)] → Gmail
组件:
- agent_mcp.py –MCP客户端/编排器
- mcpserver_postgres.py –MCP服务器公开数据库工具
- mcp_server_gmail.py –MCP服务器暴露电子邮件工具
- report_generator.py –HTML/CSV生成器(传统助手)
🛠️ 先决条件
- Python 3.9+
- PostgreSQL 12+(本地或远程)
- Gmail帐户(建议开发人员使用应用程序密码)
- 可选:用于聊天的OpenAI API密钥
📦 安装
1) 安装依赖项
cd mcp-reports-agent
pip install -r requirements.txt
2) 环境变量
复制示例并编辑您的值:
cp .env.example .env
示例 .env (如果使用应用程序密码,OAuth2字段是可选的):
# PostgreSQL
DB_HOST=localhost
DB_PORT=5432
DB_NAME=reports_db
DB_USER=postgres
DB_PASS=your_password
# Gmail OAuth2
GMAIL_CLIENT_ID=your_client_id.apps.googleusercontent.com
GMAIL_CLIENT_SECRET=your_client_secret
GMAIL_REFRESH_TOKEN=your_refresh_token
# O usar App Password (más simple)
GMAIL_APP_PASSWORD=your_16_char_app_password
GMAIL_FROM=your_email@gmail.com
# Optional LLM (Streamlit chat)
# OPENAI_API_KEY=sk-your_api_key
3) 初始化数据库
psql -h localhost -U postgres -d reports_db -f init_db.sql
4) (可选)从Excel加载数据
放置文件并运行:
python3 load_crypto_transactions.py --file /path/to/transacciones_cripto.xlsx
预期列(允许大小写/重音): fecha, usuario, tipo_transaccion, criptomoneda, monto, tc.
选项A:应用程序密码(推荐用于开发)
- Ve ahttps://myaccount.google.com/apppasswords
- 为“邮件”生成应用程序密码
- 在中使用该密码
GMAIL_APP_PASSWORD
选项B:OAuth2(生产)
- 使用谷歌云控制台
- 创建项目并启用Gmail API
- Crea认证OAuth2(桌面应用程序)
- 下载JSON并运行:
python setup_gmail_oauth.py
🚀 用法
流线型Web UI
streamlit run app.py
选项卡:
- 表格:选择日期、收件人、主题,然后发送报告。
- 聊天(LLM):要求提供自然语言的报告;助手提取参数并触发电子邮件。集
OPENAI_API_KEY在.env或Streamlit的秘密。
测试MCP服务器
python3 agent_mcp.py --test
这将测试PostgreSQL和Gmail MCP服务器,并列出可用的工具。
运行代理(CLI)
python3 agent_mcp.py
您将被要求:
- 开始日期(YYYY-MM-DD)
- 结束日期(YYYY-MM-DD)
- 收件人(逗号分隔)
程序示例
import asyncio
from agent_mcp import MCPReportsAgent
async def generate_report():
agent = MCPReportsAgent()
result = await agent.generate_and_send_report(
date_from="2025-10-01",
date_to="2025-10-31",
recipients=["finance@company.com", "management@company.com"],
subject="Monthly Crypto Report"
)
print(result)
asyncio.run(generate_report())
与Claude Desktop一起使用
在Claude Desktop中配置MCP服务器。看 MCP_SDK_GUIDE.md.
📊 项目结构
mcp-reports-agent/
├── README.md # Main documentation (this)
├── MCP_SDK_GUIDE.md # MCP SDK guide
├── COMPARISON.md # Implementation comparison
├── QUICKSTART.md # Quick start
├── requirements.txt # Dependencies (MCP, Streamlit, OpenAI)
├── env.example # Env template
├── init_db.sql # DB initialization
│
├── app.py # 🌐 Streamlit UI (Form + Chat)
├── .streamlit/
│ └── config.toml # Streamlit theme/config
├── load_crypto_transactions.py # Excel loader → fact_transacciones_cripto
│
├── agent_mcp.py # ⭐ MCP client/orchestrator
├── mcp_server_postgres.py # MCP Server (PostgreSQL)
├── mcp_server_gmail.py # MCP Server (Gmail)
├── report_generator.py # HTML/CSV generator (legacy helper)
└── setup_gmail_oauth.py # OAuth2 helper
🔒 安全
- ✅ 凭据的环境变量
- ✅ 与PostgreSQL的TLS连接(如果启用)
- ✅ 日志中没有敏感数据
- ✅ 应用程序密码(dev)或Gmail的OAuth2(prod)
📧 电子邮件报告(加密货币)
主体包括:
- 期内合计:买入和卖出(∑金额×汇率,即monto×tc)
- 按购买量排名前5位的客户(∑金额×汇率)
- 销售额前5名客户(∑金额×汇率)
- 购买的前5个硬币(∑金额×兑换率)
- 售出前5名硬币(∑金额×兑换率)
- CSV附件:期间摘要
基准表: fact_transacciones_cripto(fecha, usuario, tipo_transaccion, criptomoneda, monto, tc)
🐛 故障排除
PostgreSQL连接
# Ensure PostgreSQL is running
pg_isready -h localhost -p 5432
# Check credentials
psql -h localhost -U postgres -d reports_db
Gmail身份验证
# Ensure API enabled / App Password valid or OAuth2 tokens configured
无结果
电子邮件明确指出“所选期间无结果”(如适用)。
📝 许可证
麻省理工学院