Skip to main content
Glama
tutran27
by tutran27

📦 StockPilot — AI-Powered Inventory Assistant with Model Context Protocol (MCP)


🌐 Live Demo & Trải nghiệm Trực tuyến


Related MCP server: MCP Inventory Manager

🎯 Điểm nổi bật & Tính năng cốt lõi

  • 🔌 Chuẩn MCP Toàn diện: Hỗ trợ đầy đủ Tools (tra cứu/nhập/xuất kho), Resources (báo cáo realtime), và Prompts (/audit, /restock).

  • 🛡️ Human-in-the-loop (HITL): Tự động chặn và yêu cầu người dùng bấm nút duyệt (CONFIRMED) trước khi thực hiện thao tác nhạy cảm.

  • 🧠 Bộ nhớ Ngữ cảnh Thông minh: Tự động nhớ thực thể (Working State: mã SKU, NCC, số HĐ) và quản lý đa phiên chat (Multi-Session).

  • 🔒 Giao dịch An toàn (ACID & Idempotency): Khóa giao dịch và chống xuất/nhập kho trùng lặp dữ liệu.


🛠️ Công nghệ cốt lõi (Tech Stack)

Thành phần

Công nghệ nổi bật

Điểm nhấn kỹ thuật

Giao thức MCP

Model Context Protocol (mcp SDK)

Chuẩn hóa kết nối giữa AI Agent và FastMCP Server (Tools, Resources, Prompts).

AI Orchestration

ReAct Agent + Groq / OpenAI

Phân tích ngôn ngữ tự nhiên, tự động gọi Tool và duy trì bộ nhớ ngữ cảnh (Working State).

Backend & API

FastAPI (Async Python 3.12)

Xử lý bất đồng bộ hiệu năng cao, tích hợp cổng kiểm soát an toàn (Human-in-the-loop).

Database

PostgreSQL 17 (Supabase)

Quản lý kho chuẩn ACID, chống trùng lặp dữ liệu (idempotency_key), Connection Pooling.

Frontend & Cloud

Streamlit + Railway

Giao diện Web tương tác thời gian thực, triển khai container hóa tự động từ GitHub.


🏗️ Kiến trúc luồng xử lý hệ thống

flowchart TD
    subgraph ClientLayer ["1. Lớp Giao diện & Người dùng"]
        User([👤 Người dùng]) <-->|Web UI| Streamlit["💻 Streamlit Web App (tutran27-stockpilot-mcp)"]
    end

    subgraph HostLayer ["2. Lớp Điều phối (Chat Host / FastAPI)"]
        Streamlit <-->|REST API / JSON| Host["🌐 FastAPI Host (Railway)"]
        Host <--> Agent["🤖 ReAct Agent Loop"]
        Agent <--> LLM["🧠 LLM (Groq / OpenAI)"]
        Agent <--> Memory["🧠 Working State & Session Memory"]
        Agent <--> HITL["🛡️ Human-in-the-loop Gate"]
        Agent <--> MCPClient["🔌 MCP Client"]
    end

    subgraph ServerLayer ["3. Lớp Giao thức MCP (FastMCP Server)"]
        MCPClient <==>|MCP Protocol| MCPServer["⚙️ FastMCP Server"]
        MCPServer --> ReadTools["📖 Read Tools (find, get_stock, alert)"]
        MCPServer --> WriteTools["✍️ Write Tools (receive, issue, add)"]
        MCPServer --> Resources["📊 Resources (summary, policy)"]
        MCPServer --> Prompts["📝 Prompts (audit, restock)"]
    end

    subgraph DBLayer ["4. Lớp Dữ liệu (Supabase Cloud Database)"]
        ReadTools & WriteTools & Memory --> DBModule["💾 asyncpg Pool (statement_cache_size=0)"]
        DBModule <==>|PgBouncer Pooler| Postgres[(🐘 Supabase PostgreSQL 17)]
    end

📁 Cấu trúc thư mục dự án

