serena-mcp-gateway 1.3.0 → 1.3.2

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/README.md CHANGED
@@ -74,12 +74,18 @@ serena-mcp --config ./projects.json
74
74
  | `-p, --port <port>` | 起始本地端口(默认 `9121`,被占用会自动顺延) |
75
75
  | `-t, --token <token>` | 指定固定 Bearer Token(至少 32 位;默认每次随机生成) |
76
76
  | `--context <name>` | 传给 Serena 的 `--context`(默认 `claude-code`) |
77
- | `--serena-ref <ref>` | 默认使用上游 `v2.1.0`,可指定其他 release tag 或完整 40 位 commit SHA |
77
+ | `--serena-ref <ref>` | 默认使用上游 `v2.1.0` 固定 commit SHA(已缓存时跳过远程 `git fetch`),可指定其他 release tag 或完整 40 位 commit SHA |
78
+ | `--timeout <sec>` | 等待 Serena 就绪的超时秒数(默认 `300`) |
78
79
  | `--allow-remote-install` | 允许在需要时下载并执行 astral.sh 的 uv 安装脚本 |
79
80
  | `--no-tunnel` | 仅启动本地 Serena + OAuth 代理,不启动 `cloudflared` |
81
+ | `--tunnel-direct` | 启动 `cloudflared` 时清除代理环境变量(直连模式) |
82
+ | `--tunnel-token <token>` | 使用 Cloudflare Named Tunnel Token(建议通过 `CLOUDFLARE_TUNNEL_TOKEN` 环境变量传入,彻底避开临时隧道 429 限流) |
83
+ | `--tunnel-url <url>` | 配合 `--tunnel-token` 指定你的公网 `https://` 域名(也可用 `CLOUDFLARE_TUNNEL_URL`) |
80
84
  | `--setup` | 仅检查并自动安装缺失依赖(`uvx` / `cloudflared`),可结合 `--no-tunnel` 仅检查本地模式依赖 |
81
85
  | `--no-auto-install` | 缺依赖时仅提示手动命令,不自动安装 |
82
86
  | `-v, --version` | 查看版本 |
83
87
  | `-h, --help` | 查看帮助 |
84
88
 
85
89
  从源码全局安装时,可运行 `./install.sh --no-tunnel` 或 `./install.ps1 --no-tunnel`。安装脚本使用同一套依赖检查,接受 `--no-auto-install` 和 `--allow-remote-install`。
