Skip to main content
Glama
by ricleedo

distance-matrix

Calculate travel distances and durations between multiple origin and destination points using various transportation modes including driving, walking, bicycling, and transit.

Instructions

Calculate travel distance and time between multiple origins and destinations

Input Schema

NameRequiredDescriptionDefault
destinationsYesArray of destination locations
modeNoTravel mode
originsYesArray of origin locations

Input Schema (JSON Schema)

{ "properties": { "destinations": { "description": "Array of destination locations", "items": { "type": "string" }, "type": "array" }, "mode": { "description": "Travel mode", "enum": [ "driving", "walking", "bicycling", "transit" ], "type": "string" }, "origins": { "description": "Array of origin locations", "items": { "type": "string" }, "type": "array" } }, "required": [ "origins", "destinations" ], "type": "object" }

Implementation Reference

  • Main handler function for the 'distance-matrix' tool. Calls Google Maps Distance Matrix API, processes results into a list, formats as Markdown using helper, and returns structured content.
    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) }`, }, ], }; } }
  • Zod schema defining input parameters for the distance-matrix tool: origins array, destinations array, optional 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, using the schema and handler from maps.ts.
    server.tool( "distance-matrix", "Calculate travel distance and time between multiple origins and destinations", distanceMatrixSchema.shape, async (params) => { return await distanceMatrix(params); } );
  • Helper function to format distance matrix results into Markdown for the 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; }

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/ricleedo/Google-Service-MCP'

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