genflow-connect 0.1.1 → 0.1.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/cli.mjs +13 -3
- package/package.json +1 -1
- package/server.mjs +94 -46
package/cli.mjs
CHANGED
|
@@ -13,6 +13,14 @@ import { fileURLToPath } from "node:url";
|
|
|
13
13
|
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
14
14
|
const SERVER = join(HERE, "server.mjs");
|
|
15
15
|
const NAME = "genflow-editor";
|
|
16
|
+
// npx 임시 캐시는 정리될 수 있어서, 패키지로 실행 중이면 경로 대신 npx로 서버를 띄우게 등록한다(개발 중 저장소 실행은 파일 경로 그대로)
|
|
17
|
+
const viaPackage = /[\\/](_npx|node_modules)[\\/]/.test(HERE);
|
|
18
|
+
const win = platform() === "win32";
|
|
19
|
+
const SERVE = viaPackage
|
|
20
|
+
? win
|
|
21
|
+
? { command: "cmd", args: ["/c", "npx", "-y", "genflow-connect@0", "serve"] }
|
|
22
|
+
: { command: "npx", args: ["-y", "genflow-connect@0", "serve"] }
|
|
23
|
+
: { command: process.execPath, args: [SERVER] };
|
|
16
24
|
const args = process.argv.slice(2);
|
|
17
25
|
const cmd = args[0] && !args[0].startsWith("--") ? args[0] : "install";
|
|
18
26
|
const flag = (n) => args.includes(`--${n}`);
|
|
@@ -37,8 +45,9 @@ const targets = {
|
|
|
37
45
|
label: "Claude Code",
|
|
38
46
|
available: () => has("claude"),
|
|
39
47
|
install() {
|
|
40
|
-
const a = ["mcp", "add", NAME, "--scope", "user", "--",
|
|
48
|
+
const a = ["mcp", "add", NAME, "--scope", "user", "--", SERVE.command, ...SERVE.args];
|
|
41
49
|
if (dry) return log(` [미리보기] claude ${a.join(" ")}`);
|
|
50
|
+
spawnSync("claude", ["mcp", "remove", NAME, "--scope", "user"], { encoding: "utf8" }); // 다시 설치해도 "이미 있음" 오류가 나지 않게
|
|
42
51
|
const r = spawnSync("claude", a, { encoding: "utf8" });
|
|
43
52
|
if (r.status !== 0) throw new Error((r.stderr || r.stdout || "claude mcp add 실패").trim());
|
|
44
53
|
},
|
|
@@ -47,8 +56,9 @@ const targets = {
|
|
|
47
56
|
label: "Codex",
|
|
48
57
|
available: () => has("codex"),
|
|
49
58
|
install() {
|
|
50
|
-
const a = ["mcp", "add", NAME, "--",
|
|
59
|
+
const a = ["mcp", "add", NAME, "--", SERVE.command, ...SERVE.args];
|
|
51
60
|
if (dry) return log(` [미리보기] codex ${a.join(" ")}`);
|
|
61
|
+
spawnSync("codex", ["mcp", "remove", NAME], { encoding: "utf8" });
|
|
52
62
|
const r = spawnSync("codex", a, { encoding: "utf8" });
|
|
53
63
|
if (r.status !== 0) throw new Error((r.stderr || r.stdout || "codex mcp add 실패").trim());
|
|
54
64
|
},
|
|
@@ -66,7 +76,7 @@ const targets = {
|
|
|
66
76
|
throw new Error(`${file} 을(를) 읽을 수 없어요. 파일이 깨져 있으면 직접 고쳐 주세요`);
|
|
67
77
|
}
|
|
68
78
|
}
|
|
69
|
-
cfg.mcpServers = { ...(cfg.mcpServers ?? {}), [NAME]: { command:
|
|
79
|
+
cfg.mcpServers = { ...(cfg.mcpServers ?? {}), [NAME]: { command: SERVE.command, args: SERVE.args } };
|
|
70
80
|
if (dry) return log(` [미리보기] ${file} 에 mcpServers.${NAME} 추가`);
|
|
71
81
|
mkdirSync(dirname(file), { recursive: true });
|
|
72
82
|
if (existsSync(file)) writeFileSync(`${file}.genflow-backup`, readFileSync(file));
|
package/package.json
CHANGED
package/server.mjs
CHANGED
|
@@ -5,22 +5,9 @@ import { z } from "zod";
|
|
|
5
5
|
import { allowedOrigins, checkConnection, ensureToken } from "./hub-security.mjs";
|
|
6
6
|
|
|
7
7
|
// 에디터 탭(브라우저)이 접속하는 로컬 WebSocket. 127.0.0.1 전용.
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
let relay = null;
|
|
12
|
-
wss.on("error", (e) => {
|
|
13
|
-
if (e.code !== "EADDRINUSE") throw e;
|
|
14
|
-
relay = new WebSocket(`ws://127.0.0.1:${PORT}/?role=mcp&token=${encodeURIComponent(ensureToken())}`);
|
|
15
|
-
relay.on("message", (raw) => {
|
|
16
|
-
const msg = JSON.parse(raw.toString());
|
|
17
|
-
const p = pending.get(msg.id);
|
|
18
|
-
if (!p) return;
|
|
19
|
-
pending.delete(msg.id);
|
|
20
|
-
msg.error ? p.reject(new Error(msg.error)) : p.resolve(msg.result);
|
|
21
|
-
});
|
|
22
|
-
relay.on("error", () => {});
|
|
23
|
-
});
|
|
8
|
+
// 3777이 다른 프로그램에 쓰이고 있으면 3778~3781로 넘어간다(에디터가 같은 순서로 찾는다)
|
|
9
|
+
const PORTS = (process.env.GENFLOW_HUB_PORTS || process.env.GENFLOW_HUB_PORT || "3777,3778,3779,3780,3781").split(",").map(Number);
|
|
10
|
+
let relay = null; // 이미 켜진 다른 GenFlow 허브가 있으면 거기에 붙어 명령만 중계한다
|
|
24
11
|
let editor = null;
|
|
25
12
|
// 접속을 전부 기억한다: 가장 최근 접속이 닫혀도(시험 접속·옛 탭 등) 아직 살아 있는 에디터 탭으로 이어지게 한다
|
|
26
13
|
const editors = new Set();
|
|
@@ -29,40 +16,101 @@ const pending = new Map();
|
|
|
29
16
|
|
|
30
17
|
// 연결 규칙(규약 §5): 브라우저는 허용된 Origin만, 그 외 로컬 프로그램은 ~/.genflow/hub-token 토큰 필요
|
|
31
18
|
const hubConfig = { origins: allowedOrigins(), token: ensureToken() };
|
|
19
|
+
const HELLO = JSON.stringify({ hello: "genflow-hub", v: 1 });
|
|
32
20
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
//
|
|
42
|
-
|
|
43
|
-
|
|
21
|
+
function attach(wss) {
|
|
22
|
+
wss.on("connection", (ws, req) => {
|
|
23
|
+
const verdict = checkConnection({ origin: req.headers.origin, url: req.url }, hubConfig);
|
|
24
|
+
if (!verdict.ok) {
|
|
25
|
+
console.error(`[hub] 연결 거부: ${verdict.reason}`);
|
|
26
|
+
ws.close(1008, "unauthorized");
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
ws.send(HELLO); // 이 포트가 GenFlow 허브가 맞다는 표시(에디터·다른 서버가 포트를 고를 때 쓴다)
|
|
30
|
+
if (!req.headers.origin && new URL(req.url ?? "/", "ws://x").searchParams.get("role") === "mcp") {
|
|
31
|
+
// 다른 AI 앱이 띄운 서버: 명령을 받아 에디터 탭으로 전달하고 결과를 돌려준다
|
|
32
|
+
ws.on("message", async (raw) => {
|
|
33
|
+
const { id, method, params } = JSON.parse(raw.toString());
|
|
34
|
+
try {
|
|
35
|
+
ws.send(JSON.stringify({ id, result: await call(method, params, tooLong) }));
|
|
36
|
+
} catch (e) {
|
|
37
|
+
ws.send(JSON.stringify({ id, error: String(e.message) }));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
editors.add(ws);
|
|
43
|
+
editor = ws;
|
|
44
|
+
ws.on("message", (raw) => {
|
|
45
|
+
const msg = JSON.parse(raw.toString());
|
|
46
|
+
const p = pending.get(msg.id);
|
|
47
|
+
if (!p) return;
|
|
48
|
+
pending.delete(msg.id);
|
|
49
|
+
msg.error ? p.reject(new Error(msg.error)) : p.resolve(msg.result);
|
|
50
|
+
});
|
|
51
|
+
ws.on("close", () => {
|
|
52
|
+
editors.delete(ws);
|
|
53
|
+
if (editor === ws) editor = [...editors].filter((w) => w.readyState === 1).pop() ?? null;
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const bindPort = (port) =>
|
|
59
|
+
new Promise((resolve, reject) => {
|
|
60
|
+
const wss = new WebSocketServer({ host: "127.0.0.1", port });
|
|
61
|
+
wss.once("listening", () => resolve(wss));
|
|
62
|
+
wss.once("error", reject);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// 포트가 이미 쓰이고 있을 때: GenFlow 허브(인사를 보냄)인지 확인하고, 맞으면 거기에 붙는다
|
|
66
|
+
const probeHub = (port) =>
|
|
67
|
+
new Promise((resolve) => {
|
|
68
|
+
const ws = new WebSocket(`ws://127.0.0.1:${port}/?role=mcp&token=${encodeURIComponent(hubConfig.token)}`);
|
|
69
|
+
const fail = () => (clearTimeout(t), ws.terminate(), resolve(null));
|
|
70
|
+
const t = setTimeout(fail, 1500);
|
|
71
|
+
ws.on("error", fail);
|
|
72
|
+
ws.on("close", fail);
|
|
73
|
+
ws.on("message", (raw) => {
|
|
74
|
+
let m;
|
|
44
75
|
try {
|
|
45
|
-
|
|
46
|
-
} catch
|
|
47
|
-
|
|
76
|
+
m = JSON.parse(raw.toString());
|
|
77
|
+
} catch {
|
|
78
|
+
return;
|
|
48
79
|
}
|
|
80
|
+
if (m.hello !== "genflow-hub") return;
|
|
81
|
+
clearTimeout(t);
|
|
82
|
+
ws.removeAllListeners("message");
|
|
83
|
+
ws.removeAllListeners("close");
|
|
84
|
+
ws.on("message", (r) => {
|
|
85
|
+
const msg = JSON.parse(r.toString());
|
|
86
|
+
const p = pending.get(msg.id);
|
|
87
|
+
if (!p) return;
|
|
88
|
+
pending.delete(msg.id);
|
|
89
|
+
msg.error ? p.reject(new Error(msg.error)) : p.resolve(msg.result);
|
|
90
|
+
});
|
|
91
|
+
ws.on("close", () => (relay = null));
|
|
92
|
+
resolve(ws);
|
|
49
93
|
});
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
editors.add(ws);
|
|
53
|
-
editor = ws;
|
|
54
|
-
ws.on("message", (raw) => {
|
|
55
|
-
const msg = JSON.parse(raw.toString());
|
|
56
|
-
const p = pending.get(msg.id);
|
|
57
|
-
if (!p) return;
|
|
58
|
-
pending.delete(msg.id);
|
|
59
|
-
msg.error ? p.reject(new Error(msg.error)) : p.resolve(msg.result);
|
|
60
94
|
});
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
95
|
+
|
|
96
|
+
let hubUp = false;
|
|
97
|
+
for (const port of PORTS) {
|
|
98
|
+
try {
|
|
99
|
+
attach(await bindPort(port));
|
|
100
|
+
hubUp = true;
|
|
101
|
+
console.error(`[hub] 포트 ${port}에서 대기`);
|
|
102
|
+
break;
|
|
103
|
+
} catch (e) {
|
|
104
|
+
if (e.code !== "EADDRINUSE") throw e;
|
|
105
|
+
relay = await probeHub(port);
|
|
106
|
+
if (relay) {
|
|
107
|
+
console.error(`[hub] 포트 ${port}의 GenFlow 허브에 연결`);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
// 다른 프로그램이 쓰는 포트: 다음 포트로
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (!hubUp && !relay) console.error(`[hub] 쓸 수 있는 포트가 없어요(${PORTS.join(", ")}). 다른 프로그램이 모두 쓰고 있어요`);
|
|
66
114
|
|
|
67
115
|
function call(method, params = {}, timeoutMs = 15000) {
|
|
68
116
|
return new Promise((resolve, reject) => {
|
|
@@ -88,7 +136,7 @@ function call(method, params = {}, timeoutMs = 15000) {
|
|
|
88
136
|
});
|
|
89
137
|
}
|
|
90
138
|
|
|
91
|
-
const server = new McpServer({ name: "genflow-editor", version: "0.1.
|
|
139
|
+
const server = new McpServer({ name: "genflow-editor", version: "0.1.2" });
|
|
92
140
|
const out = (v) => ({ content: [{ type: "text", text: JSON.stringify(v, null, 2) }] });
|
|
93
141
|
const tool = (name, description, shape, method) =>
|
|
94
142
|
server.tool(name, description, shape, async (args) => {
|