Skip to main content
Glama
README.md
# WeChat Draft Editor for Edge and MCP

[![CI](https://github.com/MosRat/wx_gzh_plugin/actions/workflows/ci.yml/badge.svg)](https://github.com/MosRat/wx_gzh_plugin/actions/workflows/ci.yml)
[![Release](https://img.shields.io/github/v/release/MosRat/wx_gzh_plugin)](https://github.com/MosRat/wx_gzh_plugin/releases/latest)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
![Platform: Windows / Edge](https://img.shields.io/badge/Platform-Windows%20%7C%20Edge-0078D4)

English | [简体中文](README.zh-CN.md)

Edit a WeChat Official Account article from a local AI agent while keeping the draft open in Microsoft Edge. Click the extension on the article you want to work on, then ask your agent to read a section, replace a paragraph, or check how a caption wraps on a phone. The extension uses your existing editor session. Saving a draft is an explicit tool call; publication stays in the WeChat interface.

```text
AI Agent (MCP Client) ──stdio──▶ agent.py ──Windows Named Pipe──▶ bridge.py (Native Host) ──Native Messaging──▶ Edge Extension (mp.weixin.qq.com)
```

## Install on Windows

You need **Windows**, **Microsoft Edge**, and **Python 3.10 or newer** on your `PATH`. Install [uv](https://docs.astral.sh/uv/getting-started/installation/) if you plan to connect an MCP client. The command-line client and native host use only the Python standard library.

1. Download **both ZIP files** from the [latest release](https://github.com/MosRat/wx_gzh_plugin/releases/latest). Extract the Edge ZIP into a permanent folder named `extension` and the MCP ZIP into a separate folder named `mcp`. If you cloned the repository instead, use its `extension` directory and run the following commands from the repository root.
2. Open `edge://extensions`, enable **Developer mode**, click **Load unpacked**, and select the extracted folder containing `manifest.json`. Copy the extension ID that Edge displays.
3. Open PowerShell in the folder containing `install.ps1` and register the native host for your Windows user:

   ```powershell
   .\install.ps1 -ExtensionId (Read-Host 'Paste the extension ID from Edge')
   ```

4. Open an article in the WeChat Official Account editor at `mp.weixin.qq.com`. Click the extension icon on that tab. When its badge reads **ON**, verify the connection in the same PowerShell window:

   ```powershell
   python bridge.py status
   python bridge.py regions
   ```

   The first command reports readiness and the article title. The second reads the title, author, summary and body, including which fields are writable. Check the title before making your first edit.

## Connect an AI agent

For Codex, run this from the folder containing `agent.py`, then start a new Codex session:

```powershell
codex mcp add wechat-editor -- uv run --locked --script "$PWD\agent.py"
```

For another stdio MCP client, set the command to `uv` and pass the arguments shown below. Replace the example script path with its absolute path on your machine. Keep `agent.py.lock`, `bridge.py`, and `device_preview.py` beside the script. On first launch, `uv` installs the locked dependencies.

```json
{
  "mcpServers": {
    "wechat-editor": {
      "command": "uv",
      "args": ["run", "--locked", "--script", "C:/Tools/wechat-editor/mcp/agent.py"]
    }
  }
}
```

Try this read-only request first:

> Read the connected draft and tell me its title. Find the last photo caption and show me how it wraps on an iPhone 13 in portrait orientation.

For an edit, quote the exact passage and its replacement. Ask the agent to read the edited paragraph back before saving. Local passage replacement requires a unique match, which helps preserve surrounding images and formatting.

## Tools

| Task | MCP tools and behavior |
| --- | --- |
| Inspect the draft | `mp_check_status` reports the connected title. `mp_get_regions` reads structured fields and their write status. `mp_get_draft` also returns body HTML. |
| Edit text | `mp_set_field` edits a writable title, author or summary. `mp_replace_passage` replaces a unique passage. `mp_write_body` appends plain text by default; its `replace` mode replaces the entire body, including images. |
| Adjust supported templates | `mp_fit_banner` adjusts heading padding and spacing. `mp_fit_caption` groups caption text by phrase. `mp_align_credits` aligns the writing and layout credit separators. |
| Inspect the live editor | `mp_get_layout` returns paragraph bounds and image statistics. `mp_show_passage` scrolls to a unique passage. `mp_visual_verify` returns a JPEG of the visible editor tab, including its sidebars. Keep the tab selected for capture. |
| Preview the article at another size | `mp_device_preview` renders a copy of the draft in a separate Edge process. Choose `iPhone 13`, `Pixel 7`, or `iPad Mini`, with optional passage and landscape arguments. WeChat's mobile client can differ in font metrics and asset loading. |
| Save | `mp_save_draft` clicks the editor's save-draft button. Check the saved status in WeChat to confirm persistence. |

## Use the CLI without an agent

The CLI accepts UTF-8 text files via `--file` for text operations, or structured JSON arguments via `--payload`. For example, to append a paragraph, read it back, and then save:

```powershell
python bridge.py append --file paragraph.txt
python bridge.py read
python bridge.py save
```

Examples using `--payload` (PowerShell 7.3+ with standard native argument passing):

```powershell
python bridge.py layout --payload '{"offset":40}'
python bridge.py replace_passage --payload '{"oldText":"Original phrase","newText":""}'
python bridge.py fit_caption --payload '["First phrase ","second phrase"]'
python bridge.py align_credits
```

Use `python bridge.py --help` for the full list of browser operations. Device emulation runs in the MCP server, which reuses a headless Edge process and closes each isolated browser context after its screenshot.

## Connection and upgrades

- **Switch articles:** click the extension on the new editor tab. Clicking it again on the connected tab disconnects it. Reconnect after refreshing the page.
- **ERR badge:** hover over the icon for the reason. For host-registration errors, rerun the installer with the ID currently shown in Edge. If multiple editor regions match, place the caret in the article body and reconnect.
- **Base Python path:** the installer records the base Python executable when run inside a standard virtual environment. Keep that base installation available.
- **Key rotation:** to replace a potentially exposed key, disconnect the extension, run `.\install.ps1 -ExtensionId <extension-id> -RotateKey`, then reconnect. Regular reinstalls preserve the key.
- **Upgrade:** extract the new files into their existing folders, reload the extension in Edge, rerun the host installer, and restart the MCP session. Keeping the unpacked extension at the same path helps preserve its ID. See [CHANGELOG.md](CHANGELOG.md) for version-specific upgrade notes (such as the v0.5 protocol change).

## How it works

The MCP server (`agent.py`) and CLI send requests through an authenticated Windows named pipe. Edge launches `bridge.py` as a Native Messaging host and restricts access to the extension ID registered by the installer. The host registration, scripts, and generated authentication key live under the current Windows user in `%LOCALAPPDATA%\WxDraftAgent`.

The extension binds requests to the selected tab and document. `extension/editor.js`, `extension/editor-support.js`, and `extension/templates.js` locate article fields and apply supported edits. `device_preview.py` handles alternative viewport rendering.

## Security, privacy, and disclaimer

- **Local-only bridge:** All communication between Edge, the native host, and the MCP server stays on your local machine over an authenticated Windows named pipe. The installer restricts NTFS permissions on `%LOCALAPPDATA%\WxDraftAgent` and its key file to the installing Windows user.
- **Isolated device preview:** `device_preview.py` sanitizes draft HTML into a sandboxed page with a strict Content Security Policy (`default-src 'none'`) and allows only HTTPS image requests to `mmbiz.qpic.cn` and `mmbiz.qlogo.cn` with redirects blocked.
- **Data shared with your AI agent:** This project collects no telemetry and contacts no author-hosted servers. However, any draft text, HTML, or screenshots requested through MCP tools are sent to the MCP client and AI model provider you configure.
- **Timeout safety:** After a write timeout, read the current draft before retrying because the editor may already have applied the change.
- **Non-affiliation:** This is an independent open-source project and is not affiliated with, endorsed by, or supported by Tencent or WeChat. Changes to the WeChat Official Account editor DOM may require extension updates.

## Development and contributing

Issues and pull requests are welcome. From a source checkout on Windows, install the locked runtime and development dependencies and run the validation suite:

```powershell
$env:UV_DEFAULT_INDEX = 'https://pypi.org/simple'
uv lock --check --script agent.py
python tests/check_locks.py
uv sync --locked
uv run --locked ruff check .
uv run --locked ruff format --check .
node --test tests/worker.test.cjs
uv run --locked python -m unittest discover -s tests -v
```

The browser tests require Microsoft Edge. For the worker tests, use Node 22 or later. If the default `uv` cache is inaccessible, set `UV_CACHE_DIR` to a writable user directory. Windows CI also runs `./tests/installer.ps1` under both Windows PowerShell and PowerShell 7 against a temporary profile and registry subtree. Keep the PEP 723 runtime requirements in `agent.py` and the dependencies in `pyproject.toml` in sync when updating `agent.py.lock` and `uv.lock`.

### Commit messages

Use Conventional Commits with an imperative subject, for example `fix(bridge): discard expired replies` or `test(editor): cover caption wrapping`. Use `chore(release): prepare vX.Y.Z` for release preparation. Describe incompatible protocol changes in the commit body and the release notes.

## License

Released under the [MIT License](LICENSE).

TDQS

B3.2/5.0

Scored across 14 tools

Disambiguation4/5

Most tools have clearly distinct purposes: reading state (mp_check_status, mp_get_draft, mp_get_regions, mp_get_layout), writing (mp_write_body, mp_set_field, mp_replace_passage), and verification (mp_visual_verify, mp_device_preview). The main overlaps are mp_get_draft vs mp_get_regions (both return title/body, one adds field metadata) and mp_write_body vs mp_replace_passage (whole-body write vs local replace), which descriptions do partially clarify.

Naming Consistency4/5

All tools share the mp_ prefix and mostly follow a verb_noun pattern (check_status, get_draft, set_field, write_body, show_passage, align_credits, fit_caption, replace_passage, save_draft). Minor deviations exist: mp_visual_verify and mp_device_preview lead with an adjective/noun rather than a verb, but the set remains predictable.

Tool Count4/5

14 tools is within a reasonable range for a page-editing automation server, and each maps to a concrete operation. A few highly specialized formatting tools (mp_align_credits, mp_fit_caption, mp_fit_banner) feel niche, but they correspond to real editorial tasks.

Completeness3/5

The surface covers reading, metadata/body writing, layout inspection, visual and device verification, and saving. However, there is no image upload/insertion tool, no way to create a new draft or article, and no publish/export step, which are notable gaps for a WeChat article editor workflow.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues