Streamlining Mapbox Development with the Model Context Protocol DevKit
Overview of Modern Location Development
Building mapping applications traditionally requires a constant context-switch between the IDE, documentation, and the
By implementing the

Prerequisites and Technical Foundation
To effectively use the
Crucially, you must be comfortable using command-line interface (CLI) tools. The server operates best when paired with an MCP-compatible client. While
Key Libraries and Architecture
The
- mcpdk: This is the officialAnthropicSDK for building MCP servers. It handles the low-level protocol communication and tool registration, allowing developers to focus on tool logic rather than connection management.
- TypeScript: The entire codebase usesTypeScriptto ensure type safety. This reduces runtime errors when the LLM attempts to pass arguments to various tools.
- Zod: The server utilizesZodschemas for runtime validation. These schemas serve a dual purpose: they validate the data coming from the LLM and provide the metadata (descriptions) that the LLM uses to understand how to call the tool.
Code Walkthrough: Token Creation Logic
Understanding how a tool is structured is key to mastering the DevKit. Let's look at the implementation of the token creation tool, which inherits from a base
Defining the Schema
Every tool starts with a schema. This schema defines the parameters the LLM can manipulate. For a token, we need notes, scopes, and potentially an expiration time.
import { z } from 'zod';
const CreateTokenSchema = z.object({
note: z.string().describe("A description of the token's purpose"),
scopes: z.array(z.string()).describe("The Mapbox scopes to grant the token"),
allowedUrls: z.array(z.string()).optional().describe("Restrict token to specific URLs"),
expires: z.string().optional().describe("ISO 8601 timestamp for token expiration")
});
The .describe() methods are the most critical part here. They act as the "documentation" for the AI. When the agent reads the tool's manifest, it sees these descriptions and uses them to decide which user input should map to which JSON field.
Implementing the Tool Logic
The implementation class handles the actual HTTP request to the
export class CreateTokenTool extends MapboxApiBaseTool {
name = "create_token";
description = "Creates a new Mapbox public access token with specified scopes.";
async execute(input: z.infer<typeof CreateTokenSchema>) {
const username = this.getUsernameFromToken();
const url = `https://api.mapbox.com/tokens/v2/${username}`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.accessToken}`
},
body: JSON.stringify(input)
});
if (!response.ok) throw new Error("Failed to create token");
return await response.json();
}
}
This pattern separates the interface (the schema) from the implementation (the API call). The execute function only runs if the input matches the
Syntax Notes and Conventions
When working within the MCP ecosystem, certain conventions help maintain compatibility. The create_style, list_tokens), which is the standard expected by most MCP clients.
A notable pattern in the DevKit is the use of "Style Helpers." Instead of forcing the LLM to guess the entire
Practical Examples: The "Halloween Night" Workflow
Imagine you are building a holiday-themed landing page. Instead of manually picking hex codes for a dark map, you can prompt your coding agent: "Create a Halloween-themed style and apply it to my local index.html."
- Style Generation: The agent calls the
style_helpertool. It identifies that "Halloween" implies dark backgrounds, orange labels, and purple accents. It sends these preferences to theMapboxStyles API. - Visualization: The agent then calls
preview_style, which returns a URL. The agent can even open your browser automatically so you can inspect the "vibe." - Local Integration: Once the style is created, the agent searches your local directory for an HTMLfile. It finds the
mapboxgl.Mapinitialization and updates thestyleproperty with the new Style URL it just generated. - Refinement: If the map is too dark, you simply say, "Make the labels more readable." The agent updates the existing style in-place. This iterative loop happens in seconds rather than minutes.
Tips and Gotchas
Security is paramount when working with AI and API keys. The
Another common mistake is providing overly large