serena-mcp-gateway 1.0.0 → 1.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.
package/README.md CHANGED
@@ -4,24 +4,21 @@
4
4
 
5
5
  ---
6
6
 
7
- ## 一、给同事复用(下载即用)
7
+ ## 一、安装即用(Windows / macOS / Linux)
8
8
 
9
- 将本目录打包发给同事,或推送到公司 Git 仓库(如 `git clone <repo> serena-mcp-gateway`),然后执行对应平台的一键脚本:
10
-
11
- ### macOS / Linux
12
9
  ```bash
13
- cd serena-mcp-gateway
14
- ./install.sh
15
- ```
10
+ # 全局安装(推荐)
11
+ npm i -g serena-mcp-gateway
16
12
 
17
- ### Windows (PowerShell)
18
- ```powershell
19
- cd serena-mcp-gateway
20
- powershell -ExecutionPolicy Bypass -File .\install.ps1
13
+ # 或免安装直接运行
14
+ npx serena-mcp-gateway ./proj-a ./proj-b
21
15
  ```
22
16
 
23
- > 一键脚本会自动检查并安装缺失的 `uv`(提供 `uvx`)与 `cloudflared`,并通过 `npm install -g .` 注册全局命令 `serena-mcp`(无需手动改 `.zshrc` 或环境变量)。
24
- > 不想全局安装也可以直接运行:`node serena-mcp.mjs <项目路径>`(Windows 也可直接运行 `start-serena.cmd`)。
17
+ > **内置依赖自动安装**:首次运行时如果本机缺少 `uvx` 或 `cloudflared`,工具会按当前系统自动选择可用方式安装并立即生效(无需重开终端):
18
+ > - **Windows**:自动依次尝试 `winget` → `scoop` → `choco` → 官方 PowerShell 脚本(`uv`)/ GitHub Releases 直下二进制到 `~/.serena-mcp/bin`(`cloudflared`,无需 `winget` 和管理员权限)。
19
+ > - **macOS**:自动尝试 `brew` → 官方脚本 / GitHub Releases 直下二进制。
20
+ > - **Linux**:自动使用官方脚本 / 静态二进制。
21
+ > - 也可单独执行 `serena-mcp --setup` 预先完成环境自检与安装。
25
22
 
26
23
  ---
27
24
 
@@ -73,4 +70,6 @@ serena-mcp --config ./projects.json
73
70
  | `-t, --token <token>` | 指定固定 Bearer Token(至少 32 位;默认每次随机生成) |
74
71
  | `--context <name>` | 传给 Serena 的 `--context`(默认 `claude-code`) |
75
72
  | `--no-tunnel` | 仅启动本地 Serena + OAuth 代理,不启动 `cloudflared` |
73
+ | `--setup` | 仅检查并自动安装缺失依赖(`uvx` / `cloudflared`),不启动服务 |
74
+ | `--no-auto-install` | 缺依赖时仅提示手动命令,不自动安装 |
76
75
  | `-h, --help` | 查看帮助 |
