地图API服务

一个基于Google Maps API的MCP服务器,提供地理编码和静态地图生成功能。

  • by muddassir-lateef
  • Python
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 muddassir-lateef/googleMapsMCPServer

使用说明

谷歌地图MCP服务器

该项目提供了一个模型上下文协议(MCP)服务器,该服务器公开了与谷歌地图API交互的工具。它是用 FastMCP 并且可以用作创建自己的MCP服务器的示例。

特性

服务器公开了以下MCP功能:

  • 工具:

    • geocode:对地址字符串进行地理编码,并返回纬度和经度。
    • get_map:返回给定纬度和经度的静态地图图像的URL。
  • 鼓励:

    • get_map_for_address:一个模板,生成一个用户友好的提示,要求提供特定地址的地图。

项目结构

.
├── src
│   └── googlemaps_mcp_server
│       └── main.py         # The MCP server implementation
├── tests
│   └── __init__.py
├── .gitignore
├── poetry.lock
├── pyproject.toml
├── README.md
└── test_client.py          # An example client to test the server

设置和安装

先决条件

  • Python 3.10+
  • 诗歌 用于依赖性管理。

安装

Use Poetry to install the project dependencies from the `poetry.lock` file.
```bash
poetry install
```

配置

此服务器需要Google Maps API密钥才能运行。

  1. 创建一个 .env 文件: 创建一个名为的文件 .env 在项目的根。

    touch .env
    
  2. 添加API密钥: 打开 .env 文件,并按以下格式添加您的Google Maps API密钥:

    GOOGLE_MAPS_API_KEY="YOUR_GOOGLE_MAPS_API_KEY"
    

    替换 "YOUR_GOOGLE_MAPS_API_KEY" 用你真正的钥匙。服务器从环境中加载此密钥。

运行服务器

要启动MCP服务器,请运行 main.py 脚本:

poetry run python src/googlemaps_mcp_server/main.py

服务器将启动并监听 http://0.0.0.0:8000/mcp.

使用客户端

示例客户端在 test_client.py 演示如何连接到服务器并使用其工具。

要运行客户端,请执行以下操作:

poetry run python test_client.py

客户将:

  1. 打电话给 geocode 工具。
  2. 使用结果调用 get_map 该工具支持多种缩放级别。
  3. 列出并使用 get_map_for_address 提示。

输出示例

Geocoding 'University of Passau, Germany'...
Geocode Result: {
  "latitude": 48.5677779,
  "longitude": 13.4527974
}
Getting map for lat=48.5677779, lon=13.4527974 at zoom=18...
Map Result (zoom=18): { "map_url": "https://maps.googleapis.com/maps/api/staticmap?center=48.5677779%2C13.4527974&zoom=18&size=1024x1024&maptype=hybrid&scale=2&key=..." }
...

--- Testing Prompts ---
Available Prompts: ['get_map_for_address']
Generated Prompt: You are a maps assistant that is supposed to first use geocode tool to get geo coordinates of the address. Then use get_map tool to get a url of the map snippet of the address.By default show map of hybrid type.Now Show me a map of University of Passau, Germany.
--- Prompt Test Complete ---

端点

  • /mcp/geocode?address=<address>:对地址字符串进行地理编码,并返回纬度和经度。
  • /mcp/get_map?lat=<latitude>&lon=<longitude>&maptype=<maptype>:返回静态地图图像的URL。 maptype 可以是以下之一 roadmap, satellite, hybrid, terrain.