@langlink/l10n-mcp 0.1.1 → 0.2.1
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/dist/index.js +173 -12
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7,6 +7,128 @@ import { basename, resolve } from "node:path";
|
|
|
7
7
|
import { pathToFileURL } from "node:url";
|
|
8
8
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
9
9
|
|
|
10
|
+
// packages/l10n-mcp/src/http.ts
|
|
11
|
+
import { createServer } from "node:http";
|
|
12
|
+
import { timingSafeEqual } from "node:crypto";
|
|
13
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
14
|
+
var MAX_BODY_BYTES = 10 * 1024 * 1024;
|
|
15
|
+
function sendJson(response, status, value) {
|
|
16
|
+
const body = JSON.stringify(value);
|
|
17
|
+
response.writeHead(status, {
|
|
18
|
+
"content-type": "application/json; charset=utf-8",
|
|
19
|
+
"content-length": Buffer.byteLength(body),
|
|
20
|
+
"cache-control": "no-store"
|
|
21
|
+
});
|
|
22
|
+
response.end(body);
|
|
23
|
+
}
|
|
24
|
+
async function readBody(request) {
|
|
25
|
+
const chunks = [];
|
|
26
|
+
let size = 0;
|
|
27
|
+
for await (const chunk of request) {
|
|
28
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
29
|
+
size += buffer.byteLength;
|
|
30
|
+
if (size > MAX_BODY_BYTES) throw new Error("request body too large");
|
|
31
|
+
chunks.push(buffer);
|
|
32
|
+
}
|
|
33
|
+
if (chunks.length === 0) return void 0;
|
|
34
|
+
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
35
|
+
}
|
|
36
|
+
function timingSafeEqualString(a, b) {
|
|
37
|
+
const aBuf = Buffer.from(a);
|
|
38
|
+
const bBuf = Buffer.from(b);
|
|
39
|
+
if (aBuf.length !== bBuf.length) {
|
|
40
|
+
Buffer.compare(bBuf, bBuf);
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return timingSafeEqual(aBuf, bBuf);
|
|
44
|
+
}
|
|
45
|
+
function authorized(request, token) {
|
|
46
|
+
if (!token) return true;
|
|
47
|
+
const header = request.headers.authorization;
|
|
48
|
+
if (typeof header !== "string" || !header.startsWith("Bearer ")) return false;
|
|
49
|
+
return timingSafeEqualString(header.slice("Bearer ".length), token);
|
|
50
|
+
}
|
|
51
|
+
function createHttpMcpServer(options) {
|
|
52
|
+
return createServer(async (request, response) => {
|
|
53
|
+
const url = new URL(request.url ?? "/", "http://localhost");
|
|
54
|
+
if (url.pathname === "/health") {
|
|
55
|
+
sendJson(response, 200, { ok: true, transport: "streamable-http" });
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (url.pathname !== "/mcp") {
|
|
59
|
+
sendJson(response, 404, { error: "not found" });
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (!authorized(request, options.token)) {
|
|
63
|
+
sendJson(response, 401, { error: "unauthorized" });
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (request.method === "GET" || request.method === "DELETE") {
|
|
67
|
+
sendJson(response, 405, {
|
|
68
|
+
jsonrpc: "2.0",
|
|
69
|
+
error: { code: -32e3, message: "Method not allowed." },
|
|
70
|
+
id: null
|
|
71
|
+
});
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (request.method !== "POST") {
|
|
75
|
+
sendJson(response, 405, { error: "method not allowed" });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
let body;
|
|
79
|
+
try {
|
|
80
|
+
body = await readBody(request);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
sendJson(response, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const server = options.createServer();
|
|
86
|
+
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: void 0 });
|
|
87
|
+
try {
|
|
88
|
+
await server.connect(transport);
|
|
89
|
+
await transport.handleRequest(request, response, body);
|
|
90
|
+
response.on("close", () => {
|
|
91
|
+
void transport.close();
|
|
92
|
+
void server.close();
|
|
93
|
+
});
|
|
94
|
+
} catch (error) {
|
|
95
|
+
console.error("[langlink-l10n-mcp] HTTP request failed", error);
|
|
96
|
+
if (!response.headersSent) {
|
|
97
|
+
sendJson(response, 500, {
|
|
98
|
+
jsonrpc: "2.0",
|
|
99
|
+
error: { code: -32603, message: "Internal server error" },
|
|
100
|
+
id: null
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function isLoopbackHost(host) {
|
|
107
|
+
const h = host.toLowerCase();
|
|
108
|
+
return h === "127.0.0.1" || h === "::1" || h === "localhost";
|
|
109
|
+
}
|
|
110
|
+
function listenHttpMcp(options) {
|
|
111
|
+
const host = options.host ?? "127.0.0.1";
|
|
112
|
+
const port = options.port ?? 8765;
|
|
113
|
+
if (!options.token && !isLoopbackHost(host)) {
|
|
114
|
+
return Promise.reject(
|
|
115
|
+
new Error("L10N MCP HTTP token is required when binding a non-loopback host")
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
const server = createHttpMcpServer(options);
|
|
119
|
+
return new Promise((resolvePromise, reject) => {
|
|
120
|
+
server.once("error", reject);
|
|
121
|
+
server.listen(port, host, () => {
|
|
122
|
+
const address = server.address();
|
|
123
|
+
if (!address || typeof address === "string") {
|
|
124
|
+
reject(new Error("failed to determine HTTP address"));
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
resolvePromise({ server, host, port: address.port, url: `http://${host}:${address.port}/mcp` });
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
10
132
|
// packages/l10n-mcp/src/config.ts
|
|
11
133
|
import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
12
134
|
import { homedir } from "node:os";
|
|
@@ -330,7 +452,7 @@ function sdlState(segDefs) {
|
|
|
330
452
|
}
|
|
331
453
|
return Object.keys(state).length > 0 ? state : void 0;
|
|
332
454
|
}
|
|
333
|
-
function makeSegment(fileIndex, unitId, segmentIndex, sourceContainer, targetContainer, mid, sdl, sdlNode, xml, warnings) {
|
|
455
|
+
function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer, targetContainer, mid, sdl, sdlNode, xml, warnings) {
|
|
334
456
|
const sourceTags = sourceContainer ? elementSignature(sourceContainer) : [];
|
|
335
457
|
const targetTags = targetContainer ? elementSignature(targetContainer) : [];
|
|
336
458
|
const id = `${fileIndex}:${unitId}:${mid ?? segmentIndex}`;
|
|
@@ -340,7 +462,7 @@ function makeSegment(fileIndex, unitId, segmentIndex, sourceContainer, targetCon
|
|
|
340
462
|
}
|
|
341
463
|
return {
|
|
342
464
|
id,
|
|
343
|
-
index:
|
|
465
|
+
index: ordinal,
|
|
344
466
|
location: { fileIndex, unitId, ...mid === void 0 ? {} : { mid }, segmentIndex },
|
|
345
467
|
sourceInner: sourceContainer ? xml.slice(sourceContainer.innerStart, sourceContainer.innerEnd) : "",
|
|
346
468
|
targetInner: targetContainer ? xml.slice(targetContainer.innerStart, targetContainer.innerEnd) : "",
|
|
@@ -349,6 +471,7 @@ function makeSegment(fileIndex, unitId, segmentIndex, sourceContainer, targetCon
|
|
|
349
471
|
sourceTags,
|
|
350
472
|
targetTags,
|
|
351
473
|
targetRange: targetContainer ? { start: targetContainer.innerStart, end: targetContainer.innerEnd } : null,
|
|
474
|
+
...sourceContainer === void 0 ? {} : { sourceNode: sourceContainer },
|
|
352
475
|
...targetContainer === void 0 ? {} : { targetNode: targetContainer },
|
|
353
476
|
...sdlNode === void 0 ? {} : { sdlNode },
|
|
354
477
|
...sdl === void 0 ? {} : { sdl }
|
|
@@ -407,6 +530,7 @@ function parseXliff(xml, options = {}) {
|
|
|
407
530
|
fileIndex,
|
|
408
531
|
unitId,
|
|
409
532
|
segmentIndex,
|
|
533
|
+
segments.length,
|
|
410
534
|
source,
|
|
411
535
|
target,
|
|
412
536
|
segmentId,
|
|
@@ -440,6 +564,7 @@ function parseXliff(xml, options = {}) {
|
|
|
440
564
|
fileIndex,
|
|
441
565
|
unitId,
|
|
442
566
|
segmentIndex,
|
|
567
|
+
segments.length,
|
|
443
568
|
sourceMrk,
|
|
444
569
|
targetMrk ?? (targetMrks.length === 0 ? targetElement : void 0),
|
|
445
570
|
mid,
|
|
@@ -454,7 +579,7 @@ function parseXliff(xml, options = {}) {
|
|
|
454
579
|
}
|
|
455
580
|
if (sourceElement || targetElement) {
|
|
456
581
|
segments.push(
|
|
457
|
-
makeSegment(fileIndex, unitId, 0, sourceElement, targetElement, void 0, sdl, sdlNode, xml, warnings)
|
|
582
|
+
makeSegment(fileIndex, unitId, 0, segments.length, sourceElement, targetElement, void 0, sdl, sdlNode, xml, warnings)
|
|
458
583
|
);
|
|
459
584
|
} else {
|
|
460
585
|
warnings.push(`${fileIndex}:${unitId}: no source/target`);
|
|
@@ -2088,18 +2213,23 @@ function checkWhitespace(context) {
|
|
|
2088
2213
|
function checkFullwidthHalfwidth(context) {
|
|
2089
2214
|
const target = context.target;
|
|
2090
2215
|
if (target.trim().length === 0) return;
|
|
2091
|
-
const
|
|
2092
|
-
|
|
2216
|
+
const exempt = new Set(context.locale?.fullwidthExemptChars ?? []);
|
|
2217
|
+
const fullwidthPattern = /[\uff10-\uff19\uff21-\uff3a\uff41-\uff5a]/gu;
|
|
2218
|
+
for (const match of target.matchAll(fullwidthPattern)) {
|
|
2219
|
+
const char = match[0];
|
|
2220
|
+
if (exempt.has(char)) continue;
|
|
2221
|
+
const index = match.index ?? 0;
|
|
2093
2222
|
draft(context, {
|
|
2094
2223
|
ruleId: "FULLWIDTH_HALFWIDTH",
|
|
2095
2224
|
message: "\u8BD1\u6587\u4E2D\u5B57\u6BCD/\u6570\u5B57\u4F7F\u7528\u5168\u89D2\u5F62\u5F0F",
|
|
2096
|
-
evidence: `found '${
|
|
2225
|
+
evidence: `found '${char}'`,
|
|
2097
2226
|
suggestion: "\u5B57\u6BCD\u4E0E\u6570\u5B57\u901A\u5E38\u4F7F\u7528\u534A\u89D2\u5F62\u5F0F",
|
|
2098
|
-
targetRange: { start:
|
|
2227
|
+
targetRange: { start: index, end: index + 1 }
|
|
2099
2228
|
});
|
|
2100
2229
|
return;
|
|
2101
2230
|
}
|
|
2102
|
-
|
|
2231
|
+
const expectFullwidthPunctuation = context.locale?.fullwidthPunctuation ?? context.targetIsCjk;
|
|
2232
|
+
if (!expectFullwidthPunctuation) return;
|
|
2103
2233
|
const mixed = /[\u4e00-\u9fff][,;:!?]|[,;:!?][\u4e00-\u9fff]/u.exec(target);
|
|
2104
2234
|
if (mixed === null || mixed.index === void 0) return;
|
|
2105
2235
|
draft(context, {
|
|
@@ -2237,10 +2367,17 @@ function checkEmptyTargetGroup(context) {
|
|
|
2237
2367
|
function checkUntranslatedCopyGroup(context) {
|
|
2238
2368
|
const { source, target } = context;
|
|
2239
2369
|
if (source.trim().length === 0 || target.trim().length === 0) return;
|
|
2240
|
-
|
|
2370
|
+
const normalizedSource = normalizeForCompare(source);
|
|
2371
|
+
if (normalizedSource !== normalizeForCompare(target)) return;
|
|
2372
|
+
const policy = context.untranslatedCopy;
|
|
2373
|
+
if (policy?.whitelist?.some((entry) => normalizeForCompare(entry) === normalizedSource)) return;
|
|
2241
2374
|
const tokens = wordTokens(source);
|
|
2242
2375
|
const cjkChars = Array.from(source).filter((char) => CJK_RE.test(char)).length;
|
|
2243
|
-
|
|
2376
|
+
const maxShort = policy?.shortSegmentMaxLength ?? 4;
|
|
2377
|
+
if (tokens.length <= 1 && cjkChars < 2 && source.trim().length <= maxShort) return;
|
|
2378
|
+
if (tokens.length <= 1 && policy?.singleTokenExemptions?.some((entry) => normalizeForCompare(entry) === normalizedSource)) {
|
|
2379
|
+
return;
|
|
2380
|
+
}
|
|
2244
2381
|
draft(context, {
|
|
2245
2382
|
ruleId: "UNTRANSLATED_COPY",
|
|
2246
2383
|
message: "\u8BD1\u6587\u4E0E\u6E90\u6587\u5B8C\u5168\u76F8\u540C\uFF08\u672A\u7FFB\u8BD1\uFF09",
|
|
@@ -2318,6 +2455,8 @@ function buildContext(segment, options, glossary) {
|
|
|
2318
2455
|
targetIsCjk: isCjkHeavy(segment.targetText),
|
|
2319
2456
|
glossary,
|
|
2320
2457
|
drafts: [],
|
|
2458
|
+
...options.locale === void 0 ? {} : { locale: options.locale },
|
|
2459
|
+
...options.untranslatedCopy === void 0 ? {} : { untranslatedCopy: options.untranslatedCopy },
|
|
2321
2460
|
...termHits === void 0 ? {} : { termHits }
|
|
2322
2461
|
};
|
|
2323
2462
|
}
|
|
@@ -2493,7 +2632,9 @@ function checkDocument(document, options = {}) {
|
|
|
2493
2632
|
const qaOptions = {
|
|
2494
2633
|
...options.glossary === void 0 ? {} : { glossary: options.glossary },
|
|
2495
2634
|
...segmentTermHits.size === 0 ? {} : { segmentTermHits },
|
|
2496
|
-
...options.enabledRules === void 0 ? {} : { enabledRules: options.enabledRules }
|
|
2635
|
+
...options.enabledRules === void 0 ? {} : { enabledRules: options.enabledRules },
|
|
2636
|
+
...options.untranslatedCopy === void 0 ? {} : { untranslatedCopy: options.untranslatedCopy },
|
|
2637
|
+
...options.locale === void 0 ? {} : { locale: options.locale }
|
|
2497
2638
|
};
|
|
2498
2639
|
const qa = runDeterministicChecks(document.segments, qaOptions);
|
|
2499
2640
|
return { qa, segmentTermHits };
|
|
@@ -2707,7 +2848,27 @@ async function main() {
|
|
|
2707
2848
|
return;
|
|
2708
2849
|
}
|
|
2709
2850
|
if (command === "--help" || command === "-h") {
|
|
2710
|
-
console.log("Usage: langlink-l10n-mcp [config
|
|
2851
|
+
console.log("Usage: langlink-l10n-mcp [config ...] | [--http [--host 127.0.0.1] [--port 8765] [--token-env L10N_MCP_HTTP_TOKEN]]");
|
|
2852
|
+
return;
|
|
2853
|
+
}
|
|
2854
|
+
const http = command === "serve" || command === "--http" || process.argv.includes("--http");
|
|
2855
|
+
if (http) {
|
|
2856
|
+
const portIndex = process.argv.indexOf("--port");
|
|
2857
|
+
const hostIndex = process.argv.indexOf("--host");
|
|
2858
|
+
const tokenEnvIndex = process.argv.indexOf("--token-env");
|
|
2859
|
+
const tokenEnv = tokenEnvIndex >= 0 ? process.argv[tokenEnvIndex + 1] : "L10N_MCP_HTTP_TOKEN";
|
|
2860
|
+
const token = tokenEnv ? process.env[tokenEnv] : void 0;
|
|
2861
|
+
const host = hostIndex >= 0 ? process.argv[hostIndex + 1] : "127.0.0.1";
|
|
2862
|
+
const port = portIndex >= 0 ? Number.parseInt(process.argv[portIndex + 1] ?? "", 10) : 8765;
|
|
2863
|
+
const { url } = await listenHttpMcp({
|
|
2864
|
+
...host ? { host } : {},
|
|
2865
|
+
port,
|
|
2866
|
+
...token ? { token } : {},
|
|
2867
|
+
createServer: () => createL10nMcpServer({
|
|
2868
|
+
...process.env.L10N_ASSET_BASE_URL ? { assetBaseUrl: process.env.L10N_ASSET_BASE_URL } : {}
|
|
2869
|
+
})
|
|
2870
|
+
});
|
|
2871
|
+
console.error(`[langlink-l10n-mcp] streamable HTTP ready at ${url}`);
|
|
2711
2872
|
return;
|
|
2712
2873
|
}
|
|
2713
2874
|
const server = createL10nMcpServer({
|