codesign-mcp-next 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,458 +1,458 @@
1
- # codesign-mcp-next
2
-
3
- [![npm version](https://img.shields.io/npm/v/codesign-mcp-next?label=npm&color=cb3837)](https://www.npmjs.com/package/codesign-mcp-next)
4
- [![Node.js](https://img.shields.io/badge/node-%3E%3D20-339933?logo=node.js&logoColor=white)](https://nodejs.org/)
5
- [![TypeScript](https://img.shields.io/badge/TypeScript-5.9-3178c6?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
6
- [![MCP](https://img.shields.io/badge/MCP-stdio-000000)](https://modelcontextprotocol.io/)
7
- [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
8
-
9
- English | [简体中文](#简体中文)
10
-
11
- Local MCP server for Tencent CoDesign sharing links. It exposes artboards, official layer specs, preview images, and designer-exported slices for design-to-code workflows.
12
-
13
- ## Features
14
-
15
- - Read CoDesign sharing links through MCP tools.
16
- - List designs and artboards from a sharing URL.
17
- - Fetch official `meta_url` specs with layers, groups, text, colors, CSS, and coordinates.
18
- - Download designer-exported slice assets from the official slice manifest.
19
- - Copy the current CoDesign screen and layer selection to an Agent with the optional Tampermonkey userscript.
20
- - Keep login state in a persistent local Chromium profile.
21
- - Store runtime files in the caller workspace by default.
22
- - Provide a reusable MCP prompt and read-only workflow resource for design-to-code tasks.
23
-
24
- ## Installation
25
-
26
- Run directly with npm:
27
-
28
- ```bash
29
- npx -y codesign-mcp-next
30
- ```
31
-
32
- Or install globally:
33
-
34
- ```bash
35
- npm install -g codesign-mcp-next
36
- codesign-mcp-next
37
- ```
38
-
39
- ## MCP Configuration
40
-
41
- For most MCP clients, use the zero-config form:
42
-
43
- ```json
44
- {
45
- "mcpServers": {
46
- "codesign-mcp-next": {
47
- "command": "npx",
48
- "args": ["-y", "codesign-mcp-next@latest"]
49
- }
50
- }
51
- }
52
- ```
53
-
54
- The server detects its workspace in this order:
55
-
56
- 1. `CODESIGN_WORKSPACE_DIR`
57
- 2. `INIT_CWD`, which is normally the directory where `npx` was launched
58
- 3. `process.cwd()`
59
-
60
- Runtime files are written under the detected workspace:
61
-
62
- ```text
63
- <workspace>/.codesign-mcp/profile
64
- <workspace>/.codesign-mcp/artifacts
65
- <workspace>/.codesign-mcp/codesign-mcp.log
66
- ```
67
-
68
- The default runtime directory is not created when the MCP server merely starts,
69
- lists tools, or serves prompts/resources. It is created only when a tool needs
70
- local state or artifacts, such as `codesign_login`, browser-backed CoDesign
71
- reads, or downloads. Before that happens, default file logs go to stderr.
72
- If `CODESIGN_LOG_FILE` is set explicitly, the server treats that path as an
73
- explicit logging request and creates its parent directory when writing logs.
74
-
75
- Use the `codesign_status` tool to inspect `workspaceRoot` and `workspaceRootSource`. If your MCP client launches servers from a global application directory instead of the project directory, set one of these explicitly:
76
-
77
- ```json
78
- {
79
- "mcpServers": {
80
- "codesign-mcp-next": {
81
- "command": "npx",
82
- "args": ["-y", "codesign-mcp-next@latest"],
83
- "cwd": "F:/your-project"
84
- }
85
- }
86
- }
87
- ```
88
-
89
- Or:
90
-
91
- ```text
92
- CODESIGN_WORKSPACE_DIR=F:/your-project
93
- ```
94
-
95
- Optional environment variables:
96
-
97
- ```text
98
- CODESIGN_DATA_DIR=F:/your-project/.codesign-mcp
99
- CODESIGN_PROFILE_DIR=F:/your-project/.codesign-mcp/profile
100
- CODESIGN_ARTIFACTS_DIR=F:/your-project/.codesign-mcp/artifacts
101
- CODESIGN_LOG_FILE=F:/your-project/.codesign-mcp/codesign-mcp.log
102
- CODESIGN_IDLE_MS=600000
103
- CODESIGN_KEEP_BROWSER=1
104
- CODESIGN_CACHE_TTL_MS=60000
105
- CODESIGN_INTEGRITY_STRICT=0
106
- CODESIGN_RETRY_MAX_ATTEMPTS=3
107
- CODESIGN_RETRY_BASE_MS=300
108
- CODESIGN_API_RPS=5
109
- CODESIGN_CDN_RPS=10
110
- CODESIGN_LOG_LEVEL=info
111
- ```
112
-
113
- ## Tools
114
-
115
- - `codesign_status`: Show runtime paths, browser state, and profile state.
116
- - `codesign_login`: Open a visible Chromium window for CoDesign QR-code login.
117
- - `codesign_logout`: Clear the persisted profile.
118
- - `list_artboards`: Resolve a CoDesign sharing URL into designs and artboards.
119
- - `get_artboard_spec`: Fetch official CoDesign `meta_url` specs with layers, text, colors, CSS, groups, and slice metadata. Every response includes a `spec.hierarchy` tree built from `parent_id` and `layerIndex`; each node carries `rect` (artboard-absolute), `relativeRect` (relative to the artboard), and `relativeToParent` (relative to its direct parent) so a full-page layout can be mapped to DOM without rebuilding the tree yourself. With `layerObjectId`, `selectionScope` can be `layer`, `subtree`, or `region`; selected responses also include flat layers/groups, selection bounds, and relative coordinates. Optional `targetPlatform` accepts natural-language platform names such as `web`, `Android`, `安卓`, `iOS`, `微信小程序`, or `mini program` and returns platform-adjusted metadata. Optional `targetUnit`, `customScale`, `customWidth`, and `remBasePx` mirror CoDesign's platform settings.
120
- - `get_artboard_image`: Fetch preview or cover images for visual comparison.
121
- - `download_slice`: Download designer-exported slice assets from the official slice manifest.
122
- - `debug_collect_network`: Collect a redacted network summary for diagnosis.
123
-
124
- ## Caching and tool output
125
-
126
- Sharing details, meta JSON, and slice manifests are cached in memory for 60 seconds (`CODESIGN_CACHE_TTL_MS`, in milliseconds). Concurrent calls for the same resource share a single request. Set the variable to `0` to disable caching. The cache is process-local and is never written to disk.
127
-
128
- Successful tool results keep the complete payload in `structuredContent`. `content[0].text` contains only a short summary, so the same JSON is not counted twice in the model context. For `get_artboard_spec`, the text summary additionally includes a two-level `hierarchy` skeleton, artboard dimensions, layer/group/slice counts, and any integrity warnings, so clients that ignore `structuredContent` still receive the structural cues needed for design-to-code. If your MCP client does not read `structuredContent`, pass `includeFullText: true` to any data tool (`list_artboards`, `get_artboard_spec`, `get_artboard_image`, `download_slice`) to receive the full JSON as text instead. `debug_collect_network` keeps full records in the text channel because those records are the diagnostic output itself.
129
-
130
- All headless tools reuse one browser page, so the sharing page is opened only once per session instead of once per tool call. The headed login window uses an exclusive page and mode switches wait for in-flight calls to finish.
131
-
132
- ## Rate limiting and retries
133
-
134
- Every outbound request (sharing API, meta JSON, slice downloads) passes through a token bucket and a retry wrapper. `CODESIGN_API_RPS` (default `5`) and `CODESIGN_CDN_RPS` (default `10`) are per-host budgets; set either to `0` to disable throttling for that group. Only transient statuses (`408`, `429`, `500`, `502`, `503`, `504`) and network errors are retried, up to `CODESIGN_RETRY_MAX_ATTEMPTS` (default `3`) with exponential backoff and jitter. A `Retry-After` header is honored and capped by `CODESIGN_RETRY_AFTER_MAX_MS` (default `30000`). Exhausted retries return `RATE_LIMITED` with an actionable suggestion instead of a misleading `SHARING_NOT_FOUND`. `codesign_status` exposes the live bucket statistics under `rateLimit`.
135
-
136
- ## Data integrity
137
-
138
- Responses are validated before they enter the cache, so an incomplete spec, manifest, or sharing detail is never cached or returned as a successful result. `list_artboards`, `get_artboard_spec`, and `download_slice` include an `integrity` field with `ok`, `warnings`, and `stats`. Structural corruption (duplicate layer ids, malformed rects, empty layer trees, missing `screenshot.url`) fails the tool. Ambiguous findings, such as orphaned `parent_id`, are warnings by default; set `CODESIGN_INTEGRITY_STRICT=1` to make warnings fatal.
139
-
140
- Two CoDesign fields are deliberately **not** used as completeness checks, based on real sharing data: `slices_count` does not equal the number of manifest items, and slice `exportables[].size` is an export multiplier (for example `3`), not a byte count. `download_slice` only uses `screenshot.length` / `screenshot.size` as byte hints when they are present and positive; otherwise it verifies each file against HTTP `Content-Length` (when the response is not compressed), its MIME type, and its magic bytes. Each download result carries `verified` with the byte count, detected format, SHA-256, and encoding. Exportables without a URL are reported in `skipped` instead of being silently dropped, and `requested` / `downloaded` / `reused` counts are returned so partial results are visible.
141
-
142
- ## Security Notes
143
-
144
- Remote metadata and artifact downloads are restricted to `https://codesign.qq.com` and `https://cdn*.codesign.qq.com`. Unsupported hosts fail explicitly with `REMOTE_URL_NOT_ALLOWED`.
145
-
146
- `codesign_logout` only clears a profile directory inside the configured runtime directory. Unsafe profile paths fail explicitly with `PROFILE_DIR_UNSAFE`.
147
-
148
- ## Prompts
149
-
150
- - `implement_codesign_page`: Generate a structured design-to-code prompt for a full page or copied CoDesign selection. It guides an Agent to call `list_artboards`, `get_artboard_spec`, and `download_slice` in the intended order.
151
-
152
- ## Resources
153
-
154
- - `codesign://workflow/design-to-code`: Read-only workflow guidance for using the MCP tools without cropping preview screenshots or fabricating unavailable CoDesign data.
155
-
156
- For design-to-code work, prefer this flow:
157
-
158
- ```text
159
- list_artboards -> get_artboard_spec -> download_slice
160
- ```
161
-
162
- Preview screenshots are for visual comparison, not for production slicing.
163
-
164
- ## CoDesign AI Selection Userscript
165
-
166
- The optional userscript adds a **Copy for AI** button beside the currently selected CoDesign layer. It reads the active screen and layer identifiers already present in the CoDesign page, then copies a structured Agent prompt. It does not make network requests or copy the sharing password.
167
-
168
- 1. Install [Tampermonkey](https://www.tampermonkey.net/) or another compatible userscript manager.
169
- 2. Open [codesign-ai-selection.user.js](https://raw.githubusercontent.com/DOIT008/codesign-mcp/main/userscripts/codesign-ai-selection.user.js) and confirm installation.
170
- 3. Open a CoDesign sharing page in annotation mode and select a layer or group in the layer tree.
171
- 4. Click **Copy for AI** in the right inspector, then paste the copied prompt into an Agent that has `codesign-mcp-next` connected.
172
- 5. Provide the sharing password separately when the link requires one.
173
-
174
- The copied reference has this shape:
175
-
176
- ```json
177
- {
178
- "type": "codesign-selection",
179
- "sharingUrl": "https://codesign.qq.com/s/<PROJECT_ID>",
180
- "screenId": "<SCREEN_ID>",
181
- "screenName": "<SCREEN_NAME>",
182
- "layerObjectId": "<LAYER_OBJECT_ID>",
183
- "layerName": "<LAYER_NAME>",
184
- "selectionScope": "region"
185
- }
186
- ```
187
-
188
- `selectionScope=region` returns non-ancestor layers and groups fully contained by the selected bounds, which is useful when visually related elements are siblings rather than strict children. Use `subtree` when only the selected node's strict descendants are wanted. The script reports an explicit error if CoDesign changes the DOM selectors and the current selection cannot be read.
189
-
190
- ## Design-to-Code Workflow Prompt
191
-
192
- Before asking an Agent to implement a page from CoDesign, make sure the MCP server is connected. If the design requires login, call `codesign_status` first. When the profile does not exist or CoDesign is not logged in, call `codesign_login`, scan the QR code in the opened browser window, and then call `codesign_status` again to confirm the login state.
193
-
194
- Use this prompt template with your Agent. Replace `<PROJECT_ID>` with the value after `/s/` in the CoDesign sharing URL. If the sharing link does not require a password, remove the password line.
195
-
196
- ```text
197
- Implement this CoDesign design as a page:
198
-
199
- Link: https://codesign.qq.com/s/<PROJECT_ID>
200
- Password: <PASSWORD_IF_REQUIRED>
201
-
202
- Requirements:
203
- 1. First call the codesign-mcp-next list_artboards tool to get the artboard list.
204
- 2. Then call get_artboard_spec to get the official specification data. If the target platform is known, pass it as targetPlatform, for example `Android`, `iOS`, or `微信小程序`. If the user specifies custom platform settings, also pass targetUnit, customScale, customWidth, and remBasePx.
205
- 3. Unless the user explicitly requests an absolute/canvas-style implementation, translate the coordinates into semantic page structure and normal document flow before coding. Prefer Flexbox/Grid for page layout and avoid recreating the whole page as globally absolute-positioned layers. An explicit user layout requirement takes precedence over this default.
206
- 4. If the design contains designer-exported slice assets, prefer download_slice. Do not crop assets from the full-page preview image.
207
- 5. Use preview images only for visual comparison, not as production asset sources.
208
- 6. Implement the page in the target directory specified by the user; if none is given, ask before writing files.
209
- ```
210
-
211
- ## Development
212
-
213
- ```bash
214
- npm install
215
- npm test
216
- node scripts/stdio-smoke.mjs
217
- ```
218
-
219
- ## Publishing
220
-
221
- This package is intended to be published from GitHub Actions using npm Trusted Publishing. Do not store long-lived npm tokens in the repository.
222
-
223
- Before first publish:
224
-
225
- 1. Configure npm Trusted Publishing for this GitHub repository and `.github/workflows/publish.yml`.
226
- 2. Enable 2FA on npm for account and publishing protection.
227
- 3. Create and push a version tag matching `package.json`, for example `v0.3.2`, to trigger publication.
228
-
229
- ## License
230
-
231
- MIT
232
-
233
- ---
234
-
235
- ## 简体中文
236
-
237
- [English](#codesign-mcp-next) | 简体中文
238
-
239
- 面向腾讯 CoDesign 分享链接的本地 MCP 服务器。它为设计还原流程提供画板列表、官方图层标注、预览图以及设计师导出的切图资源。
240
-
241
- ## 功能
242
-
243
- - 通过 MCP 工具读取 CoDesign 分享链接。
244
- - 从分享链接获取设计稿和画板列表。
245
- - 获取官方 `meta_url` 标注数据,包括图层、分组、文字、颜色、CSS 和坐标。
246
- - 从官方切图清单下载设计师导出的切图资源。
247
- - 通过可选油猴脚本,把当前 CoDesign 画板和图层选区复制给 Agent。
248
- - 使用本地 Chromium profile 持久化扫码登录态。
249
- - 默认把运行数据写入调用方项目目录。
250
- - 提供可复用的 MCP 提示词和只读流程资源,服务于设计还原任务。
251
-
252
- ## 安装
253
-
254
- 直接通过 npm 运行:
255
-
256
- ```bash
257
- npx -y codesign-mcp-next
258
- ```
259
-
260
- 也可以全局安装:
261
-
262
- ```bash
263
- npm install -g codesign-mcp-next
264
- codesign-mcp-next
265
- ```
266
-
267
- ## MCP 配置
268
-
269
- 大多数 MCP 客户端可以直接使用零配置写法:
270
-
271
- ```json
272
- {
273
- "mcpServers": {
274
- "codesign-mcp-next": {
275
- "command": "npx",
276
- "args": ["-y", "codesign-mcp-next@latest"]
277
- }
278
- }
279
- }
280
- ```
281
-
282
- 服务器会按这个顺序判断工作区:
283
-
284
- 1. `CODESIGN_WORKSPACE_DIR`
285
- 2. `INIT_CWD`,通常是启动 `npx` 时所在的目录
286
- 3. `process.cwd()`
287
-
288
- 运行数据会写入识别到的工作区:
289
-
290
- ```text
291
- <workspace>/.codesign-mcp/profile
292
- <workspace>/.codesign-mcp/artifacts
293
- <workspace>/.codesign-mcp/codesign-mcp.log
294
- ```
295
-
296
- MCP 服务仅启动、列出工具或读取提示词/资源时,不会创建默认运行目录。
297
- 只有工具确实需要本地状态或产物时才会创建,例如 `codesign_login`、
298
- 需要浏览器访问 CoDesign 的读取工具,或下载资源工具。在此之前,默认文件
299
- 日志会写到 stderr。如果显式设置了 `CODESIGN_LOG_FILE`,服务会把它视为
300
- 明确的日志写入请求,并在写日志时创建对应父目录。
301
-
302
- 可以通过 `codesign_status` 工具查看 `workspaceRoot` 和 `workspaceRootSource`。如果 MCP 客户端从全局应用目录启动服务器,而不是从项目目录启动,可以显式设置其中一种:
303
-
304
- ```json
305
- {
306
- "mcpServers": {
307
- "codesign-mcp-next": {
308
- "command": "npx",
309
- "args": ["-y", "codesign-mcp-next@latest"],
310
- "cwd": "F:/your-project"
311
- }
312
- }
313
- }
314
- ```
315
-
316
- 或者:
317
-
318
- ```text
319
- CODESIGN_WORKSPACE_DIR=F:/your-project
320
- ```
321
-
322
- 可选环境变量:
323
-
324
- ```text
325
- CODESIGN_DATA_DIR=F:/your-project/.codesign-mcp
326
- CODESIGN_PROFILE_DIR=F:/your-project/.codesign-mcp/profile
327
- CODESIGN_ARTIFACTS_DIR=F:/your-project/.codesign-mcp/artifacts
328
- CODESIGN_LOG_FILE=F:/your-project/.codesign-mcp/codesign-mcp.log
329
- CODESIGN_IDLE_MS=600000
330
- CODESIGN_KEEP_BROWSER=1
331
- CODESIGN_CACHE_TTL_MS=60000
332
- CODESIGN_INTEGRITY_STRICT=0
333
- CODESIGN_RETRY_MAX_ATTEMPTS=3
334
- CODESIGN_RETRY_BASE_MS=300
335
- CODESIGN_API_RPS=5
336
- CODESIGN_CDN_RPS=10
337
- CODESIGN_LOG_LEVEL=info
338
- ```
339
-
340
- ## 工具
341
-
342
- - `codesign_status`:查看运行路径、浏览器状态和 profile 状态。
343
- - `codesign_login`:打开可见 Chromium 窗口,用于扫码登录 CoDesign。
344
- - `codesign_logout`:清理持久化 profile。
345
- - `list_artboards`:把 CoDesign 分享链接解析为设计稿和画板列表。
346
- - `get_artboard_spec`:读取官方 CoDesign `meta_url` 标注数据,包括图层、文字、颜色、CSS、分组和切图元数据。所有结果都包含按 `parent_id` 与 `layerIndex` 构建的 `spec.hierarchy` 层级树;每个节点带 `rect`(相对画板绝对坐标)、`relativeRect`(相对画板)和 `relativeToParent`(相对直接父节点),整页还原可直接映射为 DOM,无需自行拼树。传入 `layerObjectId` 后,`selectionScope` 可设为 `layer`、`subtree` 或 `region`;选区结果还包含扁平图层/分组、选区边界和相对坐标。可选的 `targetPlatform` 支持 `web`、`Android`、`安卓`、`iOS`、`微信小程序`、`mini program` 等自然语言平台名称,并返回平台转换后的标注信息。`targetUnit`、`customScale`、`customWidth`、`remBasePx` 对应 CoDesign 的平台设置。
347
- - `get_artboard_image`:获取预览图或封面图,主要用于视觉对比。
348
- - `download_slice`:从官方切图清单下载设计师导出的切图资源。
349
- - `debug_collect_network`:收集脱敏后的网络摘要,用于诊断。
350
-
351
- ## 缓存与工具输出
352
-
353
- 分享详情、标注 JSON 和切图清单会在内存中缓存 60 秒(`CODESIGN_CACHE_TTL_MS`,单位毫秒),相同资源的并发调用会合并为一次请求。设为 `0` 可关闭缓存。缓存只存在于当前进程,不会写入磁盘。
354
-
355
- 工具成功结果把完整数据放在 `structuredContent` 中,`content[0].text` 只返回简短摘要,避免同一份 JSON 在模型上下文里被计算两次。`get_artboard_spec` 的文本摘要还会带出两层 `hierarchy` 骨架、画板尺寸、图层/分组/切图数量以及完整性告警,因此不读取 `structuredContent` 的客户端也能拿到还原所需的结构信息。如果你的 MCP 客户端不读取 `structuredContent`,可以对任意数据工具(`list_artboards`、`get_artboard_spec`、`get_artboard_image`、`download_slice`)传 `includeFullText: true`,改为接收完整 JSON 文本。`debug_collect_network` 的诊断记录本身就是输出内容,因此仍在文本通道中保留完整记录。
356
-
357
- 所有 headless 工具复用同一个浏览器页面,分享页在每个会话中只打开一次,而不是每次工具调用都打开。登录用的 headed 窗口使用独占页面,模式切换会等待正在进行的调用结束。
358
-
359
- ## 限流与重试
360
-
361
- 所有出站请求(分享接口、标注 JSON、切图下载)都会先经过令牌桶节流再进入重试包装。`CODESIGN_API_RPS`(默认 `5`)和 `CODESIGN_CDN_RPS`(默认 `10`)是按 host 分组的配额,设为 `0` 可关闭对应分组的节流。只有瞬时状态码(`408`、`429`、`500`、`502`、`503`、`504`)和网络错误会重试,最多 `CODESIGN_RETRY_MAX_ATTEMPTS` 次(默认 `3`),采用指数退避加抖动。`Retry-After` 响应头会被遵守,上限由 `CODESIGN_RETRY_AFTER_MAX_MS` 控制(默认 `30000` 毫秒)。重试耗尽后返回 `RATE_LIMITED` 和可操作建议,而不再误报为 `SHARING_NOT_FOUND`。`codesign_status` 会在 `rateLimit` 字段中暴露实时的桶统计。
362
-
363
- ## 数据完整性
364
-
365
- 响应在进入缓存前会先经过校验,因此不完整的标注、切图清单或分享详情不会被缓存,也不会作为成功结果返回。`list_artboards`、`get_artboard_spec` 和 `download_slice` 会返回 `integrity` 字段,包含 `ok`、`warnings` 和 `stats`。结构性损坏(图层 object_id 重复、rect 非法、图层树为空、缺少 `screenshot.url`)会直接让工具失败;语义存疑的情况(例如 `parent_id` 悬空)默认只告警,设置 `CODESIGN_INTEGRITY_STRICT=1` 可让告警也升级为错误。
366
-
367
- 基于真实分享数据的实测,CoDesign 有两个字段**不能**用于完整性判断:`slices_count` 不等于切图清单条目数,`exportables[].size` 是导出倍率(例如 `3`)而不是字节数。`download_slice` 只有在 `screenshot.length` / `screenshot.size` 存在且为正数时才把它们当作字节数;否则通过 HTTP `Content-Length`(响应未压缩时)、MIME 类型和文件魔数校验每个文件。每个下载项都带 `verified` 字段,记录字节数、识别出的格式、SHA-256 和编码方式。没有 URL 的导出项会进入 `skipped` 而不是被静默丢弃,同时返回 `requested` / `downloaded` / `reused` 计数,让部分成功可见。
368
-
369
- ## 安全说明
370
-
371
- 远程标注数据和资源下载仅允许访问 `https://codesign.qq.com` 与 `https://cdn*.codesign.qq.com`。不支持的远程主机会明确返回 `REMOTE_URL_NOT_ALLOWED`。
372
-
373
- `codesign_logout` 只会清理位于运行目录内的 profile 目录。不安全的 profile 路径会明确返回 `PROFILE_DIR_UNSAFE`。
374
-
375
- ## 提示词
376
-
377
- - `implement_codesign_page`:为完整页面或复制的 CoDesign 选区生成结构化设计还原提示词,引导 Agent 按预期顺序调用 `list_artboards`、`get_artboard_spec` 和 `download_slice`。
378
-
379
- ## 资源
380
-
381
- - `codesign://workflow/design-to-code`:只读流程说明,用于指导 MCP 工具的正确组合方式,避免裁剪预览图或伪造不可用的 CoDesign 数据。
382
-
383
- 设计还原建议优先使用:
384
-
385
- ```text
386
- list_artboards -> get_artboard_spec -> download_slice
387
- ```
388
-
389
- 预览截图只适合做视觉对比,不应作为生产切图来源。
390
-
391
- ## CoDesign AI 选区油猴脚本
392
-
393
- 可选油猴脚本会在当前选中图层的右侧标注面板中增加“复制给 AI”按钮。它读取 CoDesign 页面中已有的当前画板 ID 和图层对象 ID,生成结构化 Agent 提示词;脚本不会请求额外接口,也不会复制分享访问码。
394
-
395
- 1. 安装 [Tampermonkey](https://www.tampermonkey.net/) 或其他兼容的用户脚本管理器。
396
- 2. 打开 [codesign-ai-selection.user.js](https://raw.githubusercontent.com/DOIT008/codesign-mcp/main/userscripts/codesign-ai-selection.user.js) 并确认安装。
397
- 3. 在 CoDesign 分享页进入标注模式,从左侧图层树选中一个图层或分组。
398
- 4. 点击右侧检查器中的“复制给 AI”,把提示词粘贴到已经连接 `codesign-mcp-next` 的 Agent。
399
- 5. 分享链接需要访问码时,单独向 Agent 提供访问码。
400
-
401
- 复制出的引用格式如下:
402
-
403
- ```json
404
- {
405
- "type": "codesign-selection",
406
- "sharingUrl": "https://codesign.qq.com/s/<PROJECT_ID>",
407
- "screenId": "<SCREEN_ID>",
408
- "screenName": "<SCREEN_NAME>",
409
- "layerObjectId": "<LAYER_OBJECT_ID>",
410
- "layerName": "<LAYER_NAME>",
411
- "selectionScope": "region"
412
- }
413
- ```
414
-
415
- `selectionScope=region` 会返回选中边界内完整包含的非祖先图层和分组,适合视觉上属于同一区域、但在设计文件中互为兄弟节点的情况。只需要选中节点的严格子层级时,可改用 `subtree`。如果 CoDesign 更新 DOM 结构导致无法读取当前选区,脚本会明确报错,不会复制不完整引用。
416
-
417
- ## 设计还原流程提示词
418
-
419
- 让 Agent 基于 CoDesign 实现页面前,请先确认 MCP 服务器已经连接。如果设计稿需要登录,先调用 `codesign_status`。当 profile 不存在或者 CoDesign 尚未登录时,调用 `codesign_login`,在打开的浏览器窗口中扫码登录,然后再次调用 `codesign_status` 确认登录状态。
420
-
421
- 可以把下面的模板发给 Agent。将 `<PROJECT_ID>` 替换为 CoDesign 分享链接中 `/s/` 后面的项目 ID。如果分享链接不需要密码,删除密码这一行。
422
-
423
- ```text
424
- 请基于这个 CoDesign 设计稿实现页面:
425
-
426
- 链接:https://codesign.qq.com/s/<PROJECT_ID>
427
- 密码:<PASSWORD_IF_REQUIRED>
428
-
429
- 要求:
430
- 1. 先调用 codesign-mcp-next 的 list_artboards 获取画板列表。
431
- 2. 再调用 get_artboard_spec 获取官方标注信息。如果已知目标开发平台,把它作为 targetPlatform 传入,例如 `Android`、`iOS` 或 `微信小程序`。如果用户指定了自定义平台设置,同时传入 targetUnit、customScale、customWidth 和 remBasePx。
432
- 3. 除非用户明确要求 absolute/canvas 式实现,否则先把坐标信息转译为语义化页面结构和正常文档流,再开始编码。页面布局优先使用 Flexbox/Grid,不要把整页还原成全局绝对定位图层;用户明确提出的布局要求优先于该默认策略。
433
- 4. 如果设计稿里存在设计师导出的切图资源,优先调用 download_slice 获取,不要从整页预览图里自行裁图。
434
- 5. 预览图只用于视觉对比,不作为生产切图来源。
435
- 6. 在用户指定的目标目录中实现页面;若用户未指定,先询问再写文件。
436
- ```
437
-
438
- ## 开发
439
-
440
- ```bash
441
- npm install
442
- npm test
443
- node scripts/stdio-smoke.mjs
444
- ```
445
-
446
- ## 发布
447
-
448
- 建议通过 GitHub Actions 的 npm Trusted Publishing 发布。不要在仓库里保存长期有效的 npm token。
449
-
450
- 首次发布前:
451
-
452
- 1. 在 npm 上为这个 GitHub 仓库和 `.github/workflows/publish.yml` 配置 Trusted Publishing。
453
- 2. 为 npm 账号开启 2FA。
454
- 3. 创建并推送与 `package.json` 一致的版本标签,例如 `v0.3.2`,触发发布。
455
-
456
- ## 许可证
457
-
458
- MIT
1
+ # codesign-mcp-next
2
+
3
+ [![npm version](https://img.shields.io/npm/v/codesign-mcp-next?label=npm&color=cb3837)](https://www.npmjs.com/package/codesign-mcp-next)
4
+ [![Node.js](https://img.shields.io/badge/node-%3E%3D20-339933?logo=node.js&logoColor=white)](https://nodejs.org/)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.9-3178c6?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
6
+ [![MCP](https://img.shields.io/badge/MCP-stdio-000000)](https://modelcontextprotocol.io/)
7
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
8
+
9
+ English | [简体中文](#简体中文)
10
+
11
+ Local MCP server for Tencent CoDesign sharing links. It exposes artboards, official layer specs, preview images, and designer-exported slices for design-to-code workflows.
12
+
13
+ ## Features
14
+
15
+ - Read CoDesign sharing links through MCP tools.
16
+ - List designs and artboards from a sharing URL.
17
+ - Fetch official `meta_url` specs with layers, groups, text, colors, CSS, and coordinates.
18
+ - Download designer-exported slice assets from the official slice manifest.
19
+ - Copy the current CoDesign screen and layer selection to an Agent with the optional Tampermonkey userscript.
20
+ - Keep login state in a persistent local Chromium profile.
21
+ - Store runtime files in the caller workspace by default.
22
+ - Provide a reusable MCP prompt and read-only workflow resource for design-to-code tasks.
23
+
24
+ ## Installation
25
+
26
+ Run directly with npm:
27
+
28
+ ```bash
29
+ npx -y codesign-mcp-next
30
+ ```
31
+
32
+ Or install globally:
33
+
34
+ ```bash
35
+ npm install -g codesign-mcp-next
36
+ codesign-mcp-next
37
+ ```
38
+
39
+ ## MCP Configuration
40
+
41
+ For most MCP clients, use the zero-config form:
42
+
43
+ ```json
44
+ {
45
+ "mcpServers": {
46
+ "codesign-mcp-next": {
47
+ "command": "npx",
48
+ "args": ["-y", "codesign-mcp-next@latest"]
49
+ }
50
+ }
51
+ }
52
+ ```
53
+
54
+ The server detects its workspace in this order:
55
+
56
+ 1. `CODESIGN_WORKSPACE_DIR`
57
+ 2. `INIT_CWD`, which is normally the directory where `npx` was launched
58
+ 3. `process.cwd()`
59
+
60
+ Runtime files are written under the detected workspace:
61
+
62
+ ```text
63
+ <workspace>/.codesign-mcp/profile
64
+ <workspace>/.codesign-mcp/artifacts
65
+ <workspace>/.codesign-mcp/codesign-mcp.log
66
+ ```
67
+
68
+ The default runtime directory is not created when the MCP server merely starts,
69
+ lists tools, or serves prompts/resources. It is created only when a tool needs
70
+ local state or artifacts, such as `codesign_login`, browser-backed CoDesign
71
+ reads, or downloads. Before that happens, default file logs go to stderr.
72
+ If `CODESIGN_LOG_FILE` is set explicitly, the server treats that path as an
73
+ explicit logging request and creates its parent directory when writing logs.
74
+
75
+ Use the `codesign_status` tool to inspect `workspaceRoot` and `workspaceRootSource`. If your MCP client launches servers from a global application directory instead of the project directory, set one of these explicitly:
76
+
77
+ ```json
78
+ {
79
+ "mcpServers": {
80
+ "codesign-mcp-next": {
81
+ "command": "npx",
82
+ "args": ["-y", "codesign-mcp-next@latest"],
83
+ "cwd": "F:/your-project"
84
+ }
85
+ }
86
+ }
87
+ ```
88
+
89
+ Or:
90
+
91
+ ```text
92
+ CODESIGN_WORKSPACE_DIR=F:/your-project
93
+ ```
94
+
95
+ Optional environment variables:
96
+
97
+ ```text
98
+ CODESIGN_DATA_DIR=F:/your-project/.codesign-mcp
99
+ CODESIGN_PROFILE_DIR=F:/your-project/.codesign-mcp/profile
100
+ CODESIGN_ARTIFACTS_DIR=F:/your-project/.codesign-mcp/artifacts
101
+ CODESIGN_LOG_FILE=F:/your-project/.codesign-mcp/codesign-mcp.log
102
+ CODESIGN_IDLE_MS=600000
103
+ CODESIGN_KEEP_BROWSER=1
104
+ CODESIGN_CACHE_TTL_MS=60000
105
+ CODESIGN_INTEGRITY_STRICT=0
106
+ CODESIGN_RETRY_MAX_ATTEMPTS=3
107
+ CODESIGN_RETRY_BASE_MS=300
108
+ CODESIGN_API_RPS=5
109
+ CODESIGN_CDN_RPS=10
110
+ CODESIGN_LOG_LEVEL=info
111
+ ```
112
+
113
+ ## Tools
114
+
115
+ - `codesign_status`: Show runtime paths, browser state, and profile state.
116
+ - `codesign_login`: Open a visible Chromium window for CoDesign QR-code login.
117
+ - `codesign_logout`: Clear the persisted profile.
118
+ - `list_artboards`: Resolve a CoDesign sharing URL into designs and artboards.
119
+ - `get_artboard_spec`: Fetch official CoDesign `meta_url` specs with layers, text, colors, CSS, groups, and slice metadata. Every response includes a `spec.hierarchy` tree built from `parent_id` and `layerIndex`; each node carries `rect` (artboard-absolute), `relativeRect` (relative to the artboard), and `relativeToParent` (relative to its direct parent) so a full-page layout can be mapped to DOM without rebuilding the tree yourself. With `layerObjectId`, `selectionScope` can be `layer`, `subtree`, or `region`; selected responses also include flat layers/groups, selection bounds, and relative coordinates. Optional `targetPlatform` accepts natural-language platform names such as `web`, `Android`, `安卓`, `iOS`, `微信小程序`, or `mini program` and returns platform-adjusted metadata. Optional `targetUnit`, `customScale`, `customWidth`, and `remBasePx` mirror CoDesign's platform settings.
120
+ - `get_artboard_image`: Fetch preview or cover images for visual comparison.
121
+ - `download_slice`: Download designer-exported slice assets from the official slice manifest.
122
+ - `debug_collect_network`: Collect a redacted network summary for diagnosis.
123
+
124
+ ## Caching and tool output
125
+
126
+ Sharing details, meta JSON, and slice manifests are cached in memory for 60 seconds (`CODESIGN_CACHE_TTL_MS`, in milliseconds). Concurrent calls for the same resource share a single request. Set the variable to `0` to disable caching. The cache is process-local and is never written to disk.
127
+
128
+ Successful tool results keep the complete payload in `structuredContent`. `content[0].text` contains only a short summary, so the same JSON is not counted twice in the model context. For `get_artboard_spec`, the text summary additionally includes a two-level `hierarchy` skeleton, artboard dimensions, layer/group/slice counts, and any integrity warnings, so clients that ignore `structuredContent` still receive the structural cues needed for design-to-code. If your MCP client does not read `structuredContent`, pass `includeFullText: true` to any data tool (`list_artboards`, `get_artboard_spec`, `get_artboard_image`, `download_slice`) to receive the full JSON as text instead. `debug_collect_network` keeps full records in the text channel because those records are the diagnostic output itself.
129
+
130
+ All headless tools reuse one browser page, so the sharing page is opened only once per session instead of once per tool call. The headed login window uses an exclusive page and mode switches wait for in-flight calls to finish.
131
+
132
+ ## Rate limiting and retries
133
+
134
+ Every outbound request (sharing API, meta JSON, slice downloads) passes through a token bucket and a retry wrapper. `CODESIGN_API_RPS` (default `5`) and `CODESIGN_CDN_RPS` (default `10`) are per-host budgets; set either to `0` to disable throttling for that group. Only transient statuses (`408`, `429`, `500`, `502`, `503`, `504`) and network errors are retried, up to `CODESIGN_RETRY_MAX_ATTEMPTS` (default `3`) with exponential backoff and jitter. A `Retry-After` header is honored and capped by `CODESIGN_RETRY_AFTER_MAX_MS` (default `30000`). Exhausted retries return `RATE_LIMITED` with an actionable suggestion instead of a misleading `SHARING_NOT_FOUND`. `codesign_status` exposes the live bucket statistics under `rateLimit`.
135
+
136
+ ## Data integrity
137
+
138
+ Responses are validated before they enter the cache, so an incomplete spec, manifest, or sharing detail is never cached or returned as a successful result. `list_artboards`, `get_artboard_spec`, and `download_slice` include an `integrity` field with `ok`, `warnings`, and `stats`. Structural corruption (duplicate layer ids, malformed rects, empty layer trees, missing `screenshot.url`) fails the tool. Ambiguous findings, such as orphaned `parent_id`, are warnings by default; set `CODESIGN_INTEGRITY_STRICT=1` to make warnings fatal.
139
+
140
+ Two CoDesign fields are deliberately **not** used as completeness checks, based on real sharing data: `slices_count` does not equal the number of manifest items, and slice `exportables[].size` is an export multiplier (for example `3`), not a byte count. `download_slice` only uses `screenshot.length` / `screenshot.size` as byte hints when they are present and positive; otherwise it verifies each file against HTTP `Content-Length` (when the response is not compressed), its MIME type, and its magic bytes. Each download result carries `verified` with the byte count, detected format, SHA-256, and encoding. Exportables without a URL are reported in `skipped` instead of being silently dropped, and `requested` / `downloaded` / `reused` counts are returned so partial results are visible.
141
+
142
+ ## Security Notes
143
+
144
+ Remote metadata and artifact downloads are restricted to `https://codesign.qq.com` and `https://cdn*.codesign.qq.com`. Unsupported hosts fail explicitly with `REMOTE_URL_NOT_ALLOWED`.
145
+
146
+ `codesign_logout` only clears a profile directory inside the configured runtime directory. Unsafe profile paths fail explicitly with `PROFILE_DIR_UNSAFE`.
147
+
148
+ ## Prompts
149
+
150
+ - `implement_codesign_page`: Generate a structured design-to-code prompt for a full page or copied CoDesign selection. It guides an Agent to call `list_artboards`, `get_artboard_spec`, and `download_slice` in the intended order.
151
+
152
+ ## Resources
153
+
154
+ - `codesign://workflow/design-to-code`: Read-only workflow guidance for using the MCP tools without cropping preview screenshots or fabricating unavailable CoDesign data.
155
+
156
+ For design-to-code work, prefer this flow:
157
+
158
+ ```text
159
+ list_artboards -> get_artboard_spec -> download_slice
160
+ ```
161
+
162
+ Preview screenshots are for visual comparison, not for production slicing.
163
+
164
+ ## CoDesign AI Selection Userscript
165
+
166
+ The optional userscript adds a **Copy for AI** button beside the currently selected CoDesign layer. It reads the active screen and layer identifiers already present in the CoDesign page, then copies a structured Agent prompt. It does not make network requests or copy the sharing password.
167
+
168
+ 1. Install [Tampermonkey](https://www.tampermonkey.net/) or another compatible userscript manager.
169
+ 2. Open [codesign-ai-selection.user.js](https://raw.githubusercontent.com/DOIT008/codesign-mcp/main/userscripts/codesign-ai-selection.user.js) and confirm installation.
170
+ 3. Open a CoDesign sharing page in annotation mode and select a layer or group in the layer tree.
171
+ 4. Click **Copy for AI** in the right inspector, then paste the copied prompt into an Agent that has `codesign-mcp-next` connected.
172
+ 5. Provide the sharing password separately when the link requires one.
173
+
174
+ The copied reference has this shape:
175
+
176
+ ```json
177
+ {
178
+ "type": "codesign-selection",
179
+ "sharingUrl": "https://codesign.qq.com/s/<PROJECT_ID>",
180
+ "screenId": "<SCREEN_ID>",
181
+ "screenName": "<SCREEN_NAME>",
182
+ "layerObjectId": "<LAYER_OBJECT_ID>",
183
+ "layerName": "<LAYER_NAME>",
184
+ "selectionScope": "region"
185
+ }
186
+ ```
187
+
188
+ `selectionScope=region` returns non-ancestor layers and groups fully contained by the selected bounds, which is useful when visually related elements are siblings rather than strict children. Use `subtree` when only the selected node's strict descendants are wanted. The script reports an explicit error if CoDesign changes the DOM selectors and the current selection cannot be read.
189
+
190
+ ## Design-to-Code Workflow Prompt
191
+
192
+ Before asking an Agent to implement a page from CoDesign, make sure the MCP server is connected. If the design requires login, call `codesign_status` first. When the profile does not exist or CoDesign is not logged in, call `codesign_login`, scan the QR code in the opened browser window, and then call `codesign_status` again to confirm the login state.
193
+
194
+ Use this prompt template with your Agent. Replace `<PROJECT_ID>` with the value after `/s/` in the CoDesign sharing URL. If the sharing link does not require a password, remove the password line.
195
+
196
+ ```text
197
+ Implement this CoDesign design as a page:
198
+
199
+ Link: https://codesign.qq.com/s/<PROJECT_ID>
200
+ Password: <PASSWORD_IF_REQUIRED>
201
+
202
+ Requirements:
203
+ 1. First call the codesign-mcp-next list_artboards tool to get the artboard list.
204
+ 2. Then call get_artboard_spec to get the official specification data. If the target platform is known, pass it as targetPlatform, for example `Android`, `iOS`, or `微信小程序`. If the user specifies custom platform settings, also pass targetUnit, customScale, customWidth, and remBasePx.
205
+ 3. Unless the user explicitly requests an absolute/canvas-style implementation, translate the coordinates into semantic page structure and normal document flow before coding. Prefer Flexbox/Grid for page layout and avoid recreating the whole page as globally absolute-positioned layers. An explicit user layout requirement takes precedence over this default.
206
+ 4. If the design contains designer-exported slice assets, prefer download_slice. Do not crop assets from the full-page preview image.
207
+ 5. Use preview images only for visual comparison, not as production asset sources.
208
+ 6. Implement the page in the target directory specified by the user; if none is given, ask before writing files.
209
+ ```
210
+
211
+ ## Development
212
+
213
+ ```bash
214
+ npm install
215
+ npm test
216
+ node scripts/stdio-smoke.mjs
217
+ ```
218
+
219
+ ## Publishing
220
+
221
+ This package is intended to be published from GitHub Actions using npm Trusted Publishing. Do not store long-lived npm tokens in the repository.
222
+
223
+ Before first publish:
224
+
225
+ 1. Configure npm Trusted Publishing for this GitHub repository and `.github/workflows/publish.yml`.
226
+ 2. Enable 2FA on npm for account and publishing protection.
227
+ 3. Create and push a version tag matching `package.json`, for example `v0.3.3`, to trigger publication.
228
+
229
+ ## License
230
+
231
+ MIT
232
+
233
+ ---
234
+
235
+ ## 简体中文
236
+
237
+ [English](#codesign-mcp-next) | 简体中文
238
+
239
+ 面向腾讯 CoDesign 分享链接的本地 MCP 服务器。它为设计还原流程提供画板列表、官方图层标注、预览图以及设计师导出的切图资源。
240
+
241
+ ## 功能
242
+
243
+ - 通过 MCP 工具读取 CoDesign 分享链接。
244
+ - 从分享链接获取设计稿和画板列表。
245
+ - 获取官方 `meta_url` 标注数据,包括图层、分组、文字、颜色、CSS 和坐标。
246
+ - 从官方切图清单下载设计师导出的切图资源。
247
+ - 通过可选油猴脚本,把当前 CoDesign 画板和图层选区复制给 Agent。
248
+ - 使用本地 Chromium profile 持久化扫码登录态。
249
+ - 默认把运行数据写入调用方项目目录。
250
+ - 提供可复用的 MCP 提示词和只读流程资源,服务于设计还原任务。
251
+
252
+ ## 安装
253
+
254
+ 直接通过 npm 运行:
255
+
256
+ ```bash
257
+ npx -y codesign-mcp-next
258
+ ```
259
+
260
+ 也可以全局安装:
261
+
262
+ ```bash
263
+ npm install -g codesign-mcp-next
264
+ codesign-mcp-next
265
+ ```
266
+
267
+ ## MCP 配置
268
+
269
+ 大多数 MCP 客户端可以直接使用零配置写法:
270
+
271
+ ```json
272
+ {
273
+ "mcpServers": {
274
+ "codesign-mcp-next": {
275
+ "command": "npx",
276
+ "args": ["-y", "codesign-mcp-next@latest"]
277
+ }
278
+ }
279
+ }
280
+ ```
281
+
282
+ 服务器会按这个顺序判断工作区:
283
+
284
+ 1. `CODESIGN_WORKSPACE_DIR`
285
+ 2. `INIT_CWD`,通常是启动 `npx` 时所在的目录
286
+ 3. `process.cwd()`
287
+
288
+ 运行数据会写入识别到的工作区:
289
+
290
+ ```text
291
+ <workspace>/.codesign-mcp/profile
292
+ <workspace>/.codesign-mcp/artifacts
293
+ <workspace>/.codesign-mcp/codesign-mcp.log
294
+ ```
295
+
296
+ MCP 服务仅启动、列出工具或读取提示词/资源时,不会创建默认运行目录。
297
+ 只有工具确实需要本地状态或产物时才会创建,例如 `codesign_login`、
298
+ 需要浏览器访问 CoDesign 的读取工具,或下载资源工具。在此之前,默认文件
299
+ 日志会写到 stderr。如果显式设置了 `CODESIGN_LOG_FILE`,服务会把它视为
300
+ 明确的日志写入请求,并在写日志时创建对应父目录。
301
+
302
+ 可以通过 `codesign_status` 工具查看 `workspaceRoot` 和 `workspaceRootSource`。如果 MCP 客户端从全局应用目录启动服务器,而不是从项目目录启动,可以显式设置其中一种:
303
+
304
+ ```json
305
+ {
306
+ "mcpServers": {
307
+ "codesign-mcp-next": {
308
+ "command": "npx",
309
+ "args": ["-y", "codesign-mcp-next@latest"],
310
+ "cwd": "F:/your-project"
311
+ }
312
+ }
313
+ }
314
+ ```
315
+
316
+ 或者:
317
+
318
+ ```text
319
+ CODESIGN_WORKSPACE_DIR=F:/your-project
320
+ ```
321
+
322
+ 可选环境变量:
323
+
324
+ ```text
325
+ CODESIGN_DATA_DIR=F:/your-project/.codesign-mcp
326
+ CODESIGN_PROFILE_DIR=F:/your-project/.codesign-mcp/profile
327
+ CODESIGN_ARTIFACTS_DIR=F:/your-project/.codesign-mcp/artifacts
328
+ CODESIGN_LOG_FILE=F:/your-project/.codesign-mcp/codesign-mcp.log
329
+ CODESIGN_IDLE_MS=600000
330
+ CODESIGN_KEEP_BROWSER=1
331
+ CODESIGN_CACHE_TTL_MS=60000
332
+ CODESIGN_INTEGRITY_STRICT=0
333
+ CODESIGN_RETRY_MAX_ATTEMPTS=3
334
+ CODESIGN_RETRY_BASE_MS=300
335
+ CODESIGN_API_RPS=5
336
+ CODESIGN_CDN_RPS=10
337
+ CODESIGN_LOG_LEVEL=info
338
+ ```
339
+
340
+ ## 工具
341
+
342
+ - `codesign_status`:查看运行路径、浏览器状态和 profile 状态。
343
+ - `codesign_login`:打开可见 Chromium 窗口,用于扫码登录 CoDesign。
344
+ - `codesign_logout`:清理持久化 profile。
345
+ - `list_artboards`:把 CoDesign 分享链接解析为设计稿和画板列表。
346
+ - `get_artboard_spec`:读取官方 CoDesign `meta_url` 标注数据,包括图层、文字、颜色、CSS、分组和切图元数据。所有结果都包含按 `parent_id` 与 `layerIndex` 构建的 `spec.hierarchy` 层级树;每个节点带 `rect`(相对画板绝对坐标)、`relativeRect`(相对画板)和 `relativeToParent`(相对直接父节点),整页还原可直接映射为 DOM,无需自行拼树。传入 `layerObjectId` 后,`selectionScope` 可设为 `layer`、`subtree` 或 `region`;选区结果还包含扁平图层/分组、选区边界和相对坐标。可选的 `targetPlatform` 支持 `web`、`Android`、`安卓`、`iOS`、`微信小程序`、`mini program` 等自然语言平台名称,并返回平台转换后的标注信息。`targetUnit`、`customScale`、`customWidth`、`remBasePx` 对应 CoDesign 的平台设置。
347
+ - `get_artboard_image`:获取预览图或封面图,主要用于视觉对比。
348
+ - `download_slice`:从官方切图清单下载设计师导出的切图资源。
349
+ - `debug_collect_network`:收集脱敏后的网络摘要,用于诊断。
350
+
351
+ ## 缓存与工具输出
352
+
353
+ 分享详情、标注 JSON 和切图清单会在内存中缓存 60 秒(`CODESIGN_CACHE_TTL_MS`,单位毫秒),相同资源的并发调用会合并为一次请求。设为 `0` 可关闭缓存。缓存只存在于当前进程,不会写入磁盘。
354
+
355
+ 工具成功结果把完整数据放在 `structuredContent` 中,`content[0].text` 只返回简短摘要,避免同一份 JSON 在模型上下文里被计算两次。`get_artboard_spec` 的文本摘要还会带出两层 `hierarchy` 骨架、画板尺寸、图层/分组/切图数量以及完整性告警,因此不读取 `structuredContent` 的客户端也能拿到还原所需的结构信息。如果你的 MCP 客户端不读取 `structuredContent`,可以对任意数据工具(`list_artboards`、`get_artboard_spec`、`get_artboard_image`、`download_slice`)传 `includeFullText: true`,改为接收完整 JSON 文本。`debug_collect_network` 的诊断记录本身就是输出内容,因此仍在文本通道中保留完整记录。
356
+
357
+ 所有 headless 工具复用同一个浏览器页面,分享页在每个会话中只打开一次,而不是每次工具调用都打开。登录用的 headed 窗口使用独占页面,模式切换会等待正在进行的调用结束。
358
+
359
+ ## 限流与重试
360
+
361
+ 所有出站请求(分享接口、标注 JSON、切图下载)都会先经过令牌桶节流再进入重试包装。`CODESIGN_API_RPS`(默认 `5`)和 `CODESIGN_CDN_RPS`(默认 `10`)是按 host 分组的配额,设为 `0` 可关闭对应分组的节流。只有瞬时状态码(`408`、`429`、`500`、`502`、`503`、`504`)和网络错误会重试,最多 `CODESIGN_RETRY_MAX_ATTEMPTS` 次(默认 `3`),采用指数退避加抖动。`Retry-After` 响应头会被遵守,上限由 `CODESIGN_RETRY_AFTER_MAX_MS` 控制(默认 `30000` 毫秒)。重试耗尽后返回 `RATE_LIMITED` 和可操作建议,而不再误报为 `SHARING_NOT_FOUND`。`codesign_status` 会在 `rateLimit` 字段中暴露实时的桶统计。
362
+
363
+ ## 数据完整性
364
+
365
+ 响应在进入缓存前会先经过校验,因此不完整的标注、切图清单或分享详情不会被缓存,也不会作为成功结果返回。`list_artboards`、`get_artboard_spec` 和 `download_slice` 会返回 `integrity` 字段,包含 `ok`、`warnings` 和 `stats`。结构性损坏(图层 object_id 重复、rect 非法、图层树为空、缺少 `screenshot.url`)会直接让工具失败;语义存疑的情况(例如 `parent_id` 悬空)默认只告警,设置 `CODESIGN_INTEGRITY_STRICT=1` 可让告警也升级为错误。
366
+
367
+ 基于真实分享数据的实测,CoDesign 有两个字段**不能**用于完整性判断:`slices_count` 不等于切图清单条目数,`exportables[].size` 是导出倍率(例如 `3`)而不是字节数。`download_slice` 只有在 `screenshot.length` / `screenshot.size` 存在且为正数时才把它们当作字节数;否则通过 HTTP `Content-Length`(响应未压缩时)、MIME 类型和文件魔数校验每个文件。每个下载项都带 `verified` 字段,记录字节数、识别出的格式、SHA-256 和编码方式。没有 URL 的导出项会进入 `skipped` 而不是被静默丢弃,同时返回 `requested` / `downloaded` / `reused` 计数,让部分成功可见。
368
+
369
+ ## 安全说明
370
+
371
+ 远程标注数据和资源下载仅允许访问 `https://codesign.qq.com` 与 `https://cdn*.codesign.qq.com`。不支持的远程主机会明确返回 `REMOTE_URL_NOT_ALLOWED`。
372
+
373
+ `codesign_logout` 只会清理位于运行目录内的 profile 目录。不安全的 profile 路径会明确返回 `PROFILE_DIR_UNSAFE`。
374
+
375
+ ## 提示词
376
+
377
+ - `implement_codesign_page`:为完整页面或复制的 CoDesign 选区生成结构化设计还原提示词,引导 Agent 按预期顺序调用 `list_artboards`、`get_artboard_spec` 和 `download_slice`。
378
+
379
+ ## 资源
380
+
381
+ - `codesign://workflow/design-to-code`:只读流程说明,用于指导 MCP 工具的正确组合方式,避免裁剪预览图或伪造不可用的 CoDesign 数据。
382
+
383
+ 设计还原建议优先使用:
384
+
385
+ ```text
386
+ list_artboards -> get_artboard_spec -> download_slice
387
+ ```
388
+
389
+ 预览截图只适合做视觉对比,不应作为生产切图来源。
390
+
391
+ ## CoDesign AI 选区油猴脚本
392
+
393
+ 可选油猴脚本会在当前选中图层的右侧标注面板中增加“复制给 AI”按钮。它读取 CoDesign 页面中已有的当前画板 ID 和图层对象 ID,生成结构化 Agent 提示词;脚本不会请求额外接口,也不会复制分享访问码。
394
+
395
+ 1. 安装 [Tampermonkey](https://www.tampermonkey.net/) 或其他兼容的用户脚本管理器。
396
+ 2. 打开 [codesign-ai-selection.user.js](https://raw.githubusercontent.com/DOIT008/codesign-mcp/main/userscripts/codesign-ai-selection.user.js) 并确认安装。
397
+ 3. 在 CoDesign 分享页进入标注模式,从左侧图层树选中一个图层或分组。
398
+ 4. 点击右侧检查器中的“复制给 AI”,把提示词粘贴到已经连接 `codesign-mcp-next` 的 Agent。
399
+ 5. 分享链接需要访问码时,单独向 Agent 提供访问码。
400
+
401
+ 复制出的引用格式如下:
402
+
403
+ ```json
404
+ {
405
+ "type": "codesign-selection",
406
+ "sharingUrl": "https://codesign.qq.com/s/<PROJECT_ID>",
407
+ "screenId": "<SCREEN_ID>",
408
+ "screenName": "<SCREEN_NAME>",
409
+ "layerObjectId": "<LAYER_OBJECT_ID>",
410
+ "layerName": "<LAYER_NAME>",
411
+ "selectionScope": "region"
412
+ }
413
+ ```
414
+
415
+ `selectionScope=region` 会返回选中边界内完整包含的非祖先图层和分组,适合视觉上属于同一区域、但在设计文件中互为兄弟节点的情况。只需要选中节点的严格子层级时,可改用 `subtree`。如果 CoDesign 更新 DOM 结构导致无法读取当前选区,脚本会明确报错,不会复制不完整引用。
416
+
417
+ ## 设计还原流程提示词
418
+
419
+ 让 Agent 基于 CoDesign 实现页面前,请先确认 MCP 服务器已经连接。如果设计稿需要登录,先调用 `codesign_status`。当 profile 不存在或者 CoDesign 尚未登录时,调用 `codesign_login`,在打开的浏览器窗口中扫码登录,然后再次调用 `codesign_status` 确认登录状态。
420
+
421
+ 可以把下面的模板发给 Agent。将 `<PROJECT_ID>` 替换为 CoDesign 分享链接中 `/s/` 后面的项目 ID。如果分享链接不需要密码,删除密码这一行。
422
+
423
+ ```text
424
+ 请基于这个 CoDesign 设计稿实现页面:
425
+
426
+ 链接:https://codesign.qq.com/s/<PROJECT_ID>
427
+ 密码:<PASSWORD_IF_REQUIRED>
428
+
429
+ 要求:
430
+ 1. 先调用 codesign-mcp-next 的 list_artboards 获取画板列表。
431
+ 2. 再调用 get_artboard_spec 获取官方标注信息。如果已知目标开发平台,把它作为 targetPlatform 传入,例如 `Android`、`iOS` 或 `微信小程序`。如果用户指定了自定义平台设置,同时传入 targetUnit、customScale、customWidth 和 remBasePx。
432
+ 3. 除非用户明确要求 absolute/canvas 式实现,否则先把坐标信息转译为语义化页面结构和正常文档流,再开始编码。页面布局优先使用 Flexbox/Grid,不要把整页还原成全局绝对定位图层;用户明确提出的布局要求优先于该默认策略。
433
+ 4. 如果设计稿里存在设计师导出的切图资源,优先调用 download_slice 获取,不要从整页预览图里自行裁图。
434
+ 5. 预览图只用于视觉对比,不作为生产切图来源。
435
+ 6. 在用户指定的目标目录中实现页面;若用户未指定,先询问再写文件。
436
+ ```
437
+
438
+ ## 开发
439
+
440
+ ```bash
441
+ npm install
442
+ npm test
443
+ node scripts/stdio-smoke.mjs
444
+ ```
445
+
446
+ ## 发布
447
+
448
+ 建议通过 GitHub Actions 的 npm Trusted Publishing 发布。不要在仓库里保存长期有效的 npm token。
449
+
450
+ 首次发布前:
451
+
452
+ 1. 在 npm 上为这个 GitHub 仓库和 `.github/workflows/publish.yml` 配置 Trusted Publishing。
453
+ 2. 为 npm 账号开启 2FA。
454
+ 3. 创建并推送与 `package.json` 一致的版本标签,例如 `v0.3.3`,触发发布。
455
+
456
+ ## 许可证
457
+
458
+ MIT