Skip to main content
Glama

Weather MCP Server

by jefercort
main.ts2.1 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; // 1). I have to create a server that uses MCP. The server should have an endpoint that accepts a post request with a json body containing some variables and retrurns a response with some calculated value based on a those variables. The server should use Zod to validate the input and output data. The server should listen on port 3000 const server = new McpServer({ name: "PRO INDUSTRIAL MCP SERVER", version: "1.0.0" }); // 2). I have to define tools that LLM permit to do actions through the server server.tool( "fetch-weather", //Tool name "tool to fetch the weather of a city", // Tool description { // here there are the input parameters that the tool accepts city: z.string().describe("City name"), }, async ({ city }) => { // This space is where the code goes something that the user wants to do const response = await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${city}&count=10&language=en&format=json`); // Here you can call an external API to get the weather of the city const dataResponse = await response.json(); if (dataResponse.length === 0) { return { content: [ { type: "text", text: `I can't find the weather for the city ${city}. Try another city.`, } ] } } const { latitude, longitude } = dataResponse.results[0]; const weatherResponse = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&hourly=temperature_2m&current=temperature_2m,precipitation,is_day,rain`); const weatherData = await weatherResponse.json(); return { content: [ { type: "text", text: JSON.stringify(weatherData, null, 2), } ] } } ) // 3). To create the conections from customers to the server const transport = new StdioServerTransport(); await server.connect(transport);

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jefercort/practice-mcp-js'

If you have feedback or need assistance with the MCP directory API, please join our Discord server