@langlink/l10n-mcp 0.1.1 → 0.2.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.
- package/dist/index.js +122 -1
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7,6 +7,107 @@ 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 { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
13
|
+
var MAX_BODY_BYTES = 10 * 1024 * 1024;
|
|
14
|
+
function sendJson(response, status, value) {
|
|
15
|
+
const body = JSON.stringify(value);
|
|
16
|
+
response.writeHead(status, {
|
|
17
|
+
"content-type": "application/json; charset=utf-8",
|
|
18
|
+
"content-length": Buffer.byteLength(body),
|
|
19
|
+
"cache-control": "no-store"
|
|
20
|
+
});
|
|
21
|
+
response.end(body);
|
|
22
|
+
}
|
|
23
|
+
async function readBody(request) {
|
|
24
|
+
const chunks = [];
|
|
25
|
+
let size = 0;
|
|
26
|
+
for await (const chunk of request) {
|
|
27
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
28
|
+
size += buffer.byteLength;
|
|
29
|
+
if (size > MAX_BODY_BYTES) throw new Error("request body too large");
|
|
30
|
+
chunks.push(buffer);
|
|
31
|
+
}
|
|
32
|
+
if (chunks.length === 0) return void 0;
|
|
33
|
+
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
34
|
+
}
|
|
35
|
+
function authorized(request, token) {
|
|
36
|
+
if (!token) return true;
|
|
37
|
+
return request.headers.authorization === `Bearer ${token}`;
|
|
38
|
+
}
|
|
39
|
+
function createHttpMcpServer(options) {
|
|
40
|
+
return createServer(async (request, response) => {
|
|
41
|
+
const url = new URL(request.url ?? "/", "http://localhost");
|
|
42
|
+
if (url.pathname === "/health") {
|
|
43
|
+
sendJson(response, 200, { ok: true, transport: "streamable-http" });
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (url.pathname !== "/mcp") {
|
|
47
|
+
sendJson(response, 404, { error: "not found" });
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (!authorized(request, options.token)) {
|
|
51
|
+
sendJson(response, 401, { error: "unauthorized" });
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (request.method === "GET" || request.method === "DELETE") {
|
|
55
|
+
sendJson(response, 405, {
|
|
56
|
+
jsonrpc: "2.0",
|
|
57
|
+
error: { code: -32e3, message: "Method not allowed." },
|
|
58
|
+
id: null
|
|
59
|
+
});
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (request.method !== "POST") {
|
|
63
|
+
sendJson(response, 405, { error: "method not allowed" });
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
let body;
|
|
67
|
+
try {
|
|
68
|
+
body = await readBody(request);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
sendJson(response, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const server = options.createServer();
|
|
74
|
+
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: void 0 });
|
|
75
|
+
try {
|
|
76
|
+
await server.connect(transport);
|
|
77
|
+
await transport.handleRequest(request, response, body);
|
|
78
|
+
response.on("close", () => {
|
|
79
|
+
void transport.close();
|
|
80
|
+
void server.close();
|
|
81
|
+
});
|
|
82
|
+
} catch (error) {
|
|
83
|
+
console.error("[langlink-l10n-mcp] HTTP request failed", error);
|
|
84
|
+
if (!response.headersSent) {
|
|
85
|
+
sendJson(response, 500, {
|
|
86
|
+
jsonrpc: "2.0",
|
|
87
|
+
error: { code: -32603, message: "Internal server error" },
|
|
88
|
+
id: null
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
function listenHttpMcp(options) {
|
|
95
|
+
const host = options.host ?? "127.0.0.1";
|
|
96
|
+
const port = options.port ?? 8765;
|
|
97
|
+
const server = createHttpMcpServer(options);
|
|
98
|
+
return new Promise((resolvePromise, reject) => {
|
|
99
|
+
server.once("error", reject);
|
|
100
|
+
server.listen(port, host, () => {
|
|
101
|
+
const address = server.address();
|
|
102
|
+
if (!address || typeof address === "string") {
|
|
103
|
+
reject(new Error("failed to determine HTTP address"));
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
resolvePromise({ server, host, port: address.port, url: `http://${host}:${address.port}/mcp` });
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
10
111
|
// packages/l10n-mcp/src/config.ts
|
|
11
112
|
import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
12
113
|
import { homedir } from "node:os";
|
|
@@ -2707,7 +2808,27 @@ async function main() {
|
|
|
2707
2808
|
return;
|
|
2708
2809
|
}
|
|
2709
2810
|
if (command === "--help" || command === "-h") {
|
|
2710
|
-
console.log("Usage: langlink-l10n-mcp [config
|
|
2811
|
+
console.log("Usage: langlink-l10n-mcp [config ...] | [--http [--host 127.0.0.1] [--port 8765] [--token-env L10N_MCP_HTTP_TOKEN]]");
|
|
2812
|
+
return;
|
|
2813
|
+
}
|
|
2814
|
+
const http = command === "serve" || command === "--http" || process.argv.includes("--http");
|
|
2815
|
+
if (http) {
|
|
2816
|
+
const portIndex = process.argv.indexOf("--port");
|
|
2817
|
+
const hostIndex = process.argv.indexOf("--host");
|
|
2818
|
+
const tokenEnvIndex = process.argv.indexOf("--token-env");
|
|
2819
|
+
const tokenEnv = tokenEnvIndex >= 0 ? process.argv[tokenEnvIndex + 1] : "L10N_MCP_HTTP_TOKEN";
|
|
2820
|
+
const token = tokenEnv ? process.env[tokenEnv] : void 0;
|
|
2821
|
+
const host = hostIndex >= 0 ? process.argv[hostIndex + 1] : "127.0.0.1";
|
|
2822
|
+
const port = portIndex >= 0 ? Number.parseInt(process.argv[portIndex + 1] ?? "", 10) : 8765;
|
|
2823
|
+
const { url } = await listenHttpMcp({
|
|
2824
|
+
...host ? { host } : {},
|
|
2825
|
+
port,
|
|
2826
|
+
...token ? { token } : {},
|
|
2827
|
+
createServer: () => createL10nMcpServer({
|
|
2828
|
+
...process.env.L10N_ASSET_BASE_URL ? { assetBaseUrl: process.env.L10N_ASSET_BASE_URL } : {}
|
|
2829
|
+
})
|
|
2830
|
+
});
|
|
2831
|
+
console.error(`[langlink-l10n-mcp] streamable HTTP ready at ${url}`);
|
|
2711
2832
|
return;
|
|
2712
2833
|
}
|
|
2713
2834
|
const server = createL10nMcpServer({
|