天气代理服务

一个轻量级的Node.js应用,代理wttr.in天气服务并实现OpenAPI规范,同时包含MCP服务器用于AI助手集成。

  • by karpikpl
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 karpikpl/weather-mcp-openapi-server

使用说明

天气代理服务

一个轻量级的Node.js应用程序,代理对wttr.in的调用,并实现用于天气数据检索的OpenAPI规范。还包括用于AI助手集成的MCP(模型上下文协议)服务器。

特性

  • ✅ 实现OpenAPI 3.1.0规范
  • 🌤️ 向wttr.in天气服务代理请求
  • 📚 带有Swagger UI的交互式API文档
  • 🤖 用于AI助手集成的MCP服务器
  • 🐳 Docker容器化,易于部署
  • 🔒 安全最佳实践(非root用户、输入验证)
  • 📊 健康检查端点
  • ⚡ 重量轻,速度快

快速开始

使用Docker Compose(推荐)

docker-compose up --build

这将启动两个服务:

  • HTTP API: http://localhost:3000
  • MCP服务器: http://localhost:3001

使用Docker

# Build the image
docker build -t wttr-proxy .

# Run the container (exposes both ports)
docker run -p 3000:3000 -p 3001:3001 wttr-proxy

地方发展

# Install dependencies
npm install

# Start both HTTP API and MCP server
npm start

# Or start them individually:
npm run start:api  # HTTP API only
npm run start:mcp  # MCP server only

# Development mode (HTTP API only)
npm run dev

MCP服务器使用情况

MCP(模型上下文协议)服务器与主要的API一起作为HTTP服务运行,使其可以通过网络进行人工智能辅助集成。

网络接入

  • MCP端点: http://localhost:3001/mcp
  • 运输:POST上的HTTP JSON-RPC 2.0
  • 健康检查: http://localhost:3001/health

集成步骤:

  1. 启动服务:

    npm start  # Starts both API and MCP server
    
  2. 配置您的AI助手 要连接到 http://localhost:3001/mcp 使用HTTP POST和JSON-RPC 2.0协议。使用提供的 mcp-config.json 作为参考:

    {
      "mcpServers": {
        "weather": {
          "url": "http://localhost:3001/mcp",
          "transport": "http",
          "method": "POST"
        }
      }
    }
    
  3. 测试连接:

    npm run test-mcp
    

MCP API示例:

列出工具:

curl -X POST http://localhost:3001/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}'

获取天气:

curl -X POST http://localhost:3001/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"get_weather","arguments":{"location":"London"}}}'

可用的MCP工具:

  • get_weather(location):获取任何地点的全面天气信息

MCP服务器提供格式化的天气数据,包括当前状况、预报和天文信息,可通过HTTP访问,用于基于网络的AI助手集成。

用法

运行后,服务将在以下位置可用:

  • HTTP API: http://localhost:3000

    • API文件:http://localhost:3000/docs
    • 健康检查:http://localhost:3000/health
    • 天气端点:http://localhost:3000/{位置}?格式=j1
  • MCP服务器: http://localhost:3001

    • MCP端点:http://localhost:3001/mcp
    • 健康检查:http://localhost:3001/health
    • 服务器信息:http://localhost:3001/info

API调用示例

# Get weather for London
curl "http://localhost:3000/London?format=j1"

# Get weather for New York
curl "http://localhost:3000/New%20York?format=j1"

# Health check
curl "http://localhost:3000/health"

API规范

该服务实现了中定义的OpenAPI规范 weather.json:

  • 端点: GET /{location}
  • 所需参数:
    • location (路径):城市或地点名称
    • format (query):JSON格式必须为“j1”
  • 响应:
    • 200:JSON格式的天气数据
    • 404:未找到位置
    • 400:无效参数
    • 500:服务器错误

环境变量

  • API_PORT:HTTP API服务器端口(默认值:3000)
  • MCP_PORT:MCP服务器端口(默认值:3001)
  • NODE_ENV:环境模式(开发/生产)

安全功能

  • Docker容器中的非root用户
  • 输入验证和净化
  • 请求超时保护
  • 错误处理和日志
  • CORS就绪(如果需要)

健康监测

该应用程序包括:

  • 健康检查端点(/health)
  • Docker健康检查
  • 优雅的停机处理
  • 请求日志记录

发展

该应用程序由以下组件构建:

  • Express.js:Web框架
  • 阿西奥斯:API调用的HTTP客户端
  • Swagger UI Express:API文件
  • MCP-SDK:模型上下文协议服务器实现
  • Node.js 18 Alpine:轻质容器底座

MCP服务器配置

对于支持MCP的AI助手,您可以使用提供的配置天气服务器 mcp-config.json:

{
  "mcpServers": {
    "weather": {
      "command": "node",
      "args": ["mcp-server.js"],
      "env": {
        "NODE_ENV": "production"
      }
    }
  }
}

使用快速代理MCP客户端进行测试

跑

uv tool run fast-agent-mcp go --servers weather

贡献

  1. 分叉存储库
  2. 创建功能分支
  3. 进行更改
  4. 彻底测试
  5. 提交拉取请求

许可证

MIT许可证