Mapbox MCP Runtime Integration Examples
Working, compilable examples showing how to integrate Mapbox MCP Server with popular agent frameworks.
Prerequisites
- Mapbox Access Token: Get one at mapbox.com/account/access-tokens
- OpenAI API Key (or other LLM provider)
- HuggingFace Token (for smolagents)
Python Examples
Setup
cd python
pip install -r requirements.txt
# Set environment variables
export MAPBOX_ACCESS_TOKEN="your_token_here"
export OPENAI_API_KEY="your_openai_key"
export HF_TOKEN="your_huggingface_token" # For smolagents1. Pydantic AI Example
Framework: Pydantic AI - Type-safe agents with validation
python pydantic_ai_example.pyFeatures:
- Type-safe tool definitions
- Environment variable support
- Hosted MCP server integration
- Real-world examples (restaurant finder, route planning)
Best for: Production applications requiring type safety and validation
2. CrewAI Example
Framework: CrewAI - Multi-agent orchestration
python crewai_example.pyFeatures:
- Multi-agent crews with specialized roles
- Task dependencies and context passing
- Location Analyst + Route Planner agents
- Real-world examples (restaurant crew, property search)
Best for: Complex workflows requiring multiple specialized agents
Agents included:
- Location Analyst: Finds places, analyzes areas
- Route Planner: Calculates routes and travel times
3. Smolagents Example
Framework: Smolagents - Hugging Face's lightweight agents
python smolagents_example.pyFeatures:
- Method 1: Direct MCP connection (recommended, minimal code)
- Method 2: Custom tools with
@tooldecorator - Lightweight and fast
- Real-world example (property search agent)
Best for: Production deployment with minimal overhead
Note: Smolagents has native MCP support via MCPClient!
TypeScript Examples
Setup
cd typescript
npm install
# Set environment variables
export MAPBOX_ACCESS_TOKEN="your_token_here"
export OPENAI_API_KEY="your_openai_key"1. Mastra Example
Framework: Mastra 1.x - Modern TypeScript agent framework
npm run mastraFeatures:
- Type-safe tool creation with Zod schemas
- Hosted MCP server integration
- Multiple Mapbox tools (directions, POI search, distance, isochrone)
- Real-world examples
Best for: TypeScript applications with strong typing
Tools included:
get-directions: Driving directions with trafficsearch-poi: Find restaurants, hotels, etc.calculate-distance: Offline distance calculationget-isochrone: Reachable area analysis
Verify it compiles:
npm run build # TypeScript type-check2. LangChain Example
Framework: LangChain - Conversational AI framework
npm run langchainFeatures:
- Conversational interface
- Tool chaining
- Memory and context management
- Multi-step workflows
Best for: Conversational applications with complex tool chains
Tools included:
- Directions, POI search, distance calculation, isochrones
- All tools use hosted Mapbox MCP server
Framework Comparison
| Framework | Language | Best For | Complexity | Type Safety |
|---|---|---|---|---|
| Pydantic AI | Python | Production apps | Medium | ⭐⭐⭐ |
| CrewAI | Python | Multi-agent systems | High | ⭐⭐ |
| Smolagents | Python | Lightweight agents | Low | ⭐⭐ |
| Mastra | TypeScript | Typed agents | Medium | ⭐⭐⭐ |
| LangChain | TypeScript | Conversational AI | High | ⭐⭐ |
Common Use Cases
1. Restaurant Finder
Find restaurants near a location with distances:
- Pydantic AI:
pydantic_ai_example.py(Example 1) - CrewAI:
crewai_example.py(Example 1) - LangChain:
langchain-example.ts(Example 3)
2. Route Planning
Calculate driving time with traffic:
- Pydantic AI:
pydantic_ai_example.py(Example 2) - Mastra:
mastra-example.ts(Example 2)
3. Property Search
Find properties with good commute:
- CrewAI:
crewai_example.py(Example 2) - Smolagents:
smolagents_example.py(Example 3)
Mapbox MCP Tools Available
All examples connect to the hosted Mapbox MCP Server at https://mcp.mapbox.com/mcp.
API Tools (require Mapbox token):
directions_tool: Driving directions with trafficcategory_search_tool: Find POIs by categorysearch_and_geocode_tool: Search for specific places or addressesreverse_geocode_tool: Coordinates to addressisochrone_tool: Reachable area within timematrix_tool: Travel time matrixstatic_map_image_tool: Static map imagesmap_matching_tool: Match GPS traces to roadsoptimization_tool: Optimize multi-stop routes
Offline Tools (free, instant):
distance_tool: Distance between pointsbearing_tool: Compass directionmidpoint_tool: Midpoint between pointspoint_in_polygon_tool: Point containment testarea_tool: Polygon areacentroid_tool: Polygon centerbuffer_tool: Create buffer zonesbbox_tool: Calculate bounding boxessimplify_tool: Simplify geometries
Utility Tools:
version_tool: Get MCP server versioncategory_list_tool: List available POI categories
Testing Examples
Python
# Run all Python examples
cd python
python pydantic_ai_example.py
python crewai_example.py
python smolagents_example.pyTypeScript
# Type-check all TypeScript examples
cd typescript
npm run build
# Run individual examples
npm run mastra
npm run langchainTroubleshooting
Missing MAPBOX_ACCESS_TOKEN
Error: MAPBOX_ACCESS_TOKEN is requiredSolution: Export the environment variable
export MAPBOX_ACCESS_TOKEN="pk.ey..."MCP Connection Failed
Error: MCP request failed: UnauthorizedSolution: Check your token has proper scopes at mapbox.com/account/access-tokens
Import Errors (Python)
ModuleNotFoundError: No module named 'crewai'Solution: Install requirements
pip install -r requirements.txtTypeScript Compilation Errors
Cannot find module '@mastra/core'Solution: Install dependencies
npm installResources
- Mapbox MCP Server
- Mapbox MCP DevKit
- Model Context Protocol
- Pydantic AI Docs
- CrewAI Docs
- Smolagents Docs
- Mastra Docs
- LangChain Docs
Contributing
Found an issue or want to add more examples? Please open a PR!
License
These examples are provided as-is for educational purposes.