get-weather
Retrieve weather information for a specified city using the Weather MCP Server's get-weather tool. Input the city name to generate accurate weather data.
Instructions
Tool to get the weather for a given city
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | The city to get the weather for |
Implementation Reference
- main.ts:19-51 (handler)The handler function for the "get-weather" tool. It takes a city parameter, logs the call timing, calls queryOllama to get weather data, and returns the response as MCP content or a formatted error message.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)Input schema for the get-weather tool using Zod, defining a single required 'city' string parameter.{ city: z.string().describe("The city to get the weather for"), },
- main.ts:13-52 (registration)Registration of the "get-weather" MCP tool using server.tool(), including name, description, input schema, and inline handler function.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'}`, } ] }; } } );
- ollamaClient.ts:26-47 (helper)Supporting helper function queryOllama that the get-weather handler calls. It attempts to query Ollama via HTTP API first, falling back to CLI execution, with timing and timeouts for reliability.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; } } }