项目简介

天气MCP服务器是一个基于Model Context Protocol (MCP) 构建的后端服务,旨在扩展大型语言模型(LLM)的功能,使其能够获取实时天气数据。它利用Clean Architecture原则,并通过集成OpenWeatherMap API、PostgreSQL数据库和Redis缓存,为LLM提供高效、可靠且生产就绪的天气信息查询能力。

主要功能点

  • 实时天气查询: 查询全球任意城市的当前天气状况(温度、湿度、风速、描述等)。
  • 多日天气预报: 获取指定城市未来1到5天的详细天气预报。
  • 天气查询历史: 记录并查询用户进行过的天气查询历史。
  • 智能缓存系统: 利用Redis缓存常用查询结果,大幅提升响应速度和API调用效率。

安装步骤

  1. 克隆仓库: 'git clone https://github.com/glaucia86/weather-mcp-server.git'
  2. 进入目录: 'cd weather-mcp-server'
  3. 安装依赖: 'npm install'
  4. 配置环境变量:
    • 复制 '.env.example' 到 '.env': 'cp .env.example .env'
    • 编辑 '.env' 文件,填入你的OpenWeatherMap API Key(免费申请)。
    • 'WEATHER_API_KEY=你的_OpenWeatherMap_API_Key'
    • 数据库和Redis的URL通常使用默认值 'postgresql://mcp_user:mcp_pass@localhost:5432/weather_mcp' 和 'redis://localhost:6379' 即可。
  5. 编译项目: 'npm run build'
  6. 启动基础设施 (PostgreSQL 和 Redis): 'npm run docker:up' (这会使用Docker启动数据库和缓存服务)
  7. 等待服务启动: 等待约30秒,确保数据库和Redis完全启动。
  8. 启动MCP服务器: 'npm run start:mcp'

服务器配置 (用于LLM客户端连接)

此MCP服务器专为与兼容MCP的LLM客户端(如Claude Desktop)集成而设计。您需要在LLM客户端的配置文件中添加以下MCP服务器信息:

  • 配置路径:

    • Windows: '%APPDATA%\Claude\claude_desktop_config.json'
    • macOS: '~/Library/Application Support/Claude/claude_desktop_config.json'
    • Linux: '~/.config/Claude/claude_desktop_config.json'
  • 配置内容示例 (JSON格式,无需用户手动修改,仅作说明): 在您的LLM客户端配置文件中,通常会有一个 'mcpServers' 部分。您需要在其中添加一个名为 '"weather-mcp"' 的条目,并指定以下关键信息:

    • '"command"': 这指示LLM客户端如何启动MCP服务器。对于此项目,应设置为 '"node"'。
    • '"args"': 这是启动命令的参数,应指向您本地MCP服务器的启动脚本路径。例如,如果您的 'weather-mcp-server' 目录在 '/Users/yourname/' 下,那么完整路径可能类似于 '"/Users/yourname/weather-mcp-server/dist/mcp-entry.js"'。请务必替换为您系统中 'weather-mcp-server' 目录的完整绝对路径。
    • '"env"': 这是传递给MCP服务器的环境变量。您需要在此处配置 'WEATHER_API_KEY'(确保与您 '.env' 文件中的密钥一致),以及 'DATABASE_URL' 和 'REDIS_URL'(通常与 '.env' 文件中的值相同)。'NODE_ENV' 和 'MCP_MODE' 也应按照示例配置。
  • 配置完成后,请完全关闭LLM客户端并重新打开,以加载新的MCP服务器配置。

基本使用方法 (在LLM客户端中)

一旦MCP服务器配置并启动,您可以在LLM客户端中直接提问来调用其功能:

  • 查询当前天气:
    • "What's the current weather in New York?" (纽约现在天气怎么样?)
    • "How's the weather in London right now?" (伦敦现在天气如何?)
  • 查询天气预报:
    • "What's the weather forecast for tomorrow in Paris?" (巴黎明天天气预报如何?)
    • "How will the weather be in Tokyo for the next 3 days?" (东京未来3天天气怎么样?)
  • 查询历史记录:
    • "Show me my weather query history." (显示我的天气查询历史。)
    • "What were the last cities I asked about the weather?" (我最近查询了哪些城市的天气?)
  • 查询系统缓存统计 (仅作演示):
    • "Show me the Redis cache statistics." (显示Redis缓存统计。)

信息

分类

网页与API