@langlink/l10n-mcp 0.1.0 → 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 CHANGED
@@ -7,74 +7,195 @@ 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";
13
114
  import { dirname, join } from "node:path";
14
115
  var SERVER_NAME = "langlink";
15
116
  var PACKAGE = "@langlink/l10n-mcp";
16
- function stdio(name) {
17
- return { command: "npx", args: ["-y", PACKAGE] };
117
+ function packageSpec(updateMode, version) {
118
+ if (updateMode === "pinned") {
119
+ if (!version) throw new Error("--pinned requires an exact version, e.g. --pinned 0.1.1");
120
+ if (!/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/u.test(version)) throw new Error("--pinned version must be exact SemVer");
121
+ return `${PACKAGE}@${version}`;
122
+ }
123
+ return `${PACKAGE}@latest`;
124
+ }
125
+ function stdio(updateMode, version) {
126
+ return { command: "npx", args: ["-y", packageSpec(updateMode, version)] };
127
+ }
128
+ function updateNote(updateMode) {
129
+ return updateMode === "latest" ? "Uses @latest: each new MCP process can pick up the latest release; pin with --pinned <version> for reproducibility." : "Uses an exact pinned version; update by changing the version in the config.";
18
130
  }
19
131
  function json(value) {
20
132
  return `${JSON.stringify(value, null, 2)}
21
133
  `;
22
134
  }
23
- function clientRecipe(client, home = homedir()) {
135
+ function clientRecipe(client, home = homedir(), updateMode = "latest", version) {
136
+ const withUpdate = { updateMode };
24
137
  switch (client) {
25
138
  case "codex":
26
139
  return {
140
+ ...withUpdate,
27
141
  client,
28
142
  path: join(home, ".codex/config.toml"),
29
143
  format: "toml",
30
144
  content: `[mcp_servers.${SERVER_NAME}]
31
145
  command = "npx"
32
- args = ["-y", "${PACKAGE}"]
146
+ args = ["-y", "${packageSpec(updateMode, version)}"]
33
147
  startup_timeout_sec = 30
34
148
  tool_timeout_sec = 120
35
149
  `,
36
150
  notes: [
37
151
  "Codex CLI reads this from ~/.codex/config.toml.",
38
- "Equivalent command: codex mcp add langlink -- npx -y @langlink/l10n-mcp"
152
+ `Equivalent command: codex mcp add langlink -- npx -y ${packageSpec(updateMode, version)}`,
153
+ updateNote(updateMode)
39
154
  ]
40
155
  };
41
156
  case "workbuddy":
42
157
  return {
158
+ ...withUpdate,
43
159
  client,
44
160
  path: join(home, ".workbuddy/mcp.json"),
45
161
  format: "json",
46
- content: json({ mcpServers: { [SERVER_NAME]: stdio(SERVER_NAME) } }),
47
- notes: ["WorkBuddy also exposes MCP configuration in its UI."]
162
+ content: json({ mcpServers: { [SERVER_NAME]: stdio(updateMode, version) } }),
163
+ notes: ["WorkBuddy also exposes MCP configuration in its UI.", updateNote(updateMode)]
48
164
  };
49
165
  case "zcode":
50
166
  return {
167
+ ...withUpdate,
51
168
  client,
52
169
  path: join(home, ".zcode/cli/config.json"),
53
170
  format: "json",
54
- content: json({ mcp: { servers: { [SERVER_NAME]: { ...stdio(SERVER_NAME), env: {} } } } }),
171
+ content: json({ mcp: { servers: { [SERVER_NAME]: { ...stdio(updateMode, version), env: {} } } } }),
55
172
  notes: [
56
173
  "ZCode user config is ~/.zcode/cli/config.json with mcp.servers.",
57
- "Alternatively merge the generic mcpServers shape into ~/.agents/mcp.json."
174
+ "Alternatively merge the generic mcpServers shape into ~/.agents/mcp.json.",
175
+ updateNote(updateMode)
58
176
  ]
59
177
  };
60
178
  case "trae":
61
179
  return {
180
+ ...withUpdate,
62
181
  client,
63
182
  path: null,
64
183
  format: "json",
65
- content: json({ mcpServers: { [SERVER_NAME]: stdio(SERVER_NAME) } }),
184
+ content: json({ mcpServers: { [SERVER_NAME]: stdio(updateMode, version) } }),
66
185
  notes: [
67
186
  "Trae manages MCP servers through its MCP settings UI; paste the JSON snippet there.",
68
- "No stable user-level config path was verified, so this command never writes Trae config."
187
+ "No stable user-level config path was verified, so this command never writes Trae config.",
188
+ updateNote(updateMode)
69
189
  ]
70
190
  };
71
191
  case "agents":
72
192
  return {
193
+ ...withUpdate,
73
194
  client,
74
195
  path: join(home, ".agents/mcp.json"),
75
196
  format: "json",
76
- content: json({ mcpServers: { [SERVER_NAME]: stdio(SERVER_NAME) } }),
77
- notes: ["Shared ~/.agents/mcp.json convention; ZCode can import this file."]
197
+ content: json({ mcpServers: { [SERVER_NAME]: stdio(updateMode, version) } }),
198
+ notes: ["Shared ~/.agents/mcp.json convention; ZCode can import this file.", updateNote(updateMode)]
78
199
  };
79
200
  }
80
201
  }
@@ -2651,7 +2772,13 @@ async function runConfigCommand(args) {
2651
2772
  if (clients.length === 0) {
2652
2773
  throw new Error("--client must be one of codex, workbuddy, zcode, trae, agents, all");
2653
2774
  }
2654
- const recipes = clients.map((client) => clientRecipe(client, home));
2775
+ const pinnedIndex = args.indexOf("--pinned");
2776
+ const pinnedVersion = pinnedIndex >= 0 ? args[pinnedIndex + 1] : void 0;
2777
+ const updateMode = pinnedIndex >= 0 ? "pinned" : "latest";
2778
+ if (updateMode === "pinned" && !pinnedVersion) {
2779
+ throw new Error("--pinned requires an exact version, e.g. --pinned 0.1.1");
2780
+ }
2781
+ const recipes = clients.map((client) => clientRecipe(client, home, updateMode, pinnedVersion));
2655
2782
  if (write) {
2656
2783
  const results = [];
2657
2784
  for (const recipe of recipes) results.push(await writeRecipe(recipe));
@@ -2681,7 +2808,27 @@ async function main() {
2681
2808
  return;
2682
2809
  }
2683
2810
  if (command === "--help" || command === "-h") {
2684
- console.log("Usage: langlink-l10n-mcp [config [--client codex|workbuddy|zcode|trae|agents|all] [--write] [--json] [--home DIR]]");
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}`);
2685
2832
  return;
2686
2833
  }
2687
2834
  const server = createL10nMcpServer({