关于 🌦️ Weather-MCP
Weather-MCP 是基于 FastAPI 的天气服务,可查询美国天气警报和指定坐标的预报,适用于 AI Agent 工作流。它还支持通过 Pydantic AI 进行自然语言交互。
主要功能
- 01查询美国当前天气警报
- 02按地理坐标获取预报
- 03通过 Pydantic AI 自然语言交互
- 04使用 FastMCP 扩展工具
适用场景
- 01查看美国某州的天气警报
- 02获取指定坐标的天气预报
MCP 相关
通过 FastMCP 向 MCP 客户端提供美国天气警报和坐标天气预报工具,并提供 SSE 端点。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
🌦️ Weather-MCP
Weather-MCP 是一个 FastAPI 服务,集成了 FastMCP 和 Pydantic AI,用于提供实时天气预警和预报。用户可以通过自然语言提示词与服务交互,从而轻松获取天气信息。
📋 目录
🚀 功能
- 获取美国各州当前生效的天气预警。
- 根据地理坐标获取详细天气预报。
- 由 Pydantic AI 驱动的自然语言交互。
- 通过 FastMCP 扩展工具集。
🛠️ 安装
先决条件
- Python 3.8 或更高版本
- OpenAI API 密钥(
OPENAI_API_KEY):如需获取 OpenAI API 密钥,请访问 OpenAI API 密钥页面
继续设置之前,请确保已满足这些先决条件。
步骤
-
克隆代码库:
git clone https://github.com/yourusername/weather-mcp.git cd weather-mcp -
创建并激活虚拟环境:
pip install uv uv venv source venv/bin/activate # On Windows: venv\Scripts\activate -
安装依赖项:
uv sync -
设置 OpenAI API 密钥
如需安全地使用 OpenAI 服务,请设置 OPENAI_API_KEY 环境变量。([DEV Community][1])
在 Unix/Linux/macOS 上:
export OPENAI_API_KEY="your-api-key-here"
如需永久保存此更改,请将上述行添加到 shell 配置文件中(例如 ~/.bashrc、~/.zshrc),然后重新加载 shell:([Hacker News][2])
source ~/.bashrc # or source ~/.zshrc
在 Windows(命令提示符)上:
setx OPENAI_API_KEY "your-api-key-here"
设置变量后,重启命令提示符以应用更改。
在 Windows(PowerShell)上:
$env:OPENAI_API_KEY = "your-api-key-here"
-
启动服务:
python weather_server.py
服务可通过 http://localhost:8000/sse 访问。
💡 用法
通过 Pydantic AI 交互
你可以通过 Pydantic AI 使用自然语言提示词与服务交互。([YouTube][1])
示例:
import asyncio
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerHTTP
# Define the MCP server connection
mcp_server = MCPServerHTTP(url="http://localhost:8000/sse")
# Create the Pydantic AI agent
agent = Agent(
model="openai:gpt-4o-mini",
mcp_servers=[mcp_server],
system_prompt="You are a helpful assistant that provides weather information."
)
async def main():
prompt = "Are there any weather alerts for California?"
async with agent.run_mcp_servers():
result = await agent.run(prompt)
print(result.output)
if __name__ == "__main__":
asyncio.run(main())
直接调用工具
或者,你可以使用 FastMCP 客户端直接调用工具。
示例:
import asyncio
from fastmcp import Client
async def main():
async with Client("http://localhost:8000/sse") as client:
tools = await client.list_tools()
print("Available tools:")
for tool in tools:
print(f"- {tool.name}: {tool.description}")
# Call the get_weather_alerts tool
state_code = "CA"
alerts_result = await client.call_tool("get_weather_alerts", {"state": state_code})
print(f"\nWeather alerts for {state_code}:\n{alerts_result[0].text}")
# Call the get_weather_forecast tool
latitude = 34.05
longitude = -118.25
forecast_result = await client.call_tool("get_weather_forecast", {
"latitude": latitude,
"longitude": longitude
})
print(f"\nWeather forecast for ({latitude}, {longitude}):\n{forecast_result[0].text}")
if __name__ == "__main__":
asyncio.run(main())
📡 API 端点
服务通过 MCP 协议公开以下工具:
-
get_weather_alerts(state: str) -> str获取指定美国州当前生效的天气预警。 -
get_weather_forecast(latitude: float, longitude: float) -> str获取指定地理坐标的天气预报。
🤝 贡献
欢迎贡献!请按以下步骤操作:
- Fork 此代码库。
- 创建新分支:
git checkout -b feature-name。 - 进行更改并提交:
git commit -m 'Add feature'。 - 推送到该分支:
git push origin feature-name。 - 创建拉取请求。([GitHub][2])
如需进行重大更改,请先创建议题,讨论你希望进行的更改。([Make a README][3])
📄 许可证
本项目采用 MIT 许可证 授权。
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。