天气数据服务器

MCP Weather Server是一个基于MCP协议的流式HTTP服务器,通过Open-Meteo API提供天气数据查询、地理位置搜索和实时天气流功能,适用于需要集成天气服务的应用开发。

  • by TJarriault
  • JavaScript
  • 本地部署
  • 7 星标
  • 更新于
在 GitHub 查看 TJarriault/mcp-weather-sample

使用说明

MCP气象服务器

一个可流式传输的HTTP MCP(模型上下文协议)服务器,用于通过Open-Meteo API获取天气数据。

🚀 特性

  • 可流式HTTP传输:与MCP 2025-03-26规范兼容
  • 会话管理:支持具有唯一ID的持久会话
  • 天气工具:地理定位和天气数据的三个主要工具
  • REST API:用于监测的健康端点
  • Kubernetes就绪:Docker容器化和K8s部署清单
  • 本地城市搜索:基于CSV的本地城市数据库,用于离线搜索

📁 项目结构

├── src/                           # Source code TypeScript
├── build/                         # Compiled JavaScript
├── static/                        # CSV city data files
├── test/                          # Test scripts and examples
│   ├── test-k8s-deployment.sh     # Kubernetes deployment tests
│   ├── test_mcp.js               # MCP functionality tests
│   ├── example-client.js         # Example MCP client
│   └── README.md                 # Test documentation
├── claude-configuration/          # Claude Desktop configuration
│   ├── claude_config*.json       # Various Claude configs
│   ├── install_claude_config.sh  # Auto-installation script
│   └── README.md                 # Configuration guide
├── k8s/                          # Kubernetes manifests
│   ├── deployment.yaml           # K8s deployment
│   ├── service.yaml              # K8s service
│   └── ingress.yaml              # K8s ingress
├── Dockerfile                    # Multi-stage Docker build
├── k8s-all-in-one.yaml          # Complete K8s deployment
├── build-deploy-container.sh                     # Automated deployment script
└── KUBERNETES_DEPLOYMENT.md     # Kubernetes deployment guide

📦 需求

cd flowise/kubernetes
MINIP=`minikube ip`
sed -i "s/CHANGEME/$MINIP/g" ingress-flowise.yaml
kubectl apply -f .

要求

规格

# Clone and install dependencies
npm install

# Compile TypeScript
npm run build

# Start the server
npm start

您需要在flowise上导入一个新的代理“Agentflow”

📦 安装

  • PORT🔧 配置

服务器使用以下环境变量:

PORT=8080 npm start

:侦听端口(默认值:3000)

例子: search_location

🛠️ 可用的MCP工具

1.

  • city_name 使用Open-Meteo地理编码API按名称搜索城市或位置的GPS坐标。
  • country 参数:
  • limit (string):要搜索的城市或地点的名称

(字符串,可选):缩小搜索范围的国家名称

{
  "query": "Paris",
  "country_filter": null,
  "total_results": 5,
  "locations": [
    {
      "name": "Paris",
      "country": "France",
      "admin1": "Île-de-France",
      "latitude": 48.8566,
      "longitude": 2.3522,
      "timezone": "Europe/Paris",
      "population": 2161000,
      "elevation": 42
    },
    {
      "name": "Paris",
      "country": "United States",
      "admin1": "Texas",
      "latitude": 33.6609,
      "longitude": -95.5555,
      "timezone": "America/Chicago",
      "population": 25171,
      "elevation": 177
    }
  ]
}

(number,可选):返回的最大结果数(默认值:5,最大值:10) search_local_cities

示例响应:

2.

  • city_name 在本地CSV数据库中搜索城市,以实现离线功能和更快的本地搜索。
  • exact_match 参数:
  • limit (string):要搜索的城市名称(不区分大小写的部分匹配)

(布尔值,可选):是搜索完全匹配还是部分匹配(默认值:false)

{
  "query": "Paris",
  "exact_match": false,
  "total_found": 3,
  "results_returned": 3,
  "cities": [
    {
      "name": "Paris",
      "latitude": 48.8566,
      "longitude": 2.3522
    },
    {
      "name": "Paris-15e-Arrondissement",
      "latitude": 48.8421,
      "longitude": 2.3008
    },
    {
      "name": "Parisot",
      "latitude": 44.0625,
      "longitude": 1.7125
    }
  ]
}

(number,可选):返回的最大结果数(默认值:10,最大值:50) get_weather

示例响应:

3.

  • latitude 获取某个位置的当前天气信息。
  • longitude 参数:
  • location_name (数字):纬度(十进制)(-90到90)

(数字):经度以十进制度数表示(-180到180)

{
  "location": "Paris",
  "weather": {
    "current_weather": {
      "temperature": 15.2,
      "windspeed": 10.5,
      "winddirection": 230,
      "weathercode": 1,
      "is_day": 1,
      "time": "2024-01-15T14:00"
    },
    "current_units": {
      "temperature": "°C",
      "windspeed": "km/h",
      "winddirection": "°",
      "weathercode": "wmo code",
      "is_day": "",
      "time": "iso8601"
    }
  },
  "lastUpdate": "2024-01-15T14:05:30.123Z"
}

(字符串,可选):可选的显示位置名称 stream_weather

示例响应:

4.

  • latitude 开始定期更新天气数据流。
  • longitude 参数:
  • location_name (数字):纬度(十进制)(-90到90)
  • interval_seconds (数字):经度以十进制度数表示(-180到180)

(字符串,可选):可选的显示位置名称

(数字,默认值:60):更新间隔(秒)

GET /health

🌐 HTTP端点

{
  "status": "OK",
  "timestamp": "2024-01-15T14:05:30.123Z"
}

服务器运行状况

POST /mcp
GET /mcp  
DELETE /mcp

返回服务器状态:

  • POSTMCP端点
  • GETMCP通信的主要端点。支持:
  • DELETE:初始化和请求发送

:SSE流建立响应

:会话终止

const initRequest = {
  jsonrpc: '2.0',
  id: 1,
  method: 'initialize',
  params: {
    protocolVersion: '2024-11-05',
    capabilities: { tools: {} },
    clientInfo: { name: 'weather-client', version: '1.0.0' }
  }
};

const response = await fetch('http://localhost:8080/mcp', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json, text/event-stream'
  },
  body: JSON.stringify(initRequest)
});

const sessionId = response.headers.get('mcp-session-id');

📋 MCP客户端使用示例

const listToolsRequest = {
  jsonrpc: '2.0',
  id: 2,
  method: 'tools/list',
  params: {}
};

const response = await fetch('http://localhost:8080/mcp', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json, text/event-stream',
    'mcp-session-id': sessionId
  },
  body: JSON.stringify(listToolsRequest)
});

1.会话初始化

const weatherRequest = {
  jsonrpc: '2.0',
  id: 3,
  method: 'tools/call',
  params: {
    name: 'get_weather',
    arguments: {
      latitude: 48.8566,
      longitude: 2.3522,
      location_name: 'Paris'
    }
  }
};

const response = await fetch('http://localhost:8080/mcp', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json, text/event-stream',
    'mcp-session-id': sessionId
  },
  body: JSON.stringify(weatherRequest)
});

2.列出可用工具

3.调用天气工具

# Start the server
PORT=8080 npm start

# Test the health endpoint
curl http://localhost:8080/health

🧪 测试

快速测试 test/ 综合测试

# Run MCP functionality tests
cd test && node test_mcp.js

# Test local city search
cd test && node test_local_cities.js

# Test location search
cd test && node test_search_location.js

# Test Kubernetes deployment (requires K8s cluster)
./test/test-k8s-deployment.sh

该项目包括在 目录:有关更多测试选项,请参阅

test/README.md

.

🐳 容器构建和Kubernetes部署

本节介绍如何将MCP Weather Sample应用程序容器化并部署到Kubernetes集群。 📦 集装箱化概述 该应用程序使用

  1. 多阶段Docker构建优化生产部署的方法:
  2. 构建阶段:编译TypeScript源代码
  3. 生产阶段:仅使用生产依赖关系创建最小运行时映像
  4. 安全:以具有最低权限的非root用户身份运行

