Skip to main content
Glama
tutran27
by tutran27

📦 StockPilot — AI-Powered Inventory Assistant with MCP


🎯 Mục đích dự án

StockPilot là dự án cá nhân thực hành và đào sâu chuẩn giao thức Model Context Protocol (MCP) của Anthropic thông qua bài toán thực tế: Quản lý kho hàng thông minh (Inventory Management).

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

  • Chuẩn MCP: Tách biệt rõ ràng giữa Chat Host, Client, và FastMCP Server.

  • Human-in-the-loop (HITL): Tự động phát hiện và yêu cầu người dùng xác nhận (PENDING $\rightarrow$ CONFIRMED) trước khi thực thi các thao tác nhạy cảm (nhập, xuất kho, tạo sản phẩm).

  • ACID & Idempotency: Thao tác Database an toàn qua asyncpg connection pool và chống trùng lặp giao dịch.

  • Docker Compose: Khởi chạy đồng bộ FastAPI, PostgreSQL 17 và pgAdmin chỉ với 1 lệnh.


Related MCP server: MCP Inventory Manager

🏗️ Kiến trúc hệ thống

flowchart TD
    User([👤 Người dùng]) <-->|REST API / Chat| Host[🌐 Chat Host / FastAPI]
    
    subgraph ChatHostApp ["Chat Host (Orchestrator)"]
        Host <--> Agent["🤖 ReAct Agent Loop"]
        Agent <--> LLM["🧠 LLM (Groq / OpenAI)"]
        Agent <--> HITL["🛡️ Human-in-the-loop Gate"]
        Agent <--> MCPClient["🔌 MCP Client"]
    end

    subgraph MCPServerApp ["MCP Server (StockPilot)"]
        MCPClient <==>|MCP Protocol| MCPServer["⚙️ FastMCP Server"]
        MCPServer --> ReadTools["📖 Read Tools (find, get_stock, alert)"]
        MCPServer --> WriteTools["✍️ Write Tools (receive, issue, add)"]
        ReadTools & WriteTools --> DBModule["💾 DB Layer (asyncpg)"]
    end

    DBModule <==>|Connection Pool| Postgres[(🐘 PostgreSQL)]

🛠️ Danh mục MCP Tools

Tool

Loại

Tham số chính

Mô tả

find_products

📖 Read

name_or_sku

Tìm kiếm sản phẩm theo tên hoặc SKU (mờ ILIKE).

get_stock

📖 Read

product_id

Lấy chi tiết tồn kho theo ID sản phẩm.

get_low_stock_products

📖 Read

limit=10

Cảnh báo danh sách sản phẩm sắp hết hàng.

get_transactions_limit

📖 Read

limit=10

Lấy lịch sử biến động kho gần nhất.

add_product

✍️ Write

sku, name, unit, ...

⚠️ Thêm mặt hàng mới vào danh mục (cần xác nhận).

receive_stock

✍️ Write

product_id, quantity, partner

⚠️ Nhập kho và lưu log giao dịch (cần xác nhận).

issue_stock

✍️ Write

product_id, quantity, partner

⚠️ Xuất kho sau khi kiểm tra số lượng tồn (cần xác nhận).


📁 Cấu trúc thư mục

stockpilot/
├── 📄 compose.yaml             # Docker Compose cho PostgreSQL & pgAdmin
├── 📄 Dockerfile               # Dockerfile cho Chat Host
├── 📄 requirements.txt         # Dependencies
├── 📄 .env.example             # Mẫu cấu hình biến môi trường
├── 📁 db/init.sql              # Schema Database & Index
├── 📁 scripts/seed_products.py # Nạp dữ liệu mẫu
├── 📁 src/
│   ├── 📁 mcp_server/          # FastMCP Server, Database & Tools
│   └── 📁 chat_host/           # FastAPI Host, ReAct Agent & HITL Confirmation
└── 📁 tests/                   # Test suite (Unit, Integration)

🚦 Hướng dẫn cài đặt & Chạy nhanh

1. Chuẩn bị môi trường & Database

# Clone repo & cấu hình .env
git clone https://github.com/tutran27/stockpilot-mcp.git
cd stockpilot-mcp
cp .env.example .env   # Điền GROQ_API_KEY hoặc OPENAI_API_KEY

# Khởi động PostgreSQL qua Docker
docker compose up -d

(pgAdmin xem DB tại http://localhost:5050admin@admin.com / admin).

2. Cài đặt thư viện & Nạp dữ liệu mẫu

python3 -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install -r requirements.txt

# Nạp dữ liệu sản phẩm mẫu vào DB
python scripts/seed_products.py

3. Khởi chạy & Truy cập hệ thống

docker compose up -d

🧪 Luồng trải nghiệm Human-in-the-loop

Bước 1: Gửi yêu cầu nhập hàng qua POST /api/chat

{
  "message": "Tôi muốn nhập 10 cái Laptop Dell XPS 13 từ NCC FPT, số HĐ: FPT-1234."
}

👉 Agent phản hồi và sinh mã xác nhận:

⚠️ Thao tác `receive_stock` cần bạn xác nhận trước khi thực hiện.
Mã hành động: `e70ce90c-575f-4462-81ae-672a29b26fdc`
Chi tiết: {'product_id': '...', 'quantity': 10, 'partner': 'FPT'}

Bước 2: Xác nhận thực thi qua POST /api/chat/confirm

{
  "action_id": "e70ce90c-575f-4462-81ae-672a29b26fdc"
}

Thao tác được chuyển trạng thái CONFIRMED và ghi nhận thành công vào Database!


📄 License

Phân phối dưới giấy phép MIT License.

F
license - not found
Not graded
quality - not tested
B
maintenance

Maintenance

0Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

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.
    18
    ISC

View all related MCP servers

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/tutran27/stockpilot-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server