天气MCP应用程序
一个模型上下文协议(MCP)服务器,具有用于获取天气数据的交互式React UI。适用于Claude Desktop、ChatGPT和任何兼容MCP的客户端。
特性
- 当前天气 -实时温度、感觉、湿度、风速/风向
- 逐时预报 -带天气图标的24小时预报
- 7天预测 -每日高温/低温和条件
- 空气质量 -美国空气质量指数、PM2.5、PM10和臭氧水平
- 单位切换 -在摄氏度和华氏度之间切换
- 双重运输 -支持stdio(克劳德桌面)和HTTP(ChatGPT)
- 速率限制 -100/小时,1000/天,30000/月
安装
# Clone the repository
git clone
cd weather-mcp-app
# Install dependencies
npm install
# Build the project
npm run build快速开始
克劳德桌面(Stdio)
# Run with stdio transport
npm run start:stdioChatGPT(HTTP)
# Run HTTP server (listens on http://localhost:3001/mcp)
npm run start
# Or in development with hot reload
npm run dev配置
克劳德桌面版
添加到您的Claude桌面设置(~/Library/Application Support/Claude/settings.json):
{
"mcpServers": {
"weather-mcp-app": {
"command": "npx",
"args": ["-y", "weather-mcp-app", "--stdio"]
}
}
}或者,如果在本地运行:
{
"mcpServers": {
"weather-mcp-app": {
"command": "node",
"args": ["path/to/weather-mcp-app/dist/main.js", "--stdio"]
}
}
}ChatGPT
使用HTTP端点: http://localhost:3001/mcp
用法
工具: get_weather
name: get_weather
description: Get current weather, hourly forecast, 7-day forecast, and air quality for a city name or zip code.输入参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
location | string | 是 | 城市名称(例如“伦敦”)或邮政编码(例如“10001,美国”) |
unit | string | 否 | "celsius" 或 "fahrenheit" (默认值: "celsius") |
示例请求
{
"name": "get_weather",
"arguments": {
"location": "San Francisco, CA",
"unit": "celsius"
}
}示例响应
{
"content": [
{
"type": "text",
"text": "Weather in San Francisco:\nMainly clear, 18°C\nFeels like 17°C\nHumidity: 65%\nWind: WNW 15 km/h\nAir Quality: Good (AQI: 35)\n\nHourly Forecast:\nNow: 18°C ☀️\n1PM: 19°C 🌤️\n...\n\n7-Day Forecast:\nToday: 15° - 20°, Mainly clear\nTue: 14° - 19°, Partly cloudy\n..."
}
],
"structuredContent": {
"current": { ... },
"hourly": [ ... ],
"daily": [ ... ],
"location": { ... },
"airQuality": { ... }
}
}项目结构
weather-mcp-app/
├── src/
│ ├── api/
│ │ └── weather-client.ts # Open-Meteo API client
│ ├── constants/
│ │ └── weather.ts # Weather codes & utilities
│ ├── handlers/
│ │ └── weather-handler.ts # MCP tool handler
│ ├── services/
│ │ └── weather-formatter.ts # Text formatting
│ ├── types/
│ │ └── weather.ts # Zod schemas & types
│ ├── errors/
│ │ └── index.ts # Custom errors
│ ├── utils/
│ │ └── logger.ts # Logging utilities
│ ├── components/ui/ # shadcn/ui components
│ ├── App.tsx # Main React UI
│ ├── server.ts # MCP server setup
│ ├── main.ts # Entry point
│ └── config.ts # Configuration
├── dist/ # Built output
│ ├── main.js # Compiled server
│ └── mcp-app.html # Compiled UI
├── package.json
├── tsconfig.json
├── vite.config.ts
└── README.md环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
PORT | HTTP服务器端口 | 3001 |
NODE_ENV | 环境模式 | development |
ALLOWED_ORIGINS | CORS起源(生产) | - |
开发命令
# Build the project
npm run build
# Build server only
npm run build:server
# Run with stdio transport
npm run start:stdio
# Run with HTTP server
npm run start
# Development mode with watch
npm run dev
# Type checking
npm run typecheck
# Run tests
npm run test
# Run unit tests
npm run test:unit
# Run e2e tests
npm run test:e2eAPI 参考
使用免费的Open-Meteo API(不需要API密钥):
- 地理编码:
https://geocoding-api.open-meteo.com/v1/search - 天气:
https://api.open-meteo.com/v1/forecast - 空气质量:
https://air-quality-api.open-meteo.com/v1/air-quality
安全
- HSTS标头在生产中启用
- CORS在开发中仅限于本地主机
- 请求体限制:10KB
- 使用Zod模式进行输入验证
- 所有请求的速率限制
- 使用Pino进行结构化日志记录
更新日志
看 更改日志.md 详细更改。
致谢
- 开放气象API -免费天气和空气质量数据API。该项目使用他们的地理编码、天气预报和空气质量API。
- MCP应用软件开发工具包 -用于在MCP服务器中启用交互式UI功能。
- shadcn/ui -为了美观且易于使用的React组件。
- 明亮的 -用于图标库。
许可证
麻省理工学院
