@zerolingua/mcp 0.1.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 +67 -0
  2. package/index.js +40 -0
  3. package/package.json +35 -0
  4. package/tools.js +234 -0
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # @zerolingua/mcp
2
+
3
+ Connect an AI agent (Claude Code, Claude Desktop, any MCP host) to a WordPress
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.
7
+
8
+ **MCP is a Zerø Lingua Pro feature.** The site needs an active license; without
9
+ one every call answers `403 license_required`. The free manual tooling is
10
+ unaffected.
11
+
12
+ ## Setup
13
+
14
+ 1. **Create an Application Password** on the site: wp-admin → Users → Profile →
15
+ Application Passwords (WordPress core, needs HTTPS in production). The agent
16
+ inherits exactly that user's capabilities — a translator's password
17
+ translates, an editor's can also approve.
18
+ 2. **Add the server to your MCP host**, with three environment variables:
19
+
20
+ Claude Code:
21
+
22
+ ```sh
23
+ claude mcp add zero-lingua \
24
+ -e ZL_SITE_URL=https://example.com \
25
+ -e ZL_USER=youruser \
26
+ -e "ZL_APP_PASSWORD=xxxx xxxx xxxx xxxx" \
27
+ -- npx -y @zerolingua/mcp
28
+ ```
29
+
30
+ Claude Desktop (`claude_desktop_config.json`):
31
+
32
+ ```json
33
+ {
34
+ "mcpServers": {
35
+ "zero-lingua": {
36
+ "command": "npx",
37
+ "args": ["-y", "@zerolingua/mcp"],
38
+ "env": {
39
+ "ZL_SITE_URL": "https://example.com",
40
+ "ZL_USER": "youruser",
41
+ "ZL_APP_PASSWORD": "xxxx xxxx xxxx xxxx"
42
+ }
43
+ }
44
+ }
45
+ }
46
+ ```
47
+
48
+ ## Tools
49
+
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 |
59
+
60
+ ## Safety model
61
+
62
+ Agent writes land as **drafts behind the review gate** — nothing serves
63
+ 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:
66
+ this process holds zero domain logic and zero secrets — it is one HTTP call per
67
+ tool, authenticated with the user's own Application Password.
package/index.js ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Zerø Lingua MCP server — stdio entry point.
4
+ *
5
+ * A thin client over the plugin's licensed REST surface (zero-lingua/v1/mcp/*):
6
+ * every tool is one HTTP call, authenticated with the user's own WordPress
7
+ * Application Password. All validation, authorization and the review gate live
8
+ * server-side; see tools.js for the tool map and README.md for setup.
9
+ */
10
+
11
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
12
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
13
+ import { TOOLS, callTool } from './tools.js';
14
+
15
+ const env = {
16
+ site: process.env.ZL_SITE_URL ?? '',
17
+ user: process.env.ZL_USER ?? '',
18
+ password: process.env.ZL_APP_PASSWORD ?? '',
19
+ };
20
+
21
+ const missing = Object.entries( { ZL_SITE_URL: env.site, ZL_USER: env.user, ZL_APP_PASSWORD: env.password } )
22
+ .filter( ( [ , value ] ) => '' === value.trim() )
23
+ .map( ( [ name ] ) => name );
24
+ if ( missing.length ) {
25
+ // stderr, never stdout: stdout belongs to the MCP protocol.
26
+ console.error( `zero-lingua-mcp: missing ${ missing.join( ', ' ) } — see deploy/mcp/README.md.` );
27
+ process.exit( 1 );
28
+ }
29
+
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
+ );
38
+ }
39
+
40
+ await server.connect( new StdioServerTransport() );
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
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.",
5
+ "type": "module",
6
+ "bin": {
7
+ "zero-lingua-mcp": "index.js"
8
+ },
9
+ "files": [
10
+ "index.js",
11
+ "tools.js",
12
+ "README.md"
13
+ ],
14
+ "engines": {
15
+ "node": ">=18"
16
+ },
17
+ "keywords": [
18
+ "mcp",
19
+ "model-context-protocol",
20
+ "wordpress",
21
+ "translation",
22
+ "zero-lingua"
23
+ ],
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/marcorubiol/zero-lingua.git",
27
+ "directory": "deploy/mcp"
28
+ },
29
+ "homepage": "https://zerolingua.studio",
30
+ "dependencies": {
31
+ "@modelcontextprotocol/sdk": "^1.0.0",
32
+ "zod": "^3.25.0"
33
+ },
34
+ "license": "GPL-2.0-or-later"
35
+ }
package/tools.js ADDED
@@ -0,0 +1,234 @@
1
+ /**
2
+ * The seven Zerø Lingua tools: their schemas, their teaching descriptions, and
3
+ * the one HTTP call each of them is.
4
+ *
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
8
+ * only jobs here are auth, tool descriptions and honest error mapping. The
9
+ * descriptions carry the workflow because the agent reads nothing else.
10
+ *
11
+ * No secrets: the client is public by design. Credentials are the USER's own
12
+ * WordPress Application Password, passed via environment — the agent inherits
13
+ * exactly that user's capabilities, nothing more.
14
+ */
15
+
16
+ import { z } from 'zod';
17
+
18
+ const WORKFLOW =
19
+ '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).';
22
+
23
+ export const TOOLS = [
24
+ {
25
+ name: 'site_status',
26
+ title: 'Site translation status',
27
+ description:
28
+ "The site's translation shape: the source language and every target language with its " +
29
+ "`live` flag (an offline language redirects visitors to the source until it goes live). " +
30
+ 'Call this first — every other tool needs a valid target language `code` from here. ' +
31
+ WORKFLOW,
32
+ method: 'GET',
33
+ route: 'site-status',
34
+ input: {},
35
+ },
36
+ {
37
+ name: 'pending_overview',
38
+ title: 'Pending work overview',
39
+ description:
40
+ "Per-language progress across the site: string counts (total / translated / approved) and item " +
41
+ '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?”.",
45
+ method: 'GET',
46
+ route: 'pending-overview',
47
+ input: {},
48
+ },
49
+ {
50
+ name: 'list_items',
51
+ title: 'List translatable items',
52
+ description:
53
+ '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.',
57
+ method: 'GET',
58
+ route: 'list-items',
59
+ input: {
60
+ type: z.string().describe('page | wp_template | wp_block | json-loops | strings | a public post-type slug'),
61
+ offset: z.number().int().min(0).optional(),
62
+ limit: z.number().int().min(1).max(100).optional().describe('default 30, max 100'),
63
+ search: z.string().optional().describe('title filter'),
64
+ },
65
+ },
66
+ {
67
+ name: 'item_strings',
68
+ title: "Read an item's strings",
69
+ description:
70
+ "An item's translatable strings: each with its stable `hash`, the `source` text, existing " +
71
+ '`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.',
75
+ method: 'GET',
76
+ 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
+ },
81
+ },
82
+ {
83
+ name: 'save_translations',
84
+ title: 'Save translations you produced',
85
+ description:
86
+ 'Write translations YOU produced (for the site\'s own AI engine use translate_batch instead). ' +
87
+ 'Every entry must carry a `hash` from item_strings — source-only entries are rejected on this ' +
88
+ '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.',
93
+ method: 'POST',
94
+ route: 'save-translations',
95
+ input: {
96
+ lang: z.string().optional().describe('default target language for entries that omit lang'),
97
+ entries: z
98
+ .array(
99
+ z.object({
100
+ hash: z.string().describe('the string hash from item_strings'),
101
+ text: z.string().describe('the translated text, placeholders ⟦…⟧ intact'),
102
+ lang: z.string().optional(),
103
+ })
104
+ )
105
+ .min(1)
106
+ .max(500),
107
+ },
108
+ },
109
+ {
110
+ name: 'approve_item',
111
+ title: 'Approve a post + language',
112
+ 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.',
117
+ method: 'POST',
118
+ route: 'approve-item',
119
+ input: {
120
+ post_id: z.number().int().describe('the post whose drafts to approve'),
121
+ lang: z.string().describe('target language code'),
122
+ },
123
+ },
124
+ {
125
+ name: 'translate_batch',
126
+ title: "Translate with the site's AI engine",
127
+ 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 ' +
133
+ 'endpoint can take minutes per call. For translations you produced yourself use save_translations.',
134
+ method: 'POST',
135
+ route: 'translate-batch',
136
+ input: {
137
+ items: z
138
+ .array(
139
+ z.object({
140
+ id: z.union( [ z.string(), z.number() ] ),
141
+ kind: z.enum( [ 'post', 'loop', 'string' ] ).optional().describe('default post'),
142
+ })
143
+ )
144
+ .max(500),
145
+ lang: z.string().describe('target language code'),
146
+ force: z.boolean().optional().describe('re-translate strings that already have a translation'),
147
+ },
148
+ },
149
+ ];
150
+
151
+ /** One honest error result: the agent should relay this, not retry blindly. */
152
+ const err = ( text ) => ( { isError: true, content: [ { type: 'text', text } ] } );
153
+
154
+ /**
155
+ * Execute one tool: exactly one HTTP call to the plugin's mcp/* namespace.
156
+ *
157
+ * @param {object} tool one of TOOLS
158
+ * @param {object} args validated tool arguments
159
+ * @param {{site: string, user: string, password: string}} env
160
+ */
161
+ export async function callTool( tool, args, env ) {
162
+ const base = env.site.replace( /\/+$/, '' );
163
+ let url = `${ base }/wp-json/zero-lingua/v1/mcp/${ tool.route }`;
164
+ const options = {
165
+ method: tool.method,
166
+ headers: {
167
+ Authorization: 'Basic ' + Buffer.from( `${ env.user }:${ env.password }` ).toString( 'base64' ),
168
+ Accept: 'application/json',
169
+ },
170
+ };
171
+ if ( 'GET' === tool.method ) {
172
+ const query = new URLSearchParams();
173
+ for ( const [ key, value ] of Object.entries( args ) ) {
174
+ if ( undefined !== value ) {
175
+ query.set( key, String( value ) );
176
+ }
177
+ }
178
+ const qs = query.toString();
179
+ if ( qs ) {
180
+ url += `?${ qs }`;
181
+ }
182
+ } else {
183
+ options.headers[ 'Content-Type' ] = 'application/json';
184
+ options.body = JSON.stringify( args );
185
+ }
186
+
187
+ let response;
188
+ try {
189
+ response = await fetch( url, options );
190
+ } catch ( cause ) {
191
+ return err(
192
+ `Could not reach ${ base }: ${ cause.message }. Check ZL_SITE_URL and that the site is up.`
193
+ );
194
+ }
195
+ const body = await response.json().catch( () => null );
196
+
197
+ if ( response.ok ) {
198
+ 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.';
203
+ }
204
+ return { content: [ { type: 'text', text } ] };
205
+ }
206
+
207
+ const code = body?.code ?? '';
208
+ const message = body?.message ?? response.statusText ?? '';
209
+ if ( 401 === response.status ) {
210
+ return err(
211
+ 'Authentication failed (401) — check ZL_USER and ZL_APP_PASSWORD. Application Passwords are ' +
212
+ 'created under Users → Profile in wp-admin and require HTTPS outside local environments.'
213
+ );
214
+ }
215
+ if ( 403 === response.status && 'license_required' === code ) {
216
+ return err(
217
+ `This site's Zerø Lingua license is not active, and the MCP surface is a Pro feature. ${ message } ` +
218
+ 'Tell the user to activate their license in the panel (License tab) — do not retry until they have.'
219
+ );
220
+ }
221
+ if ( 403 === response.status ) {
222
+ return err(
223
+ `Your WordPress user is not allowed to do this (403 ${ code }). ${ message } ` +
224
+ 'The agent inherits exactly the capabilities of the user behind the Application Password.'
225
+ );
226
+ }
227
+ if ( 429 === response.status ) {
228
+ return err( `Rate limited (429). ${ message } Wait before retrying — the budget is per user, per window.` );
229
+ }
230
+ if ( 400 === response.status ) {
231
+ return err( `Invalid arguments (400 ${ code }). ${ message }` );
232
+ }
233
+ return err( `Unexpected response ${ response.status } ${ code }. ${ message }` );
234
+ }