Skip to main content
Glama
408OS CLI MCP
by Paxton-PKJ
README.md
# 408os-cli-mcp

严格基于官方 **`408os` CLI(`408os-agent-kit`)** 构建的 **纯只读** MCP Server,同时支持 **本地 `STDIO` 协议**(面向思源笔记 Agent、Claude Desktop、Cursor 等桌面端)与 **远程 `Streamable HTTP + OAuth 2.1 DCR` 协议**(面向 **网页端 Gemini `gemini.google.com`**、Claude Web 等云端 AI 客户端),并提供 **非 Docker 原生部署** 与 **Docker 容器化部署** 两种完整方案。

---

## 核心特性

1. **严格遵守官方 CLI 规范且锁死只读**:
   - 不自行手写任何针对 `408os.cn` 的站内 HTTP 请求;100% 走官方 `408os-agent-kit` 包执行。
   - 编译期白名单过滤全部写入、`*_preview` 与 `*_commit` 工具,并强制注入 `AGENT_KIT_ALLOW_WRITES=0`。
   - 绝不在工具参数中接收任何凭据,日志与返回结果全程自动脱敏(`ag408_*`、`Bearer`、`PROXY_PASSCODE`)。
2. **毫秒级常驻加速架构(零侵入官方包)**:
   - **内置官方守护桥接**:自动拉起(或复用)官方 `408os-agent-kit/src/server.ts` 本地只读守护进程,消除每次子进程 `tsx/esbuild` 冷启动的 ~700 ms 开销,复用到 `www.408os.cn` 的 HTTPS Keep-Alive 连接池与单实例 SQLite `GrantCache`,异常时自动无感回退 `execFile`。
   - **多级缓存与单层并行编排**:启动自动后台预热 151 个三级知识点目录与收藏夹,题目静态元数据落盘缓存(7 天 TTL)并对齐官方 24 小时单题缓存;单题查询与知识点核查压平为单波 `Promise.all` 并行。
3. **一步聚合 + HTML / KaTeX / Draw.io 清洗**:
   - 自动将 `questionId` 关联补全为 `年份 + 题号 + 科目::章::知识点 Anki 三级标签 + 站内刷题链接`。
   - 自动剥离题干与解析中的大体积内嵌 Base64 图片,提取 KaTeX 公式,并按 `(y, x)` 坐标将 Draw.io `<mxGraphModel>` 图形还原为可读文本表格。
4. **原生支持网页端 Gemini(`gemini.google.com`)**:
   - HTTP 模式内置完整 **OAuth 2.1 + RFC 7591 动态客户端注册(DCR)+ PKCE `S256` + RFC 9207 `iss`**,通过独立口令确认页(`PROXY_PASSCODE`)保护个人学习数据。

---

## 获取 `AGENT_408OS_TOKEN`

