Skip to main content
Glama
Paritosh008

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
```

---