main.ts•1.72 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "weather",
version: "1.0.0",
});
function getWeatherCondition(temperature: number): string {
if (temperature < 0) return "freezing ❄️";
if (temperature < 10) return "cold 🥶";
if (temperature < 15) return "cool 🌤️";
if (temperature < 20) return "mild 😊";
if (temperature < 25) return "warm ☀️";
if (temperature < 30) return "hot 🔥";
return "very hot 🌡️";
}
server.tool(
'fetch-Weather',
'Get the weather for a given location',
{
city: z.string().describe("city name like 'Bogotá'")
},
async (params) => {
const info = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${params.city}`);
const data = await info.json();
const latitude = data.results[0].latitude;
const longitude = data.results[0].longitude;
const weather = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t_weather=true`);
const weatherData = await weather.json();
const temperature = weatherData.current_weather.temperature;
const condition = getWeatherCondition(temperature);
return {
content: [
{
type: "text",
text: `The weather in ${params.city} is ${condition} with a temperature of ${temperature}°C`,
}
]
}
}
);
const transport = new StdioServerTransport();
await server.connect(transport);