Skip to main content
Glama

mapbox_directions_by_places

Calculate navigation routes between multiple locations using place names. Supports driving, walking, cycling, and traffic-aware routing modes for trip planning.

Instructions

Get navigation route between multiple places using their names

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
placesYesArray of place names to route between
profileNoNavigation modedriving
languageNoLanguage for geocoding results

Implementation Reference

  • The main handler function that implements the mapbox_directions_by_places tool logic. It geocodes place names to coordinates, validates results, and calls the directions API to calculate routes between places.
    export async function handleDirectionsByPlaces(
      args: z.infer<typeof DirectionsByPlacesArgsSchema>
    ) {
      const { places, profile, language } = args;
      const errors: DirectionsByPlacesError[] = [];
      const coordinates: { longitude: number; latitude: number }[] = [];
      const geocodingResults: Record<string, any> = {};
    
      // 1. Geocode each place
      for (const place of places) {
        try {
          const geocodingResult = await handleGeocoding({
            searchText: place,
            limit: 1,
            language,
            fuzzyMatch: true,
          });
    
          if (geocodingResult.isError || !geocodingResult.content[0]) {
            errors.push({
              place,
              error: `Geocoding failed: ${
                JSON.parse(geocodingResult.content[0].text).message ||
                "No results found"
              }`,
            });
            geocodingResults[place] = null;
            continue;
          }
    
          const feature = JSON.parse(geocodingResult.content[0].text).results[0];
          geocodingResults[place] = feature;
          coordinates.push(feature.coordinates);
        } catch (error) {
          errors.push({
            place,
            error: `Geocoding failed: ${
              error instanceof Error ? error.message : String(error)
            }`,
          });
          geocodingResults[place] = null;
        }
      }
    
      // 2. If we don't have enough valid coordinates, return error
      if (coordinates.length < 2) {
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify({
                geocodingResults,
                directionsResult: null,
                errors: [
                  ...errors,
                  {
                    place: "general",
                    error: "Not enough valid coordinates to calculate route",
                  },
                ],
              }),
            },
          ],
          isError: true,
        };
      }
    
      // 3. Get directions using the coordinates
      try {
        const directionsResult = await handleDirections(coordinates, profile);
    
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify({
                geocodingResults,
                directionsResult: directionsResult.isError
                  ? null
                  : JSON.parse(directionsResult.content[0].text),
                errors: errors.length > 0 ? errors : undefined,
              }),
            },
          ],
          isError: directionsResult.isError,
        };
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify({
                geocodingResults,
                directionsResult: null,
                errors: [
                  ...errors,
                  {
                    place: "general",
                    error: `Directions calculation failed: ${
                      error instanceof Error ? error.message : String(error)
                    }`,
                  },
                ],
              }),
            },
          ],
          isError: true,
        };
      }
    }
  • Zod schema for validating the mapbox_directions_by_places tool arguments, including places array, profile enum, and optional language parameter.
    export const DirectionsByPlacesArgsSchema = z.object({
      places: z
        .array(z.string())
        .min(2)
        .describe("Array of place names to route between"),
      profile: z
        .enum(["driving-traffic", "driving", "walking", "cycling"])
        .default("driving"),
      language: z
        .string()
        .regex(/^[a-z]{2}$/)
        .optional()
        .describe("Language for geocoding results"),
    });
  • Registration and routing handler that processes the mapbox_directions_by_places tool call, validates arguments using the schema, and delegates to the handler function.
    case "mapbox_directions_by_places": {
      const validatedArgs = DirectionsByPlacesArgsSchema.parse(args);
      return await handleDirectionsByPlaces(validatedArgs);
    }
  • MCP tool definition that defines the input schema and description for the mapbox_directions_by_places tool exposed to clients.
    export const DIRECTIONS_BY_PLACES_TOOL: Tool = {
      name: "mapbox_directions_by_places",
      description: "Get navigation route between multiple places using their names",
      inputSchema: {
        type: "object",
        properties: {
          places: {
            type: "array",
            items: {
              type: "string",
            },
            minItems: 2,
            description: "Array of place names to route between",
          },
          profile: {
            type: "string",
            description: "Navigation mode",
            enum: ["driving-traffic", "driving", "walking", "cycling"],
            default: "driving",
          },
          language: {
            type: "string",
            description: "Language for geocoding results",
            pattern: "^[a-z]{2}$",
          },
        },
        required: ["places"],
      },
    };
  • Type definitions for the direction-by-places tool including DirectionsByPlacesError interface and DirectionsByPlacesResponse interface.
    export interface DirectionsByPlacesError {
      place: string;
      error: string;
    }
    
    export interface DirectionsByPlacesResponse {
      geocodingResults: {
        [place: string]: MapboxGeocodingResponse["features"][0] | null;
      };
      directionsResult: MapboxDirectionsResponse | null;
      errors?: DirectionsByPlacesError[];
    }

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/AidenYangX/mapbox-mcp-server'

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