Files
trilium-wechatsync/mcp/MCP_INTEGRATION.md
T
jinghui b5acbed047 init: TriliumNext × WechatSync 多平台发布桥接
- bridge/ 常驻桥接服务器(WebSocket 9600 + HTTP 9601,平台状态缓存)
- patch/ TriliumNext 补丁(publisher 服务、API 路由、发布控制台前端)
- install.sh 一键安装 + Docker 容器 CLI 处理
- 全链路使用 host.docker.internal 解决容器访问宿主机
- mcp/ AI Agent 集成说明
2026-10-17 21:59:00 +08:00

3.1 KiB

MCP Server 集成

桥接服务器可被 AI Agent 通过 MCP (Model Context Protocol) 调用,实现「AI 写作 → 一键分发到多平台」。

桥接 HTTP API

桥接在 http://<host>:9601 暴露简单 HTTP 接口,任何语言都能调用:

方法 路径 说明
GET /status 桥接与扩展连接状态 {connected, mode, uptime}
GET /platforms 各平台登录状态(含缓存){platforms[], cached, cachedAt}
POST /request 转发任意请求到 Chrome 扩展

POST /request

{
  "method": "sync",           // 或 listPlatforms / auth 等扩展支持的方法
  "params": { "platforms": "zhihu,csdn" },
  "token": "<Token>"
}

响应:

{ "result": { ... } }

MCP 工具封装示例

用 Node 写一个最小 MCP Server,把桥接封装成工具:

import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";

const BRIDGE = process.env.BRIDGE_URL || "http://host.docker.internal:9601";

const server = new Server({ name: "wechatsync-publisher", version: "1.0.0" },
  { capabilities: { tools: {} } });

server.setRequestHandler("tools/list", async () => ({
  tools: [{
    name: "list_platforms",
    description: "列出已登录的内容平台",
    inputSchema: { type: "object", properties: {} }
  }, {
    name: "publish_article",
    description: "把 Markdown 文章同步到指定平台",
    inputSchema: {
      type: "object",
      properties: {
        file: { type: "string", description: "本地 Markdown 文件路径" },
        platforms: { type: "string", description: "逗号分隔平台 id" }
      },
      required: ["file", "platforms"]
    }
  }]
}));

server.setRequestHandler("tools/call", async (req) => {
  if (req.params.name === "list_platforms") {
    const r = await fetch(`${BRIDGE}/platforms`);
    const d = await r.json();
    return { content: [{ type: "text", text: JSON.stringify(d.platforms, null, 2) }] };
  }
  if (req.params.name === "publish_article") {
    const { file, platforms } = req.params.arguments;
    const r = await fetch(`${BRIDGE}/request`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ method: "sync", params: { file, platforms } })
    });
    const d = await r.json();
    return { content: [{ type: "text", text: JSON.stringify(d) }] };
  }
});

await server.connect(new StdioServerTransport());

Claude Desktop / 其他客户端配置

{
  "mcpServers": {
    "wechatsync": {
      "command": "node",
      "args": ["/path/to/wechatsync-mcp/index.js"],
      "env": { "BRIDGE_URL": "http://host.docker.internal:9601" }
    }
  }
}

典型工作流

  1. Agent 调用 list_platforms → 获取已登录平台
  2. Agent 生成/改写文章,写入 Markdown
  3. Agent 调用 publish_article → 桥接转发给 Chrome 扩展 → 扩展在各平台创建草稿
  4. 返回草稿编辑链接给用户

无 Chrome 扩展连接时,桥接返回缓存的平台状态,Agent 可据此判断是否需要提示用户登录。