get_weather
Retrieve current weather conditions and temperature for any city, with options for Celsius or Fahrenheit units.
Instructions
Get current weather information for a specific city
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| city | Yes | The city to get weather information for | |
| unit | No | Temperature unit | celsius |
Implementation Reference
- src/index.ts:17-26 (handler)The core handler function that implements the logic for the 'get_weather' tool, generating simulated weather information for the given city and unit.async function getWeatherInfo(city: string, unit: "celsius" | "fahrenheit" = "celsius"): Promise<string> { // Simulate weather API call const temperature = Math.floor(Math.random() * 30) + 10; // Random temp between 10-40 const conditions = ["sunny", "cloudy", "rainy", "partly cloudy"]; const condition = conditions[Math.floor(Math.random() * conditions.length)]; const tempDisplay = unit === "fahrenheit" ? `${Math.round(temperature * 9/5 + 32)}°F` : `${temperature}°C`; return `Weather in ${city}: ${tempDisplay}, ${condition}`; }
- src/index.ts:11-14 (schema)Zod schema used for input validation of the 'get_weather' tool parameters.const WeatherToolSchema = z.object({ city: z.string().describe("The city to get weather information for"), unit: z.enum(["celsius", "fahrenheit"]).optional().default("celsius").describe("Temperature unit"), });
- src/index.ts:66-70 (registration)Registration of the 'get_weather' tool in the MCP server's tool listing handler.this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [weatherTool], }; });
- src/index.ts:73-92 (handler)MCP server request handler for tool calls, dispatching to getWeatherInfo when 'get_weather' is called and returning the result.this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; switch (name) { case "get_weather": const { city, unit } = WeatherToolSchema.parse(args); const weatherInfo = await getWeatherInfo(city, unit); return { content: [ { type: "text", text: weatherInfo, }, ], }; default: throw new Error(`Unknown tool: ${name}`); } });
- src/index.ts:29-48 (schema)Definition of the 'get_weather' tool including its MCP input schema for tool discovery.const weatherTool: Tool = { name: "get_weather", description: "Get current weather information for a specific city", inputSchema: { type: "object", properties: { city: { type: "string", description: "The city to get weather information for" }, unit: { type: "string", enum: ["celsius", "fahrenheit"], default: "celsius", description: "Temperature unit" } }, required: ["city"] }, };