Skip to main content
Glama
Dagudelot

MCP Server with OpenAI Integration

by Dagudelot

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
NameRequiredDescriptionDefault
cityYesThe city to get weather information for
unitNoTemperature unitcelsius

Implementation Reference

  • 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}`; }
  • 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], }; });
  • 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, }, ], };

Other Tools

Related 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/Dagudelot/mcp-server-openai-sdk'

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