get_weather
Retrieve current weather data for any city using the 'get_weather' tool on the MCP Server with OpenAI Integration. Specify city and temperature unit (Celsius or Fahrenheit) for accurate results.
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 get_weather tool logic by generating simulated weather data 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 when parsing arguments for the get_weather tool.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:29-48 (registration)Tool definition object that registers the get_weather tool with its name, description, and input schema.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"] }, };
- src/index.ts:66-70 (registration)Registration of the tool in the MCP server's list tools request handler, making it discoverable.this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [weatherTool], }; });
- src/index.ts:77-88 (handler)The switch case in the call tool request handler that routes get_weather calls to the handler function and returns the result.case "get_weather": const { city, unit } = WeatherToolSchema.parse(args); const weatherInfo = await getWeatherInfo(city, unit); return { content: [ { type: "text", text: weatherInfo, }, ], };