🌤️ 基于FastMCP的气象MCP服务器
一个简单的 模型上下文协议(MCP) 服务器构建于 FastMCP 演示了如何将自定义工具暴露给AI运行时。\ 该示例定义了单个工具, get_weather,它返回给定位置的模拟天气数据。
______________________________________________________________________
🚀 特性
- ✅ 最小、易于理解的MCP服务器
- ⚙️ 实现a
get_weather工具 - 🧩 已准备好与MCP兼容的客户端集成
- 🐍 用干净、现代的Python编写
______________________________________________________________________
🧠 运作原理
这 FastMCP 类封装了MCP stdio服务器的样板设置和通信。\ 当服务器启动时,它会暴露 get_weather 作为其他代理或IDE可以调用的可调用MCP工具。
______________________________________________________________________
🧩 代码概述
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("weather")
@mcp.tool()
def get_weather(location: str) -> str:
"""Get the current weather for a given location."""
return f"The current weather in {location} is sunny with a temperature of 25°C."
if __name__ == "__main__":
mcp.run()
⚡️ Getting Started
1️⃣ Clone the repo
bash
Copy code
git clone https://github.com//weather-mcp.git
cd weather-mcp
2️⃣ Create a virtual environment
bash
Copy code
uv venv
source .venv/bin/activate # macOS/Linux
3️⃣ Install dependencies
bash
Copy code
uv pip install 'mcp[cli]'
4️⃣ Run the server
bash
Copy code
uv run python main.py
If everything is configured correctly, the server will start and listen for MCP stdio connections.
🔍 Example Usage
You can connect this MCP server to any MCP-compatible client (e.g., an IDE, LLM runtime, or agent).
When prompted, provide:
Setting Value
Command uv
Arguments run --directory /path/to/weather-mcp python main.py
🧰 Project Structure
bash
Copy code
weather-mcp/
│
├── main.py # MCP server source
├── pyproject.toml # Project metadata (for uv/pdm)
└── README.md # You are here
💡 Future Improvements
Integrate with a real weather API (e.g., OpenWeatherMap)
Add error handling and temperature units
Extend to multi-location forecasting and alerts
👨💻 Author
Bhavesh Kalluru
AI Engineer | Generative AI | LLM Applications | MCP Integrations
📍 Looking for full-time opportunities in the U.S.