天气查询服务

一个基于Model Context Protocol(MCP)的天气查询服务器,通过wttr.in API提供城市天气信息。

  • by calvinxu2010
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 calvinxu2010/mcp-server-weather

使用说明

MCP气象服务器

模型上下文协议(MCP)服务器,使用 wttr.in API

特性

  • get_weather:获取任何城市的当前天气状况和温度

需求

  • Python 3.10+

安装

# Using uv
uv sync

# Or using pip
pip install -e .

用法

运行服务器

# Using uv
uv run main.py

# Or directly with Python
python main.py

使用Claude Desktop进行配置

将以下内容添加到您的Claude Desktop配置文件中:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 视窗: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "weather": {
      "command": "uv",
      "args": ["--directory", "/path/to/mcp-server-weather", "run", "main.py"]
    }
  }
}

可用工具

get_weather

获取一个城市的当前天气。

参数:

  • city (string,必填):城市名称

示例响应:

Weather in London: Partly cloudy +15°C

依赖项

许可证

麻省理工学院

原始来源

Linkedin学习

https://www.linkedin.com/learning/model-context-protocol-mcp-hands-on-with-agentic-ai/articles/creating-an-mcp-server-using-python?resume=false&u=2164418

macOS通过Homebrew:

brew安装uv

Windows通过WinGet:

机翼安装--id=astral-sh.uv-e

设置项目 要设置项目,请打开要添加项目的文件夹中的代码编辑器。然后按照以下步骤设置您的项目:

使用编辑器工具或终端创建一个名为mcp server weather的新文件夹: mkdir mcp服务器天气 导航到终端中的文件夹: cd-mcp服务器天气 启动一个新的紫外线项目: 紫外线初始化 使用uv创建虚拟环境: 紫外线真空 启动虚拟环境: source.ven/bin/activate 注意:要停止虚拟环境,请在终端中运行deactivate。 在虚拟环境中安装带有CLI扩展和其他Python依赖项的Python MCP SDK: uv添加“mcp[cli]”httpx

构建气象MCP服务器

测试和运行MCP服务器 MCP服务器现在功能齐全,可以使用MCP检查器进行测试。您将在下一个视频中了解有关检查员的更多信息,但这里有一个预览:

在终端中,通过运行以下命令以开发人员模式启动MCP服务器: mcp-dev-main.py

MCP检查员现在可以在http://localhost:5173;在浏览器中打开URL 选择“连接”按钮 选择“工具”选项卡 选择“列出工具”按钮 选择get_current_weather工具 在get_current_weather面板中,输入纬度和经度,例如63.4463991、10.8127596 在“工具结果”下,您将看到一个包含天气数据的JSON对象 在终端中按Ctrl+C可退出MCP检查器。