HelloWorld mcp:带FastMCP的天气mcp服务器
一个简单而有效的MCP服务器(Python微服务),用于使用 OpenWeatherMap API,基于FastMCP(MCP生态系统中的服务器框架)构建。
特性
- 按城市名称、国家或邮政编码获取当前天气
- 湿度、温度(°C)、风速和描述
- 使用MCP工具箱的简单API接口
需求
- Python 3.11+
- OpenWeatherMap API密钥(看这里)
安装
- 克隆存储库```bash
git clone
cd HelloWorld-mcp
1. **安装依赖项** (使用PEP 621 `pyproject.toml`)```bash
uv pip install -r requirements.txt # or use your preferred tool for PEP 621 dependencies
- 配置环境
- 复制.env.example到.env并填写您的OPENWEATHER_API_KEY.
用法
作为Python模块```bash
python -m src.weather
### 使用MCP CLI```bash
mcp dev src/weather.py
在流行客户端中作为本地MCP服务器运行
您还可以使用以下配置从客户端/主机(如Claude Desktop、Cursor、VS Code等)本地运行此MCP服务器。 确保你有 uv 已安装包管理器。调整 --directory 路径以匹配您自己的项目位置。
添加或更新您的mcpServers配置(例如在主机/编辑器设置中):```json
{
"mcpServers": {
"weather": {
"command": "uv",
"args": [
"--directory",
"D:\model-context-protocol\HelloWorld-mcp",
"run",
"src/weather.py"
]
}
}
}
> **注:**
>
> - 将目录路径更改为项目所在的位置。
> - `uv` 必须全局安装或在您的环境中可用。 (请参阅uv文档)
## 工具已实施
`get_weather(location: str) -> str`:返回特定城市、国家或邮政编码的格式化天气信息。
#### 示例```
Weather in London, GB: light rain. 14.2°C, humidity 82%, wind 5.1 m/s.
配置
创建一个 .env 项目根目录中的文件:OPENWEATHER_API_KEY=your_actual_api_key_here
文件结构
src/weather.py–带气象工具逻辑的主MCP服务器src/config.py–从环境加载设置(API密钥)pyproject.toml–项目元数据和依赖关系
