You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Đọc header X-Haravan-Api-Call-Limit từ mỗi response
Auto-throttle khi bucket usage > 60: sleep 1s
Auto-throttle khi bucket usage > 75: sleep 2s
Pagination (middleware/pagination.ts)
Trigger khi fetchAll=true trong params
Haravan API max 50 records/page
Auto-loop pages cho đến khi hết data hoặc đạt limit
Max 5000 records (safety cap)
API Handler (handler.ts)
Xây dựng Haravan API request từ tool definition
Bearer token authentication
JSON response parsing
Smart Tools vs Base Tools
Smart Tools (hrv_*)
Bypass middleware chain — có customHandler
Tự quản lý pagination qua helper fetchAll()
Tự quản lý rate limiting (sleep giữa pages)
Return aggregated summary (~200-800 tokens)
Client (Claude) nhận kết quả compact, tiết kiệm context
Base Tools (haravan_*)
Đi qua middleware chain đầy đủ
1:1 mapping với Haravan REST API endpoint
Return raw API response (stripped null fields)
Dùng cho detail view, CRUD operations, drill-down
fetchAll Helper
// Haravan API max 50/page → loop until doneasyncfunctionfetchAll(client,path,resourceKey,params,options){constPAGE_SIZE=50;for(letpage=1;page<=maxPages;page++){constresponse=awaitclient.get(path,{ ...params, page,limit: PAGE_SIZE});constitems=response.data[resourceKey];allItems.push(...items);if(items.length<PAGE_SIZE)break;// Last pageawaitsleep(250);// Throttle between pages}return{items: allItems, apiCalls };}
Authentication Flow
Private Token
haravan-mcp mcp -t <token>
│
▼
Bearer <token> in Authorization header
│
▼
Haravan API (apis.haravan.com)
OAuth 2.0
haravan-mcp login -a <app_id> -s <secret>
│
▼
Open browser → accounts.haravan.com/connect/authorize
│ │
│ ▼
│ User authorizes app
│ │
│ ▼
│ Redirect to localhost:3000/callback
│ │
▼ ▼
Exchange code for token → Store in ~/.haravan-mcp/tokens.json
│
▼
haravan-mcp mcp -a <app_id> --oauth
│
▼
Load stored token → Bearer auth
Configuration
mergeConfig Priority
CLI flags > Environment variables > Defaults
Config
CLI
Env
Default
Access Token
-t <token>
HARAVAN_ACCESS_TOKEN
—
App ID
-a <appId>
HARAVAN_APP_ID
—
App Secret
-s <secret>
HARAVAN_APP_SECRET
—
API Domain
-d <domain>
—
https://apis.haravan.com
Webhook Domain
--webhook-domain
—
https://webhook.haravan.com
Transport
-m stdio|http
—
stdio
Port
-p <port>
—
3000
Tools Filter
--tools <filter>
—
preset.default
Debug
--debug
—
false
Haravan API Constraints
Constraint
Giá trị
Ảnh hưởng
Rate Limit
80 bucket, 4 req/s drain
Smart tools throttle tự động
Max per page
50 records
fetchAll loop 50/page
Date format
ISO 8601
Skill layer tính date ranges
Currency
VND (ISO 4217)
Format với dấu phẩy nghìn
Auth
Bearer token
Header mỗi request
Claude Skill Layer
Vai trò: "Bộ não" điều phối MCP Server ("Cánh tay")
User câu hỏi tự nhiên
│
▼
Claude Skill (SKILL.md)
├── Phân loại intent (Store Pulse / Inventory / Customer / Action...)
├── Chọn tools phù hợp (smart > base)
├── Xác định params (date_from, date_to, limit...)
└── Gọi song song khi tools độc lập nhau
│ MCP tool calls
▼
MCP Server — thực thi heavy lifting
├── Smart Tools: pagination lớn, batch calls, full-population scoring
└── Base Tools: CRUD, detail lookup, drill-down
│ Compact JSON response (~200-800 tokens)
▼
Claude Skill — xử lý output
├── Áp dụng formulas (ODR, RFM, ABC-FSN, GMROI...)
├── So sánh với benchmarks (Cancel <3%, COD Fail <15%...)
├── Viết insight: [Metric] + [Con số] + [Context] → [Hành động] + [Impact]
└── Format markdown: bảng, ↑↓ arrows, ⚠️🔴 alerts
│
▼
User nhận output có ngữ cảnh, actionable
Decision Tree Flow
Câu hỏi về doanh thu / đơn hàng
→ hrv_orders_summary (+ compare period nếu có)
→ hrv_top_products (nếu hỏi sản phẩm)
→ hrv_order_cycle_time (nếu hỏi tốc độ xử lý)
Câu hỏi về khách hàng
→ hrv_customer_segments (RFM 8 phân khúc)
→ haravan_customers_search (nếu tìm khách cụ thể)
Câu hỏi về tồn kho
→ hrv_inventory_health (phân loại ABC-FSN)
→ hrv_stock_reorder_plan (cần nhập hàng)
→ hrv_inventory_imbalance (chuyển kho)
Câu hỏi tổng quan / scorecard
→ Gọi song song: hrv_orders_summary + hrv_customer_segments
+ hrv_inventory_health + hrv_top_products
Hành động (tạo/sửa/xóa)
→ Xác nhận user trước → gọi base tool tương ứng
Anti-patterns Skill ngăn chặn
Anti-pattern
Hậu quả
Skill fix
Gọi haravan_orders_list trong vòng lặp để đếm đơn
200,000+ tokens bị đốt
Dùng hrv_orders_summary thay thế
Gọi detail tool trong loop để "count" records
N × detail calls thừa
Dùng smart tool có aggregation sẵn
Không truyền date_from/date_to
Fetch toàn bộ lịch sử = timeout
Skill luôn tính date range trước
Ghi vào store (create/update/delete) không xác nhận
Thao tác không thể hoàn tác
Skill bắt buộc confirmation step
Gọi >6 tools cho 1 câu hỏi
Context window bị lãng phí
Skill giới hạn và dùng smart tools
Error Handling Flow
Tool call thất bại
│
├── 401 Unauthorized → Token hết hạn → Hướng dẫn refresh token
├── 403 Forbidden → Thiếu scope → Liệt kê scope cần thêm
├── 429 Too Many Requests → Rate limited → Thông báo + chờ Retry-After
└── 500 Server Error → Haravan API lỗi → Thử lại 1 lần, báo cáo nếu tiếp tục
English: This document describes the internal architecture of Haravan MCP server including transport modes, middleware chain, smart vs base tool design, and Haravan API constraints.