ygl-trees 0.66.0 → 0.69.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.
@@ -0,0 +1,276 @@
1
+ const require_rolldown_runtime = require("./rolldown-runtime-C96Ew50Q.cjs");
2
+ let node_readline = require("node:readline");
3
+ node_readline = require_rolldown_runtime.__toESM(node_readline);
4
+ //#region packages/ops/tools/src/mcp.ts
5
+ /**
6
+ * MCP (Model Context Protocol) Server — stdio transport.
7
+ *
8
+ * Eight-tool surface designed by the unified three-pillar theory (R8 research):
9
+ * agent_brief · agent_match · kb_search · claim_evaluate
10
+ * tree_state · produce_report · doctor · task_advance
11
+ *
12
+ * Each tool routes to the CLI --json backend, inheriting its frozen contracts
13
+ * (idempotent writes, pure JSON errors, snake_case fields). Agents call tools
14
+ * directly — no command-line parsing layer.
15
+ *
16
+ * Protocol: JSON-RPC 2.0 over stdio (one message per line).
17
+ * → {"jsonrpc":"2.0","id":1,"method":"initialize","params":{...}}
18
+ * ← {"jsonrpc":"2.0","id":1,"result":{...}}
19
+ *
20
+ * @module @ygl/ops-tools/mcp
21
+ */
22
+ /** Build a CLI invocation and capture JSON stdout.
23
+ * Uses process.argv[1] (the running script — bundle or source) to re-invoke
24
+ * the CLI as a child process. This works in both dev (dist/apps/...) and
25
+ * bundle (dist/bundle/bin.cjs) contexts. */
26
+ function cliJson(parts) {
27
+ const { spawn } = require("node:child_process");
28
+ const self = process.argv[1];
29
+ return new Promise((resolve, reject) => {
30
+ const child = spawn(process.execPath, [
31
+ self,
32
+ "--json",
33
+ ...parts
34
+ ], {
35
+ env: {
36
+ ...process.env,
37
+ YGL_SERVE: "1",
38
+ YGL_MCP_CHILD: "1"
39
+ },
40
+ stdio: [
41
+ "ignore",
42
+ "pipe",
43
+ "pipe"
44
+ ]
45
+ });
46
+ let out = "";
47
+ let err = "";
48
+ child.stdout.on("data", (d) => out += d);
49
+ child.stderr.on("data", (d) => err += d);
50
+ child.on("close", (code) => {
51
+ if (out.trim()) try {
52
+ resolve(JSON.parse(out));
53
+ return;
54
+ } catch {}
55
+ reject(/* @__PURE__ */ new Error(`CLI rc=${code}: ${err.slice(0, 200) || out.slice(0, 200)}`));
56
+ });
57
+ child.on("error", (e) => reject(e));
58
+ });
59
+ }
60
+ function buildTools() {
61
+ return [
62
+ {
63
+ name: "agent_brief",
64
+ description: "Current workspace state: project, progress, counts, next actions, available skills. Always start here.",
65
+ inputSchema: {
66
+ type: "object",
67
+ properties: {}
68
+ },
69
+ execute: () => cliJson(["agent", "brief"])
70
+ },
71
+ {
72
+ name: "agent_match",
73
+ description: "Discover which skill handles a task. Returns matched skills with relevance scores.",
74
+ inputSchema: {
75
+ type: "object",
76
+ properties: { query: {
77
+ type: "string",
78
+ description: "Task description to match"
79
+ } },
80
+ required: ["query"]
81
+ },
82
+ execute: (a) => cliJson([
83
+ "agent",
84
+ "match",
85
+ String(a.query)
86
+ ])
87
+ },
88
+ {
89
+ name: "kb_search",
90
+ description: "Search knowledge notes (CJK full-text). Returns notes with title, body, tags, confidence.",
91
+ inputSchema: {
92
+ type: "object",
93
+ properties: {
94
+ query: {
95
+ type: "string",
96
+ description: "Search query (Chinese supported)"
97
+ },
98
+ eval: {
99
+ type: "boolean",
100
+ description: "Enable decision-model semantic re-ranking"
101
+ }
102
+ },
103
+ required: ["query"]
104
+ },
105
+ execute: (a) => {
106
+ const parts = [
107
+ "kb",
108
+ "search",
109
+ String(a.query)
110
+ ];
111
+ if (a.eval) parts.push("--eval");
112
+ return cliJson(parts);
113
+ }
114
+ },
115
+ {
116
+ name: "claim_evaluate",
117
+ description: "Decision model evaluates a claim's confidence (score 0-5 → grade A/B/C/D).",
118
+ inputSchema: {
119
+ type: "object",
120
+ properties: { claim_id: {
121
+ type: "string",
122
+ description: "Claim ID to evaluate"
123
+ } },
124
+ required: ["claim_id"]
125
+ },
126
+ execute: (a) => cliJson([
127
+ "research",
128
+ "claim",
129
+ "evaluate",
130
+ String(a.claim_id)
131
+ ])
132
+ },
133
+ {
134
+ name: "tree_state",
135
+ description: "All trees with node states (locked/unlocked). Visualize the dependency graph.",
136
+ inputSchema: {
137
+ type: "object",
138
+ properties: { tree: {
139
+ type: "string",
140
+ description: "Optional tree name (omit for all)"
141
+ } }
142
+ },
143
+ execute: (a) => cliJson([
144
+ "node",
145
+ "list",
146
+ ...a.tree ? [String(a.tree)] : []
147
+ ])
148
+ },
149
+ {
150
+ name: "produce_report",
151
+ description: "Assemble claims + evidence into a markdown report. Returns report ID and content.",
152
+ inputSchema: {
153
+ type: "object",
154
+ properties: {}
155
+ },
156
+ execute: () => cliJson(["produce", "report"])
157
+ },
158
+ {
159
+ name: "doctor",
160
+ description: "9-item workspace health check. Pass fix=true to auto-fix safe issues.",
161
+ inputSchema: {
162
+ type: "object",
163
+ properties: { fix: {
164
+ type: "boolean",
165
+ description: "Auto-fix dirty index + stale locks"
166
+ } }
167
+ },
168
+ execute: (a) => cliJson(["doctor", ...a.fix ? ["--fix"] : []])
169
+ },
170
+ {
171
+ name: "task_advance",
172
+ description: "Advance a task through the state machine: todo → doing → done (or roll back).",
173
+ inputSchema: {
174
+ type: "object",
175
+ properties: {
176
+ task_id: {
177
+ type: "string",
178
+ description: "Task artifact ID"
179
+ },
180
+ action: {
181
+ type: "string",
182
+ enum: [
183
+ "doing",
184
+ "done",
185
+ "todo"
186
+ ],
187
+ description: "Target status"
188
+ }
189
+ },
190
+ required: ["task_id", "action"]
191
+ },
192
+ execute: (a) => cliJson([
193
+ "task",
194
+ String(a.action),
195
+ String(a.task_id)
196
+ ])
197
+ }
198
+ ];
199
+ }
200
+ /** Run the MCP server on stdio. */
201
+ function serveMcp() {
202
+ const tools = buildTools();
203
+ const rl = node_readline.createInterface({ input: process.stdin });
204
+ const respond = (id, result) => {
205
+ process.stdout.write(JSON.stringify({
206
+ jsonrpc: "2.0",
207
+ id,
208
+ result
209
+ }) + "\n");
210
+ };
211
+ const respondErr = (id, code, message) => {
212
+ process.stdout.write(JSON.stringify({
213
+ jsonrpc: "2.0",
214
+ id,
215
+ error: {
216
+ code,
217
+ message
218
+ }
219
+ }) + "\n");
220
+ };
221
+ rl.on("line", (line) => {
222
+ if (!line.trim()) return;
223
+ let msg;
224
+ try {
225
+ msg = JSON.parse(line);
226
+ } catch {
227
+ return;
228
+ }
229
+ const { id, method, params } = msg;
230
+ switch (method) {
231
+ case "initialize":
232
+ respond(id, {
233
+ protocolVersion: "2024-11-05",
234
+ capabilities: { tools: {} },
235
+ serverInfo: {
236
+ name: "ygl-trees",
237
+ version: "0.69.0"
238
+ }
239
+ });
240
+ break;
241
+ case "tools/list":
242
+ respond(id, { tools: tools.map((t) => ({
243
+ name: t.name,
244
+ description: t.description,
245
+ inputSchema: t.inputSchema
246
+ })) });
247
+ break;
248
+ case "tools/call": {
249
+ const name = params?.name;
250
+ const tool = tools.find((t) => t.name === name);
251
+ if (!tool) {
252
+ respondErr(id, -32602, `Unknown tool: ${name}`);
253
+ break;
254
+ }
255
+ tool.execute(params?.arguments || {}).then((data) => respond(id, { content: [{
256
+ type: "text",
257
+ text: JSON.stringify(data, null, 2)
258
+ }] })).catch((e) => respond(id, {
259
+ content: [{
260
+ type: "text",
261
+ text: `Error: ${e?.message || e}`
262
+ }],
263
+ isError: true
264
+ }));
265
+ break;
266
+ }
267
+ case "ping":
268
+ respond(id, {});
269
+ break;
270
+ default: if (id !== void 0) respondErr(id, -32601, `Method not found: ${method}`);
271
+ }
272
+ });
273
+ process.stderr.write("[ygl-trees MCP] stdio server ready — 8 tools\n");
274
+ }
275
+ //#endregion
276
+ exports.serveMcp = serveMcp;
@@ -0,0 +1,64 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __esmMin = (fn, res, err) => () => {
9
+ if (err) throw err[0];
10
+ try {
11
+ return fn && (res = fn(fn = 0)), res;
12
+ } catch (e) {
13
+ throw err = [e], e;
14
+ }
15
+ };
16
+ var __exportAll = (all, no_symbols) => {
17
+ let target = {};
18
+ for (var name in all) __defProp(target, name, {
19
+ get: all[name],
20
+ enumerable: true
21
+ });
22
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
23
+ return target;
24
+ };
25
+ var __copyProps = (to, from, except, desc) => {
26
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
27
+ key = keys[i];
28
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
29
+ get: ((k) => from[k]).bind(null, key),
30
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
31
+ });
32
+ }
33
+ return to;
34
+ };
35
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
36
+ value: mod,
37
+ enumerable: true
38
+ }) : target, mod));
39
+ var __toCommonJS = (mod) => __hasOwnProp.call(mod, "module.exports") ? mod["module.exports"] : __copyProps(__defProp({}, "__esModule", { value: true }), mod);
40
+ //#endregion
41
+ Object.defineProperty(exports, "__esmMin", {
42
+ enumerable: true,
43
+ get: function() {
44
+ return __esmMin;
45
+ }
46
+ });
47
+ Object.defineProperty(exports, "__exportAll", {
48
+ enumerable: true,
49
+ get: function() {
50
+ return __exportAll;
51
+ }
52
+ });
53
+ Object.defineProperty(exports, "__toCommonJS", {
54
+ enumerable: true,
55
+ get: function() {
56
+ return __toCommonJS;
57
+ }
58
+ });
59
+ Object.defineProperty(exports, "__toESM", {
60
+ enumerable: true,
61
+ get: function() {
62
+ return __toESM;
63
+ }
64
+ });