天气服务服务器

一个提供天气信息和预测功能的模型上下文协议(MCP)服务器,适用于AI助手。

  • by markib
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 markib/weather-mcp-server-nodejs

使用说明

气象MCP服务器

一种模型上下文协议(MCP)服务器,为人工智能助手提供天气信息和预报功能。此服务器实现了安全、经过验证的天气工具,具有速率限制和输入净化功能。

特性

  • 当前天气:获取支持城市的实时天气数据
  • 天气预报:检索多日天气预报(最多7天)
  • 温度单位:支持摄氏度和华氏度
  • 安全特性:
    • 输入验证和净化
    • 限制利率以防止滥用
    • 基于位置的限制
  • MCP资源:访问服务器配置和支持的城市
  • MCP提示:预先构建的天气摘要和城市比较提示

支持城市

  • 纽约
  • 伦敦
  • 东京
  • 西贡(胡志明市)

安装

先决条件

  • Node.js 18+
  • npm或纱线

再进行

npm install

构建项目

npm run build

用法

启动服务器

npm start

测试服务器

运行测试客户端以验证功能:

npm test

MCP检查员

使用MCP检查器以交互方式测试服务器:

npm run inspector

配置

服务器通过以下方式配置 config.json:

{
  "serverName": "weather-mcp-server",
  "version": "1.0.0",
  "supportedCities": ["New York", "London", "Tokyo", "Saigon"],
  "maxForecastDays": 7,
  "maxLocationLength": 100,
  "rateLimitWindowMs": 1000,
  "maxRequestsPerWindow": 100
## MCP Client Configuration

Clients can discover and connect to this server using the `mcp.json` configuration file:

```json
{
  "mcpServers": {
    "weather-mcp-server": {
      "command": "npx",
      "args": ["tsx", "src/server.ts"]
    }
  }
}

与MCP客户端一起使用

  1. 复制配置:将服务器配置添加到MCP客户端的配置文件中
  2. 安装依赖项:确保 tsx 全球可用或使用内置版本
  3. 连接:客户端将在需要时自动启动服务器

替代配置(内置版本)

对于生产使用,您还可以将服务器配置为使用内置的JavaScript:

{
  "mcpServers": {
    "weather-mcp-server": {
      "command": "node",
      "args": ["dist/index.js"]
    }
  }
}

一定要跑 npm run build 首先生成 dist/index.js 文件。

get_current天气

检索支持城市的当前天气信息。

参数:

  • location (string):城市名称(不区分大小写)
  • units (字符串,可选):温度单位-“摄氏”或“华氏”(默认值:“摄氏”)

例子:

{
  "location": "New York",
  "units": "fahrenheit"
}

答复:

{
  "location": "New York",
  "temperature": "64.4°F",
  "condition": "Partly Cloudy",
  "humidity": "65%"
}

获取预测

检索未来1-7天的天气预报。

参数:

  • location (string):城市名称(不区分大小写)
  • days (数字):预测天数(1-7)

例子:

{
  "location": "London",
  "days": 3
}

答复:

{
  "location": "London",
  "forecast": [
    {
      "day": 1,
      "temp": "12°C",
      "condition": "Rainy"
    },
    {
      "day": 2,
      "temp": "15°C",
      "condition": "Rainy"
    },
    {
      "day": 3,
      "temp": "9°C",
      "condition": "Rainy"
    }
  ]
}

MCP资源

config://weather-mcp-server/app-config

提供服务器配置信息。

data://weather-mcp-server/cities

列出所有支持的城市。

MCP提示

天气概要

生成一个提示,用于为支持的城市创建天气摘要。

论据:

  • location (string):城市名称

比较天气

生成一个提示,用于比较多个城市之间的天气。

论据:

  • cities (array):要比较的城市名称数组

发展

项目结构

src/
├── server.ts      # Main MCP server implementation
├── tools.ts       # Weather tool handlers
├── resources.ts   # MCP resource registrations
├── prompts.ts     # MCP prompt registrations
├── schemas.ts     # Zod validation schemas
├── config.ts      # Configuration loading
├── errors.ts      # Custom error classes
├── logger.ts      # Logging utilities
└── client.ts      # Test client

运行测试

# Unit tests
npm run test:unit

# Integration tests
npm run test:integration

# Authentication tests
npm run test:auth

类型检查

npm run build

观看模式

npm run watch

认证

服务器支持承载令牌身份验证,以确保API访问的安全。

配置

在中启用身份验证 config.json:

{
  "authEnabled": true,
  "bearerToken": "your-secret-token-here"
}

或者使用环境变量:

export AUTH_ENABLED=true
export BEARER_TOKEN=your-secret-token-here

客户端认证

使用MCP客户端时,通过环境变量设置身份验证令牌:

export MCP_AUTH_TOKEN=your-secret-token-here
npm start

带身份验证的MCP客户端配置

对于经过身份验证的服务器,请使用Bearer令牌配置MCP客户端:

{
  "mcpServers": {
    "weather-mcp-server": {
      "command": "npx",
      "args": ["tsx", "src/server.ts"],
      "env": {
        "MCP_AUTH_TOKEN": "your-secret-token-here"
      }
    }
  }
}

替代身份验证方法

您还可以在开始之前通过环境变量设置令牌:

export MCP_AUTH_TOKEN=your-secret-token-here
npm start

或者使用内置版本:

{
  "mcpServers": {
    "weather-mcp-server": {
      "command": "node",
      "args": ["dist/index.js"],
      "env": {
        "MCP_AUTH_TOKEN": "your-secret-token-here"
      }
    }
  }
}

安全说明

  • 身份验证是 默认禁用 便于开发
  • 当启用时, 所有请求都需要有效的承载令牌
  • 代币应保持安全,并定期轮换
  • 返回的令牌无效或缺失 AUTH_ERROR 回应

环境配置

服务器支持通过环境变量进行配置,这些变量优先于 config.json 设置。

设置环境变量

  1. 复制示例文件:

    cp .env.example .env
    cp .env.local.example .env.local  # For local development
    
  2. 编辑中的值 .env:

    # Enable authentication
    AUTH_ENABLED=true
    BEARER_TOKEN=your-secret-token-here
    
    # MCP client token (same as bearer token)
    MCP_AUTH_TOKEN=your-secret-token-here
    

环境变量引用

变量 描述 默认值
AUTH_ENABLED 启用/禁用身份验证 false
BEARER_TOKEN 服务器身份验证的承载令牌 null
MCP_AUTH_TOKEN MCP客户端连接的令牌 -
SERVER_NAME 服务器标识符 "weather-mcp-server"
SUPPORTED_CITIES 逗号分隔的城市列表 "New York,London,Tokyo,Saigon"
MAX_FORECAST_DAYS 最大预测天数 7
RATE_LIMIT_WINDOW_MS 速率限制窗口(ms) 1000
MAX_REQUESTS_PER_WINDOW 每个窗口的最大请求数 100

错误处理

服务器使用自定义错误类型:

  • ValidationError:输入参数无效
  • NotFoundError:找不到请求的资源
  • SecurityError:检测到安全违规
  • AppError:一般应用程序错误

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 进行更改
  4. 添加新功能的测试
  5. 确保所有测试通过
  6. 提交拉取请求

许可证

ISC许可证

依赖项

  • @modelcontextprotocol/sdk:MCP协议实施
  • zod:运行时类型验证
  • typescript:类型安全的JavaScript
  • vitest:测试框架
  • tsx:TypeScript执行