stockpilot/
├── 📄 compose.yaml             # Cấu hình Docker Compose chuẩn Production
├── 📄 Dockerfile               # Đóng gói Chat Host & MCP Server
├── 📄 requirements.txt         # Danh mục thư viện phụ thuộc
├── 📄 .env.example             # File mẫu biến môi trường
├── 📁 db/
│   └── 📄 init.sql             # Khởi tạo Schema DB, Indexes và ràng buộc FK
├── 📁 scripts/
│   └── 📄 seed_products.py     # Nạp dữ liệu sản phẩm mẫu
├── 📁 src/
│   ├── 📁 chat_host/           # Chat Host Orchestrator
│   │   ├── 📄 main.py          # FastAPI Endpoints (/api/chat, /confirm, /cancel)
│   │   ├── 📄 agent.py         # Vòng lặp Agent ReAct & MCP Tool Routing
│   │   ├── 📄 memory.py        # Bộ nhớ Working State & Ánh xạ thực thể
│   │   ├── 📄 confirmation.py  # Xử lý Human-in-the-loop (Pending Actions)
│   │   ├── 📄 llm.py           # Kết nối AsyncOpenAI / Groq LLM
│   │   └── 📄 prompts.py       # System Prompts & Working State Injection
│   ├── 📁 mcp_server/          # MCP Server Layer
│   │   ├── 📄 server.py        # Định nghĩa FastMCP Server & Đăng ký Tool/Resource/Prompt
│   │   ├── 📄 db.py            # Thao tác PostgreSQL (asyncpg connection pool)
│   │   ├── 📄 tools_read.py    # MCP Read Tools
│   │   ├── 📄 tools_write.py   # MCP Write Tools
│   │   ├── 📄 resources.py     # MCP Resources
│   │   └── 📄 prompts.py       # MCP Prompts Templates
│   └── 📁 ui/
│       └── 📄 app.py           # Giao diện Web Chat Streamlit

🚦 Hướng dẫn Khởi chạy Môi trường Local (Self-Hosted)

1. Kéo mã nguồn & Cài đặt môi trường

git clone https://github.com/tutran27/stockpilot-mcp.git
cd stockpilot-mcp
cp .env.example .env

(Mở file .env và điền GROQ_API_KEY hoặc OPENAI_API_KEY, cùng chuỗi kết nối Database).

2. Khởi chạy trọn gói bằng Docker Compose

docker compose up -d --build

3. Truy cập các cổng dịch vụ Local:


🧪 Kịch bản Trải nghiệm Mẫu (Demo Flows)

Kịch bản 1: Ghi nhớ Ngữ cảnh Thông minh (Working State)

  1. Người dùng: "Kiểm tra mặt hàng Dell XPS 13"

    • 🤖 AI: Tra cứu và báo tồn kho hiện có 15 chiếc (ID: bf9ddbda-...).

  2. Người dùng: "Nhập thêm 10 cái từ NCC FPT, số HĐ: FPT-8899"

    • 🤖 AI: Tự động nhận diện sản phẩm đang nói đến là Dell XPS 13, nạp mã hóa đơn FPT-8899, nhà cung cấp FPT và tạo yêu cầu xác nhận receive_stock.

Kịch bản 2: Duyệt hành động nhạy cảm (Human-in-the-loop)

  • Khi AI yêu cầu xác nhận, giao diện Web sẽ hiển thị thẻ chi tiết kèm 2 nút bấm:

    • 🟢 Xác nhận thực hiện: Gọi POST /api/chat/confirm, cập nhật tồn kho tức thì và khóa nút.

    • 🔴 Hủy bỏ: Hủy bỏ thao tác an toàn.


📄 License

Dự án được phân phối dưới giấy phép MIT License.

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    C
    maintenance
    Enables interaction with ScanPower's inventory and shipment management system through a secure MCP interface. Supports retrieving inventory data, creating shipment plans, and managing logistics operations through natural language.
    2
    -
  • F
    license
    Not graded
    quality
    D
    maintenance
    An AI-powered inventory management system with a natural language interface, enabling CRUD operations on items and suppliers, stock transfers, and supplier management via MCP tools.
    -
  • A
    license
    Not graded
    quality
    B
    maintenance
    Unifies inventory data from four sources (Jikeyun, Supor factory, WeChat Excel, RPA) with timestamps, and exposes MCP tools for AI agents to query stock levels.
    6 npm
    ISC