How to Integrate Baidu Map MCP Server into Java AI Apps with NPX
This guide explains how to quickly integrate Baidu Map's MCP protocol into Java AI applications using NPX, covering API key setup, configuration, core map APIs, and sample Java code for seamless location and routing services.
Background
MCP (Model Completion Protocol) has become the industry‑standard communication protocol for AI, enabling large language models (LLMs) to call external tools and services through a unified interface. Baidu Map is the first domestic map service provider to fully support MCP, allowing developers to invoke map capabilities in AI applications without complex low‑level development.
Advantages of Baidu Map MCP
One‑click integration : Simple configuration enables map capabilities in AI apps.
Comprehensive features : Provides eight core map APIs covering most geospatial needs.
Lightweight deployment : Supports NPX rapid deployment without heavy development.
Feature List
Baidu Map MCP Server currently offers the following core APIs:
Function
API Interface
Use Case
Geocoding
geocoder_v2Convert address text to latitude/longitude coordinates
Reverse Geocoding
reverse_geocoding_v3Convert coordinates to structured address
Place Search
place_v2_searchSearch POI information around or within a specific area
Place Detail
place_v2_detailRetrieve detailed information of a specific POI
Batch Routing
routematrix_v2_drivingCalculate driving distance and time for multiple origin‑destination pairs
Route Planning
directionlite_v1Provide driving, walking, cycling, and public‑transport route planning
Weather Query
weather_v1Get real‑time weather and forecasts for a city
IP Location
location_ipObtain approximate location based on IP address
Quick Start
Apply for Baidu Map API Key
Before using Baidu Map MCP Server, obtain an API key (AK) by visiting the Baidu Map Open Platform, creating a developer account, generating a server‑side AK, and saving it for later configuration.
Integrate via NPX
The simplest method is to deploy the MCP Server with the NPX command‑line tool.
Configuration Steps
Open the MCP‑compatible application settings (e.g., Claude for Desktop, Cherry).
Navigate to the Developer section.
Click the “Edit Config” button to open the configuration file.
Add the following configuration:
<code>{
"mcpServers": {
"baidu-map": {
"command": "npx",
"args": ["-y", "@baidumap/mcp-server-baidu-map"],
"env": {
"BAIDU_MAP_API_KEY": "YOUR_AK"
}
}
}
}</code>After configuration, AI applications can directly call Baidu Map functions such as route planning and place search.
Developer Integration
Java Integration Example
Add the following Maven dependency:
<code><dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-mcp</artifactId>
<version>1.0.0-beta2</version>
</dependency></code>Integration Code
<code>// Initialize AI model
ChatLanguageModel model = OpenAiChatModel.builder()
.apiKey("YOUR_AI_MODEL_API_KEY")
.modelName("qwen-max-latest")
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
.logRequests(true)
.logResponses(true)
.build();
// Configure MCP transport
McpTransport transport = new StdioMcpTransport.Builder()
.command(List.of("npx", "-y", "@baidumap/mcp-server-baidu-map"))
.environment(Map.of("BAIDU_MAP_API_KEY", "YOUR_BAIDU_MAP_AK"))
.build();
// Initialize MCP client
McpClient mcpClient = new DefaultMcpClient.Builder()
.transport(transport)
.logHandler(new DefaultMcpLogMessageHandler())
.build();
// Set up tool provider
ToolProvider toolProvider = McpToolProvider.builder()
.mcpClients(List.of(mcpClient))
.build();
// Create AI service interface
Bot bot = AiServices.builder(Bot.class)
.chatLanguageModel(model)
.toolProvider(toolProvider)
.build();
// Use the service
try {
Result<String> chat = bot.chat("How do I get from Daxing Airport to the south gate of BUPT? Approximate travel time?");
System.out.println(chat.getValue());
} finally {
mcpClient.close(); // Ensure resources are released
}</code>Real‑World Example
Demo in PIG AI Platform
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.