内容
# MCP服务器 Lenza
MCP服务器用于与即时通讯软件 [Lenza](https://lenzaos.com) 交互:读取(聊天记录、消息、搜索、活跃度)和写入(发送消息、富文本、反应、线程、引用回复、提及)。共33个工具。
## 安装
```bash
pip install -r requirements.txt
```
## 配置
优先级:环境变量 → `config.json` 文件(需与 `server.py` 放置在同一目录下)。
| 参数 | 环境变量 | 描述 |
|---|---|---|
| `token` | `LENZA_TOKEN` | 永久 API Token(获取方式:`lenza-get-token.ps1`, 步骤 1) |
| `base_url` | `LENZA_BASE_URL` | 默认 `https://api.lenzaos.com` |
| `domain` | `LENZA_DOMAIN` | 空间域名,用于 `lenza_get_user` 短登录名 |
`config.json`(模板:`config.example.json`, 文件本身在 `.gitignore` 中):
```json
{
"token": "NKHF_...",
"base_url": "https://api.lenzaos.com",
"domain": "your-domain"
}
```
## 启动
```bash
python server.py
```
### 在 Claude Code 中连接
```json
{
"mcpServers": {
"lenza": {
"command": "python",
"args": ["/path/to/mcp-lenza/server.py"]
}
}
}
```
## 读取工具(21个)
| 工具 | 返回内容 |
|---|---|
| `lenza_get_unread` | 未读聊天记录,最近 N 条消息 |
| `lenza_get_chats` | 聊天列表(过滤:所有/私人/群组/频道/机器人) |
| `lenza_get_chat_info` | 聊天信息 + 成员 |
| `lenza_get_messages` | 最近 N 条聊天记录 |
| `lenza_get_messages_page` | 翻页历史记录(上一页/下一页/当前页) |
| `lenza_search_messages` | 消息搜索(全局或聊天内) |
| `lenza_get_activity` | 活跃度面板:MENTION/THREAD/REACTION/ALL |
| `lenza_get_last_messages_multi` | 多个聊天最近消息批量获取 |
| `lenza_count_messages` | 指定时间范围内的消息数量 |
| `lenza_get_chat_links` | 聊天中分享的链接 |
| `lenza_get_user` | 通过登录名/login@domain/hash 获取用户信息 |
| `lenza_get_chat_users` | 聊天成员 |
| `lenza_get_contacts` | 用户联系人 |
| `lenza_get_common_chats` | 与用户共同的聊天 |
| `lenza_get_online_status` | 最近在线状态(批量) |
| `lenza_get_who_read` | 谁读了消息 |
| `lenza_get_attachments` | 聊天文件 |
| `lenza_get_pinned_messages` | 聊天固定消息 |
| `lenza_get_profile` | 用户个人信息(用于识别“我的”提及) |
| `lenza_get_notices` | 通知(提醒) |
| `lenza_get_unread_count` | 未读消息计数 |
## 写入工具(12个)
| 工具 | 操作 |
|---|---|
| `lenza_send_message` | 发送消息(+附件、提及、HTML/MD 格式化) |
| `lenza_send_formatted_message` | 富文本:通过 `styles` 使用 11 种样式,精确位置 |
| `lenza_add_reaction` / `lenza_remove_reaction` | 消息反应(表情符号) |
| `lenza_create_thread` | 创建线程 |
| `lenza_reply_in_thread` | 在线程中回复(如果线程不存在则创建) |
| `lenza_reply_message` | 回复消息(引用回复,带引用) |
| `lenza_forward_message` | 将消息转发到其他聊天 |
| `lenza_mark_read` | 将聊天标记为已读 |
| `lenza_read_mention` | 将提及标记为已读(取消 @ 提及) |
| `lenza_edit_message` | 编辑自己的消息 |
| `lenza_cancel_message` | 取消(删除)自己的消息 |
## 富文本(`lenza_send_formatted_message`)
样式通过 `styles` 列表指定。建议通过 `target`(文本中的子字符串)传递位置 - `offset`/`length` 由服务器计算;手动指定 `offset`/`length` 作为备用方案。
```json
{
"chat_hash": "...",
"text": "冲刺总结\n任务 PROJ-123 已关闭。\n报告链接\n第一项\n第二项",
"styles": [
{"type": "h1", "target": "冲刺总结"},
{"type": "code", "target": "PROJ-123"},
{"type": "link", "target": "报告链接", "url": "https://example.com"},
{"type": "ordered_list", "target": "第一项\n第二项"}
]
}
```
类型:`bold`, `italic`, `underline`, `strikethrough`, `code`, `code_block` (+`language`), `quote`, `h1`, `link` (+`url`), `unordered_list`, `ordered_list`.
## 提及
服务器不会解析文本中的登录名 - 提及仅通过 `user_hash` 实现(从 `lenza_get_chat_users` / `lenza_get_user` 获取):
```json
{"chat_hash": "...", "text": "@名字,请查看",
"mentions": [{"user_hash": "<uuid>", "text": "@名字"}]}
```
`"user_hash": "all"` - 提及聊天中的所有人。
## 限制和注意事项
- **线程:客户端缓存**。通过 API 创建的线程在后端正确工作,但客户端 Lenza 的回复计数器可能不会立即更新 - 可以通过刷新聊天(Ctrl+R)或重启客户端解决。
- 消息**从令牌所有者发送** - 没有单独的机器人身份。
- `/message/{hash}/list` 不支持 `limit` 参数 - 返回约 30-60 条消息;使用 `lenza_get_messages_page` 获取更深的历史记录。
- `bot_assistant`(Lenzabot)聊天室不返回历史记录(HTTP 500)- 跳过。
## 典型场景
- **检查未读** → `lenza_get_unread` 或 `lenza_get_activity("MENTION")`
- **频道 X 在一天内的总结** → `lenza_get_chats` → `lenza_get_messages(hash, 50)`
- **所有频道的总结** → `lenza_get_last_messages_multi([hash1, hash2, ...])`
- **回复 Arsene** → `lenza_get_unread` → `lenza_send_message(chat_hash, ...)`
- **回复此消息** → `lenza_reply_message(chat_hash, message_hash, ...)`
- **在线程中讨论** → `lenza_reply_in_thread(message_hash, ...)`
- **添加反应** → `lenza_add_reaction(message_hash, "👍")`
## 已发现的 API 特殊性
官方 API 文档:https://api.lenzaos.com/doc (本地副本:`docs/lenza_api_swagger.yaml`)。
- `/user` 和 `/chat/get` 返回 `{created:[...]}`,不是扁平数组。
- 消息分页(`/message/{hash}/prev|next|current`)返回 `{item:[...]}`。
- `/user/by-login/` 需要 `login@domain`,不支持短登录名。
- 消息文本位于 `message` 字段中,为 JSON 字符串 `{"text","entities","version":2}`;发送消息也需要此格式(否则为纯文本)。
- **反应通过 `POST /message/{hash}/reaction` 设置**(规范中仅支持 GET/DELETE)。
- **回复链接** 不通过 `POST /message` 创建 - 使用线程(`POST /chat type=thread message_hash=...`),线程是独立的聊天室。
- 已确认的实体类型从 GUI 客户端导出:删除线为 `MessageEntityStrike`(而非 Strikethrough),链接为 `MessageEntityTextUrl`(而非 MessageEntityLink)。
- `@` 提及标记通过单独的 `PUT /message/{hash}/read/mention` 删除。
## 未实现(非必需)
创建/删除群组和频道,管理成员,设置。