第 6 篇,共 10 篇
通过 API 和 MCP 自动化 Threadsmith
高级 15 分钟
通过脚本和 AI 智能体将文字、形状、SVG 或整个项目数字化,生成 DST、PES、JEF 或 EXP 文件,并分析绣花文件。附真实请求和响应示例。
Threadsmith 的针迹引擎在应用和服务器上使用同一套 TypeScript 代码。五个引擎工具既可通过 JSON API 调用,也可作为 MCP 工具使用。这样,脚本和 AI 智能体就能创建设计,再交给别人在应用中打开。

| 工具 | API | MCP 工具名 |
|---|---|---|
threadsmith-digitize | POST /api/v1/tools/threadsmith-digitize/calculate | threadsmith_digitize |
threadsmith-stitch-file | POST /api/v1/tools/threadsmith-stitch-file/calculate | threadsmith_stitch_file |
threadsmith-design-stats | POST /api/v1/tools/threadsmith-design-stats/calculate | threadsmith_design_stats |
threadsmith-sequence-optimizer | POST /api/v1/tools/threadsmith-sequence-optimizer/calculate | threadsmith_sequence_optimizer |
threadsmith-path-boolean | POST /api/v1/tools/threadsmith-path-boolean/calculate | threadsmith_path_boolean |
API 运行在 https://threadsmith.usefulweb.site 上(主站上也可用)。项目文件格式公布在 /project.schema.json;MCP 端点是 /mcp。
所有坐标的单位都是毫米,x 轴向右,y 轴向下,(0, 0) 是绣框中心。
1. 文字数字化
POST /api/v1/tools/threadsmith-digitize/calculate
Content-Type: application/json
{
"source": "text",
"text": "Ada",
"textSize": 14,
"stitchType": "satin",
"satinWidth": 2,
"color": "navy",
"includeStitches": true
}
响应(已缩短;以下均为真实数值):
{
"summary": "394 stitches in 1 colour(s), 39.2 × 16.4 mm, 9 trim(s), about 1 min 12 s at 700 stitches/min.",
"result": {
"stitches": 394, "jumps": 10, "trims": 9, "colorChanges": 0,
"widthMm": 39.2, "heightMm": 16.4, "sewingTime": "1 min 12 s",
"topThreadM": 1.23, "bobbinThreadM": 0.3,
"hoop": { "id": "130x180", "fits": true, "overflowMm": 0 },
"density": { "maxLayers": 2.12, "areaOverMm2": 0, "threshold": 2.6 },
"colors": [{ "name": "Navy", "hex": "#1d2a52", "stitches": 394, "topThreadM": 1.23 }],
"stitchList": [[-7.28, 7, "jump"], [-7.28, 7, "stitch"], [-7.54, 6.35, "stitch"], [-7.28, 7, "stitch"], "…"],
"project": { "format": "threadsmith", "version": 1, "objects": ["…"] },
"openInApp": "https://threadsmith.usefulweb.site/app#project=eyJmb3JtYXQiOi…"
}
}
openInApp把整个项目放在 URL 片段中(片段不会发送到服务器)。把这个链接发给别人,设计就会在对方的浏览器中以可编辑副本的形式打开。stitchList(仅在设置includeStitches: true时返回)是完整的机器执行计划,格式为[x, y, type],其中 type 为stitch、jump、trim、color或end。上面开头几项分别是跳到第一个点的跳针和起针加固。presentation(未列出)包含仿真线迹效果的 SVG 预览和表格,可以直接显示。
source 的其他取值:shape(ellipse、rect、star、hexagon、heart,配合 widthMm/heightMm)、path(以毫米为单位的 pathD)、svg(SVG 标记,可选 fitWidth)和 project。

