UNPKG

@mseep/weatherapi-mcp

Version:

WeatherAPI MCP sunucusu - Hava durumu ve hava kalitesi verileri için MCP aracı

68 lines (67 loc) 2.27 kB
#!/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" }); const getWeather = async (args) => { 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", 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) => getWeather(args)); // Weather resource tanımı server.resource("weather", new ResourceTemplate("weather://{city}/current", { list: undefined }), async (uri, variables) => { const city = variables.city; 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);