使用说明
🚀 MCP Streamable Servers
MCP (Model Context Protocol) Servers ที่รองรับทั้ง HTTP และ SSE (Server-Sent Events) สำหรับข้อมูลสภาพอากาศและข้อมูลสินค้า
📋 เซิร์ฟเวอร์ที่มีให้
🌤️ Weather Server
- HTTP Version:
src/weather-server.ts(Port 3003) - SSE Version:
src/weather-server-sse.ts(Port 3004) - Features: ข้อมูลสภาพอากาศ, พยากรณ์อากาศ, Real-time updates
📱 Product Server
- HTTP Version:
src/product-server.ts(Port 3002) - SSE Version:
src/product-server-sse.ts(Port 3005) - Features: ข้อมูลสินค้า, ค้นหาสินค้า, ตรวจสอบสต็อค, Real-time stock/price updates
🛠️ การติดตั้งและเรียกใช้
1. ติดตั้ง Dependencies
npm install
2. เรียกใช้เซิร์ฟเวอร์
HTTP Servers:
# Weather Server (Port 3003)
npm run dev:weather
# Product Server (Port 3002)
npm run dev:product
# รันทั้งสองเซิร์ฟเวอร์พร้อมกัน
npm run dev:all
SSE Servers:
# Weather Server SSE (Port 3004)
npm run dev:weather-sse
# Product Server SSE (Port 3005)
npm run dev:product-sse
# รันทั้งสองเซิร์ฟเวอร์ SSE พร้อมกัน
npm run dev:all-sse
3. ทดสอบการทำงาน
Health Check:
# HTTP Servers
npm run test:health
# SSE Servers
npm run test:health-sse
MCP Tools:
# HTTP Servers
npm run test:mcp
# SSE Servers
npm run test:mcp-sse
🌐 Demo Web Interface
Weather Server SSE Demo
เปิดไฟล์ demo/weather-sse-demo.html ในเบราว์เซอร์
- URL:
file:///path/to/demo/weather-sse-demo.html - เชื่อมต่อกับ:
http://localhost:3004
Product Server SSE Demo
เปิดไฟล์ demo/product-sse-demo.html ในเบราว์เซอร์
- URL:
file:///path/to/demo/product-sse-demo.html - เชื่อมต่อกับ:
http://localhost:3005
🔌 API Endpoints
HTTP Servers
Weather Server (Port 3003)
GET /health- Health checkPOST /mcp- MCP requestsOPTIONS /mcp- CORS preflight
Product Server (Port 3002)
GET /health- Health checkPOST /mcp- MCP requestsOPTIONS /mcp- CORS preflight
SSE Servers
Weather Server SSE (Port 3004)
GET /health- Health checkGET /mcp- SSE connectionPOST /mcp- MCP requestsOPTIONS /mcp- CORS preflight
Product Server SSE (Port 3005)
GET /health- Health checkGET /mcp- SSE connectionPOST /mcp- MCP requestsOPTIONS /mcp- CORS preflight
🛠️ MCP Tools
Weather Server Tools
-
get-weather - ดูสภาพอากาศปัจจุบัน
{ "name": "get-weather", "arguments": { "city": "Bangkok" } } -
get-weather-forecast - ดูพยากรณ์อากาศ
{ "name": "get-weather-forecast", "arguments": { "city": "Bangkok", "days": 3 } }
Product Server Tools
-
get-product - ดูข้อมูลสินค้า
{ "name": "get-product", "arguments": { "productId": "P001" } } -
search-products - ค้นหาสินค้า
{ "name": "search-products", "arguments": { "query": "iPhone" } } -
check-stock - ตรวจสอบสต็อค
{ "name": "check-stock", "arguments": { "productId": "P001" } } -
get-product-specs - ดูสเปคสินค้า
{ "name": "get-product-specs", "arguments": { "productId": "P001" } }
📡 SSE Events
Weather Server SSE Events
connection- การเชื่อมต่อสำเร็จserver_info- ข้อมูลเซิร์ฟเวอร์weather_update- อัปเดตสภาพอากาศแบบ real-time (ทุก 30 วินาที)mcp_response- ผลลัพธ์ MCP requestsmcp_error- ข้อผิดพลาด MCPserver_shutdown- เซิร์ฟเวอร์ปิดตัว
Product Server SSE Events
connection- การเชื่อมต่อสำเร็จserver_info- ข้อมูลเซิร์ฟเวอร์stock_update- อัปเดตสต็อคแบบ real-time (ทุก 45 วินาที)price_update- อัปเดตราคาแบบ real-time (ทุก 60 วินาที)mcp_response- ผลลัพธ์ MCP requestsmcp_error- ข้อผิดพลาด MCPserver_shutdown- เซิร์ฟเวอร์ปิดตัว
🧪 การทดสอบด้วย Tools
cURL Examples
List Tools:
curl -X POST http://localhost:3002/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
Search Products:
curl -X POST http://localhost:3002/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"search-products","arguments":{"query":"iPhone"}}}'
Get Weather:
curl -X POST http://localhost:3003/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"get-weather","arguments":{"city":"Bangkok"}}}'
Postman Testing
- Method: POST
- URL:
http://localhost:3002/mcp(Product) หรือhttp://localhost:3003/mcp(Weather) - Headers:
Content-Type: application/json Accept: application/json - Body (JSON):
{ "jsonrpc": "2.0", "id": 1, "method": "tools/list" }
SSE Testing
เชื่อมต่อ SSE:
const eventSource = new EventSource('http://localhost:3004/mcp');
eventSource.onmessage = function(event) {
const data = JSON.parse(event.data);
console.log('SSE Event:', data);
};
ส่ง MCP Request:
fetch('http://localhost:3004/mcp', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'tools/call',
params: {
name: 'get-weather',
arguments: { city: 'Bangkok' }
}
})
});
📊 Port Summary
| Service | Type | Port | URL |
|---|---|---|---|
| Product Server | HTTP | 3002 | http://localhost:3002 |
| Weather Server | HTTP | 3003 | http://localhost:3003 |
| Weather Server | SSE | 3004 | http://localhost:3004 |
| Product Server | SSE | 3005 | http://localhost:3005 |
🔧 Development
Build:
npm run build
Clean:
npm run clean
Production:
npm run build
npm run start:weather
npm run start:product
📝 License
MIT License
🤝 Contributing
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
📞 Support
หากพบปัญหาหรือต้องการความช่วยเหลือ กรุณาสร้าง issue ใน GitHub repository
