Skip to main content
Glama
robertn702

OpenWeatherMap MCP Server

get-location-info

Retrieve precise location details using latitude and longitude coordinates through reverse geocoding, enabling accurate identification of geographic areas for enhanced weather and location-based services.

Instructions

Get location information from coordinates (reverse geocoding)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
latitudeYesLatitude coordinate
longitudeYesLongitude coordinate

Implementation Reference

  • The complete handler implementation for the 'get-location-info' tool, registered inline with server.addTool(). Performs reverse geocoding by setting coordinates on the OpenWeather client and calling client.getLocation() to retrieve location name, country, state, etc.
    server.addTool({
      name: "get-location-info",
      description: "Get location information from coordinates (reverse geocoding)",
      parameters: getLocationInfoSchema,
      execute: async (args, { session, log }) => {
        try {
          log.info("Getting location info", { 
            latitude: args.latitude,
            longitude: args.longitude
          });
          
          // Get OpenWeather client
          const client = getOpenWeatherClient(session as SessionData | undefined);
          
          // Set coordinates directly for reverse geocoding
          client.setLocationByCoordinates(args.latitude, args.longitude);
          
          // Fetch location data using reverse geocoding
          const locationData = await client.getLocation();
          
          log.info("Successfully retrieved location info", { 
            latitude: args.latitude,
            longitude: args.longitude,
            location_name: locationData?.name
          });
          
          // Format the response
          const formattedLocation = JSON.stringify({
            coordinates: {
              latitude: args.latitude,
              longitude: args.longitude
            },
            location: locationData ? {
              name: locationData.name,
              country: locationData.country,
              state: locationData.state,
              local_names: locationData.local_names
            } : {
              message: "No location data found for these coordinates"
            }
          }, null, 2);
          
          return {
            content: [
              {
                type: "text",
                text: formattedLocation
              }
            ]
          };
        } catch (error) {
          log.error("Failed to get location info", { 
            error: error instanceof Error ? error.message : 'Unknown error' 
          });
          
          // Provide helpful error messages
          if (error instanceof Error) {
            if (error.message.includes('Invalid coordinates')) {
              throw new Error(`Invalid coordinates: latitude must be between -90 and 90, longitude must be between -180 and 180.`);
            }
            if (error.message.includes('Invalid API key')) {
              throw new Error('Invalid OpenWeatherMap API key. Please check your configuration.');
            }
          }
          
          throw new Error(`Failed to get location info: ${error instanceof Error ? error.message : 'Unknown error'}`);
        }
      }
    });
  • Zod input schema defining required latitude and longitude parameters with validation ranges for the get-location-info tool.
    export const getLocationInfoSchema = z.object({
      latitude: z.number().min(-90).max(90).describe("Latitude coordinate"),
      longitude: z.number().min(-180).max(180).describe("Longitude coordinate"),
    });
  • src/main.ts:629-697 (registration)
    Registration of the 'get-location-info' tool using FastMCP server.addTool(), specifying name, description, parameters schema, and execute handler.
    server.addTool({
      name: "get-location-info",
      description: "Get location information from coordinates (reverse geocoding)",
      parameters: getLocationInfoSchema,
      execute: async (args, { session, log }) => {
        try {
          log.info("Getting location info", { 
            latitude: args.latitude,
            longitude: args.longitude
          });
          
          // Get OpenWeather client
          const client = getOpenWeatherClient(session as SessionData | undefined);
          
          // Set coordinates directly for reverse geocoding
          client.setLocationByCoordinates(args.latitude, args.longitude);
          
          // Fetch location data using reverse geocoding
          const locationData = await client.getLocation();
          
          log.info("Successfully retrieved location info", { 
            latitude: args.latitude,
            longitude: args.longitude,
            location_name: locationData?.name
          });
          
          // Format the response
          const formattedLocation = JSON.stringify({
            coordinates: {
              latitude: args.latitude,
              longitude: args.longitude
            },
            location: locationData ? {
              name: locationData.name,
              country: locationData.country,
              state: locationData.state,
              local_names: locationData.local_names
            } : {
              message: "No location data found for these coordinates"
            }
          }, null, 2);
          
          return {
            content: [
              {
                type: "text",
                text: formattedLocation
              }
            ]
          };
        } catch (error) {
          log.error("Failed to get location info", { 
            error: error instanceof Error ? error.message : 'Unknown error' 
          });
          
          // Provide helpful error messages
          if (error instanceof Error) {
            if (error.message.includes('Invalid coordinates')) {
              throw new Error(`Invalid coordinates: latitude must be between -90 and 90, longitude must be between -180 and 180.`);
            }
            if (error.message.includes('Invalid API key')) {
              throw new Error('Invalid OpenWeatherMap API key. Please check your configuration.');
            }
          }
          
          throw new Error(`Failed to get location info: ${error instanceof Error ? error.message : 'Unknown error'}`);
        }
      }
    });

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/robertn702/mcp-openweathermap'

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