登录 [408os.cn](https://www.408os.cn) → **个人中心** → **AI 工具**,申请本人专用连接密钥(格式为 `ag408_...`)。

---

## 使用方式一:非 Docker 原生运行(Node.js 直接部署)

### 环境要求

1. **Node.js `>= 22.13.0`**(推荐 Node.js 24 LTS)
2. 全局安装官方 `408os` CLI:
   ```bash
   npm install -g https://408os-note.cn-nb1.rains3.com/downloads/408os-agent-kit-1.0.4.tgz
   408os --version
   ```
3. 克隆并构建本项目:
   ```bash
   git clone https://github.com/Paxton-PKJ/408os-cli-mcp.git
   cd 408os-cli-mcp
   npm install
   npm run verify
   ```
   > 可选:执行 `npm link` 后即可在任意位置直接使用 `408os-cli-mcp`(STDIO)与 `408os-cli-mcp-http`(HTTP)命令。

---

### 场景 1A:本地 `STDIO` 模式(思源笔记 Agent / Claude Desktop / Cursor)

#### 1) 配置环境变量

- **Windows PowerShell**(写入用户级环境变量,即使宿主不传 `env` 也会自动通过注册表回退读取):
  ```powershell
  [Environment]::SetEnvironmentVariable("AGENT_408OS_TOKEN", "ag408_你的密钥", "User")
  ```
- **macOS / Linux**:
  ```bash
  export AGENT_408OS_TOKEN='ag408_你的密钥'
  ```

#### 2) 在 MCP 客户端中添加配置

```json
{
  "mcpServers": {
    "408os": {
      "command": "node",
      "args": ["/path/to/408os-cli-mcp/dist/index.js"],
      "env": {}
    }
  }
}
```

> 若你在 macOS / Linux 下宿主未继承 Shell 环境变量,可在 `env` 中填入 `"AGENT_408OS_TOKEN": "ag408_..."`;若 `408os` 安装在自定义前缀目录,可补充 `"OS408_CLI_BIN": "/path/to/408os-agent-kit/bin/408os"`。

---

### 场景 1B:非 Docker 裸机运行 `Streamable HTTP` 模式(接入 Gemini Web)

适用于在已有 Node.js 环境的 Linux VPS、内网服务器或本机直接运行,无需安装 Docker。

#### 1) 配置 `.env` 并启动

```bash
cp .env.example .env
```

编辑 `.env`:

```ini
AGENT_408OS_TOKEN=ag408_你的密钥
PROXY_PASSCODE=设置一个高强度随机口令
PUBLIC_ORIGIN=https://408os-mcp.yourdomain.com
HOST=127.0.0.1
PORT=8788
```

直接启动:

```bash
npm run start:http
# 或 npm link 后执行:408os-cli-mcp-http
```

#### 2) (可选)Linux `systemd` 常驻守护示例

创建 `/etc/systemd/system/408os-cli-mcp.service`:

```ini
[Unit]
Description=408OS CLI MCP HTTP Server
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/opt/408os-cli-mcp
EnvironmentFile=/opt/408os-cli-mcp/.env
ExecStart=/usr/bin/node /opt/408os-cli-mcp/dist/http.js
Restart=on-failure
RestartSec=3

[Install]
WantedBy=multi-user.target
```

```bash
sudo systemctl daemon-reload
sudo systemctl enable --now 408os-cli-mcp
```

---

## 使用方式二:Docker / Docker Compose 容器化部署

Docker 镜像基于 `node:24-slim`,**已在镜像内自动安装官方 `408os-agent-kit`**,宿主机无需安装 Node.js 或官方 CLI。

### 1. 配置 `.env` 并一键启动

```bash
git clone https://github.com/Paxton-PKJ/408os-cli-mcp.git
cd 408os-cli-mcp
cp .env.example .env
# 编辑 .env 填入 AGENT_408OS_TOKEN、PROXY_PASSCODE 与 PUBLIC_ORIGIN
docker compose up -d --build
```

默认仅在宿主机回环地址 `127.0.0.1:8788` 暴露端口,OAuth 授权状态与题目元数据缓存持久化在 `./data` 目录中(重启容器无需在 Gemini Web 重新关联账号)。

> **提示**:如果你的 Nginx 也运行在 Docker 容器中,可在 `docker-compose.yml` 中移除 `ports`,直接把 `408os-cli-mcp` 加入 Nginx 所在的外部网络(如 `nginx_default`),反代目标写 `http://408os-cli-mcp:8788` 即可。

---

## 公网 HTTPS 反代与接入网页端 Gemini

网页端 Gemini(`gemini.google.com`)要求公网 HTTPS(完整证书链)与不开启响应缓冲的 Streamable HTTP 反代。以下任选其一:

### 方案 A:Nginx 反向代理示例

```nginx
server {
    listen 443 ssl;
    http2 on;
    server_name 408os-mcp.yourdomain.com;

    ssl_certificate     /path/to/fullchain.pem;
    ssl_certificate_key /path/to/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8788;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port 443;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 240s;
    }
}
```

### 方案 B:Caddy 反向代理示例(自动签发证书,默认支持流式)

```caddyfile
408os-mcp.yourdomain.com {
    reverse_proxy 127.0.0.1:8788
}
```

### 方案 C:内网无公网 IP 主机使用 Cloudflare Tunnel

无需开放任何路由器端口,将 Tunnel Public Hostname `408os-mcp.yourdomain.com` 指向 `http://127.0.0.1:8788` 即可。

### 在 `gemini.google.com` 中添加

1. 打开 [gemini.google.com](https://gemini.google.com) → **Settings** → **Connected Apps**(或 `https://gemini.google.com/devmode`)。
2. 添加自定义 MCP 应用,填入:
   ```text
   https://408os-mcp.yourdomain.com/mcp
   ```
3. 在弹出的 **“408OS CLI MCP 授权”** 页面输入 `.env` 中的 `PROXY_PASSCODE` 即可完成绑定。

---

## 提供的 13 个只读工具

| 工具名 | 用途说明 |
| --- | --- |
| `408os_check_auth` | 检查官方 `408os` CLI 版本、Token 状态与只读授权范围(脱敏,不含个人信息) |
| `408os_get_answer_history` | 查询某天/近期客观题答题与错题记录(支持 `yesterday`/`today`/`last_7_days`/`date`),自动补齐年份、题号、Anki 三级标签与刷题链接 |
| `408os_get_weaknesses` | 调用官方服务端聚合(最多 90 天)获取错题 Top20 与科目/章节/知识点薄弱统计,自动转换 `科目::章::知识点` 标签 |
| `408os_list_collections` | 列出 `不会题本`、`不熟题本` 与自定义收藏夹 |
| `408os_get_collection_questions` | 按收藏夹 ID 或直接按名称(如 `"不会题本"`、`"不熟题本"`)分页读取题本内题目 |
| `408os_get_question` | 按 `question_id` **或** `year` + `question_index` 读取单题(清洗后题干、选项、Draw.io 文本表格、答案、解析,以及个人掌握度/站内笔记/错因/记忆卡) |
| `408os_get_questions` | 批量读取最多 20 道题(支持按需选择 `content` / `answer` / `analysis`) |
| `408os_search_questions` | 按科目、知识点 ID、年份/年份范围、题源(真题/王道/Relax1000)、题型、关键词搜索题目摘要 |
| `408os_list_knowledge_points` | 浏览或搜索 408OS 三级知识点目录,直接返回 `科目::章::知识点` 格式的 `anki_tag` |
| `408os_get_knowledge_point` | 查看单个知识点详情与 `anki_tag`,并一键并行核查关联真题、近期个人错题与站内笔记(适配制卡前核查流程) |
| `408os_list_notes` | 查询单题站内私密笔记,或按关键词/年份分页检索笔记 |
| `408os_get_exercise_url` | 根据 `question_id` 或 `year` + `question_index` 生成可点击的 `https://www.408os.cn/exercise?...` 链接 |
| `408os_get_exams_and_papers` | 只读查询本人模拟考记录与组卷信息 |

---

## 环境变量一览

| 变量名 | 适用模式 | 说明 |
| --- | --- | --- |
| `AGENT_408OS_TOKEN` | STDIO / HTTP | 官方 408OS 连接密钥(`ag408_...`);Windows 下未显式传入时自动回退读取当前用户注册表环境变量 |
| `PROXY_PASSCODE` | HTTP | OAuth 2.1 授权确认页面的验证口令(必填) |
| `PUBLIC_ORIGIN` | HTTP | 对外暴露的公网 HTTPS 根地址(如 `https://408os-mcp.yourdomain.com`) |
| `HOST` / `PORT` | HTTP | HTTP 监听地址与端口(默认 `0.0.0.0` / `8788`) |
| `OS408_CLI_BIN` | STDIO / HTTP | 可选:显式指定 `408os-agent-kit/bin/408os` 路径 |
| `AGENT_KIT_HOME` | STDIO / HTTP | 可选:官方缓存、常驻连接文件与题目元数据缓存目录(默认 `~/.408os`,Docker 内默认 `/data/agent-kit`) |
| `OS408_OAUTH_STATE_PATH` | HTTP | 可选:OAuth 客户端与令牌持久化文件路径(默认 `<AGENT_KIT_HOME>/oauth-state.json`) |
| `OS408_DISABLE_DAEMON` | STDIO / HTTP | 可选:设为 `1` 时禁用常驻守护进程,强制退回每次 `execFile` 子进程模式 |

## License

[MIT](./LICENSE)

TDQS

A4/5.0

Scored across 13 tools

Disambiguation4/5

Most tools target clearly distinct resources, and descriptions include explicit routing hints (e.g. weaknesses vs. answer_history, single get_question vs. batch get_questions). The only real friction is the near-identical get_question/get_questions pair and the overlap between weakness aggregation and raw wrong-answer history, though both are explained in-text.

Naming Consistency5/5

Every tool uses the same 408os_ prefix plus a snake_case verb_noun pattern (get_, list_, search_, check_). Compound names like get_exams_and_papers and get_collection_questions still read consistently within the pattern.

Tool Count5/5

13 tools is squarely in the well-scoped range, and each one maps to a distinct read operation (questions, history, weaknesses, collections, notes, knowledge points, links, auth). Nothing appears redundant or padded.

Completeness4/5

For a deliberately read-only server, coverage is strong: retrieval, search, batch fetch, history, weaknesses, collections, notes, knowledge catalog, and URL building are all present. Minor gaps remain around aggregate study trends/statistics and the fact that exam/paper listing and detail are folded into one dispatcher tool rather than clearly separated.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues