@zerolingua/mcp 0.1.0 → 0.3.0

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.
Files changed (4) hide show
  1. package/README.md +85 -13
  2. package/index.js +22 -11
  3. package/package.json +8 -5
  4. package/tools.js +293 -52
package/README.md CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  Connect an AI agent (Claude Code, Claude Desktop, any MCP host) to a WordPress
4
4
  site running **Zerø Lingua**: the agent reads a page's strings, writes
5
- translations, checks what's pending and approves — the same operations the
6
- panel does, through the same server-side authorization.
5
+ translations, checks what's pending, translates and approves whole sets — the
6
+ same operations the panel does, through the same server-side authorization.
7
7
 
8
8
  **MCP is a Zerø Lingua Pro feature.** The site needs an active license; without
9
9
  one every call answers `403 license_required`. The free manual tooling is
@@ -11,6 +11,8 @@ unaffected.
11
11
 
12
12
  ## Setup
13
13
 
14
+ The server needs **Node.js 20** or later, as the MCP SDK v2 it runs on does.
15
+
14
16
  1. **Create an Application Password** on the site: wp-admin → Users → Profile →
15
17
  Application Passwords (WordPress core, needs HTTPS in production). The agent
16
18
  inherits exactly that user's capabilities — a translator's password
@@ -47,21 +49,91 @@ unaffected.
47
49
 
48
50
  ## Tools
49
51
 