健康检查

:内置容器健康监测

# Complete build and deployment pipeline
./build-deploy-container.sh all

# Individual steps
./build-deploy-container.sh build    # Build Docker image only
./build-deploy-container.sh deploy   # Deploy to Kubernetes only
./build-deploy-container.sh cleanup  # Remove deployment

🏗️ Docker构建过程

# Build the Docker image
docker build -t mcp-weather-sample:latest .

# Run container locally for testing
docker run -p 8080:8080 mcp-weather-sample:latest

# Check container health
curl http://localhost:8080/health

# View container logs
docker logs <container-id>

自动化构建和部署

  • 手动Docker操作Docker镜像详细信息 node:20-alpine 基本图像
  • :(轻量级Linux发行版)
  • 最终尺寸:~150MB(通过多阶段构建进行了优化) PORT 端口
  • :8080(可通过以下方式配置环境变量)
  • 健康检查:内置HTTP健康端点监控nextjs:nodejs用户

:非root用户(

)为了安全

⚓ Kubernetes部署架构

  • 部署组件Kubernetes部署包括:
  • 部署:3个具有滚动更新的副本
  • 服务:群集IP服务暴露端口80→8080
  • 康福特地图:环境配置管理
  • 入口:支持CORS的外部流量路由

健康探测器

resources:
  requests:
    memory: "128Mi"    # Minimum memory allocation
    cpu: "100m"        # Minimum CPU allocation (0.1 core)
  limits:
    memory: "512Mi"    # Maximum memory limit
    cpu: "500m"        # Maximum CPU limit (0.5 core)

:启动、活性和准备状态检查

  • 资源配置高可用性功能
  • 3个副本:确保吊舱故障期间的服务可用性
  • 滚动更新:零停机部署
  • 健康检查:故障时自动重启吊舱
  • 资源限制:防止资源耗尽

CORS配置

:MCP会话的正确标头处理

# Deploy everything with single command
kubectl apply -f k8s-all-in-one.yaml

# Monitor deployment progress
kubectl rollout status deployment/mcp-weather-app

# Check pod status
kubectl get pods -l app=mcp-weather-app

🚀 部署选项

# Deploy individual components
kubectl apply -f k8s/configmap.yaml
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
kubectl apply -f k8s/ingress.yaml

选项1:一体式部署

# Use the provided deployment script
./build-deploy-container.sh all

# Script features:
# - Validates kubectl connectivity
# - Builds Docker image
# - Applies Kubernetes manifests
# - Waits for deployment readiness
# - Provides access instructions

选项2:逐个组件

选项3:自动脚本

🔧 配置管理

apiVersion: v1
kind: ConfigMap
metadata:
  name: mcp-weather-config
data:
  NODE_ENV: "production"
  PORT: "8080"
  LOG_LEVEL: "info"

环境变量

# Custom domain configuration
spec:
  rules:
  - host: mcp-weather.your-domain.com  # Replace with your domain
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: mcp-weather-service
            port:
              number: 80

通过ConfigMap配置应用程序:

入口配置

# Test deployment health
./test/test-k8s-deployment.sh

# Manual health check via port-forward
kubectl port-forward service/mcp-weather-service 8080:8080
curl http://localhost:8080/health

# Debug
kubectl exec -it pods/$(kubectl get pods  --no-headers -o custom-columns=":metadata.name" | grep 'flowise'|head -1) sh


🧪 测试部署

# Test all endpoints and functionality
./test/test-k8s-deployment.sh local

# Test via ingress (requires domain configuration)
./test/test-k8s-deployment.sh ingress your-domain.com

快速验证

综合测试

# Pod status and logs
kubectl get pods -l app=mcp-weather-app
kubectl logs -l app=mcp-weather-app -f

# Service endpoints
kubectl get endpoints mcp-weather-service
kubectl describe service mcp-weather-service

# Ingress status
kubectl get ingress mcp-weather-ingress
kubectl describe ingress mcp-weather-ingress

📊 监控和故障排除