2. 构建完整项目
以 JSON 文本发送项目。这个示例是一枚蓝绿色榻榻米针徽章,带空心字体的首字母和缎纹边框:
{
"source": "project",
"project": "{\"name\":\"Agent badge\",\"hoop\":\"100x100\",\"objects\":[{\"kind\":\"ellipse\",\"cx\":0,\"cy\":0,\"rx\":30,\"ry\":30,\"color\":\"teal\",\"stitch\":{\"type\":\"tatami\",\"angle\":30}},{\"kind\":\"text\",\"text\":\"HI\",\"x\":0,\"y\":8,\"size\":18,\"font\":\"outline\",\"weight\":3,\"color\":\"gold\",\"stitch\":{\"type\":\"satin\"}},{\"kind\":\"ellipse\",\"cx\":0,\"cy\":0,\"rx\":30,\"ry\":30,\"color\":\"navy\",\"stitch\":{\"type\":\"satin\",\"mode\":\"border\",\"width\":3}}]}"
}
结果:"3,495 stitches in 3 colour(s), 63.4 × 63.4 mm, 3 trim(s), about 5 min 54 s at 700 stitches/min."(3 种颜色共 3,495 针,63.4 × 63.4 mm,3 次剪线,按每分钟 700 针约需 5 分 54 秒),缝制顺序为 ["o1", "o2", "o3"],各颜色用量为 Teal 2,637 针 / Old gold 228 针 / Navy 630 针。
对象类型:path(d)、rect(x y w h r)、ellipse(cx cy rx ry)、polygon(cx cy radius sides inner)、text(text x y size font weight align spacing slant)和 stitches(points)。每个对象都有 color(调色板中的线 ID、内置线 ID(如 ruby)或 #rrggbb)、stitch,以及可选的 underlay、start、end。缺少的值会使用合理的默认值;无效的值会返回通俗易懂的错误信息,例如 Object 1: unknown kind "blob" (use path, rect, ellipse, polygon, text, stitches).(对象 1:未知类型“blob”)。
3. 生成 DST 文件
把第 1 步得到的针迹列表(作为 JSON 字符串)传给文件写入工具:
{ "format": "dst", "label": "ADA", "stitches": "[[-7.28,7,\"jump\"],[-7.28,7,\"stitch\"],[-7.54,6.35,\"stitch\"], …]" }
真实响应:
{
"summary": "stitches.dst: 1,820 bytes, 394 stitch and 41 jump records, 0 colour change(s); round-trip verified.",
"result": {
"format": "dst", "bytes": 1820, "records": 436, "stitchRecords": 394, "jumpRecords": 41,
"verified": true, "maxErrorMm": 0.07,
"header": { "LA": "ADA", "ST": "435", "CO": "0", "+X": "197", "-X": "195", "+Y": "82", "-Y": "82",
"AX": "+ 197", "AY": "- 70", "MX": "+ 0", "MY": "+ 0", "PD": "******" },
"firstRecords": [
{ "index": 1, "hex": "66 80 9b", "meaning": "jump", "dx": -73, "dy": -70 },
{ "index": 2, "hex": "00 00 03", "meaning": "stitch", "dx": 0, "dy": 0 },
{ "index": 3, "hex": "a1 42 03", "meaning": "stitch", "dx": -2, "dy": 7 }
],
"artifactUrl": "https://threadsmith.usefulweb.site/artifacts/a40945972495e824e538c3e31da1e24e/stitches.dst",
"base64": "TEE6QURBICAgICAgICAgICAgIA1TVDogICAgNDM1…"
}
}
写入工具会重新解码自己的输出,并报告 verified 和最大位置偏差(maxErrorMm,对角方向最多为 0.1 mm 机器单位的一半)。你可以从 artifactUrl 下载文件(保留约两小时),也可以解码 base64。跳针数包含剪线:每次剪线写成三个总位移为零的跳针。你也可以发送 "project": "…" 来代替 stitches,并用 "format": "pes"(Brother)、"jef"(Janome)或 "exp"(Melco)指定格式。PES 和 JEF 的结果还会附带 machineThreads,即为每个换色点选出的最接近的 Brother/Janome 色卡颜色,例如树叶模板的结果是 [{ "index": 54, "name": "Emerald Green", "hex": "#00673e" }, …]。
解读第一条记录:DST 中 y 轴向上,因此跳到 (−7.28, 7) mm 对应以 0.1 mm 为单位的 dx −73、dy −70;66 80 9b 是这两个值的平衡三进制编码,第三个字节中设置了跳针标志 0x80。
4. 分析现有文件
{
"file": { "filename": "ada.dst", "contentBase64": "TEE6QURBICAgICAg…" },
"spm": 800,
"colors": ["#1d2a52"],
"hoop": "100x100"
}
响应:DST "ADA": 394 stitches, 1 colour(s), 9 trim(s), 39.2 × 16.4 mm, about 1 min 6 s at 800 stitches/min, 1.23 m top thread.(1 种颜色共 394 针,9 次剪线,39.2 × 16.4 mm,按每分钟 800 针约需 1 分 6 秒,面线 1.23 m) ——这些数字与数字化工具给出的完全一致,说明文件写出再读回后没有任何偏差。结果中还会列出超出密度上限的 hotspots,并对长于 12.1 mm 或短于 0.3 mm 的针迹给出警告。
5. 通过 MCP 实现相同操作
把你的 MCP 客户端连接到 https://threadsmith.usefulweb.site/mcp(可流式 HTTP)。tools/list 会返回这三个工具;用相同的参数调用它们:
{ "jsonrpc": "2.0", "id": 2, "method": "tools/call",
"params": { "name": "threadsmith_digitize",
"arguments": { "source": "shape", "shape": "star", "widthMm": 50, "heightMm": 50, "stitchType": "contour", "color": "citrine" } } }
响应中的 structuredContent.summary 为 "323 stitches in 1 colour(s), 45.4 × 43.1 mm, 1 trim(s), about 30 s at 700 stitches/min."(1 种颜色共 323 针,45.4 × 43.1 mm,1 次剪线,按每分钟 700 针约需 30 秒), structuredContent.result 包含的字段与 API 返回的相同。
推荐的智能体工作流程
- 根据用户的需求构建项目 JSON(参见
/project.schema.json)。 - 以
source: "project"调用threadsmith_digitize;查看warnings、hoop.fits和density。 - 调整设置(缩小尺寸、降低密度、更换针法),直到不再出现警告。
- 把
openInApp链接交给用户检查和微调,和/或调用threadsmith_stitch_file生成 DST 文件。 - 提醒用户先试绣:估算值仅供规划参考,DST/EXP 文件不含线色信息,PES/JEF 文件也只包含最接近的机器色卡颜色。
准备好试试了吗?