Skip to main content
Glama
xiaozhi-kid-assistant MCP Server

xiaozhi-kid-assistant MCP Server

README.md
# xiaozhi-kid-assistant

> 把小智 AI 语音终端变成孩子书桌上的学习陪聊助手——一套自研 MCP 工具集 + 固件定制,给官方云装上"家长可控的脑子"。

ESP32-S3 设备跑小智固件连官方云做语音对话(ASR/LLM/TTS),一台服务器跑自研 MCP 工具集。官方云大脑在对话中按需 function call 调用服务器侧工具:**联网搜索带意图拦截**(游戏攻略/抽卡/暴力色情挡掉)、**数学题 sympy 验算**(堵 LLM 算错还夸)、**收藏夹**、**闲聊/学习双模式**,全程对话日志落 SD 卡并回传服务器,供家长侧加工成孩子画像与学习观察。

## 功能一览

- 🛡️ **搜索意图门**:glm-4-flash 分类 ALLOW/BLOCK/COMPLEX,"为玩服务"的提问拦掉并回温和引导话术,"为学知识"的放行
- 🔍 **双车道搜索**:快车道(双查询搜索 → 精简成 ≤80 字播报体,~8s)、慢车道(Claude Code 多轮综合复杂问题)
- 🧮 **math_solve 验算**:讲题前强制 sympy 精确计算,禁止心算——堵"算错答案还夸答对"的漏洞
- 💾 **收藏夹 v2**:按 key 去重更新、分类、语音友好输出
- 🎚️ **双模式开关**:闲聊模式读记忆主动抛话题、学习模式接住引导;每日首次唤醒问一次模式
- 📜 **对话日志闭环**:固件 hook STT/TTS 文字落 SD 卡 JSONL → 整文件幂等上传服务器 → 入库清洗(工具调用/闹钟事件标注、重复播报标记)
- ⏰ **固件定制**:常显时钟、语音唤醒运行时开关(省电)、本地软闹钟、UI 定制

## 架构

```mermaid
flowchart LR
    FW[ESP32-S3 终端<br/>小智固件 v2.6.4] <-->|WebSocket 音频| 官方云[小智官方云<br/>ASR·LLM·TTS]
    官方云 -.->|function call| BRIDGE[mcp_pipe.py 桥接]
    BRIDGE <-->|stdio MCP| TOOLS[assistant_tools.py<br/>工具集]
    TOOLS -->|web_search / chat| ZHIPU[智谱 BigModel]
    FW -->|对话日志上传| RECV[chatlog_receiver.py] --> LOG[(chatlogs/)]
```

详见 [`docs/architecture.md`](docs/architecture.md)。

## 快速开始

需要:一台公网服务器(≥1G 内存)、小智 AI 账号 + 已激活的 ESP32 设备、智谱 BigModel API Key、Python 3.10+。

```bash
git clone https://github.com/<你>/xiaozhi-kid-assistant.git
cd xiaozhi-kid-assistant
pip install -r requirements.txt
cp .env.example .env   # 填 ZHIPU_API_KEY / MCP_ENDPOINT / X_LOG_TOKEN
```

完整部署(桥接 systemd 单元、日志接收端、固件编译烧录、角色提示词配置)见 [`docs/deployment.md`](docs/deployment.md)。

## 仓库结构

```
├── src/
│   ├── assistant_tools.py    # MCP 工具集(意图门+双车道搜索+math_solve+收藏夹+chat_mode)
│   ├── mcp_pipe.py           # MCP stdio <-> WebSocket 桥接
│   ├── chatlog_receiver.py   # 对话日志接收端(HTTP,原子写+口令+入库清洗)
│   └── search_api.py         # 慢车道搜索脚本(Claude Code 调用)
├── scripts/start_bridge.sh   # 桥接启动(读 .env)
├── firmware/                 # 基于 xiaozhi-esp32 (MIT) 的定制补丁
│   ├── custom.patch          #   源码 diff(UI/常显时钟/唤醒开关/日志插桩)
│   ├── alarm_controller.h    #   本地软闹钟
│   ├── chat_logger.h         #   对话日志速记员(SD 落盘+上传)
│   └── sdkconfig.custom      #   编译配置
├── docs/
│   ├── architecture.md       # 架构与数据流
│   ├── deployment.md         # 部署指南 + systemd 单元
│   └── role-prompt-template.md  # 角色提示词模板(填好粘到小智控制台)
├── mcp_config.json           # MCP 服务器配置示例
├── requirements.txt
└── .env.example              # 环境变量模板(密钥全走这里,勿硬编码)
```

