消息路由中枢系统

MCP Hub Server 是一个用于在工具间传递消息和共享上下文的中枢系统,支持LifeBuilder、Infinite PM和Knowledge Ingestor三个工具之间的协作。

  • by RYN6666999
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 RYN6666999/mcp-hub-mvp

使用说明

MCP Hub Server (Project 謀多多 / MANDALARY)

MCP Hub Server 是「懶人天才工作流系統」的中樞神經系統,負責在三個工具之間轉送訊息與共享上下文:

  • Tool 1: LifeBuilder - 執行與情緒(數位邊緣系統)
  • Tool 2: Infinite PM - 認知與規劃(外掛前額葉)
  • Tool 3: Knowledge Ingestor - 感知與記憶(數位海馬迴)

🏗️ Architecture

├── server/                 # Python FastAPI Backend
│   ├── api/               # API Routes
│   │   ├── tool_routes.py      # Tool 1/2/3 事件端點
│   │   ├── session_routes.py   # Session/Event 查詢
│   │   └── feedback_routes.py  # 反饋循環端點
│   ├── services/          # Business Logic
│   │   ├── mcp_router.py       # MCP 路由規則引擎
│   │   └── gemini_service.py   # Gemini AI 整合
│   ├── db/                # Database Layer
│   │   ├── models.py           # SQLAlchemy ORM
│   │   └── schemas.py          # Pydantic Schemas
│   └── infra/             # Infrastructure
│       ├── config.py           # 設定管理
│       └── database.py         # 資料庫連接
├── client/                # React Frontend (Simulator UI)
│   └── src/
│       ├── App.tsx             # 主應用元件
│       ├── api.ts              # API Client
│       └── types.ts            # TypeScript 型別
└── worker/                # n8n Configuration (Future)

🚀 Quick Start

Prerequisites

  • Python 3.11+
  • Node.js 18+ (for frontend)
  • PostgreSQL database

Installation

# Install Python dependencies
pip install -r server/requirements.txt

# Install frontend dependencies
cd client && npm install

Run Development

# Backend (port 5000)
uvicorn server.main:app --host 0.0.0.0 --port 5000 --reload

# Frontend (included in backend static files)
cd client && npm run build

📡 API Endpoints

Method Endpoint Description
POST /api/v1/tool1/events Tool 1 (LifeBuilder) 事件輸入
POST /api/v1/tool2/events Tool 2 (Infinite PM) 事件輸入
POST /api/v1/tool3/events Tool 3 (Knowledge Ingestor) 事件輸入
GET /api/v1/sessions 列出所有 Sessions (分頁)
GET /api/v1/sessions/:id/events 取得 Session 的所有 Events
GET /api/v1/decisions 查詢決策記錄
POST /api/v1/feedback 提交反饋
GET /health 健康檢查

🧪 Testing with cURL

1. Tool 1: User Input → Plan Request (Rule A)

curl -X POST http://localhost:5000/api/v1/tool1/events \
  -H "Content-Type: application/json" \
  -d '{
    "event_type": "user_input",
    "payload": {
      "message": "我想建立一個後端伺服器"
    }
  }'

Expected Response:

{
  "session_id": "uuid",
  "event_id": "uuid",
  "routing_log": "Rule A: Triggering Planner (Tool 2)",
  "next_action": {
    "next_tool": "TOOL2",
    "action": "plan_request",
    "context": { "user_message": "我想建立一個後端伺服器" }
  }
}

2. Tool 2: Plan Result → Archive (Rule B)

curl -X POST http://localhost:5000/api/v1/tool2/events \
  -H "Content-Type: application/json" \
  -d '{
    "session_id": "YOUR_SESSION_ID",
    "event_type": "plan_result",
    "payload": {
      "steps": ["初始化專案", "設定路由", "部署"],
      "topic": "backend_server"
    }
  }'

Expected Response:

{
  "session_id": "uuid",
  "event_id": "uuid",
  "routing_log": "Rule B: Archiving Decision",
  "next_action": {
    "action": "archived",
    "decision_record_id": "uuid"
  }
}

🔀 MCP Routing Rules

Rule Trigger Action
Rule A TOOL1 + user_input 觸發 Tool 2 規劃
Rule B TOOL2 + plan_result 歸檔決策記錄
Rule C Default 記錄日誌

🔧 Environment Variables

Variable Description
DATABASE_URL PostgreSQL 連接字串
GEMINI_API_KEY Google Gemini API Key
N8N_WEBHOOK_URL n8n Webhook URL

🧪 Running Tests

cd server
python -m pytest tests/ -v

📦 Tech Stack

Backend:

  • Python 3.11
  • FastAPI 0.109
  • SQLAlchemy 2.0
  • Pydantic 2.6
  • Google Generative AI SDK

Frontend:

  • React 18
  • Vite
  • TypeScript
  • Lucide Icons

Database:

  • PostgreSQL (Supabase/Neon)

📄 License

MIT