Mapbox MCP Python Tutorial: Build a Location-Aware AI Agent
Learn how to connect Mapbox MCP to a Python smolagents project and build an AI agent that can search places, calculate routes, and reason with live location data.
On this page
A normal AI agent can explain where a restaurant is, but it cannot reliably calculate the route, check nearby places, or reason about travel time unless those capabilities are connected to live location data. Mapbox now provides an MCP server that exposes geocoding, place search, directions, isochrones, maps, and other geospatial tools to AI applications. This tutorial shows how to connect that server to a Python agent with smolagents and turn a language model into a location-aware assistant.
What the Mapbox MCP server adds to a Python agent
Model Context Protocol (MCP) is a standard for connecting AI models and agents to external tools. Instead of writing separate integration code for every Mapbox API, the MCP server exposes location capabilities as tools that an MCP-compatible agent can call when needed. The current Mapbox MCP server supports tasks including place search, geocoding, reverse geocoding, directions, travel-time analysis, isochrones, static maps, and offline geographic calculations. That makes it useful for agents that need to answer questions about the physical world rather than only generate text.
This is a different problem from adding web search to an agent. Web search can find pages that mention a location, while a dedicated location tool can return structured geographic information that software can use directly. It can also complement an AI agent by giving the agent reliable tools for routing and spatial reasoning instead of asking the language model to guess coordinates or travel times.
What you need before starting
The setup has four main requirements: Python, an MCP-compatible agent framework, Node.js, and a Mapbox access token. Mapbox's own Smolagents example uses the smolagents and mcp packages, while Node.js is used to run the Mapbox MCP server. A Mapbox access token is required because the server makes authenticated requests to Mapbox services.
- Python installed on your development machine
- Node.js LTS or later
- A Mapbox account and access token
- A supported language model configured for smolagents
- The
smolagentsMCP support package
Keep the Mapbox token in an environment variable rather than putting it directly into source code. That makes the example safer to reuse and prevents the credential from being accidentally committed to a repository.
Install the Python and MCP dependencies
Create a new project and install smolagents with its MCP support. Mapbox's example specifically documents the MCP extra as one supported installation method.
pip install "smolagents[mcp]"The installation gives the Python agent the pieces it needs to communicate with MCP tools. You do not need to clone the Mapbox server repository when using its npm package, because the server can be launched through npx.
Store your Mapbox token outside the code
Set the token as an environment variable before starting the agent. On macOS or Linux, the basic form is:
export MAPBOX_ACCESS_TOKEN="your_token_here"On Windows PowerShell, use:
$env:MAPBOX_ACCESS_TOKEN="your_token_here"The important part is the variable name: MAPBOX_ACCESS_TOKEN. The Mapbox MCP server reads this variable when it starts and uses the token to authenticate requests to Mapbox services.
Do not replace the placeholder with a token and then commit the file to Git. Environment variables keep credentials separate from the application code, but you should still protect the machine or deployment environment where the variable is stored.
Connect smolagents to Mapbox MCP
The key part of the integration is an MCP server configuration. Mapbox's official Python example starts the npm package with npx and passes the environment variable containing the access token. The agent then sees Mapbox capabilities as callable tools instead of needing individual API wrappers in the Python application.
import os
from mcp import StdioServerParameters
from smolagents import CodeAgent, InferenceClientModel, MCPClient
server_parameters = StdioServerParameters(
command="npx",
args=["-y", "@mapbox/mcp-server"],
env={
"MAPBOX_ACCESS_TOKEN": os.environ["MAPBOX_ACCESS_TOKEN"]
}
)
with MCPClient(server_parameters) as tools:
model = InferenceClientModel()
agent = CodeAgent(
tools=tools,
model=model
)
```
result = agent.run(
"How long does it take to drive from Big Ben to the Eiffel Tower?"
)
print(result)The exact model configuration can vary because smolagents supports different model providers. The important integration point is the MCP client: it starts the Mapbox server, discovers its tools, and makes those tools available to the agent. Mapbox's own example uses the same basic pattern and notes that structured output should be enabled when connecting MCP tools so the agent can handle structured results such as directions and geocoding data correctly.
Make the agent solve a real location problem
A useful test should require more than a text answer. For example, ask the agent to find a coffee shop near a destination and then calculate how long it takes to reach it. The agent can first search or geocode the location, use place-search capabilities to find relevant points of interest, and then call routing tools when it has the required coordinates. Mapbox documents these tools as separate capabilities that can be composed for location-aware workflows.
result = agent.run(
"""
Find coffee shops near the Eiffel Tower.
Choose one suitable result and calculate the walking
distance and estimated walking time from the Eiffel Tower.
Explain which location you used.
"""
```
)
print(result)This illustrates the important architectural change: the language model does not need to know the answer beforehand. It interprets the request, decides which external capability is needed, receives structured information from Mapbox, and then turns that information into a response.
Use routing, reachability, and place data together
Once the basic connection works, the same pattern can support more useful workflows. The Mapbox MCP server includes directions for driving, walking, and cycling, while its isochrone tool can calculate areas reachable within selected travel-time or distance limits. Its place tools can search for points of interest and retrieve additional information about a selected place.
For example, a travel assistant could take a hotel location, find restaurants within a chosen walking range, calculate routes to the candidates, and present the results. A logistics agent could compare travel times between multiple stops. A local-discovery application could combine place search with geographic reachability instead of relying on an AI model to estimate distances from memory.
Worked example: Suppose a user asks, "Find three pharmacies near my destination that can be reached within 15 minutes by car." The agent can identify the destination, search for pharmacies, calculate travel-time reachability, and use routing data to compare the candidates. The useful part is not that the model knows what a pharmacy is; it is that the model can orchestrate location tools against current geographic data.
Why structured location data matters
Language models are good at producing plausible descriptions, but location applications need precise objects such as coordinates, routes, distances, and travel durations. Mapbox's own research on grounding language models in location data highlights this distinction: once an application has to place a pin, draw a route, or determine what is actually reachable, fluent text is not enough.
That distinction also explains why an MCP integration can be more useful than simply asking a general-purpose model a geography question. The model handles interpretation and reasoning, while the location service supplies structured geographic evidence. The result is a division of responsibilities that is easier to test: the agent decides what it needs, and Mapbox performs the geographic operation.
Watch the token and tool boundaries
Giving an agent access to external tools also creates a new security boundary. The Mapbox server requires an access token, and its documentation recommends controlling which tools are enabled when you need a narrower configuration. The server supports command-line options for enabling or disabling specific tools, which can reduce the available surface area for an application that only needs a subset of Mapbox functionality.
For production systems, also log which tool the agent selected, validate user-controlled location inputs, and keep credentials outside prompts. Do not assume that because an agent can call a routing tool it should automatically have access to every other geographic operation. A smaller toolset is usually easier to understand, test, and monitor.
How to tell whether the integration is working
A successful first test should produce evidence that the agent actually used location tools rather than simply generating a plausible answer. Try a question that requires coordinates or routing, such as asking for a driving time between two well-known locations. Mapbox's own Smolagents example uses a drive-time question between Big Ben and the Eiffel Tower for this purpose. ([GitHub][3])
If the agent cannot connect, check the Node.js installation, confirm that MAPBOX_ACCESS_TOKEN is available to the process, and verify that the MCP server package can start independently. If the agent connects but produces poor answers, inspect the tool calls and the structured data returned to the model before changing the prompt. That separates an MCP connection problem from a reasoning or application-design problem.
Where this pattern goes next
The useful idea behind this integration is not simply adding maps to a chatbot. It is giving an AI agent access to a specialized source of real-world state that language models cannot reliably reconstruct from training data. Mapbox now positions its MCP server as infrastructure for location-aware AI applications, with tools spanning search, geocoding, routing, reachability, and map rendering.
For a first project, keep the agent narrow: one model, a small set of location tools, and one task that can be verified independently. Once that works, add multi-step workflows such as travel planning, local discovery, delivery routing, or geographic analysis. The important milestone is not making the agent sound intelligent; it is making sure every location-dependent answer can be traced back to the geographic tool that supplied the evidence.
Written by


