喂饭级MCP开发教程,让你从0到1开发调试一个MCP服务。
Configuration
View docs{
"mcpServers": {
"869413421-build_mcp": {
"command": "uv",
"args": [
"run",
"build_mcp"
],
"env": {
"API_KEY": "YOUR_API_KEY"
}
}
}
}You build an MCP (Model Context Protocol) server that provides IP-based location and nearby POI (points of interest) search using the 高德地图 API. This server can be accessed locally or remotely, enabling structured tool calls from LLMs to fetch location data and POI results for end users’ queries. It emphasizes robust configuration, logging, retryable HTTP requests, and clear response wrapping to improve reliability in production deployments.
Use an MCP client to connect to the server and perform two core actions: locate the user’s location from their IP and search for nearby POIs using latitude and longitude. The server exposes two tools: locate_ip and search_nearby. When you start the server with the proper transport, your client can invoke these tools in a structured, predictable way and receive standardized responses.
Prerequisites you need to meet before installing and running the MCP server:
- Python 3.10 or newer.
- UV (Python packaging and environment runner) installed to manage virtual environments and dependencies.
1) Install UV on your system.
# Linux or macOS
curl -LsSf https://astral.sh/uv/install.sh | sh2) Create a project directory and enter it.
uv init build-mcp
cd build-mcp3) Create a virtual environment and activate it.
uv venv
source .venv/Scripts/activate4) Install required MCP dependencies.
uv add mcp[cli] httpx pytestYou organize the project under a src layout with common components and a services module, including a configuration loader, a logger, and the MCP service logic. A sample YAML configuration defines API access, retry behavior, and logging.
# src/build_mcp/config.yaml
api_key: test
base_url: https://restapi.amap.com
proxy: http://127.0.0.1:10809
log_level: INFO
max_retries: 5
retry_delay: 1
backoff_factor: 2
log_dir: /var/log/build_mcpThe server can be started with different transport modes. Use stdio for local process calls and streamable-http for remote, cloud, or remote-enabled deployments.
# Start stdio protocol MCP service
uv run build_mcp
# Start streamable-http protocol MCP service
uv run build_mcp streamable-httpPlace sensitive values such as API keys in environment variables or secure storage in production. The example uses an API_KEY variable to supply the 高德 API key.
export API_KEY=YOUR_API_KEY_HERE
uv run build_mcp- Ensure the API key is valid and has the necessary高德地图 API permissions.
- Check logs in the configured log directory for troubleshooting and enable higher log levels if needed.
- When starting with streamable-http, confirm the transport is accessible at the expected URL and port.
If you plan to integrate with an LLM-driven workflow, you can rely on the two primary tools exposed by the MCP server: locate_ip to resolve a user’s location from IP, and search_nearby to fetch POI data around that location.
Fetches geolocation information based on an IP address, returning province, city, district, latitude and longitude data.
Searches for POIs around a given location (lng,lat) with optional keywords, types, and radius, returning a list of nearby POIs.