Back to MCP directory
publicPublicdnsLocal runtime

weather-mcp-ts

一个基于TypeScript开发的天气MCP服务器,提供模拟天气数据和相关功能,包括实时天气、多日预报、天气警报等功能,支持多种启动方式和API访问。

article

README

🚀 天气MCP服务器

这是一个使用TypeScript开发的天气MCP(模型上下文协议)服务器,可提供模拟天气数据及相关功能。

🚀 快速开始

安装依赖

pnpm install

安装与构建

# 安装依赖
pnpm install

# 构建项目
pnpm run build

启动方式

1. MCP协议(标准输入输出)

# 用于MCP客户端集成
pnpm start

2. HTTP API服务器 🆕

# 启动HTTP服务器(默认端口8080)
pnpm run start:http

# 启动HTTP服务器(指定端口3001)
pnpm run start:http:3001

# 或者直接指定端口
node dist/http-server.js 8080

3. 简易HTTP服务器(推荐) 🚀

# 使用默认端口8080启动
pnpm run start:simple

# 使用特定端口启动
pnpm run start:simple:3000  # 开发端口
pnpm run start:simple:5000  # 备用端口
pnpm run start:simple:9000  # 高端口

# 或者使用serve命令
pnpm run serve              # 默认端口8080
pnpm run serve:dev          # 开发端口3000
pnpm run serve:prod         # 生产端口8080

# 或者直接指定任意端口
node dist/simple-http-server.js 4000

动态端口支持

简易HTTP服务器支持动态端口配置:

# 使用任意你想要的端口
node dist/simple-http-server.js 3000
node dist/simple-http-server.js 5000
node dist/simple-http-server.js 9000
node dist/simple-http-server.js 12345

服务器将自动执行以下操作:

  • 若未指定端口,将默认使用端口8080
  • 接受任意有效的端口号作为命令行参数
  • 在启动消息和文档中显示实际使用的端口

HTTP API使用示例

一旦HTTP服务器启动,你可以通过以下方式使用它:

浏览器访问

  • 打开 http://localhost:8080 查看API文档
  • 直接点击链接测试各种API

curl命令

# 获取香港的当前天气
curl "http://localhost:8080/weather?location=Hong Kong"

# 获取东京的5天天气预报
curl "http://localhost:8080/forecast?location=Tokyo&days=5"

# 获取天气警报
curl "http://localhost:8080/alerts?location=Hong Kong"

# 搜索地点
curl "http://localhost:8080/locations?q=kong"

# 获取统计信息
curl "http://localhost:8080/stats"

JavaScript/Fetch

// 获取天气数据
const response = await fetch('http://localhost:8080/weather?location=Tokyo');
const weather = await response.json();
console.log(weather);

开发模式

# 监听模式编译
pnpm run dev

✨ 主要特性

  • 🌤️ 获取当前天气信息
  • 📅 多日天气预报
  • ⚠️ 天气警报系统
  • 🔍 地点搜索功能
  • 📊 天气统计
  • 🎭 支持模拟数据

💻 使用示例

基础用法

以下是使用curl命令获取天气信息的示例:

# 获取香港的当前天气
curl "http://localhost:8080/weather?location=Hong Kong"

高级用法

使用JavaScript的fetch API获取东京的天气数据:

// 获取天气数据
const response = await fetch('http://localhost:8080/weather?location=Tokyo');
const weather = await response.json();
console.log(weather);

📦 安装指南

安装依赖

pnpm install

构建项目

pnpm run build

启动服务器

根据不同的需求,选择合适的启动方式,具体可参考“🚀 快速开始”部分的“启动方式”内容。

📚 详细文档

可用工具

1. get_current_weather

获取指定地点的当前天气信息。 参数:

  • location(字符串):地点名称 示例:
{
  "location": "Hong Kong"
}

2. get_weather_forecast

获取指定地点的天气预报。 参数:

  • location(字符串):地点名称
  • days(数字,可选):预报天数(1 - 7天,默认3天) 示例:
{
  "location": "Tokyo",
  "days": 5
}

3. get_weather_alerts

获取天气警报信息。 参数:

  • location(字符串,可选):地点名称 示例:
{
  "location": "Hong Kong"
}

4. search_locations

搜索支持的地点。 参数:

  • query(字符串):搜索关键词 示例:
{
  "query": "kong"
}

5. get_weather_stats

获取天气统计信息。 参数:

支持的地点

目前支持以下地点的详细天气数据:

  • 香港
  • 东京、大阪、京都、广岛、札幌、福冈(日本)
  • 伦敦(英国)
  • 纽约(美国)
  • 悉尼(澳大利亚) 其他地点将返回随机生成的模拟数据。

HTTP API端点

| 端点 | 方法 | 参数 | 描述 | |----------|--------|------------|-------------| | / | GET | - | API文档页面 | | /mcp | POST | JSON-RPC 2.0 | MCP协议端点 | | /weather | GET | location(必需) | 获取当前天气 | | /forecast | GET | location(必需),days(可选) | 获取天气预报 | | /alerts | GET | location(可选) | 获取天气警报 | | /locations | GET | q(必需) | 搜索地点 | | /stats | GET | - | 获取统计信息 |

🔧 技术细节

技术规格

  • 语言:TypeScript
  • 包管理器:pnpm
  • MCP SDK:@modelcontextprotocol/sdk
  • Node.js:>=18

项目结构

src/
├── index.ts              # MCP服务器主文件(标准输入输出)
├── http-server.ts        # HTTP API服务器
├── simple-http-server.ts # 简易HTTP服务器
├── weather-service.ts    # 天气服务逻辑
├── mock-data.ts          # 模拟数据
└── types.ts              # TypeScript类型定义

📄 许可证

本项目采用MIT许可证。

help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client