90
+
91
+ > **VPN / TUN 模式提示**:如果开启了 VPN 的 TUN 模式导致 `cloudflared` 报 `429` 或无法建立隧道,请在 VPN 规则中将 `cloudflared` 进程或 `*.trycloudflare.com`、`*.argotunnel.com` 设为 `DIRECT`(直连),也可使用 `--tunnel-direct`、切换代理节点、`CLOUDFLARE_TUNNEL_TOKEN`(自有 Named Tunnel)或 `serena-mcp --no-tunnel`。
package/installer.mjs CHANGED
@@ -343,11 +343,16 @@ export async function ensureDependencies({
343
343
  }
344
344
 
345
345
  if (missing.includes('cloudflared')) {
346
- const ok = await installCloudflared();
347
- if (ok) {
348
- console.log('[Setup] cloudflared 安装成功!');
349
- } else {
350
- console.error('[Setup] cloudflared 自动安装失败。');
346
+ try {
347
+ const ok = await installCloudflared();
348
+ if (ok) {
349
+ console.log('[Setup] cloudflared 安装成功!');
350
+ } else {
351
+ console.error('[Setup] cloudflared 自动安装失败。');
352
+ }
353
+ } catch (error) {
354
+ console.error(`[Setup] cloudflared 自动安装失败: ${error.message}`);
355
+ console.error('[Hint] 若仅在本机使用,可改用 --no-tunnel 跳过 cloudflared。');
351
356
  }
352
357
  }
353
358
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serena-mcp-gateway",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Cross-platform Serena MCP launcher with OAuth 2.1 proxy, Cloudflare Tunnel, and multi-project parallel routing",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/serena-mcp.mjs CHANGED
@@ -9,7 +9,9 @@ import { spawn, spawnSync } from 'node:child_process';
9
9
  import { startAuthProxy } from './auth-proxy.mjs';
10
10
  import { ensureDependencies, hydratePath } from './installer.mjs';
11
11
 
12
- const DEFAULT_SERENA_REF = 'v2.1.0';
12
+ // Pinned commit SHA for Serena v2.1.0 so uvx skips remote `git fetch` on cached runs.
13
+ const DEFAULT_SERENA_REF = '5e4f5cd173c7ff3eed37ee3f1de6c266b937c5dc';
14
+ const DEFAULT_STARTUP_TIMEOUT_SEC = 300;
13
15
  const IS_WIN = process.platform === 'win32';
14
16
  const PKG_VERSION = JSON.parse(
15
17
  fs.readFileSync(
@@ -33,10 +35,14 @@ Serena MCP Gateway (跨平台单项目 / 多项目并行启动器)
33
35
  -c, --config <path> 项目配置 JSON(支持数组或 {"别名": "路径"} 对象)
34
36
  -p, --port <number> 起始本地端口(默认 9121,冲突时自动顺延,支持多终端并行)
35
37
  -t, --token <token> 自定义 Bearer Token(>=32 字符;默认每次随机生成)
36
- --serena-ref <ref> Serena release tag 或完整 40 位 commit SHA(默认 v2.1.0)
38
+ --serena-ref <ref> Serena release tag 或完整 40 位 commit SHA(默认 v2.1.0 固定 SHA)
39
+ --timeout <sec> 等待 Serena 就绪的超时秒数(默认 300)
37
40
  --allow-remote-install 允许执行 uv 官方远端安装脚本
38
41
  --context <name> 传给 Serena 的 --context(默认 claude-code)
39
42
  --no-tunnel 仅启动本地 Serena + OAuth 代理,不启动 cloudflared
43
+ --tunnel-direct 启动 cloudflared 时清除代理环境变量(直连模式)
44
+ --tunnel-token <t> 使用 Cloudflare Named Tunnel Token(建议用 CLOUDFLARE_TUNNEL_TOKEN 环境变量,避开 429)
45
+ --tunnel-url <url> 配合 --tunnel-token 指定公网 HTTPS 域名(如 https://mcp.example.com)
40
46
  --setup 仅检查并自动安装缺失依赖(uvx / cloudflared),不启动服务
41
47
  --no-auto-install 缺依赖时仅提示手动安装命令,不自动安装
42
48
  -v, --version 显示当前版本号
@@ -120,9 +126,13 @@ function parseArgs(argv) {
120
126
  basePort: 9121,
121
127
  token: process.env.MCP_TOKEN || '',
122
128
  serenaRef: DEFAULT_SERENA_REF,
129
+ startupTimeoutSec: DEFAULT_STARTUP_TIMEOUT_SEC,
123
130
  allowRemoteInstall: false,
124
131
  context: 'claude-code',
125
132
  noTunnel: false,
133
+ tunnelDirect: false,
134
+ tunnelToken: process.env.CLOUDFLARE_TUNNEL_TOKEN || '',
135
+ tunnelUrl: process.env.CLOUDFLARE_TUNNEL_URL || '',
126
136
  setup: false,
127
137
  noAutoInstall: false,
128
138
  version: false,
@@ -143,6 +153,17 @@ function parseArgs(argv) {
143
153
  opts.version = true;
144
154
  } else if (arg === '--no-tunnel') {
145
155
  opts.noTunnel = true;
156
+ } else if (arg === '--tunnel-direct') {
157
+ opts.tunnelDirect = true;
158
+ } else if (arg === '--tunnel-token') {
159
+ opts.tunnelToken = readValue(arg, argv[++i]);
160
+ console.warn('[Security] 命令行 Tunnel Token 可能出现在历史记录中,建议使用 CLOUDFLARE_TUNNEL_TOKEN 环境变量。');
161
+ } else if (arg === '--tunnel-url') {
162
+ const rawUrl = readValue(arg, argv[++i]).replace(/\/+$/, '');
163
+ if (!/^https:\/\/[a-z0-9.-]+(?::\d+)?$/i.test(rawUrl)) {
164
+ throw new Error(`无效 --tunnel-url(需为 https:// 域名): ${rawUrl}`);
165
+ }
166
+ opts.tunnelUrl = rawUrl;
146
167
  } else if (arg === '--setup') {
147
168
  opts.setup = true;
148
169
  } else if (arg === '--no-auto-install') {
@@ -160,6 +181,12 @@ function parseArgs(argv) {
160
181
  opts.context = readValue(arg, argv[++i]);
161
182
  } else if (arg === '--allow-remote-install') {
162
183
  opts.allowRemoteInstall = true;
184
+ } else if (arg === '--timeout') {
185
+ const val = Number(readValue(arg, argv[++i]));
186
+ if (!Number.isInteger(val) || val < 10 || val > 3600) {
187
+ throw new Error(`无效超时时间(需 10~3600 秒): ${argv[i]}`);
188
+ }
189
+ opts.startupTimeoutSec = val;
163
190
  } else if (arg === '--serena-ref') {
164
191
  opts.serenaRef = readValue(arg, argv[++i]);
165
192
  if (!/^(?:[0-9a-f]{40}|v[0-9]+\.[0-9]+\.[0-9]+(?:-[A-Za-z0-9.-]+)?)$/i.test(opts.serenaRef)) {
@@ -225,20 +252,34 @@ function releasePort(port, locks) {
225
252
  }
226
253
  }
227
254
 
228
- function waitForPort(port, label, child, timeoutMs = 120_000) {
255
+ function waitForPort(port, label, child, timeoutMs = DEFAULT_STARTUP_TIMEOUT_SEC * 1000) {
229
256
  const start = Date.now();
230
257
  return new Promise((resolve, reject) => {
231
258
  let settled = false;
259
+ const hintTimer = setTimeout(() => {
260
+ if (!settled) {
261
+ console.log(
262
+ `[Wait] 仍在等待 Serena [${label}] 就绪(若首次运行正在下载依赖,可能需要 1~3 分钟)...`
263
+ );
264
+ }
265
+ }, 15_000);
266
+
267
+ const cleanup = () => {
268
+ clearTimeout(hintTimer);
269
+ child.off('exit', onExit);
270
+ child.off('error', onError);
271
+ };
232
272
 
233
273
  const onExit = (code) => {
234
274
  if (!settled) {
235
275
  settled = true;
276
+ cleanup();
236
277
  reject(new Error(`Serena [${label}] 在就绪前意外退出 (code=${code})`));
237
278
  }
238
279
  };
239
280
  const onError = (error) => {
240
281
  settled = true;
241
- child.off('exit', onExit);
282
+ cleanup();
242
283
  reject(error);
243
284
  };
244
285
  child.once('exit', onExit);
@@ -251,8 +292,7 @@ function waitForPort(port, label, child, timeoutMs = 120_000) {
251
292
  socket.destroy();
252
293
  if (settled) return;
253
294
  settled = true;
254
- child.off('exit', onExit);
255
- child.off('error', onError);
295
+ cleanup();
256
296
  resolve();
257
297
  });
258
298
  socket.once('error', () => {
@@ -260,9 +300,13 @@ function waitForPort(port, label, child, timeoutMs = 120_000) {
260
300
  if (settled) return;
261
301
  if (Date.now() - start >= timeoutMs) {
262
302
  settled = true;
263
- child.off('exit', onExit);
264
- child.off('error', onError);
265
- reject(new Error(`等待 Serena [${label}] (端口 ${port}) 就绪超时`));
303
+ cleanup();
304
+ reject(
305
+ new Error(
306
+ `等待 Serena [${label}] (端口 ${port}) 就绪超时(${Math.round(timeoutMs / 1000)}s)。` +
307
+ '若开了 VPN TUN 模式或首次拉取较慢,可尝试关闭 TUN、使用 --timeout 600 或 --no-tunnel。'
308
+ )
309
+ );
266
310
  return;
267
311
  }
268
312
  setTimeout(probe, 500);
@@ -443,7 +487,7 @@ async function main() {
443
487
  pipePrefixed(child.stdout, `[serena:${target.name}]`, process.stdout);
444
488
  pipePrefixed(child.stderr, `[serena:${target.name}]`, process.stderr);
445
489
 
446
- await waitForPort(port, target.name, child);
490
+ await waitForPort(port, target.name, child, opts.startupTimeoutSec * 1000);
447
491
  child.on('exit', (code, signal) => {
448
492
  if (shuttingDown) return;
449
493
  console.error(`[Error] Serena [${target.name}] 已退出 (code=${code}, signal=${signal})`);
@@ -498,24 +542,45 @@ async function main() {
498
542
  return;
499
543
  }
500
544
 
501
- console.log(`[Start] 启动 Cloudflare Tunnel -> http://127.0.0.1:${proxyPort}`);
502
- const cf = spawn(
503
- 'cloudflared',
504
- ['tunnel', '--protocol', 'http2', '--url', `http://127.0.0.1:${proxyPort}`],
505
- {
506
- stdio: ['ignore', 'pipe', 'pipe'],
507
- shell: false,
508
- detached: !IS_WIN,
509
- env: childEnv,
545
+ const cfEnv = { ...childEnv };
546
+ delete cfEnv.CLOUDFLARE_TUNNEL_TOKEN;
547
+ if (opts.tunnelDirect) {
548
+ for (const key of ['HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'http_proxy', 'https_proxy', 'all_proxy']) {
549
+ delete cfEnv[key];
510
550
  }
551
+ console.log('[Tunnel] 已启用 --tunnel-direct:已清除 cloudflared 代理环境变量(若开 TUN 请同时在代理软件中放行 DIRECT)。');
552
+ }
553
+
554
+ const useNamedTunnel = Boolean(opts.tunnelToken);
555
+ const cfArgs = useNamedTunnel
556
+ ? ['tunnel', '--protocol', 'http2', 'run', '--token', opts.tunnelToken]
557
+ : ['tunnel', '--protocol', 'http2', '--url', `http://127.0.0.1:${proxyPort}`];
558
+
559
+ console.log(
560
+ `[Start] 启动 Cloudflare ${useNamedTunnel ? 'Named ' : ''}Tunnel -> http://127.0.0.1:${proxyPort}`
511
561
  );
562
+ const cf = spawn('cloudflared', cfArgs, {
563
+ stdio: ['ignore', 'pipe', 'pipe'],
564
+ shell: false,
565
+ detached: !IS_WIN,
566
+ env: cfEnv,
567
+ });
512
568
  children.push(cf);
513
569
 
514
570
  let printedUrl = false;
571
+ let tunnelRateLimited = false;
572
+ if (useNamedTunnel && opts.tunnelUrl) {
573
+ printedUrl = true;
574
+ printEndpoints(opts.tunnelUrl);
575
+ }
576
+
515
577
  const watchTunnelUrl = (chunk) => {
516
578
  const text = chunk.toString();
517
579
  process.stderr.write(text);
518
- if (!printedUrl) {
580
+ if (/status 429|Tunnelstore request failed/i.test(text)) {
581
+ tunnelRateLimited = true;
582
+ }
583
+ if (!printedUrl && !useNamedTunnel) {
519
584
  const match = text.match(/https:\/\/[a-z0-9-]+\.trycloudflare\.com/i);
520
585
  if (match) {
521
586
  printedUrl = true;
@@ -533,6 +598,20 @@ async function main() {
533
598
  cf.on('exit', (code) => {
534
599
  if (!shuttingDown) {
535
600
  console.error(`[Error] cloudflared 已退出 (code=${code})`);
601
+ if (tunnelRateLimited) {
602
+ console.error(
603
+ '[Hint] Cloudflare 临时隧道触发 429 限流(通常是 VPN/TUN 节点共享出口 IP 请求过多所致)。\n' +
604
+ ' 可选解决方式(无需关闭 TUN):\n' +
605
+ ' 1) 在代理规则中将 cloudflared / *.trycloudflare.com / *.argotunnel.com 设为 DIRECT(直连);\n' +
606
+ ' 2) 切换冷门代理节点或稍等 1~5 分钟重试(若走环境变量代理可加 --tunnel-direct);\n' +
607
+ ' 3) 使用自有 Cloudflare Named Tunnel(设置 CLOUDFLARE_TUNNEL_TOKEN 或 --tunnel-token);\n' +
608
+ ' 4) 仅本机使用时加 --no-tunnel。'
609
+ );
610
+ } else if (!printedUrl) {
611
+ console.error(
612
+ '[Hint] Cloudflare Tunnel 未能建立连接。若开启了 VPN TUN 模式,请在代理规则中将 cloudflared / *.trycloudflare.com / *.argotunnel.com 设为 DIRECT(直连),或尝试 --tunnel-direct / --no-tunnel。'
613
+ );
614
+ }
536
615
  shutdown(code || 1);
537
616
  }
538
617
  });