本地开发
前置条件
- 需要安装 Node.js(用于运行
wrangler本地开发服务器) - 需要一个已经开通 Workers 与 KV 的 Cloudflare 账号
- 需要先把两个 TTF 字体上传到 KV(具体见“配置与部署”页面)
安装
git clone https://github.com/lingion/plot-mcp-worker
cd plot-mcp-worker
npm install # 安装 wrangler、typescript、expr-eval、opentype.js、resvg-wasm依赖版本已经锁定在 package-lock.json 中;如果你更偏好干净安装,使用 npm ci 也能得到同样的依赖树。
脚本(来自 package.json)
| npm 脚本 | 实际命令 | 用途 |
|---|---|---|
dev | wrangler dev | 启动本地 Worker 开发服务器(底层由 Miniflare 提供) |
deploy | wrangler deploy | 把当前代码推送到 Cloudflare Workers |
check | tsc --noEmit | 只做 TypeScript 类型检查,不输出任何构建产物 |
test:smoke | node scripts/smoke.mjs | 访问 /healthz 和若干工具调用,做最基本烟雾测试 |
probe:force-analysis | node scripts/local-force-analysis-probe.mjs | 在本地生成 8 组受力分析样例 |
check:force-drift | node scripts/force-analysis-drift-check.mjs | 比较本地和已部署 SVG 的字节级哈希是否漂移 |
check:force-struct | node scripts/force-analysis-struct-diff.mjs | 比较本地与线上 SVG 的结构差异 |
check:force-compact | node scripts/force-analysis-compact-diagnose.mjs | 对受力分析输出做紧凑诊断 |
check:force-verify | node scripts/force-analysis-verify.mjs | 完整验证受力分析输出 |
check:bundle-fingerprint | node scripts/deploy-bundle-fingerprint.mjs | 为线上 Worker bundle 计算指纹 |
check:deploy-preflight | node scripts/deploy-preflight.mjs | 部署前执行一轮健康检查 |
check:deploy-and-verify | node scripts/deploy-and-verify.mjs | 部署后立刻验证 |
check:remote-health | node scripts/remote-health.mjs | 访问线上 /healthz 并解析返回的 JSON |
典型工作流
# 1. 先做类型检查
npm run check
# 2. 对本地开发服务器做烟雾测试(需要另一个终端先跑 wrangler dev)
npm run dev # 终端 1
npm run test:smoke # 终端 2
# 3. 部署前先验证受力分析输出是否发生漂移
npm run probe:force-analysis
npm run check:force-drift
# 4. 部署
npm run deploy
# 5. 部署后验证线上服务
npm run check:remote-health如何用 curl 调 MCP 端点
# initialize
curl -s -X POST https://your-worker.workers.dev/mcp -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}'
# tools/list
curl -s -X POST https://your-worker.workers.dev/mcp -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'
# tools/call -> plot
curl -s -X POST https://your-worker.workers.dev/mcp -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"plot","arguments":{"expr":"sin(x)","x_min":-6.28,"x_max":6.28,"title":"Sine","render":{"format":"link"}}}}'如何直接调用 Web 端点
这些 Web 端点(例如 /plot、/png)都会接受一个带 gzip 压缩的 base64url JSON,通过 ?d= 查询参数传入。最省事的路径仍然是直接先调用 MCP 工具,让工具自己返回最终 URL;如果你希望在 Node 里手工构造,也可以这样生成编码后的 payload:
import { toCompressedBase64UrlFromJson } from "./src/utils.js";
const d = await toCompressedBase64UrlFromJson({
expr: "sin(x)", x_min: -6.28, x_max: 6.28, points: 1000,
title: "Sine", xlabel: "x", ylabel: "y"
});
console.log("https://your-worker.workers.dev/plot?d=" + d);