carlos-flow 2.14.0 → 2.14.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.
@@ -0,0 +1,139 @@
1
+ // 最小 MCP stdio 服务端(零依赖)
2
+ //
3
+ // 为什么自己写而不继续用 @modelcontextprotocol/sdk:
4
+ // Cindy 插件(chip)要求随包 Node 程序**自带全部依赖** —— Forge 打包时会跳过
5
+ // node_modules,也禁止安装期/首跑期联网装依赖。SDK 一旦成为运行时依赖,
6
+ // 这个插件就不可能以 .cindy 形态装进 Cindy(只能靠绝对路径跑仓库里的副本)。
7
+ //
8
+ // 本实现只覆盖本插件真正用到的 MCP 子集:
9
+ // 握手(initialize)、初始化通知、心跳(ping)、工具目录与工具调用,
10
+ // 外加 resources/prompts 的空列表应答(宿主做能力探测时会问)。
11
+ // 传输形态:逐行 JSON-RPC 2.0(MCP stdio 规范),stdout 只写协议消息,日志走 stderr。
12
+ import readline from "node:readline";
13
+
14
+ // 声明的协议版本:客户端请求里的版本若在支持列表内就回声,否则回最新的。
15
+ const SUPPORTED_PROTOCOLS = ["2025-06-18", "2025-03-26", "2024-11-05"];
16
+ const LATEST_PROTOCOL = SUPPORTED_PROTOCOLS[0];
17
+
18
+ export class StdioServer {
19
+ constructor({ name, version, tools, callTool, log }) {
20
+ this.name = name;
21
+ this.version = version;
22
+ this.tools = tools;
23
+ this.callTool = callTool;
24
+ this.log = typeof log === "function" ? log : () => {};
25
+ this.initialized = false;
26
+ this.pending = new Set();
27
+ }
28
+
29
+ reply(id, result) {
30
+ if (id === undefined || id === null) return;
31
+ this.write({ jsonrpc: "2.0", id, result });
32
+ }
33
+
34
+ replyError(id, code, message) {
35
+ if (id === undefined || id === null) return;
36
+ this.write({ jsonrpc: "2.0", id, error: { code, message } });
37
+ }
38
+
39
+ write(message) {
40
+ try {
41
+ process.stdout.write(JSON.stringify(message) + "\n");
42
+ } catch (e) {
43
+ // 客户端已断开(EPIPE):不再尝试写,避免把进程搞崩
44
+ try {
45
+ this.log("stdout 写入失败:" + (e && e.message ? e.message : e));
46
+ } catch { /* ignore */ }
47
+ }
48
+ }
49
+
50
+ async handle(message) {
51
+ const method = message && message.method;
52
+ const id = message && message.id;
53
+ if (typeof method !== "string") return; // 客户端回来的响应(本服务端不发反向请求):忽略
54
+
55
+ switch (method) {
56
+ case "initialize": {
57
+ const asked = message.params && message.params.protocolVersion;
58
+ const protocolVersion = SUPPORTED_PROTOCOLS.includes(asked) ? asked : LATEST_PROTOCOL;
59
+ this.initialized = true;
60
+ this.reply(id, {
61
+ protocolVersion,
62
+ capabilities: { tools: { listChanged: false } },
63
+ serverInfo: { name: this.name, version: this.version },
64
+ });
65
+ return;
66
+ }
67
+ case "notifications/initialized":
68
+ case "notifications/cancelled":
69
+ // 规范里这两个是**通知**(没有 id),我们不回复。
70
+ // 但实测有客户端(包括本仓库自带的测试脚本)会带 id 发通知 ——
71
+ // 那时若不回包,调用方就会**永久挂起**(比报错更难查)。
72
+ // 因此:带 id 的「通知」按空结果应答,无 id 的照旧静默。
73
+ this.reply(id, {});
74
+ return;
75
+ case "ping":
76
+ this.reply(id, {});
77
+ return;
78
+ case "tools/list":
79
+ this.reply(id, { tools: this.tools });
80
+ return;
81
+ case "resources/list":
82
+ this.reply(id, { resources: [] });
83
+ return;
84
+ case "prompts/list":
85
+ this.reply(id, { prompts: [] });
86
+ return;
87
+ case "tools/call": {
88
+ const params = message.params || {};
89
+ const name = params.name;
90
+ const args = params.arguments === undefined || params.arguments === null ? {} : params.arguments;
91
+ let result;
92
+ try {
93
+ result = await this.callTool(name, args);
94
+ } catch (e) {
95
+ // 工具内部抛错 → 按 MCP 约定以「工具结果 + isError」返回,
96
+ // 而不是 JSON-RPC 协议错误(后者会让宿主把整个会话当故障)。
97
+ result = {
98
+ content: [{ type: "text", text: "ERROR: " + (e && e.message ? e.message : String(e)) }],
99
+ isError: true,
100
+ };
101
+ }
102
+ this.reply(id, result);
103
+ return;
104
+ }
105
+ default:
106
+ // 带 id 的未知方法必须回错(否则客户端会一直等);
107
+ // 不带 id 的是通知,静默忽略。
108
+ this.replyError(id, -32601, "Method not found: " + method);
109
+ }
110
+ }
111
+
112
+ async start() {
113
+ const rl = readline.createInterface({ input: process.stdin, crlfDelay: Infinity });
114
+ for await (const line of rl) {
115
+ const text = line.trim();
116
+ if (!text) continue;
117
+ let message = null;
118
+ try {
119
+ message = JSON.parse(text);
120
+ } catch {
121
+ // 非法 JSON:没有 id 可回,记 stderr 后继续(不要因一条脏行整进程退出)
122
+ this.log("收到无法解析的 JSON-RPC 行,已忽略");
123
+ continue;
124
+ }
125
+ // 不串行等待:flow_run_verify 这类调用可能跑上百秒,
126
+ // 期间其它请求(如 ping)必须还能得到响应。
127
+ const p = this.handle(message)
128
+ .catch((e) => this.log("处理消息失败:" + (e && e.message ? e.message : e)))
129
+ .finally(() => this.pending.delete(p));
130
+ this.pending.add(p);
131
+ }
132
+ // stdin 关闭(宿主退出):等手上的请求收尾再返回
133
+ await Promise.allSettled([...this.pending]);
134
+ }
135
+ }
136
+
137
+ export function serveStdio(options) {
138
+ return new StdioServer(options).start();
139
+ }