50
- | tool | what it does |
51
- |---------------------|-----------------------------------------------------------|
52
- | `site_status` | source + target languages, per-language live flag |
53
- | `pending_overview` | per-language counts: what's translated / pending / missing|
54
- | `list_items` | one content type's items with per-language status |
55
- | `item_strings` | an item's strings: hash, source, translations, review meta|
56
- | `save_translations` | write agent-produced translations (drafts, by hash) |
57
- | `approve_item` | sign off a post + language (the review gate) |
58
- | `translate_batch` | translate with the site's own configured AI engine |
52
+ | tool | what it does |
53
+ |---------------------|---------------------------------------------------------------|
54
+ | `site_status` | source + target languages, per-language live flag |
55
+ | `pending_overview` | per-language counts: what's translated / pending / missing |
56
+ | `list_items` | one content type's items with per-language status |
57
+ | `item_strings` | an item's strings: hash, source, translations, review meta |
58
+ | `save_translations` | write agent-produced translations (drafts, by hash) |
59
+ | `approve_item` | sign off one post + language (the review gate) |
60
+ | `translate_set` | translate a set with the site's own AI engine (dry run first) |
61
+ | `approve_set` | sign off a set, chunk by chunk, by cursor |
62
+ | `queue_status` | follow the queued jobs `translate_set` started |
63
+ | `translate_batch` | **deprecated** — use `translate_set`; still works as before |
64
+
65
+ `translate_set`, `approve_set` and `queue_status` need **Zerø Lingua 0.8.0** or
66
+ later on the site; against an older plugin they answer `404 rest_no_route`, and
67
+ the other seven keep working.
68
+
69
+ ## Working with sets
70
+
71
+ The agent never lists or splits a set itself: it **describes** it and the
72
+ server resolves it, checks rights per item and reports what it did not do.
73
+
74
+ ```json
75
+ {
76
+ "selector": {
77
+ "scope": { "type": "page" },
78
+ "langs": ["fr"],
79
+ "status": ["needs_review"]
80
+ },
81
+ "dry_run": true
82
+ }
83
+ ```
84
+
85
+ - **The set:** `scope` (`{ "type": … }` — any type `list_items` takes —,
86
+ `{ "document": <post id> }` or `{ "site_identity": true }`) **or** `targets`
87
+ (`[{ "id", "kind" }]`, at most 500). `langs` is required and never empty.
88
+ Optional filters: `status` (`translated`, `needs_review`, `in_progress`,
89
+ `not_translated`; aliases `done`, `todo`), `search`, `kinds` (strings only),
90
+ `taxonomies` (terms only).
91
+ - **Dry run first.** The same call with `dry_run: true` answers the number
92
+ (strings to pay for and the month's `budget`, or rows to approve) and writes
93
+ nothing.
94
+ - **Translating** runs in the request for one document or up to 50 targets, and
95
+ for any re-translation with `force` except a whole type
96
+ (`mode: "request"`: repeat with the returned `cursor` until `done`) and is
97
+ **queued** for anything bigger (`mode: "queue"`: one job per language, followed
98
+ with `queue_status`). The server decides; the answer says which.
99
+ - **Approving** goes chunk by chunk: repeat with the same selector and the
100
+ returned `cursor` until `done: true`.
101
+ - **Refusals** come by identity in `refused[]` (`id`, `kind`, `lang`, `reason`,
102
+ `message`, plus `in` for a string of a unit and `slot` for a field's own
103
+ translation). Something that was already done is not a refusal.
104
+
105
+ ## Protocol
106
+
107
+ Speaks MCP revision **2026-07-28** (the stateless core) via SDK v2, and still
108
+ serves hosts that open with the 2025-era `initialize` handshake — the same tool
109
+ map backs both, so no host is left behind while the ecosystem moves.
59
110
 
60
111
  ## Safety model
61
112
 
62
113
  Agent writes land as **drafts behind the review gate** — nothing serves
63
114
  publicly until a human (or an agent with publish rights on that content)
64
- approves it. Placeholders wrapped in ⟦ ⟧ are guarded server-side: an entry that
65
- drops one is skipped, never saved broken. All authorization is the server's:
115
+ approves it. Placeholders wrapped in ⟦ ⟧ and inline markup (a link, a bold) are
116
+ guarded server-side: an entry that drops, adds or repoints one is skipped, never
117
+ saved broken. All authorization is the server's:
66
118
  this process holds zero domain logic and zero secrets — it is one HTTP call per
67
119
  tool, authenticated with the user's own Application Password.
120
+
121
+ ## Versions
122
+
123
+ - **0.3.0** — the selector tools: `translate_set`, `approve_set` and
124
+ `queue_status` (they need Zerø Lingua 0.8.0). `translate_batch` is deprecated
125
+ in its description and keeps working. The descriptions are rewritten:
126
+ `approve_item` and `approve_set` name what they refused in `refused[]`,
127
+ `save_translations` states the inline-markup rule, `pending_overview` reads its
128
+ `words`, and `item_strings` / `translate_batch` accept `term`, `menu` and
129
+ `form`. A result that needs a next step (a `cursor` to follow, a queued job)
130
+ says so, and a site without the new routes gets an honest message.
131
+ - **0.2.0** — MCP SDK v2: protocol revision 2026-07-28 plus the 2025 era.
132
+ - **0.1.0** — the first seven tools.
133
+
134
+ ## Development
135
+
136
+ ```sh
137
+ npm ci
138
+ npm test # node --test: routes exist in McpRoutes.php, the selector speaks the contract
139
+ ```
package/index.js CHANGED
@@ -6,10 +6,14 @@
6
6
  * every tool is one HTTP call, authenticated with the user's own WordPress
7
7
  * Application Password. All validation, authorization and the review gate live
8
8
  * server-side; see tools.js for the tool map and README.md for setup.
9
+ *
10
+ * Speaks the 2026-07-28 protocol revision through SDK v2 — and, because
11
+ * serveStdio defaults to `legacy: 'serve'`, still speaks the 2025 era to hosts
12
+ * that open with `initialize`. One tool map, both eras, no branch of ours.
9
13
  */
10
14
 
11
- import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
12
- import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
15
+ import { McpServer } from '@modelcontextprotocol/server';
16
+ import { serveStdio } from '@modelcontextprotocol/server/stdio';
13
17
  import { TOOLS, callTool } from './tools.js';
14
18
 
15
19
  const env = {
@@ -27,14 +31,21 @@ if ( missing.length ) {
27
31
  process.exit( 1 );
28
32
  }
29
33
 
30
- const server = new McpServer( { name: 'zero-lingua', version: '0.1.0' } );
31
-
32
- for ( const tool of TOOLS ) {
33
- server.registerTool(
34
- tool.name,
35
- { title: tool.title, description: tool.description, inputSchema: tool.input },
36
- async ( args ) => callTool( tool, args ?? {}, env )
37
- );
34
+ /**
35
+ * One server per connection, built on demand. The factory is what lets a
36
+ * single definition serve both protocol eras: serveStdio decides the era from
37
+ * the opening exchange and pins one instance to it.
38
+ */
39
+ function buildServer() {
40
+ const server = new McpServer( { name: 'zero-lingua', version: '0.3.0' } );
41
+ for ( const tool of TOOLS ) {
42
+ server.registerTool(
43
+ tool.name,
44
+ { title: tool.title, description: tool.description, inputSchema: tool.input },
45
+ async ( args ) => callTool( tool, args ?? {}, env )
46
+ );
47
+ }
48
+ return server;
38
49
  }
39
50
 
40
- await server.connect( new StdioServerTransport() );
51
+ serveStdio( () => buildServer() );
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@zerolingua/mcp",
3
- "version": "0.1.0",
4
- "description": "MCP server for Zerø Lingua — lets AI agents read a site's strings, write translations, and approve pending work through the plugin's licensed REST surface.",
3
+ "version": "0.3.0",
4
+ "description": "MCP server for Zerø Lingua — lets AI agents read a site's strings, write translations, and approve pending work through the plugin's licensed REST surface. Speaks MCP 2026-07-28 and the 2025 era.",
5
5
  "type": "module",
6
+ "scripts": {
7
+ "test": "node --test tools.test.mjs"
8
+ },
6
9
  "bin": {
7
10
  "zero-lingua-mcp": "index.js"
8
11
  },
@@ -12,7 +15,7 @@
12
15
  "README.md"
13
16
  ],
14
17
  "engines": {
15
- "node": ">=18"
18
+ "node": ">=20"
16
19
  },
17
20
  "keywords": [
18
21
  "mcp",
@@ -28,8 +31,8 @@
28
31
  },
29
32
  "homepage": "https://zerolingua.studio",
30
33
  "dependencies": {
31
- "@modelcontextprotocol/sdk": "^1.0.0",
32
- "zod": "^3.25.0"
34
+ "@modelcontextprotocol/server": "^2.0.0",
35
+ "zod": "^4.4.3"
33
36
  },
34
37
  "license": "GPL-2.0-or-later"
35
38
  }
package/tools.js CHANGED
@@ -1,10 +1,11 @@
1
1
  /**
2
- * The seven Zerø Lingua tools: their schemas, their teaching descriptions, and
2
+ * The ten Zerø Lingua tools: their schemas, their teaching descriptions, and
3
3
  * the one HTTP call each of them is.
4
4
  *
5
5
  * This process holds ZERO domain logic on purpose. Placeholder guarding, the
6
- * per-hash three-tier authorization, the review gate and the license gate all
7
- * live server-side (src/RestApi/McpRoutes.php) and hold for any client — so the
6
+ * per-hash three-tier authorization, the review gate, the license gate and the
7
+ * resolution of a set (docs/SELECTOR-CONTRACT.md) all live server-side
8
+ * (src/RestApi/McpRoutes.php, src/Selector/) and hold for any client — so the
8
9
  * only jobs here are auth, tool descriptions and honest error mapping. The
9
10
  * descriptions carry the workflow because the agent reads nothing else.
10
11
  *
@@ -17,8 +18,84 @@ import { z } from 'zod';
17
18
 
18
19
  const WORKFLOW =
19
20
  'Typical session: site_status (languages) → pending_overview (what is left) → ' +
20
- 'list_items / item_strings (read) → save_translations or translate_batch (write drafts) → ' +
21
- 'approve_item (sign off).';
21
+ 'list_items / item_strings (read) → save_translations (your own text) or translate_set (the ' +
22
+ "site's AI engine: dry_run first) → approve_item (one page) or approve_set (a whole set) → " +
23
+ 'queue_status (follow queued work).';
24
+
25
+ /** The contract's status words and their two aliases (SELECTOR-CONTRACT.md § Enums). */
26
+ const STATUS = [ 'translated', 'needs_review', 'in_progress', 'not_translated', 'done', 'todo' ];
27
+
28
+ /** What a unit is (SELECTOR-CONTRACT.md § El selector, `targets[].kind`). */
29
+ const UNIT_KINDS = [ 'post', 'component', 'term', 'menu', 'form', 'loop', 'string' ];
30
+
31
+ /**
32
+ * THE SELECTOR, as the wire carries it: a description of a set, never a list the
33
+ * client worked out. Validated here only for shape; every rule that is a refusal
34
+ * (exactly one of scope/targets, a filter outside its scope, an unknown language)
35
+ * is the server's, and comes back as a 400 with its `zl_selector_*` code.
36
+ */
37
+ const SELECTOR = z
38
+ .object( {
39
+ scope: z
40
+ .object( {
41
+ type: z
42
+ .string()
43
+ .optional()
44
+ .describe(
45
+ 'a type list_items takes: page | post | wp_template | wp_block | a public post-type slug | ' +
46
+ 'terms | menus | forms | json-loops | strings'
47
+ ),
48
+ document: z
49
+ .number()
50
+ .int()
51
+ .min( 1 )
52
+ .optional()
53
+ .describe( 'one post id: that post, the components it uses and the JSON loops it renders' ),
54
+ site_identity: z.literal( true ).optional().describe( 'the site name and tagline' ),
55
+ } )
56
+ .optional()
57
+ .describe( 'the set by where it lives — exactly ONE key. Send `scope` OR `targets`, never both.' ),
58
+ targets: z
59
+ .array(
60
+ z.object( {
61
+ id: z
62
+ .union( [ z.string(), z.number().int() ] )
63
+ .describe( 'numeric id; a loop\'s key for `loop`; the 32-hex hash for `string`' ),
64
+ kind: z.enum( UNIT_KINDS ),
65
+ } )
66
+ )
67
+ .min( 1 )
68
+ .max( 500 )
69
+ .optional()
70
+ .describe( 'the set by name: the { id, kind } of items from list_items, or strings by hash. Max 500.' ),
71
+ langs: z
72
+ .array( z.string() )
73
+ .min( 1 )
74
+ .describe( 'REQUIRED target language codes from site_status. Never empty: empty is an error, never «all».' ),
75
+ status: z
76
+ .array( z.enum( STATUS ) )
77
+ .optional()
78
+ .describe(
79
+ 'keep only (item, language) cells in these states; omitted = no filter. `done` = translated, ' +
80
+ '`todo` = the other three.'
81
+ ),
82
+ search: z.string().optional().describe( 'only with `scope.type`: the text filter list_items applies to that type' ),
83
+ kinds: z
84
+ .array( z.string() )
85
+ .optional()
86
+ .describe( 'only with `scope.type: "strings"`: the keys of `kinds` in list_items\' answer for that type' ),
87
+ taxonomies: z
88
+ .array( z.string() )
89
+ .optional()
90
+ .describe( 'only with `scope.type: "terms"`: the keys of `taxonomies` in list_items\' answer for that type' ),
91
+ } )
92
+ .describe( 'the set: `scope` or `targets`, plus `langs` and optional filters' );
93
+
94
+ const DRY_RUN = z.boolean().optional().describe( 'true = count and report only, write nothing. Always send it first.' );
95
+ const CURSOR = z
96
+ .string()
97
+ .optional()
98
+ .describe( 'the `cursor` of the previous answer of this run; send it with the SAME selector' );
22
99
 
23
100
  export const TOOLS = [
24
101
  {
@@ -31,7 +108,7 @@ export const TOOLS = [
31
108
  WORKFLOW,
32
109
  method: 'GET',
33
110
  route: 'site-status',
34
- input: {},
111
+ input: z.object( {} ),
35
112
  },
36
113
  {
37
114
  name: 'pending_overview',
@@ -39,29 +116,37 @@ export const TOOLS = [
39
116
  description:
40
117
  "Per-language progress across the site: string counts (total / translated / approved) and item " +
41
118
  'counts by status (`translated`, `needs_review` = translated but not signed off, `partial`, ' +
42
- '`untranslated`). Serving is approved-only, so `needs_review` work is NOT live yet. The scan is ' +
43
- 'bounded: when `scanned.complete` is false, drill into list_items per type for the rest. ' +
44
- "Use this to answer “what's left to do, where?”.",
119
+ '`untranslated`). Each language also carries `words`: the same items counted in the panel\'s ' +
120
+ 'vocabulary — `translated`, `needs_review`, `in_progress`, `not_translated` — which are the words a ' +
121
+ "selector's `status` takes in translate_set and approve_set. Serving is approved-only, so " +
122
+ '`needs_review` work is NOT live yet. The scan is bounded: when `scanned.complete` is false, drill ' +
123
+ "into list_items per type for the rest. Use this to answer “what's left to do, where?”.",
45
124
  method: 'GET',
46
125
  route: 'pending-overview',
47
- input: {},
126
+ input: z.object( {} ),
48
127
  },
49
128
  {
50
129
  name: 'list_items',
51
130
  title: 'List translatable items',
52
131
  description:
53
132
  'One content type\'s items with per-language status. `type` is `page`, `wp_template` (templates), ' +
54
- '`wp_block` (components), `json-loops`, `strings` (the orphan pool: menu labels, taxonomy terms, ' +
55
- 'site identity), or any public post-type slug (`post`, custom types). Paginate with offset/limit; ' +
56
- '`search` filters by title. Each item carries `id` and `kind` — pass those to item_strings.',
133
+ '`wp_block` (components), `terms`, `menus`, `forms`, `json-loops`, `strings` (the pool of ' +
134
+ 'site-wide strings no other item holds), or any public post-type slug (`post`, custom types). ' +
135
+ 'Paginate with offset/limit; `search` filters by text. Each item carries `id` and `kind` — pass ' +
136
+ 'those to item_strings, or as a selector\'s `targets`. The same `type` words are a selector\'s ' +
137
+ '`scope.type`; for `strings` the answer also lists the pool\'s `kinds`, for `terms` its ' +
138
+ '`taxonomies` — the values a selector\'s `kinds` / `taxonomies` filters take. Listing is for ' +
139
+ 'reading: to act on a whole type, give translate_set / approve_set a `scope` instead of paging.',
57
140
  method: 'GET',
58
141
  route: 'list-items',
59
- input: {
60
- type: z.string().describe('page | wp_template | wp_block | json-loops | strings | a public post-type slug'),
142
+ input: z.object( {
143
+ type: z
144
+ .string()
145
+ .describe( 'page | wp_template | wp_block | terms | menus | forms | json-loops | strings | a public post-type slug' ),
61
146
  offset: z.number().int().min(0).optional(),
62
147
  limit: z.number().int().min(1).max(100).optional().describe('default 30, max 100'),
63
- search: z.string().optional().describe('title filter'),
64
- },
148
+ search: z.string().optional().describe('text filter'),
149
+ } ),
65
150
  },
66
151
  {
67
152
  name: 'item_strings',
@@ -69,88 +154,238 @@ export const TOOLS = [
69
154
  description:
70
155
  "An item's translatable strings: each with its stable `hash`, the `source` text, existing " +
71
156
  '`translations` per language, and review `meta` (draft vs approved, human vs AI origin). ' +
72
- '`kind` is `post`/`component` (numeric id), `loop`, or `string` (there the id IS the hash). ' +
73
- 'The hashes are what save_translations needs. Source text may contain placeholder tokens ' +
74
- 'wrapped in ⟦ ⟧ (dynamic values, inline markup) — they must survive translation exactly.',
157
+ '`kind` is `post`/`component`, `term`, `menu` or `form` (numeric id), `loop` (the loop\'s key), ' +
158
+ 'or `string` (there the id IS the hash). The hashes are what save_translations needs. Source ' +
159
+ 'text may contain placeholder tokens wrapped in ⟦ ⟧ (dynamic values, inline markup) — they must ' +
160
+ 'survive translation exactly.',
75
161
  method: 'GET',
76
162
  route: 'item-strings',
77
- input: {
78
- id: z.string().describe('post/component ID, loop id, or the string hash itself'),
79
- kind: z.enum( [ 'post', 'component', 'loop', 'string' ] ).optional().describe('default post'),
80
- },
163
+ input: z.object( {
164
+ id: z.string().describe('post/component/term/menu/form ID, loop key, or the string hash itself'),
165
+ kind: z.enum( UNIT_KINDS ).optional().describe('default post'),
166
+ } ),
81
167
  },
82
168
  {
83
169
  name: 'save_translations',
84
170
  title: 'Save translations you produced',
85
171
  description:
86
- 'Write translations YOU produced (for the site\'s own AI engine use translate_batch instead). ' +
172
+ 'Write translations YOU produced (for the site\'s own AI engine use translate_set instead). ' +
87
173
  'Every entry must carry a `hash` from item_strings — source-only entries are rejected on this ' +
88
174
  'surface. Placeholder tokens wrapped in ⟦ ⟧ must be preserved in the translation EXACTLY as they ' +
89
- 'appear in the source, or that entry is skipped. Saves land as DRAFTS behind the review gate: ' +
90
- 'nothing serves publicly until approve_item (or a human in the panel) signs it off — never treat ' +
91
- 'a save as published. The response reports `saved` and `skipped` honestly; skipped means a guard ' +
92
- 'refused it (placeholder mismatch, rights, unknown hash), not a partial success.',
175
+ 'appear in the source, or that entry is skipped. Inline HTML holds to the same rule: when the ' +
176
+ 'source carries tags (a link, a bold), the translation must carry the same tags, in the same ' +
177
+ 'order, with the same attributes — adding, repointing or dropping one gets the entry skipped. ' +
178
+ 'Saves land as DRAFTS behind the review gate: nothing serves publicly until approve_item or ' +
179
+ 'approve_set (or a human in the panel) signs it off — never treat a save as published. The ' +
180
+ 'response reports `saved` and `skipped` honestly, and `skipped_at` lists the indexes of the ' +
181
+ 'skipped entries; skipped means a guard refused it (placeholder or markup mismatch, rights, ' +
182
+ 'unknown hash), not a partial success.',
93
183
  method: 'POST',
94
184
  route: 'save-translations',
95
- input: {
96
- lang: z.string().optional().describe('default target language for entries that omit lang'),
185
+ input: z.object( {
186
+ lang: z.string().optional().describe( 'default target language for entries that omit lang' ),
97
187
  entries: z
98
188
  .array(
99
189
  z.object({
100
190
  hash: z.string().describe('the string hash from item_strings'),
101
- text: z.string().describe('the translated text, placeholders ⟦…⟧ intact'),
191
+ text: z.string().describe('the translated text, placeholders ⟦…⟧ and inline tags intact'),
102
192
  lang: z.string().optional(),
103
193
  })
104
194
  )
105
195
  .min(1)
106
196
  .max(500),
107
- },
197
+ } ),
108
198
  },
109
199
  {
110
200
  name: 'approve_item',
111
201
  title: 'Approve a post + language',
112
202
  description:
113
- "The review gate's sign-off: approve a post's draft translations for one language, which makes " +
114
- 'them serve publicly (once the language is live — see site_status). Requires publish rights on ' +
115
- 'that post. Strings shared beyond your rights (site chrome, other authors\' content) are skipped ' +
116
- 'and counted in `skipped`, never silently — a human with broader rights finishes those in the panel.',
203
+ "The review gate's sign-off for ONE post in ONE language — the right tool for «approve this " +
204
+ 'page»: its draft translations start serving publicly (once the language is live — see ' +
205
+ 'site_status). Requires publish rights on that post. The answer carries `approved` (rows signed ' +
206
+ 'off), `items[]` (the repainted rows) and `refused[]`: what was NOT approved, by identity — ' +
207
+ '`{id, kind: "string", lang, reason, message, in}` with `reason` `approve_tier` (a site-wide or ' +
208
+ 'shared string beyond your rights: a human with broader rights finishes it in the panel) or ' +
209
+ '`held_source_words`. The numbers `skipped` and `held` count those same two piles: `held` is ' +
210
+ 'composed paragraphs that still carry words of the original, approved on their own row after a ' +
211
+ 'review, never by this call. A string that was already approved is not a refusal. For many ' +
212
+ 'posts, terms or strings at once use approve_set.',
117
213
  method: 'POST',
118
214
  route: 'approve-item',
119
- input: {
215
+ input: z.object( {
120
216
  post_id: z.number().int().describe('the post whose drafts to approve'),
121
217
  lang: z.string().describe('target language code'),
122
- },
218
+ } ),
123
219
  },
124
220
  {
125
221
  name: 'translate_batch',
126
- title: "Translate with the site's AI engine",
222
+ title: "Translate items with the site's AI engine (deprecated)",
127
223
  description:
128
- "Translate items with the SITE'S OWN configured AI engine — its provider, prompts and glossary, " +
129
- 'the same machinery the panel uses. `items`: [{id, kind: post|loop|string}] (ids/kinds from ' +
130
- 'list_items). AI output lands as DRAFTS unless the site auto-approves. IMPORTANT: `capped: true` ' +
131
- 'in the response means the per-request budget was hit before finishing — call again with the ' +
132
- 'remaining items until it is false; never assume one call completed the batch. A LOCAL model ' +
224
+ 'DEPRECATED — prefer translate_set, which takes these same items as `selector.targets` (or a ' +
225
+ 'whole `scope`), counts before it spends (`dry_run`) and finishes the set itself, by cursor or on ' +
226
+ 'the queue. This tool keeps working exactly as before, for agents already built on it. It ' +
227
+ "translates items with the SITE'S OWN configured AI engine — its provider, prompts and " +
228
+ 'glossary. `items`: [{id, kind}] (ids/kinds from list_items). AI output lands as DRAFTS unless ' +
229
+ 'the site auto-approves. `capped: true` in the response means the per-request budget ran out ' +
230
+ 'before every item was done: do not split and resend the list yourself — send the same items to ' +
231
+ 'translate_set as `targets`; what is already translated is not paid for again. A LOCAL model ' +
133
232
  'endpoint can take minutes per call. For translations you produced yourself use save_translations.',
134
233
  method: 'POST',
135
234
  route: 'translate-batch',
136
- input: {
235
+ input: z.object( {
137
236
  items: z
138
237
  .array(
139
238
  z.object({
140
239
  id: z.union( [ z.string(), z.number() ] ),
141
- kind: z.enum( [ 'post', 'loop', 'string' ] ).optional().describe('default post'),
240
+ kind: z.enum( [ 'post', 'term', 'menu', 'form', 'loop', 'string' ] ).optional().describe('default post'),
142
241
  })
143
242
  )
144
243
  .max(500),
145
244
  lang: z.string().describe('target language code'),
146
245
  force: z.boolean().optional().describe('re-translate strings that already have a translation'),
147
- },
246
+ } ),
247
+ },
248
+ {
249
+ name: 'translate_set',
250
+ title: "Translate a set with the site's AI engine",
251
+ description:
252
+ "Translate a SET with the SITE'S OWN configured AI engine — its provider, prompts and glossary. " +
253
+ 'You describe the set in `selector`; the SERVER resolves it, splits it and checks your rights per ' +
254
+ 'unit, so never list or split the items yourself. ' +
255
+ '(1) Call with `dry_run: true` FIRST and tell the user the number: `strings` (string × language ' +
256
+ 'cells that would be paid for), `source_chars`, `budget` (`fits` | `exceeds` | `uncapped` against ' +
257
+ 'what is left of the month), `mode`, and `complete` (false = the numbers are «at least»). Nothing ' +
258
+ 'is written. ' +
259
+ '(2) Call again with the SAME selector and no `dry_run`. Read `mode` from the answer, never assume ' +
260
+ 'it — the server decides it. `mode: "request"` (a `scope.document` or at most 50 `targets`; ' +
261
+ 'under `force`, any set but a whole `scope.type`): translated in this request, a chunk at a time — ' +
262
+ 'while `done` is false, call again ' +
263
+ 'with the SAME selector (and `force`) and the returned `cursor`; `retry_after` = wait that many seconds, then ' +
264
+ 'send the same cursor again. `mode: "queue"` (without `force`: any `scope.type`, ' +
265
+ '`scope.site_identity`, or more than 50 targets): this call only ENQUEUES, one job per language in `jobs[]` ({id, lang, reused}; ' +
266
+ '`reused: true` = that very job was already queued by you, nothing is paid twice) — follow each ' +
267
+ 'with queue_status, and never call translate_set again to hurry it. `stopped` {reason, message} ' +
268
+ '= the run stops short (`hash_cap`, a per-action ceiling this site set; the budget; the license; ' +
269
+ 'the key…) and has no cursor to continue: tell the user. ' +
270
+ 'What was NOT done arrives by identity in `refused[]` — `{id, kind, lang, reason, message, in?}`, ' +
271
+ 'listing up to 200, with the whole count in `refused_total` where the answer has one — relay it, ' +
272
+ 'do not retry it: `not_found`, `forbidden` (you ' +
273
+ 'cannot edit that post), `translate_tier` (a string shared beyond your rights), ' +
274
+ '`translation_locked` (under `force`). A string that is already translated is not a refusal: it ' +
275
+ 'is simply not sent again. `force: true` re-translates existing translations, always in request ' +
276
+ 'mode — and never over a whole `scope.type` (400 `zl_selector_force_queued`: narrow it to a ' +
277
+ 'document or `targets`). AI output lands as ' +
278
+ 'DRAFTS unless the site auto-approves — sign it off with approve_item / approve_set. For ' +
279
+ 'translations you produced yourself use save_translations.',
280
+ method: 'POST',
281
+ route: 'translate-set',
282
+ input: z.object( {
283
+ selector: SELECTOR,
284
+ dry_run: DRY_RUN,
285
+ cursor: CURSOR,
286
+ force: z
287
+ .boolean()
288
+ .optional()
289
+ .describe( 're-translate strings that already have a translation; always in request mode, never with `scope.type`' ),
290
+ } ),
291
+ },
292
+ {
293
+ name: 'approve_set',
294
+ title: 'Approve a set',
295
+ description:
296
+ "The review gate's sign-off for a SET — many pages, terms, menus or strings, in one or more " +
297
+ 'languages; approved text serves publicly once its language is live (site_status). For one post ' +
298
+ 'in one language approve_item is simpler. Describe the set in `selector` (the usual filter is ' +
299
+ '`status: ["needs_review"]`); the SERVER resolves it and checks your rights per unit and per ' +
300
+ 'string. ' +
301
+ '(1) Call with `dry_run: true` FIRST and tell the user the number: `units`, `cells` (unit × ' +
302
+ 'language), `actionable` (draft rows that would become approved — a field\'s own override ' +
303
+ 'counts as its own row), `refused[]` / ' +
304
+ '`refused_total`, `complete` (false = «at least»). Nothing is written — and this surface has no ' +
305
+ 'undo tool, so get a yes before step 2. ' +
306
+ '(2) Call with the SAME selector and no `dry_run`. Approving runs a chunk at a time: each answer ' +
307
+ 'carries `acted` (rows approved by this chunk), `items[]` (the repainted rows), `refused[]`, ' +
308
+ '`remaining` (cells; `remaining_complete: false` = «at least»), `done` and `cursor`. While `done` ' +
309
+ 'is false, call again with the SAME selector and that `cursor`, until `done: true` (then `cursor` ' +
310
+ 'is null). Never stop after the first chunk, and never send a cursor with a different selector ' +
311
+ '(400 `zl_selector_cursor_invalid`). It always ends: each unit is visited once per run, even ' +
312
+ 'when approving empties the very `needs_review` filter it walks. ' +
313
+ 'What was NOT approved arrives by identity in `refused[]` — `{id, kind, lang, reason, message, ' +
314
+ 'in?, slot?}`, where `in` is the unit a refused string belongs to and `slot` the field when what ' +
315
+ 'was refused is that field\'s own translation; collect it across chunks and relay ' +
316
+ 'it: `forbidden` (no publish rights on that post), `approve_tier` (a site-wide or shared string ' +
317
+ 'an editor must approve), `held_source_words` (a composed paragraph that still carries words of ' +
318
+ 'the original — reviewed on its own row), `not_found`. Something that was already approved is ' +
319
+ 'not a refusal: it is simply not counted.',
320
+ method: 'POST',
321
+ route: 'approve-set',
322
+ input: z.object( {
323
+ selector: SELECTOR,
324
+ dry_run: DRY_RUN,
325
+ cursor: CURSOR,
326
+ } ),
327
+ },
328
+ {
329
+ name: 'queue_status',
330
+ title: 'Follow queued translation work',
331
+ description:
332
+ 'Follow the jobs translate_set returns when it answers `mode: "queue"`. With `id` (a `jobs[].id`): ' +
333
+ '`state` is `queued` (still in the queue; `job`: {id, lang, slice, sent, cap, exhausted}, `sent` = ' +
334
+ 'strings sent so far), `finished` (`finished`: {at, sent, capped}; `capped: true` = it stopped at ' +
335
+ 'the site\'s per-action ceiling), or `unknown` — neither in the queue nor among the last 20 ' +
336
+ 'finished jobs. `unknown` cannot tell a job that never existed from one that was cleared or one ' +
337
+ 'that finished long ago: it is neither success nor failure, so read the result in ' +
338
+ 'pending_overview or list_items. Without `id`: `jobs[]` ({id, lang}) lists the live set jobs. ' +
339
+ 'Every answer carries `queue` (the AI queue — a non-empty `stopped`, with its `cause`, means it is ' +
340
+ 'paused: tell the user) and `budget` (the month\'s meter). Queued work runs in the background and ' +
341
+ 'the provider\'s batch mode can take hours: poll at a relaxed pace, never in a tight loop, and ' +
342
+ 'never re-send translate_set to hurry it.',
343
+ method: 'GET',
344
+ route: 'queue-status',
345
+ input: z.object( {
346
+ id: z.string().optional().describe( 'a job id from translate_set `jobs[].id`; omit to list the live jobs' ),
347
+ } ),
148
348
  },
149
349
  ];
150
350
 
151
351
  /** One honest error result: the agent should relay this, not retry blindly. */
152
352
  const err = ( text ) => ( { isError: true, content: [ { type: 'text', text } ] } );
153
353
 
354
+ /**
355
+ * The next step, said in the result as well as in the description: a description
356
+ * is read once, a result every time. Words only — every decision is the server's.
357
+ *
358
+ * @param {object} tool one of TOOLS
359
+ * @param {any} body the parsed 2xx body
360
+ */
361
+ function nextStep( tool, body ) {
362
+ if ( ! body || 'object' !== typeof body ) {
363
+ return '';
364
+ }
365
+ if ( 'translate_batch' === tool.name && true === body.capped ) {
366
+ return (
367
+ 'NOTE capped: true — the per-request AI budget ran out before every item was done. Do not split ' +
368
+ 'the list yourself: send the same items to translate_set as `selector.targets` (dry_run first); ' +
369
+ 'what is already translated is not paid for again.'
370
+ );
371
+ }
372
+ const stopped = body.stopped ? ` stopped (${ body.stopped.reason }): ${ body.stopped.message } Tell the user.` : '';
373
+ if ( 'queue' === body.mode && Array.isArray( body.jobs ) ) {
374
+ return (
375
+ 'NOTE mode: queue — nothing was translated by this call: the work is queued. Follow each job with ' +
376
+ `queue_status({ id }).${ stopped }`
377
+ );
378
+ }
379
+ if ( stopped ) {
380
+ return `NOTE${ stopped } There is no cursor to continue this run.`;
381
+ }
382
+ if ( false === body.done && body.cursor ) {
383
+ const wait = body.retry_after ? `wait ${ body.retry_after } seconds, then ` : '';
384
+ return `NOTE done: false — ${ wait }call ${ tool.name } again with the SAME selector and this cursor, until done is true.`;
385
+ }
386
+ return '';
387
+ }
388
+
154
389
  /**
155
390
  * Execute one tool: exactly one HTTP call to the plugin's mcp/* namespace.
156
391
  *
@@ -196,10 +431,9 @@ export async function callTool( tool, args, env ) {
196
431
 
197
432
  if ( response.ok ) {
198
433
  let text = JSON.stringify( body, null, 2 );
199
- if ( body && true === body.capped ) {
200
- text +=
201
- '\n\nNOTE capped: true — the per-request AI budget ran out before the batch finished. ' +
202
- 'Call translate_batch again with the remaining items; repeat until capped is false.';
434
+ const note = nextStep( tool, body );
435
+ if ( note ) {
436
+ text += `\n\n${ note }`;
203
437
  }
204
438
  return { content: [ { type: 'text', text } ] };
205
439
  }
@@ -224,6 +458,13 @@ export async function callTool( tool, args, env ) {
224
458
  'The agent inherits exactly the capabilities of the user behind the Application Password.'
225
459
  );
226
460
  }
461
+ if ( 404 === response.status && 'rest_no_route' === code ) {
462
+ return err(
463
+ `The site has no route for ${ tool.name } (404 rest_no_route): Zerø Lingua is inactive there, or ` +
464
+ 'older than this client — translate_set, approve_set and queue_status need Zerø Lingua 0.8.0 or ' +
465
+ 'later. Tell the user; until they update, use approve_item and translate_batch.'
466
+ );
467
+ }
227
468
  if ( 429 === response.status ) {
228
469
  return err( `Rate limited (429). ${ message } Wait before retrying — the budget is per user, per window.` );
229
470
  }