Skip to main content
Glama

get_distance_matrix

Calculate travel distances and times between multiple origins and destinations using Google Maps data for route planning and logistics analysis.

Instructions

Calculate distance and time between multiple origins and destinations

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
originsYesList of origin addresses or coordinates
destinationsYesList of destination addresses or coordinates
modeNoTravel modedriving

Implementation Reference

  • Implements the core logic for calculating distance matrix between multiple origins and destinations using the Google Maps Distance Matrix API.
    async calculateDistanceMatrix( origins: string[], destinations: string[], mode: TravelMode = TravelMode.driving ): Promise<ServiceResponse<DistanceMatrixResult>> { try { const response = await this.client.distancematrix({ params: { key: config.googleMapsApiKey, origins, destinations, mode, language: config.defaultLanguage as Language, }, }); return { success: true, data: { originAddresses: response.data.origin_addresses, destinationAddresses: response.data.destination_addresses, rows: response.data.rows, }, }; } catch (error) { return handleError(error); } }
  • Registers the 'get_distance_matrix' tool with MCP server, providing schema and wrapper handler that delegates to PlacesSearcher.calculateDistanceMatrix.
    server.registerTool( "get_distance_matrix", { title: "Distance Matrix", description: "Calculate distance and time between multiple origins and destinations", inputSchema: DistanceMatrixSchema, }, async (args) => { try { const result = await placesSearcher.calculateDistanceMatrix( args.origins, args.destinations, args.mode as TravelMode | undefined ); return { content: [ { type: "text", text: JSON.stringify(result, null, 2) }, ], isError: !result.success, }; } catch (error) { const errorResponse = handleError(error); return { content: [ { type: "text", text: errorResponse.error || "An unknown error occurred", }, ], isError: true, }; } } );
  • Zod schema defining the input parameters for the get_distance_matrix tool: origins, destinations, and optional mode.
    export const DistanceMatrixSchema = { origins: z.array(z.string()).describe("List of origin addresses or coordinates"), destinations: z.array(z.string()).describe("List of destination addresses or coordinates"), mode: TravelModeSchema.optional().describe("Travel mode") };

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/BACH-AI-Tools/MCP-Google-Maps'

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