package/installer.mjs ADDED
@@ -0,0 +1,388 @@
1
+ import fs from 'node:fs';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { spawnSync } from 'node:child_process';
5
+
6
+ const IS_WIN = process.platform === 'win32';
7
+ const IS_MAC = process.platform === 'darwin';
8
+ const LOCAL_BIN_DIR = path.join(os.homedir(), '.serena-mcp', 'bin');
9
+
10
+ function getExtraSearchDirs() {
11
+ const home = os.homedir();
12
+ const dirs = [
13
+ LOCAL_BIN_DIR,
14
+ path.join(home, '.local', 'bin'),
15
+ path.join(home, '.cargo', 'bin'),
16
+ ];
17
+
18
+ if (IS_WIN) {
19
+ const localAppData =
20
+ process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local');
21
+ const programFiles = process.env.ProgramFiles || 'C:\\Program Files';
22
+ const programFilesX86 =
23
+ process.env['ProgramFiles(x86)'] || 'C:\\Program Files (x86)';
24
+ dirs.push(
25
+ path.join(localAppData, 'Microsoft', 'WinGet', 'Links'),
26
+ path.join(localAppData, 'Programs', 'uv'),
27
+ path.join(home, 'scoop', 'shims'),
28
+ path.join(programFiles, 'cloudflared'),
29
+ path.join(programFilesX86, 'cloudflared'),
30
+ 'C:\\ProgramData\\chocolatey\\bin'
31
+ );
32
+ } else if (IS_MAC) {
33
+ dirs.push('/opt/homebrew/bin', '/usr/local/bin');
34
+ } else {
35
+ dirs.push('/usr/local/bin');
36
+ }
37
+
38
+ return dirs;
39
+ }
40
+
41
+ /**
42
+ * Add common user/package-manager bin directories to process.env.PATH
43
+ * so freshly installed binaries work immediately without restarting the shell.
44
+ */
45
+ export function hydratePath() {
46
+ const sep = path.delimiter;
47
+ const current = (process.env.PATH || '').split(sep).filter(Boolean);
48
+ const currentSet = new Set(
49
+ current.map((p) => (IS_WIN ? p.toLowerCase() : p))
50
+ );
51
+
52
+ const toPrepend = [];
53
+ for (const dir of getExtraSearchDirs()) {
54
+ const key = IS_WIN ? dir.toLowerCase() : dir;
55
+ if (!currentSet.has(key) && fs.existsSync(dir)) {
56
+ toPrepend.push(dir);
57
+ currentSet.add(key);
58
+ }
59
+ }
60
+
61
+ if (toPrepend.length > 0) {
62
+ process.env.PATH = [...toPrepend, ...current].join(sep);
63
+ }
64
+ }
65
+
66
+ export function commandExists(cmd) {
67
+ const res = spawnSync(cmd, ['--version'], {
68
+ stdio: 'ignore',
69
+ shell: IS_WIN,
70
+ });
71
+ return !res.error && res.status === 0;
72
+ }
73
+
74
+ function runStep(label, cmd, args, extraOpts = {}) {
75
+ console.log(`[Setup] ${label}...`);
76
+ const res = spawnSync(cmd, args, {
77
+ stdio: 'inherit',
78
+ shell: IS_WIN,
79
+ ...extraOpts,
80
+ });
81
+ return !res.error && res.status === 0;
82
+ }
83
+
84
+ async function downloadFile(url, destPath) {
85
+ fs.mkdirSync(path.dirname(destPath), { recursive: true });
86
+ const res = await fetch(url, { redirect: 'follow' });
87
+ if (!res.ok) {
88
+ throw new Error(`HTTP ${res.status} ${res.statusText}`);
89
+ }
90
+ const buf = Buffer.from(await res.arrayBuffer());
91
+ const tmpPath = `${destPath}.tmp-${Date.now()}`;
92
+ fs.writeFileSync(tmpPath, buf);
93
+ fs.renameSync(tmpPath, destPath);
94
+ }
95
+
96
+ async function installUv() {
97
+ if (IS_WIN) {
98
+ if (commandExists('winget')) {
99
+ if (
100
+ runStep('通过 winget 安装 uv', 'winget', [
101
+ 'install',
102
+ '--id',
103
+ 'astral-sh.uv',
104
+ '-e',
105
+ '--accept-source-agreements',
106
+ '--accept-package-agreements',
107
+ ])
108
+ ) {
109
+ hydratePath();
110
+ if (commandExists('uvx')) return true;
111
+ }
112
+ }
113
+
114
+ if (commandExists('scoop')) {
115
+ if (runStep('通过 scoop 安装 uv', 'scoop', ['install', 'uv'])) {
116
+ hydratePath();
117
+ if (commandExists('uvx')) return true;
118
+ }
119
+ }
120
+
121
+ if (commandExists('choco')) {
122
+ if (runStep('通过 choco 安装 uv', 'choco', ['install', 'uv', '-y'])) {
123
+ hydratePath();
124
+ if (commandExists('uvx')) return true;
125
+ }
126
+ }
127
+
128
+ // Zero-dependency official PowerShell installer (works without winget or admin)
129
+ if (
130
+ runStep(
131
+ '未检测到 winget/scoop,改用官方 PowerShell 脚本安装 uv',
132
+ 'powershell',
133
+ [
134
+ '-NoProfile',
135
+ '-ExecutionPolicy',
136
+ 'ByPass',
137
+ '-Command',
138
+ 'irm https://astral.sh/uv/install.ps1 | iex',
139
+ ],
140
+ { shell: false }
141
+ )
142
+ ) {
143
+ hydratePath();
144
+ if (commandExists('uvx')) return true;
145
+ }
146
+
147
+ if (commandExists('pip')) {
148
+ if (runStep('尝试通过 pip 安装 uv', 'pip', ['install', '--user', 'uv'])) {
149
+ hydratePath();
150
+ if (commandExists('uvx')) return true;
151
+ }
152
+ }
153
+
154
+ return false;
155
+ }
156
+
157
+ if (IS_MAC && commandExists('brew')) {
158
+ if (runStep('通过 Homebrew 安装 uv', 'brew', ['install', 'uv'])) {
159
+ hydratePath();
160
+ if (commandExists('uvx')) return true;
161
+ }
162
+ }
163
+
164
+ // macOS (without brew) / Linux fallback: official shell script
165
+ if (
166
+ runStep(
167
+ '通过官方脚本安装 uv',
168
+ 'sh',
169
+ ['-c', 'curl -LsSf https://astral.sh/uv/install.sh | sh'],
170
+ { shell: false }
171
+ )
172
+ ) {
173
+ hydratePath();
174
+ if (commandExists('uvx')) return true;
175
+ }
176
+
177
+ return false;
178
+ }
179
+
180
+ async function downloadCloudflaredDirect() {
181
+ fs.mkdirSync(LOCAL_BIN_DIR, { recursive: true });
182
+
183
+ if (IS_WIN) {
184
+ const arch = process.arch === 'ia32' ? '386' : 'amd64';
185
+ const url = `https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-${arch}.exe`;
186
+ const dest = path.join(LOCAL_BIN_DIR, 'cloudflared.exe');
187
+ console.log(`[Setup] 直接下载 cloudflared (${arch}) -> ${dest}`);
188
+ await downloadFile(url, dest);
189
+ hydratePath();
190
+ return commandExists('cloudflared');
191
+ }
192
+
193
+ if (IS_MAC) {
194
+ const arch = process.arch === 'arm64' ? 'arm64' : 'amd64';
195
+ const url = `https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-darwin-${arch}.tgz`;
196
+ const tgzPath = path.join(LOCAL_BIN_DIR, `cloudflared-darwin-${arch}.tgz`);
197
+ const dest = path.join(LOCAL_BIN_DIR, 'cloudflared');
198
+ console.log(`[Setup] 直接下载 cloudflared (${arch}) -> ${dest}`);
199
+ await downloadFile(url, tgzPath);
200
+ const extracted = runStep(
201
+ '解压 cloudflared',
202
+ 'tar',
203
+ ['-xzf', tgzPath, '-C', LOCAL_BIN_DIR],
204
+ { shell: false }
205
+ );
206
+ try {
207
+ fs.unlinkSync(tgzPath);
208
+ } catch {
209
+ // ignore
210
+ }
211
+ if (extracted && fs.existsSync(dest)) {
212
+ fs.chmodSync(dest, 0o755);
213
+ hydratePath();
214
+ return commandExists('cloudflared');
215
+ }
216
+ return false;
217
+ }
218
+
219
+ // Linux static binary
220
+ const archMap = { x64: 'amd64', arm64: 'arm64', arm: 'arm', ia32: '386' };
221
+ const arch = archMap[process.arch] || 'amd64';
222
+ const url = `https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-${arch}`;
223
+ const dest = path.join(LOCAL_BIN_DIR, 'cloudflared');
224
+ console.log(`[Setup] 直接下载 cloudflared (${arch}) -> ${dest}`);
225
+ await downloadFile(url, dest);
226
+ fs.chmodSync(dest, 0o755);
227
+ hydratePath();
228
+ return commandExists('cloudflared');
229
+ }
230
+
231
+ async function installCloudflared() {
232
+ if (IS_WIN) {
233
+ if (commandExists('winget')) {
234
+ if (
235
+ runStep('通过 winget 安装 cloudflared', 'winget', [
236
+ 'install',
237
+ '--id',
238
+ 'Cloudflare.cloudflared',
239
+ '-e',
240
+ '--accept-source-agreements',
241
+ '--accept-package-agreements',
242
+ ])
243
+ ) {
244
+ hydratePath();
245
+ if (commandExists('cloudflared')) return true;
246
+ }
247
+ }
248
+
249
+ if (commandExists('scoop')) {
250
+ if (
251
+ runStep('通过 scoop 安装 cloudflared', 'scoop', [
252
+ 'install',
253
+ 'cloudflared',
254
+ ])
255
+ ) {
256
+ hydratePath();
257
+ if (commandExists('cloudflared')) return true;
258
+ }
259
+ }
260
+
261
+ if (commandExists('choco')) {
262
+ if (
263
+ runStep('通过 choco 安装 cloudflared', 'choco', [
264
+ 'install',
265
+ 'cloudflared',
266
+ '-y',
267
+ ])
268
+ ) {
269
+ hydratePath();
270
+ if (commandExists('cloudflared')) return true;
271
+ }
272
+ }
273
+
274
+ try {
275
+ return await downloadCloudflaredDirect();
276
+ } catch (err) {
277
+ console.error(`[Setup] 直接下载 cloudflared 失败: ${err.message}`);
278
+ return false;
279
+ }
280
+ }
281
+
282
+ if (IS_MAC && commandExists('brew')) {
283
+ if (
284
+ runStep('通过 Homebrew 安装 cloudflared', 'brew', [
285
+ 'install',
286
+ 'cloudflared',
287
+ ])
288
+ ) {
289
+ hydratePath();
290
+ if (commandExists('cloudflared')) return true;
291
+ }
292
+ }
293
+
294
+ try {
295
+ return await downloadCloudflaredDirect();
296
+ } catch (err) {
297
+ console.error(`[Setup] 直接下载 cloudflared 失败: ${err.message}`);
298
+ return false;
299
+ }
300
+ }
301
+
302
+ function printManualGuide(missing) {
303
+ console.error(`\n[缺少依赖] 仍未找到: ${missing.join(', ')}\n`);
304
+ if (IS_MAC) {
305
+ const pkgs = [];
306
+ if (missing.includes('uvx')) pkgs.push('uv');
307
+ if (missing.includes('cloudflared')) pkgs.push('cloudflared');
308
+ console.error(`macOS 手动安装:\n brew install ${pkgs.join(' ')}\n`);
309
+ } else if (IS_WIN) {
310
+ console.error('Windows 手动安装 (无需 winget):');
311
+ if (missing.includes('uvx')) {
312
+ console.error(
313
+ ' uv: powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"'
314
+ );
315
+ }
316
+ if (missing.includes('cloudflared')) {
317
+ console.error(
318
+ ' cloudflared (.msi): https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.msi'
319
+ );
320
+ }
321
+ console.error('\n安装完成后重新打开终端再运行。\n');
322
+ } else {
323
+ if (missing.includes('uvx')) {
324
+ console.error(' uv: curl -LsSf https://astral.sh/uv/install.sh | sh');
325
+ }
326
+ if (missing.includes('cloudflared')) {
327
+ console.error(
328
+ ' cloudflared: https://github.com/cloudflare/cloudflared/releases/latest'
329
+ );
330
+ }
331
+ console.error('');
332
+ }
333
+ }
334
+
335
+ /**
336
+ * Ensure required external binaries (uvx, and optionally cloudflared) are available.
337
+ * Automatically installs missing tools using the best method available on the host OS.
338
+ */
339
+ export async function ensureDependencies({
340
+ needTunnel = true,
341
+ autoInstall = true,
342
+ } = {}) {
343
+ hydratePath();
344
+
345
+ const checkMissing = () => {
346
+ const list = [];
347
+ if (!commandExists('uvx')) list.push('uvx');
348
+ if (needTunnel && !commandExists('cloudflared')) list.push('cloudflared');
349
+ return list;
350
+ };
351
+
352
+ let missing = checkMissing();
353
+ if (missing.length === 0) return;
354
+
355
+ if (!autoInstall) {
356
+ printManualGuide(missing);
357
+ process.exit(1);
358
+ }
359
+
360
+ console.log(
361
+ `\n[Setup] 检测到缺少依赖: ${missing.join(', ')},正在按当前系统自动选择合适方式安装...\n`
362
+ );
363
+
364
+ if (missing.includes('uvx')) {
365
+ const ok = await installUv();
366
+ if (ok) {
367
+ console.log('[Setup] uv / uvx 安装成功!');
368
+ } else {
369
+ console.error('[Setup] uv / uvx 自动安装失败。');
370
+ }
371
+ }
372
+
373
+ if (missing.includes('cloudflared')) {
374
+ const ok = await installCloudflared();
375
+ if (ok) {
376
+ console.log('[Setup] cloudflared 安装成功!');
377
+ } else {
378
+ console.error('[Setup] cloudflared 自动安装失败。');
379
+ }
380
+ }
381
+
382
+ hydratePath();
383
+ missing = checkMissing();
384
+ if (missing.length > 0) {
385
+ printManualGuide(missing);
386
+ process.exit(1);
387
+ }
388
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serena-mcp-gateway",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
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",
@@ -21,6 +21,7 @@
21
21
  "files": [
22
22
  "serena-mcp.mjs",
23
23
  "auth-proxy.mjs",
24
+ "installer.mjs",
24
25
  "start-serena.sh",
25
26
  "start-serena.cmd",
26
27
  "install.sh",
package/serena-mcp.mjs CHANGED
@@ -6,9 +6,9 @@ import os from 'node:os';
6
6
  import path from 'node:path';
7
7
  import { spawn, spawnSync } from 'node:child_process';
8
8
  import { startAuthProxy } from './auth-proxy.mjs';
9
+ import { ensureDependencies, hydratePath } from './installer.mjs';
9
10
 
10
11
  const IS_WIN = process.platform === 'win32';
11
- const IS_MAC = process.platform === 'darwin';
12
12
 
13
13
  function printHelp() {
14
14
  console.log(`
@@ -27,6 +27,8 @@ Serena MCP Gateway (跨平台单项目 / 多项目并行启动器)
27
27
  -t, --token <token> 自定义 Bearer Token(>=32 字符;默认每次随机生成)
28
28
  --context <name> 传给 Serena 的 --context(默认 claude-code)
29
29
  --no-tunnel 仅启动本地 Serena + OAuth 代理,不启动 cloudflared
30
+ --setup 仅检查并自动安装缺失依赖(uvx / cloudflared),不启动服务
31
+ --no-auto-install 缺依赖时仅提示手动安装命令,不自动安装
30
32
  -h, --help 显示帮助信息
31
33
 
32
34
  多终端并行:
@@ -108,6 +110,8 @@ function parseArgs(argv) {
108
110
  token: process.env.MCP_TOKEN || '',
109
111
  context: 'claude-code',
110
112
  noTunnel: false,
113
+ setup: false,
114
+ noAutoInstall: false,
111
115
  help: false,
112
116
  projects: [],
113
117
  };
@@ -118,6 +122,10 @@ function parseArgs(argv) {
118
122
  opts.help = true;
119
123
  } else if (arg === '--no-tunnel') {
120
124
  opts.noTunnel = true;
125
+ } else if (arg === '--setup') {
126
+ opts.setup = true;
127
+ } else if (arg === '--no-auto-install') {
128
+ opts.noAutoInstall = true;
121
129
  } else if (arg === '-p' || arg === '--port') {
122
130
  const val = Number(argv[++i]);
123
131
  if (!Number.isInteger(val) || val < 1024 || val > 65535) {
@@ -152,49 +160,6 @@ function parseArgs(argv) {
152
160
  return opts;
153
161
  }
154
162
 
155
- function commandExists(cmd) {
156
- const res = spawnSync(cmd, ['--version'], {
157
- stdio: 'ignore',
158
- shell: IS_WIN,
159
- });
160
- return !res.error && res.status === 0;
161
- }
162
-
163
- function checkDependencies(needTunnel) {
164
- const missing = [];
165
- if (!commandExists('uvx')) missing.push('uvx');
166
- if (needTunnel && !commandExists('cloudflared')) missing.push('cloudflared');
167
-
168
- if (missing.length === 0) return;
169
-
170
- console.error(`\n[缺少依赖] 未在 PATH 中找到: ${missing.join(', ')}\n`);
171
- if (IS_MAC) {
172
- const pkgs = [];
173
- if (missing.includes('uvx')) pkgs.push('uv');
174
- if (missing.includes('cloudflared')) pkgs.push('cloudflared');
175
- console.error(`macOS 一键安装:\n brew install ${pkgs.join(' ')}\n`);
176
- } else if (IS_WIN) {
177
- console.error('Windows 一键安装 (PowerShell):');
178
- if (missing.includes('uvx')) {
179
- console.error(' winget install --id astral-sh.uv -e');
180
- }
181
- if (missing.includes('cloudflared')) {
182
- console.error(' winget install --id Cloudflare.cloudflared -e');
183
- }
184
- console.error('\n安装完成后请重新打开终端再运行。\n');
185
- } else {
186
- console.error('Linux 安装建议:');
187
- if (missing.includes('uvx')) {
188
- console.error(' curl -LsSf https://astral.sh/uv/install.sh | sh');
189
- }
190
- if (missing.includes('cloudflared')) {
191
- console.error(' 参考 https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/');
192
- }
193
- console.error('');
194
- }
195
- process.exit(1);
196
- }
197
-
198
163
  function isPortFree(port) {
199
164
  return new Promise((resolve) => {
200
165
  const tester = net.createServer();
@@ -304,7 +269,18 @@ async function main() {
304
269
  process.exit(0);
305
270
  }
306
271
 
307
- checkDependencies(!opts.noTunnel);
272
+ hydratePath();
273
+
274
+ if (opts.setup) {
275
+ await ensureDependencies({ needTunnel: true, autoInstall: true });
276
+ console.log('\n[OK] 环境依赖检查完成,uvx 与 cloudflared 均已就绪!');
277
+ process.exit(0);
278
+ }
279
+
280
+ await ensureDependencies({
281
+ needTunnel: !opts.noTunnel,
282
+ autoInstall: !opts.noAutoInstall,
283
+ });
308
284
 
309
285
  const token = opts.token || crypto.randomBytes(24).toString('hex');
310
286
  if (token.length < 32) {