Skip to main content
Glama

get-current-air-pollution

Retrieve real-time air quality data for any location using city name or coordinates. Access accurate pollution levels via the OpenWeatherMap MCP Server to monitor environmental conditions effectively.

Instructions

Get current air quality data

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
locationYesCity name (e.g., 'New York') or coordinates (e.g., 'lat,lon')

Implementation Reference

  • src/main.ts:519-626 (registration)
    Registers the 'get-current-air-pollution' tool with the MCP server, including the inline handler function that fetches and formats current air pollution data.
    server.addTool({ name: "get-current-air-pollution", description: "Get current air quality data", parameters: getCurrentAirPollutionSchema, execute: async (args, { session, log }) => { try { log.info("Getting current air pollution", { location: args.location }); // Get OpenWeather client const client = getOpenWeatherClient(session as SessionData | undefined); // Configure client for this request configureClientForLocation(client, args.location); // Fetch current air pollution data const pollutionData = await client.getCurrentAirPollution(); log.info("Successfully retrieved current air pollution", { location: args.location, aqi: pollutionData.aqi }); // Format the response const formattedPollution = JSON.stringify({ location: args.location, coordinates: { latitude: pollutionData.lat, longitude: pollutionData.lon }, air_quality: { index: pollutionData.aqi, description: pollutionData.aqiName, scale: "1 (Good) to 5 (Very Poor)" }, pollutants: { carbon_monoxide: { value: pollutionData.components.co, unit: "μg/m³", description: "Carbon monoxide" }, nitrogen_monoxide: { value: pollutionData.components.no, unit: "μg/m³", description: "Nitrogen monoxide" }, nitrogen_dioxide: { value: pollutionData.components.no2, unit: "μg/m³", description: "Nitrogen dioxide" }, ozone: { value: pollutionData.components.o3, unit: "μg/m³", description: "Ozone" }, sulphur_dioxide: { value: pollutionData.components.so2, unit: "μg/m³", description: "Sulphur dioxide" }, pm2_5: { value: pollutionData.components.pm2_5, unit: "μg/m³", description: "Fine particles matter" }, pm10: { value: pollutionData.components.pm10, unit: "μg/m³", description: "Coarse particulate matter" }, ammonia: { value: pollutionData.components.nh3, unit: "μg/m³", description: "Ammonia" } }, timestamp: pollutionData.dt.toISOString() }, null, 2); return { content: [ { type: "text", text: formattedPollution } ] }; } catch (error) { log.error("Failed to get current air pollution", { error: error instanceof Error ? error.message : 'Unknown error' }); // Provide helpful error messages if (error instanceof Error) { if (error.message.includes('city not found')) { throw new Error(`Location "${args.location}" not found. Please check the spelling or try using coordinates.`); } if (error.message.includes('Invalid API key')) { throw new Error('Invalid OpenWeatherMap API key. Please check your configuration.'); } } throw new Error(`Failed to get current air pollution: ${error instanceof Error ? error.message : 'Unknown error'}`); } } });
  • The execute function that implements the tool logic: resolves OpenWeather client, configures location, fetches pollution data, formats it with AQI and pollutants, returns formatted JSON.
    execute: async (args, { session, log }) => { try { log.info("Getting current air pollution", { location: args.location }); // Get OpenWeather client const client = getOpenWeatherClient(session as SessionData | undefined); // Configure client for this request configureClientForLocation(client, args.location); // Fetch current air pollution data const pollutionData = await client.getCurrentAirPollution(); log.info("Successfully retrieved current air pollution", { location: args.location, aqi: pollutionData.aqi }); // Format the response const formattedPollution = JSON.stringify({ location: args.location, coordinates: { latitude: pollutionData.lat, longitude: pollutionData.lon }, air_quality: { index: pollutionData.aqi, description: pollutionData.aqiName, scale: "1 (Good) to 5 (Very Poor)" }, pollutants: { carbon_monoxide: { value: pollutionData.components.co, unit: "μg/m³", description: "Carbon monoxide" }, nitrogen_monoxide: { value: pollutionData.components.no, unit: "μg/m³", description: "Nitrogen monoxide" }, nitrogen_dioxide: { value: pollutionData.components.no2, unit: "μg/m³", description: "Nitrogen dioxide" }, ozone: { value: pollutionData.components.o3, unit: "μg/m³", description: "Ozone" }, sulphur_dioxide: { value: pollutionData.components.so2, unit: "μg/m³", description: "Sulphur dioxide" }, pm2_5: { value: pollutionData.components.pm2_5, unit: "μg/m³", description: "Fine particles matter" }, pm10: { value: pollutionData.components.pm10, unit: "μg/m³", description: "Coarse particulate matter" }, ammonia: { value: pollutionData.components.nh3, unit: "μg/m³", description: "Ammonia" } }, timestamp: pollutionData.dt.toISOString() }, null, 2); return { content: [ { type: "text", text: formattedPollution } ] }; } catch (error) { log.error("Failed to get current air pollution", { error: error instanceof Error ? error.message : 'Unknown error' }); // Provide helpful error messages if (error instanceof Error) { if (error.message.includes('city not found')) { throw new Error(`Location "${args.location}" not found. Please check the spelling or try using coordinates.`); } if (error.message.includes('Invalid API key')) { throw new Error('Invalid OpenWeatherMap API key. Please check your configuration.'); } } throw new Error(`Failed to get current air pollution: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
  • Zod schema defining input parameters for the tool: a location string (city or coordinates).
    export const getCurrentAirPollutionSchema = z.object({ location: z.string().describe("City name (e.g., 'New York') or coordinates (e.g., 'lat,lon')"), });
  • Helper function to get or create a cached OpenWeatherAPI client instance based on the session's API key.
    export function getOpenWeatherClient(session: SessionData | null | undefined): OpenWeatherAPI { // For stdio transport, use the global session const effectiveSession = session || getStdioSession(); if (!effectiveSession) { throw new Error("No authentication session available"); } const { apiKey } = effectiveSession; // Check cache first let client = clientCache.get(apiKey); if (!client) { // Create new client client = new OpenWeatherAPI({ key: apiKey, // Default to metric units, can be overridden per request units: "metric" }); // Cache the client clientCache.set(apiKey, client); } return client; }
  • Helper to parse location string and configure the API client with coordinates or city name.
    export function configureClientForLocation( client: OpenWeatherAPI, location: string, units?: Units ): OpenWeatherAPI { // Parse location const parsed = parseLocation(location); // Set location based on type if (parsed.type === 'coordinates' && parsed.latitude && parsed.longitude) { client.setLocationByCoordinates(parsed.latitude, parsed.longitude); } else if (parsed.type === 'city' && parsed.city) { client.setLocationByName(parsed.city); } else { throw new Error("Invalid location format"); } // Set units if provided if (units) { client.setUnits(units); } return client; }

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