distance-matrix
Calculate precise travel distances and times between multiple origins and destinations using specified travel modes (driving, walking, bicycling, transit).
Instructions
Calculate travel distance and time between multiple origins and destinations
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| destinations | Yes | Array of destination locations | |
| mode | No | Travel mode | |
| origins | Yes | Array of origin locations |
Implementation Reference
- src/maps.ts:399-454 (handler)The handler function that implements the core logic of the 'distance-matrix' tool by calling the Google Maps Distance Matrix API, processing the response into a matrix of distances and durations between origins and destinations, and formatting it as Markdown.export async function distanceMatrix( params: z.infer<typeof distanceMatrixSchema>, 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.distancematrix({ params: { origins: params.origins, destinations: params.destinations, mode: (params.mode || "driving") as any, key: apiKey, }, }); const rows = response.data.rows; const results = []; for (let i = 0; i < params.origins.length; i++) { for (let j = 0; j < params.destinations.length; j++) { const element = rows[i].elements[j]; results.push({ origin: params.origins[i], destination: params.destinations[j], distance: element.distance?.text || "N/A", duration: element.duration?.text || "N/A", status: element.status, }); } } return { content: [ { type: "text" as const, text: formatDistanceMatrixToMarkdown(results), }, ], }; } catch (error) { return { content: [ { type: "text" as const, text: `Error calculating distance matrix: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } }
- src/maps.ts:132-139 (schema)Zod schema defining the input parameters for the 'distance-matrix' tool: origins and destinations as arrays of strings, and optional travel mode.export const distanceMatrixSchema = z.object({ origins: z.array(z.string()).describe("Array of origin locations"), destinations: z.array(z.string()).describe("Array of destination locations"), mode: z .enum(["driving", "walking", "bicycling", "transit"]) .optional() .describe("Travel mode"), });
- src/index.ts:100-107 (registration)Registration of the 'distance-matrix' tool on the MCP server, linking the name, description, schema, and handler function.server.tool( "distance-matrix", "Calculate travel distance and time between multiple origins and destinations", distanceMatrixSchema.shape, async (params) => { return await distanceMatrix(params); } );
- src/maps.ts:55-66 (helper)Helper function to format the distance matrix results into a readable Markdown structure for the tool response.function formatDistanceMatrixToMarkdown(results: any[]): string { let markdown = `# Distance Matrix Results\n\n`; results.forEach((result, index) => { markdown += `## ${index + 1}. ${result.origin} → ${result.destination}\n`; markdown += `Distance: ${result.distance} \n`; markdown += `Duration: ${result.duration} \n`; markdown += `Status: ${result.status} \n\n`; }); return markdown; }