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
| Name | Required | Description | Default |
|---|---|---|---|
| origins | Yes | List of origin addresses or coordinates | |
| destinations | Yes | List of destination addresses or coordinates | |
| mode | No | Travel mode | driving |
Implementation Reference
- src/services/places.ts:256-283 (handler)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); } }
- src/mcp/create-server.ts:165-201 (registration)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, }; } } );
- src/schemas/tool-schemas.ts:31-35 (schema)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") };