
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
open-mcp-weather-demo
Advanced tools
一个基于 Model Context Protocol (MCP) 的天气查询服务器,使用 OpenWeatherMap API 提供实时天气信息和天气预报。
@mcp.tool()
装饰器,便于其他模型通过 MCP 协议调用cd mcp-weather
uv sync
# 直接运行
python main.py
# 或者使用 uv
uv run main.py
在你的 MCP 客户端配置文件中添加:
{
"mcpServers": {
"weather": {
"command": "python",
"args": ["path/to/mcp-weather/main.py"]
}
}
}
你也可以直接在代码中导入和使用这些工具:
import asyncio
from main import get_current_weather, get_weather_forecast
async def example():
# 获取当前天气
weather = await get_current_weather("Beijing,CN", "metric")
print(weather)
# 获取天气预报
forecast = await get_weather_forecast("London,GB", "metric")
print(forecast)
asyncio.run(example())
获取指定位置的当前天气信息。
参数:
location
(必需): 城市名称,格式如 "London,GB" 或 "Beijing,CN"units
(可选): 温度单位,"metric" (摄氏度) 或 "imperial" (华氏度),默认为 "metric"示例:
{
"location": "Beijing,CN",
"units": "metric"
}
返回信息:
获取指定位置的5天天气预报。
参数:
location
(必需): 城市名称,格式如 "London,GB" 或 "Beijing,CN"units
(可选): 温度单位,"metric" (摄氏度) 或 "imperial" (华氏度),默认为 "metric"示例:
{
"location": "New York,US",
"units": "imperial"
}
返回信息:
支持多种城市名称格式:
"Beijing,CN"
- 城市名,国家代码"London,GB"
- 城市名,国家代码"New York,US"
- 城市名,国家代码"Tokyo,JP"
- 城市名,国家代码"Paris,FR"
- 城市名,国家代码本服务器使用 OpenWeatherMap API,已配置 API Key。如需使用自己的 API Key,请修改 main.py
中的 OPENWEATHER_API_KEY
变量。
本项目使用 FastMCP
框架,它提供了简洁的 @mcp.tool()
装饰器来定义 MCP 工具:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("weather")
@mcp.tool()
async def get_current_weather(location: str, units: str = "metric") -> str:
"""Get current weather information for a specific location."""
# 实现逻辑
return weather_info
服务器包含完善的错误处理机制:
mcp-weather/
├── main.py # 主服务器代码 (使用 FastMCP)
├── demo.py # 演示脚本
├── test_tools.py # 工具测试脚本
├── pyproject.toml # 项目配置和依赖
├── README.md # 项目文档
└── uv.lock # 依赖锁定文件
mcp[cli]>=1.12.1
: MCP 协议支持httpx>=0.25.0
: 异步 HTTP 客户端pydantic>=2.0.0
: 数据验证和序列化你可以通过以下方式测试服务器:
uv run python test_tools.py
uv run python demo.py
uv run python main.py
本项目使用 MIT 许可证。
欢迎提交 Issue 和 Pull Request 来改进这个项目!
如果你遇到任何问题或有疑问,请创建 GitHub Issue 或联系开发者。
FAQs
Weather query MCP server using OpenWeatherMap API
We found that open-mcp-weather-demo demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.