Skip to main content
Glama

get-weather

Retrieve current weather conditions for any city by specifying the location name. This tool provides essential weather information through the Weather MCP Server.

Instructions

Tool to get the weather for a given city

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
cityYesThe city to get the weather for

Implementation Reference

  • main.ts:19-51 (handler)
    The handler function for the 'get-weather' tool. It invokes queryOllama to fetch weather data for the given city, handles errors gracefully, and returns the response in MCP content format.
    async ({ city }) => { console.log(`🌤️ MCP tool called for: ${city}`); const toolStartTime = Date.now(); try { const response = await queryOllama(city); const toolEndTime = Date.now(); console.log(`✅ MCP tool completed in ${toolEndTime - toolStartTime}ms`); return { content: [ { type: "text", text: response, } ] }; } catch (error) { const toolEndTime = Date.now(); console.error(`❌ MCP tool failed after ${toolEndTime - toolStartTime}ms:`, error); // Return a helpful error message instead of throwing return { content: [ { type: "text", text: `Unable to get weather for ${city}. Ollama may be slow or unavailable. Error: ${error instanceof Error ? error.message : 'Unknown error'}`, } ] }; } }
  • main.ts:16-18 (schema)
    Zod schema defining the input parameter 'city' for the get-weather tool.
    { city: z.string().describe("The city to get the weather for"), },
  • main.ts:13-52 (registration)
    Registration of the 'get-weather' tool using McpServer.tool(), including name, description, input schema, and handler reference.
    server.tool( "get-weather", "Tool to get the weather for a given city", { city: z.string().describe("The city to get the weather for"), }, async ({ city }) => { console.log(`🌤️ MCP tool called for: ${city}`); const toolStartTime = Date.now(); try { const response = await queryOllama(city); const toolEndTime = Date.now(); console.log(`✅ MCP tool completed in ${toolEndTime - toolStartTime}ms`); return { content: [ { type: "text", text: response, } ] }; } catch (error) { const toolEndTime = Date.now(); console.error(`❌ MCP tool failed after ${toolEndTime - toolStartTime}ms:`, error); // Return a helpful error message instead of throwing return { content: [ { type: "text", text: `Unable to get weather for ${city}. Ollama may be slow or unavailable. Error: ${error instanceof Error ? error.message : 'Unknown error'}`, } ] }; } } );
  • Core helper function that queries Ollama for weather information using HTTP API with fallback to CLI, implementing the actual LLM-based weather query logic.
    export async function queryOllama(city: string): Promise<string> { console.log(`🌤️ Querying weather for ${city}...`); const startTime = Date.now(); try { const result = await queryOllamaHTTP(city); const endTime = Date.now(); console.log(`✅ Query completed in ${endTime - startTime}ms`); return result; } catch (error) { console.error("❌ HTTP API failed, trying CLI..."); try { const result = await queryOllamaCLI(city); const endTime = Date.now(); console.log(`✅ CLI query completed in ${endTime - startTime}ms`); return result; } catch (cliError) { console.error("❌ Both HTTP and CLI failed"); throw error; } } }

Other Tools

Latest Blog Posts

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/codewith1984/weather-mcp-server-typescript'

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