Skip to main content
Glama

geocode

Convert addresses into geographic coordinates for mapping and location-based applications.

Instructions

Convert an address to coordinates

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
addressYesThe address to geocode

Implementation Reference

  • The handler function that performs geocoding using Google Maps API, handles errors, and formats the markdown output.
    export async function geocode(
      params: z.infer<typeof geocodeSchema>,
      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.geocode({
          params: {
            address: params.address,
            key: apiKey,
          },
        });
    
        const results = response.data.results;
        if (results.length === 0) {
          return {
            content: [
              {
                type: "text" as const,
                text: "No results found for the given address.",
              },
            ],
          };
        }
    
        const location = results[0];
        return {
          content: [
            {
              type: "text" as const,
              text: formatLocationToMarkdown(
                "Geocoded Location", 
                location.formatted_address, 
                location.geometry.location.lat, 
                location.geometry.location.lng,
                location.place_id
              ),
            },
          ],
        };
      } catch (error) {
        return {
          content: [
            {
              type: "text" as const,
              text: `Error geocoding address: ${
                error instanceof Error ? error.message : String(error)
              }`,
            },
          ],
        };
      }
    }
  • Zod schema defining the input parameters for the geocode tool: an address string.
    export const geocodeSchema = z.object({
      address: z.string().describe("The address to geocode"),
    });
  • src/index.ts:60-67 (registration)
    Registration of the 'geocode' tool in the MCP server, providing name, description, input schema, and handler wrapper.
    server.tool(
      "geocode",
      "Convert an address to coordinates",
      geocodeSchema.shape,
      async (params) => {
        return await geocode(params);
      }
    );
  • Helper function to format geocoding results into markdown with title, address, coordinates, place ID, and Google Maps link.
    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;

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/CaptainCrouton89/maps-mcp'

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