MCP Weather Server
by Paritosh008
README.md
# π€οΈ MCP Weather Server
A simple **Model Context Protocol (MCP) Weather Server** built with Node.js that exposes weather functionality as an MCP tool.
This project demonstrates how an MCP server can expose tools that an AI application or MCP client can discover and execute.
> π Built as a practical project to understand **MCP Servers, MCP Tools, stdio transport, Zod validation, and external API integration**.
---
## β¨ Features
* π MCP server using the official MCP SDK
* π οΈ Custom MCP tool: `getWeatherDataByCityName`
* π Weather lookup by city name
* π Environment-variable based API configuration
* β
Input validation using Zod
* π‘ MCP communication using `StdioServerTransport`
* β‘ Built with Node.js and JavaScript
* π§ͺ Testable with MCP Inspector
* π€ Compatible with MCP clients that support stdio transport
---
## ποΈ Architecture
```text
βββββββββββββββββββββββ
β MCP Client β
β β
β Cursor / Inspector β
β Other MCP Client β
ββββββββββββ¬βββββββββββ
β
β MCP / stdio
βΌ
βββββββββββββββββββββββ
β MCP Weather β
β Server β
β index.js β
ββββββββββββ¬βββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β getWeatherDataByCityNameβ
ββββββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β Weather Service β
β External API β
βββββββββββββββββββββββ
```
---
## π§° Tech Stack
| Technology | Purpose |
| ------------- | ----------------------------- |
| Node.js | Runtime |
| JavaScript | Application language |
| MCP SDK | Model Context Protocol server |
| Zod | Input validation |
| pnpm | Package manager |
| MCP Inspector | MCP server testing |
| Weather API | Real-time weather data |
---
## π Project Structure
```text
MyMcp/
β
βββ index.js
βββ package.json
βββ pnpm-lock.yaml
βββ .env
βββ .env.example
βββ .gitignore
βββ README.md
```
---
## π¦ Installation
### 1. Clone the repository
```bash
git clone https://github.com/YOUR_USERNAME/mcp-weather-server.git
```
```bash
cd mcp-weather-server
```
### 2. Install dependencies
This project uses pnpm.
```bash
pnpm install
```
---
## π Environment Variables
Create a `.env` file:
```env
WEATHER_API_KEY=your_weather_api_key
```
> β οΈ Never commit your `.env` file or API key to GitHub.
The repository should contain `.env.example` instead:
```env
WEATHER_API_KEY=your_weather_api_key_here
```
---
## βΆοΈ Run the MCP Server
Start the server with:
```bash
node index.js
```
For an MCP stdio server, **no web page or server URL is expected to appear in the terminal**.
The process waits for an MCP client to communicate with it through stdin/stdout.
---
## π§ͺ Test With MCP Inspector
You can test the MCP server without Cursor Agent.
Run:
```bash
pnpm dlx @modelcontextprotocol/inspector node index.js
```
The MCP Inspector will provide a local URL.
Open it in your browser.
You should see the available MCP tool:
```text
getWeatherDataByCityName
```
### Tool Input
```json
{
"city": "Delhi"
}
```
The MCP server then calls the weather service and returns weather information.
---
## π οΈ Available MCP Tool
### `getWeatherDataByCityName`
Gets weather information for a specified city.
#### Input
```json
{
"city": "Delhi"
}
```
#### Example Response
```json
{
"temp": "30Β°C",
"forecast": "Clear"
}
```
---
## π How MCP Works in This Project
The request flow is:
```text
User
β
AI / MCP Client
β
MCP Tool Call
β
getWeatherDataByCityName
β
Weather API
β
Weather Data
β
MCP Server
β
MCP Client
β
AI Response
```
The important concept is that the AI does not directly contain the weather functionality.
Instead, the AI can discover and call an MCP tool exposed by the server.
---
## π§ What I Learned
This project helped me understand:
* What MCP is
* Why MCP servers are useful
* MCP server architecture
* MCP tools
* Tool schemas
* Zod validation
* `StdioServerTransport`
* MCP client/server communication
* External API integration
* API key management
* Environment variables
* MCP Inspector
* Testing MCP tools without relying on a paid AI agent
---
## π Security
Never expose API keys in source code.
β Don't do:
```javascript
const API_KEY = "my-secret-key";
```
β
Use environment variables:
```javascript
const API_KEY = process.env.WEATHER_API_KEY;
```
And add `.env` to `.gitignore`:
```gitignore
.env
node_modules/
```
---
## π MCP Concepts Demonstrated
```text
MCP
β
βββ MCP Server
β
βββ MCP Client
β
βββ Tools
β
βββ Tool Schema
β
βββ Transport
β βββ stdio
β
βββ External APIs
```
---
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo issues