reverse-geocode
Convert geographic coordinates to human-readable addresses using Google Maps data. Input latitude and longitude to retrieve location information.
Instructions
Convert coordinates to an address
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| latitude | Yes | The latitude | |
| longitude | Yes | The longitude |
Implementation Reference
- src/maps.ts:203-259 (handler)The main handler function for the reverse-geocode tool. It uses Google Maps reverse geocoding API to convert latitude and longitude to an address, formats the result using formatLocationToMarkdown, and returns it as markdown text.export async function reverseGeocode( params: z.infer<typeof reverseGeocodeSchema>, extra?: any ) { const apiKey = process.env.GOOGLE_MAPS_API_KEY; if (!apiKey) { throw new Error("GOOGLE_MAPS_API_KEY is required"); } try { const response = await googleMapsClient.reverseGeocode({ params: { latlng: [params.latitude, params.longitude], key: apiKey, }, }); const results = response.data.results; if (results.length === 0) { return { content: [ { type: "text" as const, text: "No results found for the given coordinates.", }, ], }; } const location = results[0]; return { content: [ { type: "text" as const, text: formatLocationToMarkdown( "Reverse Geocoded Location", location.formatted_address, params.latitude, params.longitude, location.place_id ), }, ], }; } catch (error) { return { content: [ { type: "text" as const, text: `Error reverse geocoding coordinates: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } }
- src/maps.ts:109-112 (schema)Zod schema defining the input parameters for the reverse-geocode tool: latitude and longitude as numbers.export const reverseGeocodeSchema = z.object({ latitude: z.number().describe("The latitude"), longitude: z.number().describe("The longitude"), });
- src/index.ts:70-77 (registration)Registration of the reverse-geocode tool in the MCP server, linking the name, description, schema, and handler function.server.tool( "reverse-geocode", "Convert coordinates to an address", reverseGeocodeSchema.shape, async (params) => { return await reverseGeocode(params); } );
- src/maps.ts:7-13 (helper)Helper function to format the geocoded location data into markdown, used by the reverseGeocode handler.function formatLocationToMarkdown(title: string, address: string, lat: number, lng: number, placeId?: string): string { let markdown = `# ${title}\n\n`; markdown += `Address: ${address} \n`; markdown += `Coordinates: ${lat}, ${lng} \n`; if (placeId) markdown += `Place ID: \`${placeId}\` \n`; markdown += `Google Maps: [View on Maps](https://maps.google.com/?q=${lat},${lng})`; return markdown;