README.mdβ’8.96 kB
# π BookStore MCP - Complete Setup Guide
## π― Project Overview
Full-featured bookstore MCP server vα»i 6 tools vΓ 4 interactive widgets, sα» dα»₯ng **Fractal MCP SDK** chΓnh thα»ng.
### Features:
- β
**6 MCP Tools**: search, detail, cart, checkout, history
- β
**4 React Widgets**: vα»i useWidgetProps, useWidgetState, useLayout, useGlobalContext
- β
**Zod Validation**: Type-safe input schemas
- β
**Structured Content**: Proper widget props
- β
**Dark/Light Theme**: Responsive design
- β
**In-Memory Database**: 8 sample books
---
## π¦ Step 1: Create Project
```bash
# Create directory
mkdir bookstore-mcp
cd bookstore-mcp
# Initialize npm
npm init -y
```
---
## π Step 2: Create File Structure
```bash
# Create directories
mkdir -p src/ui
mkdir -p src/server
mkdir -p dist/ui
mkdir -p dist/server
# Create files (copy from artifacts)
touch src/server/index.ts
touch src/server/database.ts
touch src/server/types.ts
touch src/ui/BookListWidget.tsx
touch src/ui/BookDetailWidget.tsx
touch src/ui/CartWidget.tsx
touch src/ui/OrderHistoryWidget.tsx
touch package.json
touch tsconfig.json
```
**Sao chΓ©p nα»i dung tα»« cΓ‘c artifacts vΓ o files tΖ°Ζ‘ng α»©ng:**
1. `package.json` - tα»« artifact "package.json"
2. `tsconfig.json` - tα»« artifact "tsconfig.json"
3. `src/server/types.ts` - tα»« artifact "src/server/types.ts"
4. `src/server/database.ts` - tα»« artifact "src/server/database.ts"
5. `src/server/index.ts` - tα»« artifact "src/server/index.ts - BookStore MCP Server"
6. `src/ui/BookListWidget.tsx` - tα»« artifact "src/ui/BookListWidget.tsx"
7. `src/ui/BookDetailWidget.tsx` - tα»« artifact "src/ui/BookDetailWidget.tsx"
8. `src/ui/CartWidget.tsx` - tα»« artifact "src/ui/CartWidget.tsx"
9. `src/ui/OrderHistoryWidget.tsx` - tα»« artifact "src/ui/OrderHistoryWidget.tsx"
---
## π₯ Step 3: Install Dependencies
```bash
# Install production dependencies
npm install @fractal-mcp/oai-hooks@latest @fractal-mcp/oai-server@latest zod
# Install dev dependencies
npm install --save-dev \
@fractal-mcp/cli@latest \
@types/node \
@types/react \
@types/react-dom \
react \
react-dom \
typescript \
vite
```
---
## ποΈ Step 4: Build Project
```bash
# Build React widgets first (creates HTML files)
npm run build:ui
# Build TypeScript server
npm run build:server
# Or build all at once
npm run build
```
- Or if error run
```bash
chmod +x fix-widgets.sh
./fix-widgets.sh
```
**Expected output:**
```
dist/
βββ ui/
β βββ book-list/index.html
β βββ book-detail/index.html
β βββ cart/index.html
β βββ order-history/index.html
βββ server/
βββ index.js
βββ database.js
βββ types.js
```
---
## π Step 5: Start Server Locally
```bash
npm start
```
**Expected console output:**
```
π Starting BookStore MCP Server...
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β π BOOKSTORE MCP SERVER β
β Status: β
Running β
β Port: 8000 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
---
## π§ͺ Step 6: Test Local Server
**Terminal 1 (Server):**
```bash
npm start
```
**Terminal 2 (Test):**
```bash
# Test health endpoint
curl http://localhost:8000/health
# Open in browser
open http://localhost:8000
```
BαΊ‘n sαΊ½ thαΊ₯y UI vα»i thΓ΄ng tin server!
---
## π Step 7: Deploy to Render
### 7.1 Push to GitHub
```bash
# Initialize git
git init
git add .
git commit -m "Initial commit: BookStore MCP"
# Create repo on GitHub, then:
git remote add origin https://github.com/YOUR_USERNAME/bookstore-mcp.git
git branch -M main
git push -u origin main
```
### 7.2 Deploy on Render
1. Truy cαΊp https://render.com
2. Click **"New +"** β **"Web Service"**
3. Connect GitHub repository
4. Settings:
- **Name:** `bookstore-mcp`
- **Environment:** `Node`
- **Build Command:** `npm install && npm run build`
- **Start Command:** `npm start`
- **Instance Type:** Free
5. Click **"Create Web Service"**
### 7.3 Get URL
Sau khi deploy xong (3-5 phΓΊt):
```
https://bookstore-mcp-xxxx.onrender.com
```
Test: Mα» URL trong browser, bαΊ‘n sαΊ½ thαΊ₯y server info page!
---
## π Step 8: Connect to ChatGPT
### Method 1: Direct URL (Recommended)
1. Mα» **ChatGPT** β **Settings** (βοΈ)
2. Click **"Apps SDK"** (hoαΊ·c "Integrations")
3. Click **"Add App"**
4. NhαΊp URL: `https://bookstore-mcp-xxxx.onrender.com`
5. ChatGPT sαΊ½ auto-discover MCP endpoint
6. Click **"Connect"**
### Method 2: MCP Endpoint
NαΊΏu method 1 khΓ΄ng work:
1. URL: `https://bookstore-mcp-xxxx.onrender.com/mcp`
2. Transport: **SSE (Server-Sent Events)**
3. No authentication required
---
## π¬ Step 9: Test in ChatGPT
Sau khi connect thΓ nh cΓ΄ng, thα» cΓ‘c lα»nh:
```
1. "Tìm sÑch vỠProgramming"
2. "TΓ¬m sΓ‘ch TypeScript giΓ‘ dΖ°α»i $50"
3. "Cho tΓ΄i xem chi tiαΊΏt sΓ‘ch cΓ³ ID lΓ 1"
4. "ThΓͺm sΓ‘ch ID 2 vΓ o giα» hΓ ng"
5. "ThΓͺm 3 cuα»n sΓ‘ch ID 3 vΓ o giα»"
6. "Xem giα» hΓ ng cα»§a tΓ΄i"
7. "Thanh toΓ‘n ΔΖ‘n hΓ ng"
8. "Xem lα»ch sα» ΔΖ‘n hΓ ng"
```
ChatGPT sαΊ½:
- β
Show text response
- β
Render interactive widget UI
- β
Widget cΓ³ dark/light theme
- β
Data update real-time
---
## π Troubleshooting
### Issue 1: Build fails
```bash
# Clean vΓ rebuild
npm run clean
npm install
npm run build
```
### Issue 2: Widget khΓ΄ng hiα»n thα»
**Check:**
```bash
# Verify widget HTML files exist
ls -la dist/ui/*/index.html
# Should output:
# dist/ui/book-list/index.html
# dist/ui/book-detail/index.html
# dist/ui/cart/index.html
# dist/ui/order-history/index.html
```
**Fix:**
```bash
npm run build:ui
```
### Issue 3: ChatGPT connection failed
**Verify endpoints:**
```bash
curl https://your-app.onrender.com/health
# Should return JSON with status: ok
curl https://your-app.onrender.com/mcp
# Should return SSE stream
```
**Common fixes:**
- ΔαΊ£m bαΊ£o Render service Δang running (khΓ΄ng sleep)
- Check Render logs for errors
- Verify PORT env variable (Render auto-sets this)
### Issue 4: Module not found errors
```bash
# Reinstall dependencies
rm -rf node_modules package-lock.json
npm install
```
---
## π Architecture
```
βββββββββββββββ
β ChatGPT β User asks: "TΓ¬m sΓ‘ch TypeScript"
ββββββββ¬βββββββ
β calls MCP tool: search-books
β
βββββββββββββββββββββββ
β MCP Server β
β (src/server) β
β β’ Register tools β
β β’ Process request β
β β’ Return data β
ββββββββ¬βββββββββββββββ
β returns structuredContent
β
βββββββββββββββββββββββ
β React Widget UI β
β (dist/ui/*.html) β
β β’ useWidgetProps() β
β β’ Render UI β
β β’ User interacts β
βββββββββββββββββββββββ
```
---
## π¨ Customization
### Add new book:
Edit `src/server/database.ts`:
```typescript
{
id: '9',
title: 'Your Book Title',
author: 'Author Name',
price: 39.99,
category: 'Programming',
// ... other fields
}
```
### Add new tool:
Edit `src/server/index.ts`:
```typescript
registerOpenAIWidget(
server,
{
id: "your-tool",
title: "Your Tool",
// ... config
},
async (args) => {
// Handler logic
}
);
```
### Modify widget UI:
Edit `src/ui/*.tsx` files, then:
```bash
npm run build:ui
```
---
## π Resources
- **Fractal MCP SDK:** https://github.com/fractal-mcp/sdk
- **OpenAI Apps SDK:** https://developers.openai.com/apps-sdk/
- **Model Context Protocol:** https://modelcontextprotocol.io
- **Render Docs:** https://render.com/docs
---
## β
Success Checklist
- [ ] Project created vα»i ΔΓΊng structure
- [ ] Dependencies installed
- [ ] Build successful (cΓ³ dist/ui/\*/index.html)
- [ ] Server chαΊ‘y local (port 8000)
- [ ] Deployed to Render
- [ ] ChatGPT connected
- [ ] All 6 tools hoαΊ‘t Δα»ng
- [ ] Widgets hiα»n thα» ΔΓΊng
- [ ] Theme switching works
---
## π Done!
BαΊ‘n ΔΓ£ cΓ³ mα»t **BookStore MCP Server** hoΓ n chα»nh vα»i:
- β
6 powerful tools
- β
4 beautiful widgets
- β
Full Fractal MCP SDK features
- β
ChatGPT integration
- β
Production-ready code
**Next steps:**
- Add more books to database
- Implement user authentication
- Add payment processing
- Deploy to production domain
- Share with community!
Happy coding! ππ