# Pod not starting
kubectl describe pod <pod-name>
kubectl logs <pod-name> --previous

# Service not accessible
kubectl port-forward service/mcp-weather-service 8080:80
curl http://localhost:8080/health

# Image pull issues
kubectl describe pod <pod-name> | grep -A 5 "Events:"

正在检查部署状态

常见故障排除

# Scale to 5 replicas
kubectl scale deployment mcp-weather-app --replicas=5

# Verify scaling
kubectl get pods -l app=mcp-weather-app

🔄 扩展和更新

# Update image version
kubectl set image deployment/mcp-weather-app \
  mcp-weather-app=mcp-weather-sample:v2.0.0

# Monitor rollout
kubectl rollout status deployment/mcp-weather-app

# Rollback if needed
kubectl rollout undo deployment/mcp-weather-app

水平缩放 滚动更新有关完整的部署说明和高级配置,请参阅

KUBERNETES_DEPLOYMENT.md

. claude-configuration/ ⚙️ Claude桌面集成

# Auto-install Claude configuration
cd claude-configuration && ./install_claude_config.sh

# Validate HTTP configuration
cd claude-configuration && ./validate_claude_http.sh

该项目包括预配置的Claude Desktop配置

  • **目录:**可用配置: claude_config.json 基础
  • :-最小设置 claude_config_detailed.json 详细的
  • :-完整选项 claude_config_http.json 超文本传输协议

: -HTTP传输特定有关详细的设置说明,请参阅

claude配置/README.md

.

🔍 使用MCP检查器

  1. 您可以使用官方的MCP Inspector在用户友好的界面中直观地探索和测试您的天气服务器工具。 安装和设置

    npx @modelcontextprotocol/inspector
    
  2. 安装MCP检查器(需要Node.js):

    PORT=8080 npm start
    
  3. 启动天气服务器:

    • 配置MCP检查器 http://localhost:8080/mcp
    • : HTTP (Streamable)
    • 服务器URL: 2024-11-05

运输:

协议版本:

  • 视觉界面功能MCP检查员提供:search_location🛠️ 工具资源管理器 get_weather:浏览所有可用工具( stream_weather,
  • ,)
  • 📝 交互式表单:易于使用的工具参数输入表单
  • 🎯 实时测试:执行工具并查看实时响应
  • 📊 响应查看器:JSON格式的结果,语法突出显示
  • 🔄 会话管理:可视化会话状态和连接状态

📈 请求/响应历史记录

  1. :跟踪所有API交互 工作流示例
  2. 连接 使用检查器连接到您的服务器
  3. 探索三种可用的天气工具
    • 测试位置搜索 search_location
    • : city_name: "Paris"
  4. **工具:**参数:
    • 获取天气数据 get_weather
    • :
  5. **工具:**参数:使用以前搜索的坐标
    • 监控流媒体 stream_weather
    • :

工具:

  • 观看实时天气更新益处
  • 无需编码:在不编写客户端代码的情况下测试MCP服务器
  • 可视化调试:准确查看客户端和服务器之间的数据流
  • 工具验证:验证参数模式和响应格式

快速原型制作

:快速迭代您的MCP服务器实现

  • Inspector在开发过程中特别有用,可以在将天气服务器集成到更大的应用程序之前确保其正常工作。🏗️ 建筑
  • Express.js:HTTP服务器
  • MCP-SDK:使用StreamableHTTPServerTransport实现MCP协议
  • 开放气象API:天气数据源

TypeScript

  1. :可编译为JavaScript的开发语言

  2. 在flowise上集成MCP服务器(低代码GenAI工具) 在flowise上初始化代理流

  3. 通过MCP检查器检索MCP配置 在检查器界面上复制/粘贴表单“服务器条目”

配置“MCP自定义”工具,并在flowise上添加MCP服务器公开的工具

  • 添加从检查器复制的MCP配置规范📄 规格
  • MCP协议:2024年11月5日和2025年3月26日版本
  • 运输:支持SSE的流式HTTP
  • 数据格式:JSON-RPC 2.0 天气API

:

开放气象API

MCP TypeScript SDK