Real-time weather alerts and forecasts via FastAPI integrated with Pydantic AI and FastMCP
Weather-MCP is a FastAPI-based MCP server that integrates real-time weather alerts and forecasts through Model Context Protocol (MCP). This server enhances AI applications by providing natural language interaction capabilities, leveraging Pydantic AI for conversational engagement while connecting to the backend via FastMCP. The purpose of Weather-MCP is to serve as a bridge between AI applications and essential weather data, allowing users to query their local or remote environments through simple, intuitive prompts.
Weather-MCP offers several core features that make it an invaluable tool for integrating climate data into various AI applications. These include:
By adhering to the Model Context Protocol (MCP), Weather-MCP ensures that it can be seamlessly integrated into a wide range of applications. This protocol defines a standardized set of guidelines for exchanging context between the AI application and backend tools such as weather forecasting systems and data sources.
The architecture of Weather-MCP is built around the Model Context Protocol (MCP), which enables it to communicate effectively with other components in an ecosystem. The server's core functionality revolves around handling MCP requests, interpreting them using Pydantic AI, and executing backend tools like FastMCP to fetch or generate relevant weather data.
Weather-MCP leverages FastAPI for server operation, ensuring high performance and scalability. It also integrates with Pydantic AI to handle natural language processing (NLP) tasks, making the interaction between the user and the system more intuitive and user-friendly. Additionally, it utilizes FastMCP tools to interact with external weather services, providing a comprehensive solution for real-time weather intelligence.
graph TD
A[AI Application] -->|MCP Client| B[MCP Protocol]
B --> C[MCP Server]
C --> D[Data Source/Tool]
style A fill:#e1f5fe
style C fill:#f3e5f5
style D fill:#e8f5e8
MCP Client | Resources | Tools | Prompts | Status |
---|---|---|---|---|
Claude Desktop | ✅ | ✅ | ✅ | Full Support |
Continue | ✅ | ✅ | ✅ | Full Support |
Cursor | ❌ | ✅ | ❌ | Tools Only |
To set up the Weather-MCP server, follow these steps:
OPENAI_API_KEY
)Ensure you have these prerequisites in place before proceeding.
Clone the repository:
git clone https://github.com/yourusername/weather-mcp.git
cd weather-mcp
Create and activate a virtual environment:
pip install uv
uv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Install dependencies:
uv sync
Set the OpenAI API Key
To securely use OpenAI services, set the OPENAI_API_KEY
environment variable:
On Unix/Linux/macOS:
export OPENAI_API_KEY="your-api-key-here"
To make this change permanent, add the above line to your shell's configuration file (e.g., ~/.bashrc
, ~/.zshrc
) and reload the shell:
source ~/.bashrc # or source ~/.zshrc
On Windows (Command Prompt):
setx OPENAI_API_KEY "your-api-key-here"
After setting the variable, restart your Command Prompt to apply the changes.
$env:OPENAI_API_KEY = "your-api-key-here"
python weather_server.py
The server will be accessible at http://localhost:8000/sse
.
Weather-MCP can significantly enhance several AI application workflows by providing real-time and accurate weather data. Here are two realistic scenarios:
An emergency response system can utilize Weather-MCP to fetch active weather alerts for specific states, enabling officials to quickly monitor and respond to severe weather conditions. For example, if a state experiences unexpected heavy rainfall, the system could notify emergency personnel through natural language prompts, ensuring that response teams are prepared.
Smart agriculture can leverage Weather-MCP to retrieve detailed weather forecasts for specific geographic regions. Farmers can use this data to plan planting seasons, monitor soil conditions, and manage crop irrigation. For instance, if a region is expected to receive a drought, the server can provide personalized prompts or alerts about water-saving techniques and local weather patterns.
Weather-MCP ensures compatibility with various MCP clients, including but not limited to Claude Desktop, Continue, and Cursor. These applications can connect to Weather-MCP through the Model Context Protocol (MCP), allowing them to fetch real-time weather data and generate relevant insights for users. Below are examples of how these integrations work:
You can interact with the server using natural language prompts via Pydantic AI:
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())
Alternatively, you can directly invoke the tools using FastMCP’s client:
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())
Weather-MCP is designed to be highly performant and compatible with a wide range of AI applications. It can handle a significant number of requests per minute, ensuring smooth operation even under heavy load conditions. The compatibility matrix below highlights the supported platforms:
Client | Resources | Tools | Prompts | Status |
---|---|---|---|---|
Claude Desktop | ✅ | ✅ | ✅ | Full Integration |
Continue | ✅ | ✅ | ✅ | Full Integration |
Cursor | ❌ | ✅ | ❌ | Limited Tools Support |
For advanced configuration and security, follow these best practices:
Example of advanced configuration:
{
"server-name": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-[name]"],
"env": {
"API_KEY": "your-api-key"
}
}
}
This comprehensive documentation ensures that Weather-MCP is a robust and valuable addition to any AI application ecosystem. It covers all key aspects, from setup and integration to advanced configuration and security measures.
This documentation positions Weather-MCP as a powerful tool for real-time weather data management in both emergency response systems and smart agriculture solutions.
Learn to connect to MCP servers over HTTP with Python SDK using SSE for efficient protocol communication
Explore community contributions to MCP including clients, servers, and projects for seamless integration
Python MCP client for testing servers avoid message limits and customize with API key
Next-generation MCP server enhances documentation analysis with AI-powered neural processing and multi-language support
SingleStore MCP Server for database querying schema description ER diagram generation SSL support and TypeScript safety
Powerful GitLab MCP Server enables AI integration for project management, issues, files, and collaboration automation