位置历史MCP服务器
一个全面的模型上下文协议(MCP)服务器,用于个人位置跟踪,具有智能位置识别、自动聚类和谷歌位置集成功能。
特性
- 实时GPS跟踪:以高精度存储iOS应用程序中的位置点
- 智能位置检测:自动聚类以确定您在哪里花费时间
- 地点识别:用于自动命名的Google Places API集成
- 手动标签:说出你经常去的地方(家、工作、健身房等)
- 访问跟踪:自动检测到达/离开时间
- 旅行统计:行驶距离、平均速度、移动时间
- 13 MCP工具:全面的AI可访问位置查询
- PostGIS支持:高级地理空间查询和半径搜索
建筑
iOS App → location-history-mcp-server → PostgreSQL (PostGIS)
↓
ChatGPT / Claude
(13 MCP tools)可用的MCP工具
位置查询
get_current_location-您最近的GPS位置get_location_history-日期范围的GPS点get_location_at_time-\[具体时间\]你在哪里?search_locations_near-查找坐标/地址附近的访问get_travel_stats-距离、速度、行进时间
场所管理
get_frequent_places-访问次数最多的地方(按访问次数排序)list_all_places-所有已标识的位置(标记和未标记)label_place-说出一个地方(例如,“家”、“Equinox健身房”)get_unlabeled_frequent_places-建议标签位置
地点查询
get_place_visits-所有对特定地点的访问get_time_at_place-在一个地方花费的总时间enrich_place_with_google-从Google Places API获取企业名称process_recent_locations-触发位置检测/聚类
先决条件
- PostgreSQL数据库与PostGIS
- 云SQL实例或本地PostgreSQL - 启用PostGIS扩展 - 建议使用与健康数据存储相同的数据库
- Google Places API密钥 (可选,但推荐)
- 在Google云控制台中启用Places API - 用于自动解析地名 - 成本:每次API调用约0.005美元
- iOS位置跟踪器应用程序
- 请参阅: location-tracker-ios (配套应用程序)
设置
1.安装依赖项
npm install2.配置环境变量
创建 .env 文件:
# Database (use your health-data-storage Cloud SQL connection)
DATABASE_URL=postgresql://user:pass@/health_data?host=/cloudsql/PROJECT:REGION:INSTANCE
# Google Places API (optional)
GOOGLE_PLACES_API_KEY=your_api_key_here
# User ID (your email)
DEFAULT_USER_ID=lucashanson132@gmail.com3.初始化数据库
服务器在第一次运行时自动创建表:
location_points-带时间戳的GPS坐标places-确定的重要地点place_visits-带到达/离开时间的访问记录
npm run dev4.部署到云端运行
GOOGLE_CLOUD_PROJECT=your-project-id ./deploy.shiOS应用程序配置
更新 location-tracker-ios/LocationTracker/APIClient.swift:
private let baseURL = "https://location-history-mcp-server-xxxxx.run.app"更新 location-tracker-ios/LocationTracker/Config.swift:
static let userId = "lucashanson132@gmail.com"使用ChatGPT
- 转到ChatGPT设置→ GPT连接器
- 添加服务器URL:
https://your-server.run.app/sse - 开始提问:
"Where was I last Tuesday at 3pm?"
"How much did I travel this week?"
"How many times did I visit the gym this month?"
"Show me my most frequent places"
"Label place #5 as 'Home'"使用Claude Desktop
添加到 claude_desktop_config.json:
{
"mcpServers": {
"location-history": {
"url": "https://your-server.run.app/sse"
}
}
}位置检测的工作原理
- 上传:iOS应用程序以10个为一批上传GPS点
- 存储:积分存储在
location_points桌子 - 聚类:后台流程识别“停止”(在50米内停留>5分钟)
- 地点创建:创建新地点或与现有地点匹配
- 访问记录:跟踪到达/离开时间
place_visits - 谷歌丰富 (可选):反向地理编码以获取企业名称
- 手动标签:你说出重要的地方
数据库模式
位置点
- id, user_id, latitude, longitude
- accuracy, altitude, speed, course
- timestamp, device_model, device_os
- place_id (FK to places)
- geom (PostGIS geography point)地点
- id, user_id, name, category
- center_lat, center_lng, radius
- address, google_place_id, google_place_name
- visit_count, created_at, updated_at
- geom (PostGIS geography point)地点_访问
- id, user_id, place_id (FK)
- arrival_time, departure_time, duration_minutesAPI终点
POST /upload-iOS应用上传位置批次GET /health-健康检查GET /sse-MCP SSE传输POST /message-MCP消息处理程序GET /tools-列出可用的MCP工具
查询示例
“昨天下午2点我在哪里?”
Tool: get_location_at_time
Args: { timestamp: "2025-10-26T14:00:00Z" }
Returns: Lat/lng coordinates“我这周在工作上花了多长时间?”
Tool: get_time_at_place
Args: {
place_name: "Work",
start_date: "2025-10-20T00:00:00Z",
end_date: "2025-10-27T00:00:00Z"
}
Returns: Total hours, visit count, average visit duration“我最常去的地方是什么?”
Tool: get_frequent_places
Returns: Sorted list with visit counts, coordinates, names隐私和安全
- 您的数据:所有位置数据都保留在您的Cloud SQL数据库中
- 无第三方存储:MCP服务器不存储数据,只查询数据
- 可选Google API:仅当你明确地丰富了一个地方时才使用
- 用户控制:您决定标记哪些地方并与AI共享
发展
# Local development
npm run dev
# Build TypeScript
npm run build
# Run production
npm start成本考虑
- 云运行:免费套餐涵盖了典型的个人使用
- 云SQL:与健康数据存储共享,无需额外费用
- Google Places API:~0.005美元/通话,仅用于致富
- 总计:个人使用通常为0-3美元/月
故障排除
未显示任何位置
- 检查iOS应用程序是否正在上传:查找“✅ 已成功上传“日志”
- 验证Cloud Run环境变量中的DATABASE_URL
- 运行健康检查:
curl https://your-server.run.app/health
未检测到的位置
- 确保您在一个地点停留5分钟以上
- 手动触发:使用
process_recent_locations工具 - 检查中的群集设置
PlacesAnalyzer构造函数
Google Places无法正常工作
- 验证在云运行中是否设置了GOOGLE_PLACES_API_KEY
- 在Google云控制台中启用Places API
- 检查API配额和帐单
支持
对于问题:
- 检查云运行日志:
gcloud run services logs read location-history-mcp-server - 验证数据库连接
- 检查iOS应用程序日志
许可证
MIT许可证
______________________________________________________________________
