Skip to main content
Glama
Dagudelot

MCP Server with OpenAI Integration

by Dagudelot

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

Implementation Reference

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

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

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

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