#
tokens: 1565/50000 5/5 files
lines: off (toggle) GitHub
raw markdown copy
# Directory Structure

```
├── .gitignore
├── package-lock.json
├── package.json
├── README.md
├── src
│   └── server.ts
└── tsconfig.json
```

# Files

--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------

```
.env
node_modules/
dist/
.vscode/
.idea/
*.log
npm-debug.log*
.DS_Store
Thumbs.db

```

--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------

```markdown
# WeatherAPI MCP Server

An MCP server that provides current weather and air quality data using WeatherAPI.

## Features

- Get current weather data for any city
- Air quality information (optional)
- Dynamic URI support for weather resources
- Easy integration with n8n, Claude Desktop App, Windsurf IDE,Cursor IDE, and other MCP clients

## Getting Started

### Get WeatherAPI Key

1. Go to [WeatherAPI.com](https://www.weatherapi.com)
2. Sign up for a free account
3. After signing in, go to your dashboard
4. Copy your API key from the "API Keys" section

### MCP Configuration

Add the following configuration to your Windsurf MCP config file:

```json
{
  "mcpServers": {
    "weather": {
      "command": "npx",
      "args": ["-y", "@swonixs/weatherapi-mcp"],
      "env": {
        "WEATHER_API_KEY": "YOUR_API_KEY_HERE"
      }
    }
  }
}
```

Replace `YOUR_API_KEY_HERE` with the API key you obtained from WeatherAPI.com.

### Tools

#### get_weather

Get current weather data for a specified city.

Parameters:
- `location` (string): City name

Example response:
```json
{
  "location": "London",
  "country": "United Kingdom",
  "temp_c": 15.0,
  "condition": "Partly cloudy",
  "humidity": 71,
  "wind_kph": 14.4,
  "air_quality": {
    "co": 230.3,
    "no2": 13.5,
    "o3": 52.9,
    "pm2_5": 8.5,
    "pm10": 12.1,
    "us-epa-index": 1
  }
}
```

### Repository

[WeatherAPI MCP Server](https://github.com/swonixs/weatherapi-mcp)

## License

MIT

```

--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------

```json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ES2022",
    "moduleResolution": "node",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "declaration": true,
    "allowJs": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

```

--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------

```json
{
  "name": "@swonixs/weatherapi-mcp",
  "version": "1.1.0",
  "description": "WeatherAPI MCP sunucusu - Hava durumu ve hava kalitesi verileri için MCP aracı",
  "type": "module",
  "main": "dist/server.js",
  "types": "dist/server.d.ts",
  "bin": {
    "weatherapi-mcp": "./dist/server.js"
  },
  "scripts": {
    "build": "tsc",
    "start": "node dist/server.js",
    "dev": "ts-node --esm src/server.ts",
    "prepare": "npm run build"
  },
  "keywords": [
    "mcp",
    "weather",
    "weatherapi",
    "hava-durumu",
    "modelcontextprotocol",
    "air-quality"
  ],
  "author": "swonixs",
  "license": "MIT",
  "dependencies": {
    "@modelcontextprotocol/sdk": "^1.7.0",
    "axios": "^1.6.7",
    "dotenv": "^16.4.5",
    "zod": "^3.22.4"
  },
  "devDependencies": {
    "@types/node": "^20.11.25",
    "ts-node": "^10.9.2",
    "typescript": "^5.4.2"
  },
  "files": [
    "dist",
    "README.md"
  ]
}

```

--------------------------------------------------------------------------------
/src/server.ts:
--------------------------------------------------------------------------------

```typescript
#!/usr/bin/env node

import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import axios from 'axios';
import dotenv from 'dotenv';

// .env dosyasını yükle
dotenv.config();

// Weather API yapılandırması
const API_KEY = process.env.WEATHER_API_KEY;
if (!API_KEY) {
  throw new Error('WEATHER_API_KEY çevre değişkeni tanımlanmamış!');
}
const BASE_URL = 'http://api.weatherapi.com/v1';

// MCP sunucusu oluştur
const server = new McpServer({
  name: "WeatherAPI",
  version: "1.1.0"
});

// Weather API tool tanımı
type WeatherArgs = {
  location: string;
};

const getWeather = async (args: WeatherArgs) => {
  try {
    const response = await axios.get(`${BASE_URL}/current.json`, {
      params: {
        key: API_KEY,
        q: args.location
      }
    });

    const data = response.data;
    return {
      content: [{
        type: "text" as const,
        text: JSON.stringify({
          location: data.location.name,
          country: data.location.country,
          temp_c: data.current.temp_c,
          condition: data.current.condition.text,
          humidity: data.current.humidity,
          wind_kph: data.current.wind_kph
        }, null, 2)
      }]
    };
  } catch (error) {
    if (axios.isAxiosError(error)) {
      throw new Error(`Hava durumu bilgisi alınamadı: ${error.response?.data?.error?.message || error.message}`);
    }
    throw error;
  }
};

// Tool'u kaydet
server.tool(
  "get_weather",
  {
    location: z.string().describe("Şehir adı")
  },
  async (args: WeatherArgs) => getWeather(args)
);

// Weather resource tanımı
server.resource(
  "weather",
  new ResourceTemplate("weather://{city}/current", { list: undefined }),
  async (uri, variables) => {
    const city = variables.city as string;
    const result = await getWeather({ location: city });
    return {
      contents: [{
        uri: uri.href,
        text: result.content[0].text
      }]
    };
  }
);

// Sunucuyu başlat
const transport = new StdioServerTransport();
await server.connect(transport);

```