实时数据服务器

MCP Streamable Servers 是一个支持HTTP和SSE协议的实时数据服务器,提供天气和产品数据的实时更新。

  • by pong-pk007
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 pong-pk007/mcp-streamable-servers

使用说明

🚀 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 check
  • POST /mcp - MCP requests
  • OPTIONS /mcp - CORS preflight

Product Server (Port 3002)

  • GET /health - Health check
  • POST /mcp - MCP requests
  • OPTIONS /mcp - CORS preflight

SSE Servers

Weather Server SSE (Port 3004)

  • GET /health - Health check
  • GET /mcp - SSE connection
  • POST /mcp - MCP requests
  • OPTIONS /mcp - CORS preflight

Product Server SSE (Port 3005)

  • GET /health - Health check
  • GET /mcp - SSE connection
  • POST /mcp - MCP requests
  • OPTIONS /mcp - CORS preflight

🛠️ MCP Tools

Weather Server Tools

  1. get-weather - ดูสภาพอากาศปัจจุบัน

    {
      "name": "get-weather",
      "arguments": { "city": "Bangkok" }
    }
    
  2. get-weather-forecast - ดูพยากรณ์อากาศ

    {
      "name": "get-weather-forecast",
      "arguments": { "city": "Bangkok", "days": 3 }
    }
    

Product Server Tools

  1. get-product - ดูข้อมูลสินค้า

    {
      "name": "get-product",
      "arguments": { "productId": "P001" }
    }
    
  2. search-products - ค้นหาสินค้า

    {
      "name": "search-products",
      "arguments": { "query": "iPhone" }
    }
    
  3. check-stock - ตรวจสอบสต็อค

    {
      "name": "check-stock",
      "arguments": { "productId": "P001" }
    }
    
  4. 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 requests
  • mcp_error - ข้อผิดพลาด MCP
  • server_shutdown - เซิร์ฟเวอร์ปิดตัว

Product Server SSE Events

  • connection - การเชื่อมต่อสำเร็จ
  • server_info - ข้อมูลเซิร์ฟเวอร์
  • stock_update - อัปเดตสต็อคแบบ real-time (ทุก 45 วินาที)
  • price_update - อัปเดตราคาแบบ real-time (ทุก 60 วินาที)
  • mcp_response - ผลลัพธ์ MCP requests
  • mcp_error - ข้อผิดพลาด MCP
  • server_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

  1. Method: POST
  2. URL: http://localhost:3002/mcp (Product) หรือ http://localhost:3003/mcp (Weather)
  3. Headers:
    Content-Type: application/json
    Accept: application/json
    
  4. 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

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

📞 Support

หากพบปัญหาหรือต้องการความช่วยเหลือ กรุณาสร้าง issue ใน GitHub repository