LangChain MCP Weather Application
A complete example demonstrating how to build and consume a Model Context Protocol (MCP) server using:
- Python
- FastMCP
- LangChain
- OpenAI
- Open-Meteo APIs
The project exposes weather information through an MCP server and allows a LangChain agent to discover and invoke weather tools dynamically.
Features
The weather tool returns:
- Latitude
- Longitude
- Timezone
- Humidity
- Wind Speed
- Weather Conditions
- Sunrise Time
- Sunset Time
- 7-Day Forecast
The application demonstrates:
- Creating an MCP server with FastMCP
- Registering MCP tools
- Connecting to MCP servers with LangChain
- Discovering tools dynamically
- Building AI agents that invoke MCP tools
- Integrating external REST APIs
Architecture
+-------------------+
| LangChain Agent |
+---------+---------+
|
v
+-------------------+
| MCP Client |
| MultiServerMCP |
+---------+---------+
|
| stdio
|
v
+-------------------+
| MCP Server |
| FastMCP |
+---------+---------+
|
v
+-------------------+
| Weather Service |
+---------+---------+
|
v
+-------------------+
| Open-Meteo APIs |
+-------------------+
Project Structure
project/
│
├── server.py
├── client.py
├── agent.py
├── weather_service.py
├── requirements.txt
├── .env
└── README.md
Components
1. weather_service.py
Contains the business logic responsible for:
Geocoding
Converts a city name into:
- Latitude
- Longitude
- Timezone
Uses:
https://geocoding-api.open-meteo.com
Weather Retrieval
Fetches:
- Current humidity
- Wind speed
- Weather conditions
- Sunrise
- Sunset
- 7-day forecast
Uses:
https://api.open-meteo.com
Example Output
{
"city": "Matadi",
"latitude": -5.799,
"longitude": 13.440,
"timezone": "Africa/Kinshasa",
"humidity": 82,
"wind_speed": 12.4,
"weather_conditions": 1,
"sunrise": "2026-06-01T06:03",
"sunset": "2026-06-01T17:58",
"forecast": [
{
"date": "2026-06-01",
"min_temp": 20.2,
"max_temp": 29.1
}
]
}
2. server.py
Creates the MCP server.
MCP Server Initialization
mcp = FastMCP("WeatherServer")
Tool Registration
@mcp.tool()
def weather(city: str):
return get_weather(city)
Start Server
mcp.run()
The server exposes the weather tool to any MCP-compatible client.
3. client.py
Demonstrates connecting to the MCP server and discovering tools.
Create MCP Client
client = MultiServerMCPClient(
{
"weather_server": {
"transport": "stdio",
"command": "python",
"args": ["server.py"]
}
}
)
Discover Tools
tools = await client.get_tools()
Example Output
TOOLS
[StructuredTool(name='weather', ...)]
4. agent.py
Builds an AI agent capable of invoking MCP tools.
Load Environment Variables
load_dotenv()
Create OpenAI Model
llm = ChatOpenAI(
model="gpt-5-nano"
)
Retrieve MCP Tools
tools = await client.get_tools()
Create Agent
agent = create_agent(
model=llm,
tools=tools,
system_prompt="You are a weather assistant."
)
Invoke Agent
result = await agent.ainvoke(
{
"messages": [
{
"role": "user",
"content": "What is the weather in Matadi?"
}
]
}
)
Installation
Clone Repository
git clone https://github.com/your-org/weather-mcp.git
cd weather-mcp
Create Virtual Environment
Using venv
python -m venv .venv
Activate:
Linux / macOS
source .venv/bin/activate
Windows
.venv\Scripts\activate
Install Dependencies
pip install -r requirements.txt
requirements.txt
mcp
langchain
langchain-openai
langchain-mcp-adapters
python-dotenv
requests
openai
Environment Variables
Create a .env file:
OPENAI_API_KEY=your_openai_api_key
Running the Application
Option 1: Run MCP Server
python server.py
The server starts and waits for MCP client requests.
Option 2: Test MCP Client
python client.py
Example:
TOOLS
[StructuredTool(name='weather', ...)]
Option 3: Run LangChain Agent
python agent.py
Example:
{
"city": "Matadi",
"latitude": -5.799,
"longitude": 13.440,
"timezone": "Africa/Kinshasa",
"humidity": 82,
"wind_speed": 12.4,
"weather_conditions": 1,
"sunrise": "2026-06-01T06:03",
"sunset": "2026-06-01T17:58",
"forecast": [
{
"date": "2026-06-01",
"min_temp": 20.2,
"max_temp": 29.1
}
]
}
MCP Workflow
User Question
|
v
LangChain Agent
|
v
MCP Tool Discovery
|
v
Weather Tool
|
v
Open-Meteo APIs
|
v
Weather Data
|
v
Agent Response
Example Queries
What is the weather in Paris?
What is the humidity in New York?
Give me the 7-day forecast for Tokyo.
When is sunrise in London?
What is the wind speed in Matadi?
Future Enhancements
- Multiple MCP servers
- Weather alerts
- Historical weather data
- Air quality information
- LangGraph integration
- Redis caching
- Azure deployment
- Streaming responses
- RAG integration
- Multi-agent orchestration
Technologies Used
- Python
- FastMCP
- LangChain
- OpenAI
- Open-Meteo API
- AsyncIO
- MCP (Model Context Protocol)
Learning Objectives
This project teaches:
- MCP Server Development
- MCP Tool Registration
- MCP Client Integration
- LangChain Tool Discovery
- AI Agent Tool Calling
- REST API Integration
- Async Python Programming
- OpenAI + LangChain Integration
License
MIT License
Copyright (c) 2026
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files to deal in the Software without restriction.