carlos-flow 2.11.3 → 2.12.1

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.
@@ -1,204 +1,204 @@
1
- // Carlos MCP Bridge — 安全 TCP 桥接层
2
- // 监听 127.0.0.1:19099,仅本地访问
3
- // 用法: node mcp-bridge.js [端口]
4
- // 环境变量: CARLOS_BRIDGE_TOKEN(必填,用于认证)
5
-
6
- import { spawn } from "node:child_process";
7
- import { createServer } from "node:net";
8
- import { createHash } from "node:crypto";
9
- import { fileURLToPath } from "node:url";
10
- import path from "node:path";
11
-
12
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
13
- const PORT = parseInt(process.argv[2] || process.env.BRIDGE_PORT || "19099", 10);
14
- const TOKEN = process.env.CARLOS_BRIDGE_TOKEN;
15
- // 路径自探测:环境变量优先(向后兼容),否则按 bridge/ 的位置自动定位项目根目录。
16
- // 这样在 Cursor / VS Code / Windsurf 里只需填 bridge 脚本路径,无需再手写 ROOT 绝对路径。
17
- const PROJECT_ROOT = path.resolve(__dirname, "..");
18
- const MCP_ROOT = process.env.CARLOS_FLOW_MCP_ROOT
19
- ? path.resolve(process.env.CARLOS_FLOW_MCP_ROOT)
20
- : PROJECT_ROOT;
21
- const MCP_SERVER = path.resolve(MCP_ROOT, "src/index.js");
22
- const MAX_REQUEST_SIZE = 1024 * 1024; // 1MB
23
- const RATE_LIMIT = 10; // 每秒最多请求数
24
- // 请求超时:必须大于服务端的最长单次执行时间。
25
- // 服务端 flow_run_verify 的内部超时是 120s(真实 spawn verify.cmd),
26
- // 其它工具都是毫秒级。
27
- // 旧实现固定 5s —— 结果是任何耗时 >5s 的验证命令(如 `npm test`)必然超时失败,
28
- // 用户只看到「Timeout」,而服务端那边命令其实跑得好好的。
29
- // 现在默认 180s(> 服务端 120s 上限),可用 CARLOS_BRIDGE_TIMEOUT_MS 覆盖。
30
- const REQUEST_TIMEOUT = Number(process.env.CARLOS_BRIDGE_TIMEOUT_MS) || 180000; // 180s
31
-
32
- if (!TOKEN) {
33
- process.stderr.write("[bridge] ❌ CARLOS_BRIDGE_TOKEN 未设置(fail-closed)\n");
34
- process.exit(1);
35
- }
36
-
37
- // 启动 MCP server 子进程
38
- // windowsHide: true —— Windows 下隐藏子进程控制台窗口,避免 Bridge 重启时弹黑框。
39
- const child = spawn(process.execPath, [MCP_SERVER], {
40
- stdio: ["pipe", "pipe", "pipe"],
41
- env: { ...process.env },
42
- windowsHide: true,
43
- });
44
-
45
- let childStdout = "";
46
- const pendingClients = new Map(); // reqId -> { socket, timer }
47
-
48
- child.stdout.on("data", (data) => {
49
- childStdout += data.toString("utf8");
50
- const lines = childStdout.split("\n");
51
- childStdout = lines.pop() || "";
52
- for (const line of lines) {
53
- const t = line.trim();
54
- if (!t) continue;
55
- try {
56
- const resp = JSON.parse(t);
57
- if (resp.jsonrpc === "2.0" && resp.id !== undefined) {
58
- const client = pendingClients.get(resp.id);
59
- if (client) {
60
- clearTimeout(client.timer);
61
- pendingClients.delete(resp.id);
62
- try { client.socket.write(t + "\n"); } catch {}
63
- try { client.socket.end(); } catch {}
64
- }
65
- }
66
- } catch {}
67
- }
68
- });
69
-
70
- child.stderr.on("data", (data) => {
71
- process.stderr.write("[mcp] " + data.toString("utf8"));
72
- });
73
-
74
- child.on("error", (e) => {
75
- process.stderr.write("[Carlos-Diagnosis] ❌ MCP server 子进程启动失败: " + e.message + "\n");
76
- process.stderr.write(
77
- "[Carlos-Suggestion] 💡 请确认 Node.js 可用(node -v),且 " +
78
- MCP_SERVER +
79
- " 存在;若路径被移动过,可设置环境变量 CARLOS_FLOW_MCP_ROOT 指向项目根目录。\n",
80
- );
81
- });
82
-
83
- child.on("exit", (code) => {
84
- process.stderr.write(`[bridge] MCP server 退出 (code=${code})\n`);
85
- for (const [, c] of pendingClients) {
86
- clearTimeout(c.timer);
87
- try { c.socket.end(); } catch {}
88
- }
89
- pendingClients.clear();
90
- });
91
-
92
- // 速率限制(per-connection sliding window)
93
- const rateLimitMap = new Map(); // socket -> [timestamps]
94
-
95
- function isRateLimited(socket) {
96
- const now = Date.now();
97
- const key = socket.remoteAddress + ":" + socket.remotePort;
98
- const timestamps = rateLimitMap.get(key) || [];
99
- const windowStart = now - 1000;
100
- const recent = timestamps.filter((t) => t > windowStart);
101
- recent.push(now);
102
- rateLimitMap.set(key, recent);
103
- return recent.length > RATE_LIMIT;
104
- }
105
-
106
- // TCP 服务器
107
- const server = createServer((socket) => {
108
- let buf = "";
109
- let size = 0;
110
-
111
- socket.on("data", (data) => {
112
- size += data.length;
113
- if (size > MAX_REQUEST_SIZE) {
114
- socket.write(JSON.stringify({ error: "413 Payload Too Large" }) + "\n");
115
- socket.end();
116
- return;
117
- }
118
-
119
- if (isRateLimited(socket)) {
120
- socket.write(JSON.stringify({ error: "429 Too Many Requests" }) + "\n");
121
- socket.end();
122
- return;
123
- }
124
-
125
- buf += data.toString("utf8");
126
- const lines = buf.split("\n");
127
- buf = lines.pop() || "";
128
- for (const line of lines) {
129
- const t = line.trim();
130
- if (!t) continue;
131
-
132
- // 健康检查
133
- if (t === "GET /health") {
134
- // 必须回传**身份**,否则客户端无法判断“这个 bridge 是不是我该用的那个”。
135
- //
136
- // 背景:早先 /health 只回 {status:"ok"},客户端只要看到 ok 就复用 ——
137
- // 于是端口上若跑着**另一个 ROOT / 另一个 CARLOS_FLOW_HOME** 的 bridge,
138
- // 客户端会静默连过去:它启动时的 HOME 生效、调用方传的 CARLOS_FLOW_HOME
139
- // 完全失效(状态写到别人的目录),且协议可能不匹配导致挂死。
140
- // 现在回传 root / flow_home / pid,客户端逐一比对,不符就当“未运行”。
141
- const health = {
142
- status: "ok",
143
- root: MCP_ROOT,
144
- flow_home: process.env.CARLOS_FLOW_HOME || "",
145
- pid: process.pid,
146
- port: PORT,
147
- };
148
- socket.write(JSON.stringify(health) + "\n");
149
- continue;
150
- }
151
-
152
- try {
153
- const req = JSON.parse(t);
154
-
155
- // Token 认证
156
- const reqToken = req.params?._token || req._token;
157
- if (reqToken !== TOKEN) {
158
- socket.write(JSON.stringify({ jsonrpc: "2.0", id: req.id, error: { code: -1, message: "Unauthorized" } }) + "\n");
159
- continue;
160
- }
161
-
162
- if (req.jsonrpc === "2.0") {
163
- const reqId = req.id;
164
- const timer = setTimeout(() => {
165
- if (pendingClients.has(reqId)) {
166
- pendingClients.delete(reqId);
167
- try { socket.write(JSON.stringify({ jsonrpc: "2.0", id: reqId, error: { code: -1, message: "Timeout" } }) + "\n"); } catch {}
168
- try { socket.end(); } catch {}
169
- }
170
- }, REQUEST_TIMEOUT);
171
-
172
- pendingClients.set(reqId, { socket, timer });
173
- child.stdin.write(t + "\n");
174
- }
175
- } catch {}
176
- }
177
- });
178
-
179
- socket.on("error", () => {});
180
- socket.on("close", () => {
181
- for (const [reqId, c] of pendingClients) {
182
- if (c.socket === socket) {
183
- clearTimeout(c.timer);
184
- pendingClients.delete(reqId);
185
- }
186
- }
187
- });
188
- });
189
-
190
- server.on("error", (e) => {
191
- process.stderr.write("[bridge] TCP 错误: " + e.message + "\n");
192
- });
193
-
194
- server.listen(PORT, "127.0.0.1", () => {
195
- process.stderr.write(`[bridge] Carlos MCP Bridge listening on 127.0.0.1:${PORT}\n`);
196
- });
197
-
198
- // 优雅退出
199
- process.on("SIGTERM", () => {
200
- child.stdin.end();
201
- child.kill("SIGTERM");
202
- server.close();
203
- process.exit(0);
1
+ // Carlos MCP Bridge — 安全 TCP 桥接层
2
+ // 监听 127.0.0.1:19099,仅本地访问
3
+ // 用法: node mcp-bridge.js [端口]
4
+ // 环境变量: CARLOS_BRIDGE_TOKEN(必填,用于认证)
5
+
6
+ import { spawn } from "node:child_process";
7
+ import { createServer } from "node:net";
8
+ import { createHash } from "node:crypto";
9
+ import { fileURLToPath } from "node:url";
10
+ import path from "node:path";
11
+
12
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
13
+ const PORT = parseInt(process.argv[2] || process.env.BRIDGE_PORT || "19099", 10);
14
+ const TOKEN = process.env.CARLOS_BRIDGE_TOKEN;
15
+ // 路径自探测:环境变量优先(向后兼容),否则按 bridge/ 的位置自动定位项目根目录。
16
+ // 这样在 Cursor / VS Code / Windsurf 里只需填 bridge 脚本路径,无需再手写 ROOT 绝对路径。
17
+ const PROJECT_ROOT = path.resolve(__dirname, "..");
18
+ const MCP_ROOT = process.env.CARLOS_FLOW_MCP_ROOT
19
+ ? path.resolve(process.env.CARLOS_FLOW_MCP_ROOT)
20
+ : PROJECT_ROOT;
21
+ const MCP_SERVER = path.resolve(MCP_ROOT, "src/index.js");
22
+ const MAX_REQUEST_SIZE = 1024 * 1024; // 1MB
23
+ const RATE_LIMIT = 10; // 每秒最多请求数
24
+ // 请求超时:必须大于服务端的最长单次执行时间。
25
+ // 服务端 flow_run_verify 的内部超时是 120s(真实 spawn verify.cmd),
26
+ // 其它工具都是毫秒级。
27
+ // 旧实现固定 5s —— 结果是任何耗时 >5s 的验证命令(如 `npm test`)必然超时失败,
28
+ // 用户只看到「Timeout」,而服务端那边命令其实跑得好好的。
29
+ // 现在默认 180s(> 服务端 120s 上限),可用 CARLOS_BRIDGE_TIMEOUT_MS 覆盖。
30
+ const REQUEST_TIMEOUT = Number(process.env.CARLOS_BRIDGE_TIMEOUT_MS) || 180000; // 180s
31
+
32
+ if (!TOKEN) {
33
+ process.stderr.write("[bridge] ❌ CARLOS_BRIDGE_TOKEN 未设置(fail-closed)\n");
34
+ process.exit(1);
35
+ }
36
+
37
+ // 启动 MCP server 子进程
38
+ // windowsHide: true —— Windows 下隐藏子进程控制台窗口,避免 Bridge 重启时弹黑框。
39
+ const child = spawn(process.execPath, [MCP_SERVER], {
40
+ stdio: ["pipe", "pipe", "pipe"],
41
+ env: { ...process.env },
42
+ windowsHide: true,
43
+ });
44
+
45
+ let childStdout = "";
46
+ const pendingClients = new Map(); // reqId -> { socket, timer }
47
+
48
+ child.stdout.on("data", (data) => {
49
+ childStdout += data.toString("utf8");
50
+ const lines = childStdout.split("\n");
51
+ childStdout = lines.pop() || "";
52
+ for (const line of lines) {
53
+ const t = line.trim();
54
+ if (!t) continue;
55
+ try {
56
+ const resp = JSON.parse(t);
57
+ if (resp.jsonrpc === "2.0" && resp.id !== undefined) {
58
+ const client = pendingClients.get(resp.id);
59
+ if (client) {
60
+ clearTimeout(client.timer);
61
+ pendingClients.delete(resp.id);
62
+ try { client.socket.write(t + "\n"); } catch {}
63
+ try { client.socket.end(); } catch {}
64
+ }
65
+ }
66
+ } catch {}
67
+ }
68
+ });
69
+
70
+ child.stderr.on("data", (data) => {
71
+ process.stderr.write("[mcp] " + data.toString("utf8"));
72
+ });
73
+
74
+ child.on("error", (e) => {
75
+ process.stderr.write("[Carlos-Diagnosis] ❌ MCP server 子进程启动失败: " + e.message + "\n");
76
+ process.stderr.write(
77
+ "[Carlos-Suggestion] 💡 请确认 Node.js 可用(node -v),且 " +
78
+ MCP_SERVER +
79
+ " 存在;若路径被移动过,可设置环境变量 CARLOS_FLOW_MCP_ROOT 指向项目根目录。\n",
80
+ );
81
+ });
82
+
83
+ child.on("exit", (code) => {
84
+ process.stderr.write(`[bridge] MCP server 退出 (code=${code})\n`);
85
+ for (const [, c] of pendingClients) {
86
+ clearTimeout(c.timer);
87
+ try { c.socket.end(); } catch {}
88
+ }
89
+ pendingClients.clear();
90
+ });
91
+
92
+ // 速率限制(per-connection sliding window)
93
+ const rateLimitMap = new Map(); // socket -> [timestamps]
94
+
95
+ function isRateLimited(socket) {
96
+ const now = Date.now();
97
+ const key = socket.remoteAddress + ":" + socket.remotePort;
98
+ const timestamps = rateLimitMap.get(key) || [];
99
+ const windowStart = now - 1000;
100
+ const recent = timestamps.filter((t) => t > windowStart);
101
+ recent.push(now);
102
+ rateLimitMap.set(key, recent);
103
+ return recent.length > RATE_LIMIT;
104
+ }
105
+
106
+ // TCP 服务器
107
+ const server = createServer((socket) => {
108
+ let buf = "";
109
+ let size = 0;
110
+
111
+ socket.on("data", (data) => {
112
+ size += data.length;
113
+ if (size > MAX_REQUEST_SIZE) {
114
+ socket.write(JSON.stringify({ error: "413 Payload Too Large" }) + "\n");
115
+ socket.end();
116
+ return;
117
+ }
118
+
119
+ if (isRateLimited(socket)) {
120
+ socket.write(JSON.stringify({ error: "429 Too Many Requests" }) + "\n");
121
+ socket.end();
122
+ return;
123
+ }
124
+
125
+ buf += data.toString("utf8");
126
+ const lines = buf.split("\n");
127
+ buf = lines.pop() || "";
128
+ for (const line of lines) {
129
+ const t = line.trim();
130
+ if (!t) continue;
131
+
132
+ // 健康检查
133
+ if (t === "GET /health") {
134
+ // 必须回传**身份**,否则客户端无法判断“这个 bridge 是不是我该用的那个”。
135
+ //
136
+ // 背景:早先 /health 只回 {status:"ok"},客户端只要看到 ok 就复用 ——
137
+ // 于是端口上若跑着**另一个 ROOT / 另一个 CARLOS_FLOW_HOME** 的 bridge,
138
+ // 客户端会静默连过去:它启动时的 HOME 生效、调用方传的 CARLOS_FLOW_HOME
139
+ // 完全失效(状态写到别人的目录),且协议可能不匹配导致挂死。
140
+ // 现在回传 root / flow_home / pid,客户端逐一比对,不符就当“未运行”。
141
+ const health = {
142
+ status: "ok",
143
+ root: MCP_ROOT,
144
+ flow_home: process.env.CARLOS_FLOW_HOME || "",
145
+ pid: process.pid,
146
+ port: PORT,
147
+ };
148
+ socket.write(JSON.stringify(health) + "\n");
149
+ continue;
150
+ }
151
+
152
+ try {
153
+ const req = JSON.parse(t);
154
+
155
+ // Token 认证
156
+ const reqToken = req.params?._token || req._token;
157
+ if (reqToken !== TOKEN) {
158
+ socket.write(JSON.stringify({ jsonrpc: "2.0", id: req.id, error: { code: -1, message: "Unauthorized" } }) + "\n");
159
+ continue;
160
+ }
161
+
162
+ if (req.jsonrpc === "2.0") {
163
+ const reqId = req.id;
164
+ const timer = setTimeout(() => {
165
+ if (pendingClients.has(reqId)) {
166
+ pendingClients.delete(reqId);
167
+ try { socket.write(JSON.stringify({ jsonrpc: "2.0", id: reqId, error: { code: -1, message: "Timeout" } }) + "\n"); } catch {}
168
+ try { socket.end(); } catch {}
169
+ }
170
+ }, REQUEST_TIMEOUT);
171
+
172
+ pendingClients.set(reqId, { socket, timer });
173
+ child.stdin.write(t + "\n");
174
+ }
175
+ } catch {}
176
+ }
177
+ });
178
+
179
+ socket.on("error", () => {});
180
+ socket.on("close", () => {
181
+ for (const [reqId, c] of pendingClients) {
182
+ if (c.socket === socket) {
183
+ clearTimeout(c.timer);
184
+ pendingClients.delete(reqId);
185
+ }
186
+ }
187
+ });
188
+ });
189
+
190
+ server.on("error", (e) => {
191
+ process.stderr.write("[bridge] TCP 错误: " + e.message + "\n");
192
+ });
193
+
194
+ server.listen(PORT, "127.0.0.1", () => {
195
+ process.stderr.write(`[bridge] Carlos MCP Bridge listening on 127.0.0.1:${PORT}\n`);
196
+ });
197
+
198
+ // 优雅退出
199
+ process.on("SIGTERM", () => {
200
+ child.stdin.end();
201
+ child.kill("SIGTERM");
202
+ server.close();
203
+ process.exit(0);
204
204
  });
package/check-all.sh CHANGED
File without changes
package/ghost.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "name": "Carlos 工作流",
6
6
  "description": "机械门禁 MCP:留痕/派发/裁定/审计/快照/免检/还原。自动管理 Bridge 后台常驻,首次调用 ~3-5s,后续 ~1s。",
7
7
  "whenToUse": "需要走 Carlos 工作流门禁时使用:留痕/派发/裁定/审计/快照/免检/还原",
8
- "version": "2.11.3",
8
+ "version": "2.12.1",
9
9
  "kind": "mcp",
10
10
  "entry": "client/carlos.js",
11
11
  "command": "门禁",
@@ -17,6 +17,31 @@
17
17
  },
18
18
  "settingsHtml": "settings.html",
19
19
  "tools": [
20
+ {
21
+ "name": "flow_mode",
22
+ "description": "切换工作流模式:pro(专业)/ guide(向导)。",
23
+ "parameters": {
24
+ "type": "object",
25
+ "properties": {
26
+ "sessionId": {
27
+ "type": "string"
28
+ },
29
+ "mode": {
30
+ "type": "string",
31
+ "enum": [
32
+ "pro",
33
+ "guide"
34
+ ]
35
+ },
36
+ "reason": {
37
+ "type": "string"
38
+ }
39
+ },
40
+ "required": [
41
+ "mode"
42
+ ]
43
+ }
44
+ },
20
45
  {
21
46
  "name": "flow_protocol",
22
47
  "description": "读取 Carlos 工作流协议全文(路由档位 T0/T1/T2、四段合同、报告规格、错误分层、状态字段、审计事件类型、免检触发词、任务卡模板)。",