## 关键设计

| 问题 | 解法 |
|------|------|
| 孩子问游戏攻略/抽卡 | 意图门 BLOCK + 温和转移话术,不批评不提"拦截" |
| LLM 算错数学题还夸"答对啦" | 讲题强制 `math_solve`(sympy 精确值),禁止心算 |
| 搜索改写丢专有名词跑偏 | 双查询(原话 + 改写词都搜),原话走 pro、改写词走 std |
| LLM 用记忆编型号/日期 | 精简铁律:名称/型号/数字只准来自搜索资料原文 |
| 想知道孩子都问了什么 | `search_log.jsonl` 记每次 query/decision/lane/engines/answer |
| 对话日志拿不到 | 固件 hook 上屏文字必经路 → SD JSONL → 整文件幂等上传 |

## 致谢

- [xiaozhi-esp32](https://github.com/78/xiaozhi-esp32)(MIT)—— 设备固件基底
- [智谱 BigModel](https://bigmodel.cn) —— LLM 与 web_search API
- [FastMCP](https://github.com/jlowin/fastmcp) —— MCP 工具框架

## 协议

MIT。固件补丁为 xiaozhi-esp32(MIT)衍生作品,已署名上游。详见 [LICENSE](LICENSE)。

## 声明

个人项目,不提供持续维护与支持,按"现状"提供。本项目只包含代码与模板,**不含任何儿童个人信息或运行时数据**——部署时产生的对话日志、收藏夹、画像数据归部署者所有,请自行妥善保管,`.gitignore` 已默认排除。为孩子使用前请家长评估内容拦截策略是否适合自家情况。

---

# English

> Turn the xiaozhi AI voice terminal into a kid's desk learning companion — a self-built MCP toolset + firmware customizations that give the official cloud a "parent-controllable brain."

An ESP32-S3 device runs the xiaozhi firmware connected to the official cloud for voice dialogue (ASR/LLM/TTS), while a server runs the self-built MCP toolset. The cloud brain function-calls server-side tools as needed during conversation: **intent-filtered web search** (blocks game guides / gacha / violent or sexual content), **sympy math verification** (catches LLM math errors it would otherwise praise), **favorites**, **chat / study dual mode**. Dialogue logs are written to SD card and synced to the server for parent-side processing into a child profile and learning observations.

## Features

- 🛡️ **Search intent gate**: glm-4-flash classifies ALLOW/BLOCK/COMPLEX — "for-play" questions blocked with a gentle redirect, "for-knowledge" questions allowed
- 🔍 **Dual-lane search**: fast lane (dual-query search → condensed to ≤80-char spoken style, ~8s), slow lane (Claude Code multi-turn synthesis for complex questions)
- 🧮 **math_solve verification**: forced sympy exact computation before teaching any math — no mental math — plugs the "wrong answer praised as correct" hole
- 💾 **Favorites v2**: key-dedup updates, categorization, voice-friendly output
- 🎚️ **Dual-mode switch**: chat mode reads memory and proactively raises topics; study mode catches and guides; asks mode once per day on first wake
- 📜 **Dialogue logging loop**: firmware hooks STT/TTS text to SD JSONL → idempotent full-file upload to server → ingest cleaning (tool-call/alarm tagging, duplicate-playback marking)
- ⏰ **Firmware customization**: always-on clock, runtime wake-word switch (power saving), local soft alarm, UI customization

## Architecture

```mermaid
flowchart LR
    FW[ESP32-S3 Terminal<br/>xiaozhi firmware v2.6.4] <-->|WebSocket audio| Cloud[xiaozhi Official Cloud<br/>ASR·LLM·TTS]
    Cloud -.->|function call| BRIDGE[mcp_pipe.py bridge]
    BRIDGE <-->|stdio MCP| TOOLS[assistant_tools.py<br/>toolset]
    TOOLS -->|web_search / chat| ZHIPU[Zhipu BigModel]
    FW -->|dialogue log upload| RECV[chatlog_receiver.py] --> LOG[(chatlogs/)]
```

See [`docs/architecture.md`](docs/architecture.md).

## Quick start

Requirements: a public-facing server (≥1GB RAM), a xiaozhi AI account + activated ESP32 device, a Zhipu BigModel API key, Python 3.10+.

```bash
git clone https://github.com/<you>/xiaozhi-kid-assistant.git
cd xiaozhi-kid-assistant
pip install -r requirements.txt
cp .env.example .env   # fill ZHIPU_API_KEY / MCP_ENDPOINT / X_LOG_TOKEN
```

Full deployment (bridge systemd unit, log receiver, firmware build & flash, role prompt config) in [`docs/deployment.md`](docs/deployment.md).

## Repository structure

```
├── src/
│   ├── assistant_tools.py    # MCP toolset (intent gate + dual-lane search + math_solve + favorites + chat_mode)
│   ├── mcp_pipe.py           # MCP stdio <-> WebSocket bridge
│   ├── chatlog_receiver.py   # dialogue log receiver (HTTP, atomic write + token + ingest cleaning)
│   └── search_api.py         # slow-lane search script (called by Claude Code)
├── scripts/start_bridge.sh   # bridge launcher (reads .env)
├── firmware/                 # custom patches based on xiaozhi-esp32 (MIT)
│   ├── custom.patch          #   source diff (UI / always-on clock / wake switch / log hooks)
│   ├── alarm_controller.h    #   local soft alarm
│   ├── chat_logger.h         #   dialogue log stenographer (SD logging + upload)
│   └── sdkconfig.custom      #   build config
├── docs/
│   ├── architecture.md       # architecture & data flow
│   ├── deployment.md         # deployment guide + systemd units
│   └── role-prompt-template.md  # role prompt template (fill in, paste to xiaozhi console)
├── mcp_config.json           # MCP server config example
├── requirements.txt
└── .env.example              # env var template (all secrets here, never hardcode)
```

## Key design

| Problem | Solution |
|---------|----------|
| Child asks for game guides / gacha | Intent gate BLOCK + gentle redirect, no criticism, no mention of "blocking" |
| LLM gets math wrong and praises "correct" | Forced `math_solve` (sympy exact) before teaching |
| Search rewrite drops proper nouns, drifts | Dual query (raw + rewritten both searched); raw → pro, rewritten → std |
| LLM hallucinates models / dates | Condensing rule: names / models / numbers only from search material |
| Want to know what the child asked | `search_log.jsonl` records every query / decision / lane / engines / answer |
| Dialogue logs inaccessible | Firmware hooks on-screen text → SD JSONL → idempotent full-file upload |

## Credits

- [xiaozhi-esp32](https://github.com/78/xiaozhi-esp32) (MIT) — device firmware base
- [Zhipu BigModel](https://bigmodel.cn) — LLM and web_search API
- [FastMCP](https://github.com/jlowin/fastmcp) — MCP tool framework

## License

MIT. Firmware patches are derivative works of xiaozhi-esp32 (MIT), with attribution to upstream. See [LICENSE](LICENSE).

## Disclaimer

Personal project, no ongoing maintenance or support, provided "as is". This project contains only code and templates — **no children's personal data or runtime data**. Dialogue logs, favorites, and profile data generated during deployment belong to the deployer; keep them safe. `.gitignore` excludes them by default. Before using with a child, parents should assess whether the content-filtering strategy fits their situation.