mi-health-mcp
Allows logging into a Xiaomi account and querying personal Mi Fitness health data, including daily steps, calories, heart rate, sleep, stress, SpO2, and authentication status, through Xiaomi's APIs.
This server does not currently have a deployable release on Glama. Follow the repository's setup instructions to install and configure it for your MCP client, including any required credentials and local dependencies.
mi-health-mcp
项目简介
基于 wusaki0723/mi-health-mcp 改造的个人小米运动健康(Mi Fitness,中国大陆区)MCP 服务端,运行在 Cloudflare Workers + Cloudflare KV 上,零运行时依赖,免费额度即可长期运行。
相比原版(仅支持亲友接口 /app/v1/relatives/...),本仓库做了以下关键改造:
直接查询个人数据(不再依赖亲友列表):切换为小米运动健康个人聚合日报接口
POST /app/v1/data/get_aggregated_fitness_data_by_time(tag: "daily_report"),与小米手环及小米运动健康 App 首页展示的步数、活动卡路里、心率、压力、有效站立、中高强度活动、睡眠和血氧口径完全一致。passToken自动续期与 64 位nonce精度修复:当短期serviceToken过期或返回 401/403 时,Worker 自动使用 KV 中保存的pass_token调用serviceLogin静默续期并重试;修复了 JavaScriptJSON.parse解析小米 64 位整数nonce精度丢失导致clientSign验签失败的问题。本机同区登录助手:提供
scripts/local_qr_login.py,在本机同 IP 挂起longPolling长连接等待确认后直接写入线上 Cloudflare KV,避开边缘节点跨国风控与无长连接监听导致的“二维码已过期”。本地时区自适应与分页早停:从心率
latest_hr.time - latest_hr.dbTime及睡眠段timezone自动推断设备本地时区偏移;日报查询使用reverse: true配合cutoffDate早停,避免全量翻页触发 Worker 超时。
Related MCP server: mifit
致谢与上游项目
原版 MCP Worker 架构与加解密实现:wusaki0723/mi-health-mcp
认证与协议逆向参考:Misty02600/mi-fitness-python
个人聚合日报接口
get_aggregated_fitness_data_by_time参考:Aaron-gx/mihealth-api
暴露的 MCP 工具
health_auth_status:查看当前凭证是否存在、状态、用户 ID 与更新时间(不返回凭证明文)。health_latest:查询最近 48 小时的官方日报快照(today_steps、today_calories、today_heart_rate、today_stress、today_valid_stand、today_intensity、latest_sleep、latest_spo2及last_cloud_update_time)。health_steps:查询近days(1–30,默认 7)每日步数、距离、步行热量与总活动卡路里日报序列。health_heart:查询近days(1–30,默认 7)每日心率日报序列(最低、最高、平均、静息、最新心率及心率区间时长)。health_sleep:查询近days(1–30,默认 7)每日睡眠日报序列(总时长、评分、深浅睡、REM、清醒时长与分段明细)。health_login_start/health_login_poll:发起与轮询小米账号登录。
部署步骤
前置条件:Node.js 20+ 与 Cloudflare 账号。
git clone https://github.com/Tyleraltight/mi-health-mcp.git
cd mi-health-mcp
npm install
npx wrangler login
npx wrangler kv namespace create MI_HEALTH_KV将输出的 KV Namespace ID 填入 wrangler.toml(或复制到已忽略的 wrangler.local.toml 中),然后设置鉴权密钥并部署:
npx wrangler secret put AUTH_TOKEN
npx wrangler deploy
# 若使用 wrangler.local.toml:
npm run deploy:localAUTH_TOKEN 请使用高强度随机字符串,切勿写入源码或提交到 Git。
登录授权(推荐本机脚本)
由于小米账号登录对长轮询与登录地 IP 敏感,推荐在本地运行辅助脚本(需 uv 或 Python + httpx):
uv run --with httpx python scripts/local_qr_login.py脚本会打印一次性登录 URL 并在后台持续监听 lp 长轮询结果。直接在本地浏览器打开该 URL 并确认登录后,脚本会自动提取 serviceToken、ssecurity 与 passToken 并写入远程 MI_HEALTH_KV。
客户端接入
适用于所有支持 Streamable HTTP(HTTP POST)的客户端(如 Antigravity、Magpie MCP Router、Meta Muse、RikkaHub、Cursor 等):
Endpoint URL:
https://<你的Worker域名>/mcpHTTP Header:
Authorization: Bearer <你的 AUTH_TOKEN>
在 Antigravity(或 Magpie Hub)中配置示例:
"mi-health": {
"serverUrl": "https://<你的Worker域名>/mcp",
"headers": {
"Authorization": "Bearer <你的 AUTH_TOKEN>"
}
}对于 Meta Muse 等服务器端直连远程 MCP 的客户端,无需为 Worker 额外配置 CORS,直接提供 /mcp 地址并通过安全表单传入 Bearer Token 即可复用同一份 KV 凭证。
实测踩坑与经验记录
为什么不用
get_fitness_data_by_time自己算步数和卡路里小米云端原始明细接口会同时存在手环硬件
sid(纯数字)和手机计步sid(hlth.gen_*),自己累加容易翻倍;且steps记录里的calories仅含步行消耗,远低于 App 首页展示的活动总卡路里(calories键)。小米运动健康 App 首页和手环同步使用的是服务器端融合日报接口
POST /app/v1/data/get_aggregated_fitness_data_by_time(tag: "daily_report")。改用该接口后,实测步数与卡路里均与手环和 App 100% 对齐。注意
daily_report每条记录的time是对应本地日历日期的 UTC 零点时间戳(time % 86400 == 0),直接格式化为YYYY-MM-DD即可,不要再重复叠加时区偏移;而latest_hr.dbTime才是真实 UTC 秒级时间戳(latest_hr.time - latest_hr.dbTime恰好等于设备本地时区偏移)。
为什么小米运动健康 App 扫码提示
The QR isn't supported小米运动健康 App 内的扫一扫仅用于绑定手表、手环等硬件设备,不支持扫描账号登录二维码。请使用浏览器直接打开
loginUrl,或使用米家 App / 小米手机「设置 > 小米账号 > 扫一扫」。手机系统相机扫码常会后台预加载一次链接,导致一次性登录态被提前消费;电脑浏览器直接访问最稳妥。
为什么从云端 Worker 发起登录常提示“异地美国”或“二维码已过期”
Cloudflare Workers 出口节点位于海外,容易触发跨国异地登录提示;且小米
longPolling机制要求后台持续挂起lp长连接,若未持续监听即点击确认,页面会报错过期。使用本仓库
scripts/local_qr_login.py在本地同 IP 挂起长连接并直写 KV 可一次解决;同时注意不要使用httpx默认 cookie jar 跨域名自动累积同名cUserId,需手动合并 Cookie。
为什么自动续期在 JS 里会报
x-xiaomi-status-code: 400https://account.xiaomi.com/pass/serviceLogin?_json=true&sid=miothealth返回的nonce是 64 位大整数,超出 JavaScriptNumber.MAX_SAFE_INTEGER(2^53 - 1)。直接JSON.parse会丢失末几位精度,导致SHA1("nonce=" + nonce + "&" + ssecurity)算出的clientSign校验失败。解析响应前需用正则将"nonce":(-?\d+)转为字符串后再JSON.parse。
为什么
daily_report分页会拖慢甚至超时get_aggregated_fitness_data_by_time默认带reverse: true会按倒序翻完所有历史分页。必须传入cutoffDate在覆盖目标天数后立刻停止翻页,单次查询即可在数秒内返回。
开发与测试
npm test使用边界
仅供登录你自己的小米账号并查询本人已授权的健康数据,请勿用于侵犯他人隐私或违反小米用户协议的用途。
许可证
本项目遵循 GNU General Public License v3.0(GPL-3.0)。
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
This server cannot be deployed
Maintenance
Related MCP Connectors
Read-only health data: sleep, HRV recovery, macros, micros, workouts, lift trends, and plans.
Access COROS activity, training and health data, and manage training plans and workouts.
Read wearables and lab health data — sleep, activity, workouts, timeseries, lab tests and orders.
Read and log your supplement intake, meals, and lab measurements from Micros.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceEnables LLM clients to query Xiaomi Mi Fitness family member sleep, heart rate, and step data via MCP, with QR-code login authentication.18GPL 3.0
- AlicenseNot gradedqualityBmaintenanceLets AI assistants read your own Mi Fitness (Xiaomi cloud) health data — steps, heart rate, sleep, SpO2, stress, weight and workouts — through local MCP tools with QR-code login. Runs locally over stdio, returns results as JSON, and withholds GPS tracks unless explicitly allowed.613 PyPI17MIT
- AlicenseCqualityCmaintenanceEnables AI clients to query and analyze your own Xiaomi/Mi Fitness health data — daily activity, minute-level heart rate, sleep stages, workouts, body composition, SpO₂, stress, and abnormal heart beat events — after syncing it from the Xiaomi cloud into a local SQLite database. Exposes 14 tools covering connection status, incremental sync control, and metric/date-range queries, with everything stored and served locally.14MIT
- AlicenseCqualityBmaintenanceEnables syncing Xiaomi Mi Fitness cloud health data into a local SQLite cache and querying it through MCP tools for daily activity, heart rate, sleep, workouts, body measurements, blood oxygen, stress, and abnormal heartbeat records.142MIT