HaoMeme MCP
# HaoMeme MCP |豪老師迷因梗圖產生器(MCP Server)
把 [HaoMeme 網頁版迷因產生器](https://lanternko.github.io/HaoMeme/) 包成一個 **MCP server**:AI 助理只要呼叫工具,就能批次產生 1280×720 的豪老師風格梗圖 PNG。它用 Playwright 驅動本機 headless Chrome/Chromium 載入隨附的原始網頁 App,因此字型、人像、筆刷、版式與網頁版完全一致。
> 給 AI 的快速上手:**工具描述本身就內含完整教學**(欄位限制、版式、範例)。呼叫 `haomeme_catalog` 一次可取得即時人像 key 清單、所有枚舉值、預設台詞與同一份指南。**不需要讀任何其他檔案就能開始產生圖片。**
## 工具一覽
| 工具 | 用途 |
|---|---|
| `haomeme_catalog` | 先查人像 key、模板、參數範圍、輸出路徑、完整指南(不產生圖片,最便宜的探索呼叫) |
| `haomeme_generate` | 產生單張,`{ spec, outputDir? }` |
| `haomeme_batch_generate` | 產生 1–50 張,`{ items: spec[], outputDir? }`,共用同一個瀏覽器分頁,遠比逐張呼叫快 |
`spec` 欄位(全部可選,省略沿用模板預設):
| 欄位 | 說明 |
|---|---|
| `text` | 標題,最多 300 字。**換行寫成 JSON 參數裡的兩個字元 `\n`(backslash+n)**,例如 `"第一行\n第二行"`;直接塞真換行可能被 client 吃掉、兩句擠成一行 |
| `template` | `teach` / `crash` / `nope` / `sue` |
| `lineColors` | 每行一個顏色(`white` / `yellow` / `red`),最多 8 行;第 1 行白、第 2 行黃,其餘白 |
| `portrait` / `portraitIndex` | 內建人像 key(如 `A`、`O`、`I`)或 0 起算索引 |
| `backdrop` | `red`(紅底+筆刷)/ `classroom` / `oldClassroom` / `room` / `grey` / `dark` / `smoke` |
| `side` | 人像在 `left` 或 `right` |
| `textSize` `rotation` `brushSize` `portraitSize` `backgroundSize` `backgroundBlur` | 數值微調(100 = 預設) |
| `redBrush` `brushInFront` `italic` `flip` | 紅刷痕 / 刷在前 / 斜體 / 鏡像 |
版式(實際渲染驗證):`teach`、`crash` = 人像在左、大字在右;`nope` = 人像在右、文字在左;`sue` = 律師函風格。
省略 `text` 時的預設台詞:`teach` → `輪到你\n教育我了?`、`crash` → `AI教育\n翻車了?`、`nope` → `想玩GTA6?\n免談`、`sue` → `你準備收\n律師傳票`。
範例呼叫:
```json
{
"tool": "haomeme_batch_generate",
"items": [
{ "text": "老黃說退就退\\n#OpenToWork", "lineColors": ["white", "yellow"], "template": "teach", "portrait": "A", "backdrop": "red" },
{ "text": "GTA6之前\\n最後的純潔", "template": "nope", "portrait": "I", "backdrop": "dark" }
]
}
```
---
## 給 AI 代理:快速部署指南
> 把下面這段交給任何 MCP-capable 的 AI(Claude Desktop、Cline、DeepSeek Harness、Cursor、Windsurf…),它就能自己裝好。
1. 複製本 repo:`git clone https://github.com/liedwardtw/HaoMeme-mcp.git`
2. 進入目錄執行 `npm install`(或用 `npm ci` 照 lock 檔 installation)。需要 Node.js **18+**。
3. 確認有 Chrome / Edge / Chromium。沒有就執行 `npx playwright install chromium`,或設 `CHROME_PATH` 指向執行檔。
4. 把下面的 MCP host 設定加進你的 AI 用戶端,重開/重連後工具就會出現:
### MCP host 設定(stdio)
Windows:
```json
{
"mcpServers": {
"haomeme": {
"command": "node",
"args": ["C:\\path\\to\\HaoMeme-mcp\\server.mjs"],
"env": {}
}
}
}
```
macOS / Linux:
```json
{
"mcpServers": {
"haomeme": {
"command": "node",
"args": ["/absolute/path/to/HaoMeme-mcp/server.mjs"],
"env": { "CHROME_PATH": "/usr/bin/chromium" }
}
}
}
```
> 若你的 host 需要绝对/规范化的 `command` 路径,把 `node` 换成完整路径(Windows 上可用 `where node`,macOS/Linux 用 `which node`)。
### 環境變數(全部可選)
| 變數 | 用途 |
|---|---|
| `CHROME_PATH` / `HAOMEME_CHROME_PATH` | 指定 Chrome/Chromium 執行檔;沒設就自動搜尋常見路徑或 playwright bundled browser |
| `HAOMEME_APP_HTML` | 指定原始 App 的 `index.html` 路徑;預設用 repo 內的 `HaoMeme-main/index.html` |
| `HAOMEME_OUTPUT_ROOT` | 允許寫入的根目錄;預設 server 所在的 repo 目錄。產出預設在 `./output` |
| `HAOMEME_PROJECT_ROOT` | 讓 `haomeme_catalog` 回報自訂專案根目錄(純資訊用) |
### 驗證
```bash
npm test # 記憶體傳輸端到端測試:列出工具、呼叫 catalog、產生兩張圖
npm run tools # 印出 host 實際看到的 tool name + description + schema
```
第一次呼叫約 20–60 秒(啟動瀏覽器+載入字型);之後同一個分頁重複使用,很快。單張失敗回 `{ ok:false, error }`,批次中單項失敗回 `{ ok:false, index, error }` 但其餘照常產生,不會丟例外。
---
## 目錄結構
```
HaoMeme-mcp/
├── server.mjs # MCP server(stdio),跨平台自動找 Chrome/App
├── package.json / package-lock.json
├── test-client.mjs # 端到端測試(in-memory transport)
├── list-tools.mjs # 印出工具描述/schema
├── gen-*.mjs # 離線批次渲染範例(繞開換行限制、自訂每行顏色)
├── HaoMeme-main/ # 原始純前端 App:index.html、人像、字型、背景、素材、工具腳本
│ ├── index.html # 可直接用瀏覽器開啟
│ ├── portraits.js / backgrounds.js
│ ├── portraits/ portraits_orig/ src_photos/ src_thumbs/
│ ├── fonts/ # Glow Sans / Outfit,SIL OFL 1.1
│ ├── tools/ # 重新去背、算座標、打包人像/背景、切字型的 Python 腳本
│ └── README.md / NOTICE.md / LICENSE
├── examples/ # 範例產出 PNG(4 張)
└── output/ # 預設輸出目錄(執行的時候自動建立)
```
`output/.gitkeep` 讓空資料夾也能進版控;`.gitignore` 會忽略 `output/*.png`,所以你產出的圖片不會誤傳到 repo。
`gen-*.mjs` 是從原始工作區一并保留的離線批次渲染範例(含特定日期的梗圖資料與 Windows 路徑),**MCP server 不需要它們**,跨平台部署時可忽略或自行改寫。
`HaoMeme-main/` 是必要依賴:server 會用 `file://` 載入它的 `index.html`。原始 App 不需要架伺服器,人像已打包在 `portraits.js` 裡。
若把本 repo 放進 DSH 的 workspace,且原本的 `HaoMeme-main/` 是它的兄弟目錄,server 會優先Find repo 內的 `HaoMeme-main/index.html`,找不到再退回兄弟目錄,因此舊工作區配置仍可直接用。
---
## 從原始素材重建(進階)
`HaoMeme-main/tools/` 內有重新產生人像/背景/字型切片的腳本,詳見 [`HaoMeme-main/README.md`](HaoMeme-main/README.md):
```bash
pip install "rembg[cpu]" pillow numpy opencv-python
python HaoMeme-main/tools/cutout.py
python HaoMeme-main/tools/find_anchors.py
python HaoMeme-main/tools/build_portraits.py
python HaoMeme-main/tools/build_backgrounds.py
pip install fonttools brotli
python HaoMeme-main/tools/split_fonts.py
```
---
## 授權與注意事項
- **程式碼**:MIT(見 [`LICENSE`](LICENSE))。
- **影像不屬於 MIT**:`HaoMeme-main/portraits/`、`portraits_orig/`、`src_photos/`、`src_thumbs/`、`portraits.js` / `backgrounds.js` 內嵌圖片,以及由它們做成的截圖/圖示,版權與肖像權屬原作者(YouTube 頻道「拆書人」)。僅供私下玩梗,不要冒充本人發文;商用或公開使用前請自行取得授權或替換素材。詳見 [`NOTICE.md`](NOTICE.md)。
- **字型**:`HaoMeme-main/fonts/` 依 SIL Open Font License 1.1(見 `fonts/OFL.txt`)。
- 原始 App 專案頁:<https://lanternko.github.io/HaoMeme/>;MCP 封裝作者:liedwardtw。
TDQS
Scored across 3 tools
The three tools have clearly demarcated roles: catalog is pure discovery (no rendering), generate does one image, batch_generate does 1-50 with a shared browser page. The only overlap is that batch_generate with a single item duplicates generate, but descriptions explicitly route 2+ images to batch, so misselection risk is low.
All three names share the haomeme_ prefix and a predictable verb/noun pattern (catalog, generate, batch_generate). The batch qualifier is a natural modifier rather than a convention break.
Three tools map cleanly onto discover -> render one -> render many, with no filler. It is on the lean side, since catalog's enum/portrait data could arguably live in the generate descriptions, but nothing is redundant.
For a render-only domain the create path is fully covered plus discovery and batching, which is better than most generation servers. Minor gaps remain: no tool to list or clean up previously rendered files, and results return a file path rather than image bytes, though agents can work around both.