Skip to main content

FastMCP server for NTHU Complaints System

Project description

NTHU Complaints System - FastMCP Server

一個專為 NTHU 申訴系統設計的 FastMCP 伺服器,提供完整的申訴管理功能。

🚀 功能特點

🔐 用戶認證

  • 登入/登出: 安全的用戶會話管理
  • 會話驗證: 自動會話過期和驗證
  • 權限控制: 基於角色的存取控制

📝 申訴管理

  • 提交申訴: 支援匿名和實名申訴
  • 查看摘要: 獲取所有申訴的概覽
  • 詳細查看: 查看特定申訴的完整資訊
  • 狀態追蹤: 即時更新申訴處理狀態

🛠 系統監控

  • 伺服器狀態: 監控伺服器和資料庫連線
  • 會話管理: 追蹤活躍用戶會話

📦 安裝方式

使用 UVX (推薦)

# 直接執行,無需手動安裝
uvx nthu-complaints-mcp

使用 UV

# 安裝到全域環境
uv tool install nthu-complaints-mcp

# 執行
nthu-complaints-mcp

使用 Pip

# 從本地安裝
pip install .

# 或從 PyPI 安裝 (如果已發布)
pip install nthu-complaints-mcp

# 執行
nthu-complaints-mcp

⚙️ 設置

1. Firebase 配置

在執行前,需要設置 Firebase 服務帳戶金鑰:

  1. Firebase Console 下載服務帳戶金鑰
  2. 將檔案放置在以下任一位置:
    • ./firebase-service-account.json (當前目錄)
    • ~/.nthu-complaints/firebase-service-account.json (用戶目錄)
    • /etc/nthu-complaints/firebase-service-account.json (系統目錄)

2. 環境變數 (可選)

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/firebase-service-account.json"

🔧 開發設置

本地開發

# 克隆倉庫
git clone <repository-url>
cd nthu-complaints-mcp

# 使用 UV 設置開發環境
uv venv
source .venv/bin/activate  # Linux/macOS
# .venv\Scripts\activate   # Windows

# 安裝依賴
uv pip install -e ".[dev]"

# 執行伺服器
python -m nthu_complaints_mcp.server

測試

# 執行測試
pytest

# 執行測試並顯示覆蓋率
pytest --cov=nthu_complaints_mcp

代碼品質

# 格式化代碼
black src/
isort src/

# 類型檢查
mypy src/

📚 API 參考

可用工具

工具名稱 描述 參數
mcp_login_user 用戶登入 email, password
mcp_logout_user 用戶登出 session_token
mcp_check_session_status 檢查會話狀態 session_token
mcp_submit_complaint 提交申訴 session_token, title, description, category, department, priority, anonymous, contact_info
mcp_get_user_complaints_summary 獲取申訴摘要 session_token
mcp_get_complaint_details 獲取申訴詳情 session_token, case_number
mcp_server_status 獲取伺服器狀態

使用範例

1. 登入用戶

result = mcp_login_user("user@example.com", "password123")
if result["success"]:
    session_token = result["session_token"]
    print(f"登入成功: {result['user']['display_name']}")

2. 提交申訴

complaint = mcp_submit_complaint(
    session_token=session_token,
    title="圖書館設備問題",
    description="圖書館的電腦無法正常使用,影響學習效率",
    category="設施設備",
    department="圖書館",
    priority="medium",
    anonymous=False,
    contact_info={"phone": "0912345678", "preferred_contact": "email"}
)

if complaint["success"]:
    print(f"申訴已提交,案件編號: {complaint['case_number']}")

3. 查看申訴摘要

summary = mcp_get_user_complaints_summary(session_token)
if summary["success"]:
    print(f"總共有 {summary['total_count']} 個申訴")
    for complaint in summary["complaints"]:
        print(f"- {complaint['case_number']}: {complaint['title']} ({complaint['status']})")

4. 查看申訴詳情

details = mcp_get_complaint_details(session_token, "NTHU20241219120000")
if details["success"]:
    complaint = details["complaint"]
    print(f"標題: {complaint['title']}")
    print(f"狀態: {complaint['status']}")
    print(f"描述: {complaint['description']}")

🏗 架構

模組結構

src/nthu_complaints_mcp/
├── __init__.py          # 套件初始化
├── config.py            # 配置管理
├── firebase_client.py   # Firebase 客戶端
├── session_manager.py   # 會話管理
├── tools.py            # MCP 工具實現
└── server.py           # 主伺服器

資料流

  1. 認證: 用戶通過 mcp_login_user 登入,獲得會話令牌
  2. 授權: 每個操作都需要有效的會話令牌
  3. 資料存取: 通過 Firebase Firestore 進行資料持久化
  4. 會話管理: 自動處理會話過期和清理

🔒 安全性

  • 會話管理: 使用 UUID 作為會話令牌,自動過期機制
  • 權限控制: 用戶只能查看自己的申訴 (管理員除外)
  • 資料驗證: 所有輸入都經過驗證和清理
  • Firebase 安全規則: 配合 Firestore 安全規則進行多層保護

🐛 故障排除

常見問題

Firebase 初始化失敗

解決方案:
1. 檢查 firebase-service-account.json 是否存在
2. 確認 JSON 格式正確
3. 驗證 Firebase 專案設置

用戶登入失敗

解決方案:
1. 確認 Firebase Authentication 已啟用
2. 檢查用戶是否存在於 Firestore 'users' 集合
3. 驗證用戶權限設置

權限被拒絕

解決方案:
1. 檢查 Firestore 安全規則
2. 確認服務帳戶權限
3. 驗證用戶角色設置

日誌和調試

# 啟用詳細日誌
export PYTHONPATH=.
python -m nthu_complaints_mcp.server --log-level DEBUG

📄 許可證

MIT License - 詳見 LICENSE 檔案

🤝 貢獻

歡迎提交 Issue 和 Pull Request!

開發流程

  1. Fork 專案
  2. 創建功能分支 (git checkout -b feature/amazing-feature)
  3. 提交更改 (git commit -m 'Add amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 開啟 Pull Request

📞 支援


注意: 這是一個開發版本,建議在生產環境中使用前進行充分測試。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nthu_complaints_mcp-1.7.0.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nthu_complaints_mcp-1.7.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file nthu_complaints_mcp-1.7.0.tar.gz.

File metadata

  • Download URL: nthu_complaints_mcp-1.7.0.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for nthu_complaints_mcp-1.7.0.tar.gz
Algorithm Hash digest
SHA256 d814711d05656a08a153e7976498387ada0891e89c469147e38c3b7f462a11ed
MD5 328aa0526988e93ece886791d1d86b3d
BLAKE2b-256 be5d66b339644fc3034aa565f9ffa5af4a1a3ab95bc1351a16f9bf7facda7828

See more details on using hashes here.

File details

Details for the file nthu_complaints_mcp-1.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nthu_complaints_mcp-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed74a544bd162eeca48519a1e858d995cdd6f4072aff9966d8073bbfd6675765
MD5 fc8b1c03822af308f451787bcf7d8f3b
BLAKE2b-256 442a27def00de619e84281eb9af70f422d21eadf2cc01d8a2030849a27668166

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page