使用说明
Email Sender MCP Server
這是一個使用 Model Context Protocol (MCP) 的郵件發送服務,使用 SSE (Server-Sent Events) 傳輸協議,部署在 Google Cloud Run 上。
✨ 功能特色
- ✉️ 發送自訂郵件
- 🎃 發送萬聖節邀請郵件(預設模板)
- 🚨 發送系統異常警示郵件(預設模板)
- 📎 支援附件
- 🌐 HTTP/SSE 傳輸協議
- ☁️ 部署在 Google Cloud Run
🚀 快速開始
已部署的服務
服務 URL: https://email-sender-mcp-jt7pjdeeoa-de.a.run.app
測試連線
curl https://email-sender-mcp-jt7pjdeeoa-de.a.run.app/sse
📖 使用方式
在 Claude Desktop 中使用
-
編輯 Claude Desktop 配置文件
macOS:
~/Library/Application Support/Claude/claude_desktop_config.jsonWindows:
%APPDATA%\Claude\claude_desktop_config.json -
添加 MCP Server 配置
{ "mcpServers": { "email-sender": { "url": "https://email-sender-mcp-jt7pjdeeoa-de.a.run.app/sse", "transport": "sse" } } } -
重啟 Claude Desktop
-
開始使用
在 Claude Desktop 中,您可以直接說:
- "幫我發送一封郵件給 example@gmail.com"
- "發送萬聖節邀請給 friend@gmail.com"
- "發送系統警示郵件給 admin@company.com"
在其他 MCP 客戶端中使用
任何支援 MCP over SSE 的客戶端都可以連接:
// 連接配置
{
"url": "https://email-sender-mcp-jt7pjdeeoa-de.a.run.app/sse",
"transport": "sse"
}
使用 curl 測試(手動調用)
# 健康檢查
curl https://email-sender-mcp-jt7pjdeeoa-de.a.run.app/health
# SSE 端點
curl https://email-sender-mcp-jt7pjdeeoa-de.a.run.app/sse
🛠️ MCP Tools 說明
1. send_email - 發送自訂郵件
發送完全自訂的郵件,包括主旨、內容和附件。
參數:
receiver_email(必要): 收件者郵件地址subject(必要): 郵件主旨body(必要): 郵件內容attachment_path(可選): 附件路徑
範例使用(在 Claude 中):
幫我發送郵件給 john@example.com
主旨:會議通知
內容:明天下午 3 點開會,請準時參加。
2. send_halloween_invitation - 萬聖節邀請
使用預設的萬聖節邀請模板發送郵件。
參數:
receiver_email(必要): 收件者郵件地址
範例使用(在 Claude 中):
發送萬聖節邀請給 nanako@example.com
3. send_system_alert - 系統警示通知
使用預設的系統異常警示模板發送郵件(適用於 IT 運維場景)。
參數:
receiver_email(必要): 收件者郵件地址
範例使用(在 Claude 中):
發送系統警示給 admin@company.com
💻 本地開發
1. 安裝依賴
pip install -r requirements.txt
2. 設定環境變數
建立 .env 文件:
EMAIL_ACCOUNT=your_email@gmail.com
EMAIL_PASSWORD=your_app_password
⚠️ 重要: Gmail 需使用「應用程式密碼」而非帳號密碼
設定方式:
- 前往 Google 帳戶安全性設定
- 啟用「兩步驟驗證」
- 搜尋「應用程式密碼」
- 選擇「郵件」和「其他」,生成密碼
- 將生成的 16 位密碼填入
EMAIL_PASSWORD
3. 執行 MCP Server
python server.py
服務會在 http://localhost:8080 啟動。
4. 本地測試
# 健康檢查
curl http://localhost:8080/health
# SSE 端點
curl http://localhost:8080/sse
☁️ 部署到 Google Cloud Run
前置需求
-
安裝 gcloud CLI
# macOS brew install --cask google-cloud-sdk # 初始化 gcloud init -
安裝 Docker Desktop
# macOS brew install --cask docker
部署步驟
-
設定
.env文件(已完成) -
執行部署腳本
chmod +x deploy.sh ./deploy.sh
部署腳本會自動:
- ✅ 讀取
.env環境變數 - ✅ 建置 Docker 映像
- ✅ 推送到 Google Container Registry
- ✅ 部署到 Cloud Run
- ✅ 顯示服務 URL
查看服務狀態
# 查看日誌
gcloud run logs tail email-sender-mcp \
--region=asia-east1 \
--project=itr-aimasteryhub-lab
# 查看服務資訊
gcloud run services describe email-sender-mcp \
--region=asia-east1 \
--project=itr-aimasteryhub-lab
更新部署
當您修改代碼後,只需再次執行:
./deploy.sh
🔐 安全性建議
1. 環境變數保護
- ✅
.env文件已加入.gitignore,不會提交到 Git - ✅ 敏感資訊通過環境變數傳遞到 Cloud Run
- ⚠️ 不要在代碼中硬編碼密碼
2. 使用 Secret Manager(進階)
# 將密碼存到 Secret Manager
echo -n "your_password" | gcloud secrets create email-password \
--data-file=- \
--project=itr-aimasteryhub-lab
# 更新 Cloud Run 使用 Secret
gcloud run services update email-sender-mcp \
--region=asia-east1 \
--update-secrets=EMAIL_PASSWORD=email-password:latest \
--project=itr-aimasteryhub-lab
3. 限制存取權限
預設部署允許未經驗證的存取。如需限制:
# 移除公開存取
gcloud run services remove-iam-policy-binding email-sender-mcp \
--region=asia-east1 \
--member="allUsers" \
--role="roles/run.invoker" \
--project=itr-aimasteryhub-lab
# 授予特定使用者存取權限
gcloud run services add-iam-policy-binding email-sender-mcp \
--region=asia-east1 \
--member="user:example@gmail.com" \
--role="roles/run.invoker" \
--project=itr-aimasteryhub-lab
📊 監控與維護
查看即時日誌
gcloud run logs tail email-sender-mcp \
--region=asia-east1 \
--project=itr-aimasteryhub-lab
查看歷史日誌
gcloud run logs read email-sender-mcp \
--region=asia-east1 \
--project=itr-aimasteryhub-lab \
--limit=50
更新環境變數
gcloud run services update email-sender-mcp \
--region=asia-east1 \
--set-env-vars="EMAIL_ACCOUNT=new_email@gmail.com,EMAIL_PASSWORD=new_password" \
--project=itr-aimasteryhub-lab
💰 成本估算
Google Cloud Run 採用按使用量計費:
- 免費額度: 每月 200 萬次請求
- CPU: $0.00002400/vCPU-秒
- 記憶體: $0.00000250/GiB-秒
- 請求: $0.40/百萬次
- 網路出站: $0.12/GB
對於一般郵件服務使用量,通常每月成本 < $1 USD,甚至完全在免費額度內。
🐛 常見問題排除
Q: 郵件發送失敗,顯示 "SMTP Authentication Error"
A: 請確認:
- 使用的是 Gmail 應用程式密碼,而非帳號密碼
- 已啟用 Google 帳戶的兩步驟驗證
.env文件中的EMAIL_PASSWORD沒有多餘的空格或引號
Q: Cloud Run 部署失敗
A: 檢查:
- Docker Desktop 是否正在運行
- gcloud CLI 是否已登入:
gcloud auth list - 專案 ID 是否正確
- 必要的 API 是否已啟用
Q: 如何測試 MCP Server 是否正常運行?
A:
# 測試健康檢查端點
curl https://email-sender-mcp-jt7pjdeeoa-de.a.run.app/health
# 應該返回:{"status": "healthy"}
Q: 如何在 Claude Desktop 中驗證 MCP Server 已連接?
A:
- 重啟 Claude Desktop
- 在對話中嘗試說:"列出可用的工具"
- 應該可以看到
send_email、send_halloween_invitation、send_system_alert三個工具
📚 技術棧
- 語言: Python 3.11
- 框架: Starlette + Uvicorn
- 協議: Model Context Protocol (MCP) over SSE
- 部署: Google Cloud Run
- 容器: Docker
📝 檔案結構
MCP_sent_mail/
├── server.py # MCP Server 主程式(SSE)
├── sent_mail.py # 原始郵件發送腳本(獨立使用)
├── requirements.txt # Python 依賴套件
├── Dockerfile # Docker 建置配置
├── .dockerignore # Docker 忽略檔案
├── deploy.sh # 自動部署腳本
├── cloudbuild.yaml # Cloud Build 配置
├── .env # 環境變數(不提交到 Git)
└── README.md # 本文件
🔗 相關資源
📄 License
MIT
👤 作者
Created with ❤️ by the team
部署狀態: ✅ 已部署
服務 URL: https://email-sender-mcp-jt7pjdeeoa-de.a.run.app
最後更新: 2025/10/27