Skip to main content
Glama

preview image

weather-mcp

TypeScript MCP 服务器,提供由 OpenWeatherMap 支持的天气工具。

工具

工具

描述

get_current_weather

按城市名称或经纬度获取当前天气

get_forecast

按经纬度获取 5 天 / 3 小时预报

传输方式

模式

环境变量

适用场景

stdio(默认)

TRANSPORT=stdio

本地 Cursor / Claude Desktop

Streamable HTTP

TRANSPORT=http

部署(Render 等)/ 远程 URL

MCP 官方文档:https://modelcontextprotocol.io/docs/

远程在线服务(Render)

部署在 Render 的免费套餐上:

Cursor 配置(远程)

将其添加到 设置 → MCP 或 ~/.cursor/mcp.json:

{
  "mcpServers": {
    "weather": {
      "url": "https://weather-mcp-ts.onrender.com/mcp"
    }
  }
}

Cursor 中无需本地构建或 API 密钥——密钥存放在 Render 的环境变量中。

Render 免费套餐: 服务在无活动 ~15 分钟后关闭。下一个请求是冷启动,MCP 可能需要最多 ~50 秒才能响应。如果 Cursor 工具看起来卡在唤醒状态,请访问一次 /health。

Related MCP server: mcp-weatherapi

前提条件(仅本地)

  • Node.js 20+

  • pnpm(或 npm/yarn)

  • OpenWeatherMap API 密钥(在此获取)

本地设置

pnpm install
cp .env.example .env

编辑 .env 并设置你的密钥:

OPEN_WEATHER_MAP_API_KEY=your_key_here
TRANSPORT=stdio

构建服务器:

pnpm run build

这会将可运行的入口文件写入 dist/index.js。

添加到 Cursor(本地 stdio)

Cursor 默认不会从项目根目录启动此进程来加载环境变量,因此请通过 Node 的 --env-file 标志传入绝对路径的环境文件。

  1. 打开 Cursor 设置 → MCP,或编辑 ~/.cursor/mcp.json。

  2. 在 mcpServers 下添加一个 weather 条目:

{
  "mcpServers": {
    "weather": {
      "command": "node",
      "args": [
        "--env-file",
        "/ABSOLUTE/PATH/TO/weather-mcp/.env",
        "/ABSOLUTE/PATH/TO/weather-mcp/dist/index.js"
      ]
    }
  }
}

将 /ABSOLUTE/PATH/TO/weather-mcp 替换为你的真实项目路径(例如在仓库根目录运行 pwd)。在 .env 中保留 TRANSPORT=stdio(或不设置)。

替代方案:内联环境变量

{
  "mcpServers": {
    "weather": {
      "command": "node",
      "args": [
        "/ABSOLUTE/PATH/TO/weather-mcp/dist/index.js"
      ],
      "env": {
        "OPEN_WEATHER_MAP_API_KEY": "your_key_here",
        "TRANSPORT": "stdio"
      }
    }
  }
}

建议使用 .env + --env-file,这样密钥不会出现在 mcp.json 中。

  1. 保存后,在 设置 → MCP 中关闭/重新打开服务器(或重启 Cursor)。

  2. 确认服务器显示为已连接,并列出 get_current_weather 和 get_forecast。

代码更改后

pnpm run build

然后在 Cursor 中重新加载 MCP 服务器。

Streamable HTTP(本地)

HTTP 模式在 POST/GET /mcp(可配置)提供 MCP 服务,并额外提供 GET /health。Streamable HTTP 通过官方 SDK 的 createMcpHandler + toNodeHandler 进行配置。

pnpm run build
TRANSPORT=http PORT=3000 pnpm run start
# or
pnpm run start:http

你应该会看到类似这样的输出:

Weather MCP Server running on http://0.0.0.0:3000/mcp (Streamable HTTP)

在 Render 上部署

  1. 从此仓库(项目根目录)创建一个 Web Service。

  2. 构建:pnpm install && pnpm run build(或等效的 npm 命令)。

  3. 启动:node dist/index.js(或 pnpm run start)。

  4. 环境变量:

    • TRANSPORT=http

    • OPEN_WEATHER_MAP_API_KEY=...

    • PORT 由 Render 自动设置

  5. MCP URL:https://<your-service>.onrender.com/mcp
    此部署:https://weather-mcp-ts.onrender.com/mcp

HTTP 端点未经过身份验证。任何拥有该 URL 的人都可以调用这些工具。在广泛分享之前请添加身份验证。

手动冒烟测试(OpenWeatherMap)

在设置好 .env 后:

node --env-file=.env -e "
import { getApiKey, buildCurrentWeatherUrl, makeOWMRequest, formatCurrentWeather } from './dist/utils.js';
const apiKey = getApiKey();
const data = await makeOWMRequest(buildCurrentWeatherUrl({ apiKey, city: 'Yaounde,CM', units: 'metric' }));
console.log(data ? formatCurrentWeather(data, 'metric') : 'failed');
"

远程健康检查:

curl -s https://weather-mcp-ts.onrender.com/health

仓库结构

weather-mcp/
├── README.md
├── package.json
├── tsconfig.json
├── .env.example
└── src/
    ├── index.ts      # stdio + Streamable HTTP entry
    ├── server.ts     # tool registration
    ├── types.ts
    └── utils.ts

注意事项

  • 切勿提交 .env(它已被 gitignore)。

  • Stdio 服务器不得向 stdout 写入除 MCP JSON-RPC 之外的任何内容;日志应写入 stderr(console.error)。

  • OpenWeatherMap 的免费套餐涵盖当前天气以及此处使用的 5 天 / 3 小时预报。

许可证

ISC

Related MCP Connectors

Related MCP Servers