pico-engine 1.6.2 → 1.8.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 +117 -14
- package/dist/RulesetRegistryLoaderFs.js +24 -8
- package/dist/auth/cookies.js +32 -0
- package/dist/auth/index.js +6 -1
- package/dist/bundledKrl.js +129 -11
- package/dist/channelIdentityPolicy.js +22 -0
- package/dist/cli.js +105 -46
- package/dist/engineConfig.js +280 -0
- package/dist/identity/IdentityService.js +90 -0
- package/dist/identity/crossPico.js +106 -30
- package/dist/identity/didoModule.js +80 -18
- package/dist/identity/establishSubscription.js +2 -2
- package/dist/identity/identityPortable.js +92 -0
- package/dist/identity/index.js +10 -1
- package/dist/identity/localPeerReconcile.js +65 -0
- package/dist/identity/peerDid4.js +175 -0
- package/dist/identity/peerDid4Create.js +90 -0
- package/dist/identity/portableExportPrepare.js +196 -0
- package/dist/identity/portableMove.js +87 -0
- package/dist/identity/skyIntro.js +10 -4
- package/dist/identity/skyProtocol.js +26 -1
- package/dist/identity/skyRotation.js +121 -0
- package/dist/identity/store.js +5 -1
- package/dist/identity/subscriptionEntSync.js +72 -0
- package/dist/identity/veramoDidComm.js +41 -2
- package/dist/identity/webvh.js +4 -2
- package/dist/identity/webvhRelocation.js +115 -0
- package/dist/index.js +127 -34
- package/dist/levelDb.js +102 -0
- package/dist/logging.js +47 -8
- package/dist/oauth/OAuthService.js +12 -5
- package/dist/oauth/channelEligibility.js +7 -1
- package/dist/oauth/index.js +3 -1
- package/dist/policyConfigFromJson.js +112 -0
- package/dist/runMcp.js +27 -0
- package/dist/server.js +95 -24
- package/dist/subtreeWrangler.js +180 -0
- package/dist/uiChannelPolicies.js +16 -0
- package/krl/io.picolabs.oauth.krl +51 -5
- package/krl/io.picolabs.pds.krl +131 -10
- package/krl/io.picolabs.pico-engine-ui.krl +219 -22
- package/krl/io.picolabs.portable_subtree.krl +97 -0
- package/krl/io.picolabs.subscription.krl +226 -72
- package/krl/io.picolabs.wrangler.krl +300 -58
- package/mcp/devResources.js +118 -0
- package/mcp/devTools.js +351 -0
- package/mcp/engineClient.js +133 -0
- package/mcp/meshHttp.js +137 -0
- package/mcp/meshTools.js +189 -0
- package/mcp/runStdio.js +34 -0
- package/mcp/schemaToZod.js +56 -0
- package/package.json +22 -8
- package/public/docs/_sidebar.md +2 -1
- package/public/docs/llms-full.txt +479 -0
- package/public/docs/llms.txt +37 -0
- package/public/docs/openapi.yaml +78 -0
- package/public/docs/pico-engine.md +235 -4
- package/public/docs/pico-overview.md +117 -0
- package/public/docs/pico.md +3 -1
- package/public/docs/rulesets/README.md +25 -0
- package/public/docs/rulesets/io.picolabs.oauth.md +71 -0
- package/public/docs/rulesets/io.picolabs.pds.md +327 -0
- package/public/docs/rulesets/io.picolabs.pico-engine-ui.md +65 -0
- package/public/docs/rulesets/io.picolabs.portable_subtree.md +94 -0
- package/public/docs/rulesets/io.picolabs.subscription.md +398 -0
- package/public/docs/rulesets/io.picolabs.wrangler.md +471 -0
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +28 -26
- package/src/RulesetRegistryLoaderFs.ts +43 -11
- package/src/auth/cookies.ts +34 -0
- package/src/auth/index.ts +7 -0
- package/src/bundledKrl.ts +150 -12
- package/src/channelIdentityPolicy.ts +29 -0
- package/src/cli.ts +98 -40
- package/src/engineConfig.ts +345 -0
- package/src/identity/IdentityService.ts +189 -1
- package/src/identity/crossPico.ts +161 -41
- package/src/identity/didoModule.ts +140 -23
- package/src/identity/establishSubscription.ts +2 -2
- package/src/identity/identityPortable.ts +153 -0
- package/src/identity/index.ts +14 -0
- package/src/identity/localPeerReconcile.ts +97 -0
- package/src/identity/peerDid4.ts +197 -0
- package/src/identity/peerDid4Create.ts +121 -0
- package/src/identity/portableExportPrepare.ts +302 -0
- package/src/identity/portableMove.ts +193 -0
- package/src/identity/skyIntro.ts +16 -12
- package/src/identity/skyProtocol.ts +46 -0
- package/src/identity/skyRotation.ts +211 -0
- package/src/identity/store.ts +5 -1
- package/src/identity/subscriptionEntSync.ts +97 -0
- package/src/identity/veramoDidComm.ts +52 -2
- package/src/identity/webvh.ts +4 -4
- package/src/identity/webvhRelocation.ts +167 -0
- package/src/index.ts +179 -39
- package/src/levelDb.ts +122 -0
- package/src/logging.ts +63 -11
- package/src/oauth/OAuthService.ts +20 -6
- package/src/oauth/channelEligibility.ts +9 -0
- package/src/oauth/index.ts +7 -1
- package/src/policyConfigFromJson.ts +131 -0
- package/src/runMcp.ts +30 -0
- package/src/server.ts +133 -27
- package/src/subtreeWrangler.ts +278 -0
- package/src/uiChannelPolicies.ts +16 -0
- package/krl/io.picolabs.did-o.krl +0 -349
- package/krl/test/didtestdid.krl +0 -78
package/mcp/devTools.js
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { z } = require("zod");
|
|
4
|
+
const krlCheck = require("krl-compiler/src/check");
|
|
5
|
+
const { jsonText } = require("./engineClient");
|
|
6
|
+
|
|
7
|
+
function textResult(text, isError = false) {
|
|
8
|
+
return {
|
|
9
|
+
content: [{ type: "text", text }],
|
|
10
|
+
isError,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const emptySchema = {};
|
|
15
|
+
|
|
16
|
+
function registerDevTools(server, client) {
|
|
17
|
+
server.tool(
|
|
18
|
+
"engine_status",
|
|
19
|
+
"Local pico-engine connectivity, version, and root UI channel ECI",
|
|
20
|
+
emptySchema,
|
|
21
|
+
async () => {
|
|
22
|
+
try {
|
|
23
|
+
const dev = await client.getDevLocal().catch(() => ({}));
|
|
24
|
+
const uiCtx = await client.getUiContext().catch(() => ({}));
|
|
25
|
+
let uiEci = null;
|
|
26
|
+
try {
|
|
27
|
+
uiEci = await client.resolveUiEci();
|
|
28
|
+
} catch (_err) {
|
|
29
|
+
uiEci = null;
|
|
30
|
+
}
|
|
31
|
+
return textResult(
|
|
32
|
+
jsonText({
|
|
33
|
+
baseUrl: client.baseUrl,
|
|
34
|
+
devLocal: dev,
|
|
35
|
+
uiContext: uiCtx,
|
|
36
|
+
uiEci,
|
|
37
|
+
env: {
|
|
38
|
+
PICO_ENGINE_HOME: process.env.PICO_ENGINE_HOME || null,
|
|
39
|
+
PICO_ENGINE_BASE_URL: process.env.PICO_ENGINE_BASE_URL || null,
|
|
40
|
+
PICO_ENGINE_UI_ECI: process.env.PICO_ENGINE_UI_ECI || null,
|
|
41
|
+
},
|
|
42
|
+
hint:
|
|
43
|
+
uiEci == null
|
|
44
|
+
? "Start the engine (npm start) and ensure a root pico exists, or set PICO_ENGINE_UI_ECI."
|
|
45
|
+
: undefined,
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
} catch (err) {
|
|
49
|
+
return textResult(String(err), true);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
server.tool(
|
|
55
|
+
"validate_krl",
|
|
56
|
+
"Parse and compile KRL with schema annotation lint (same as krl-check)",
|
|
57
|
+
{
|
|
58
|
+
source: z.string().describe("KRL source text"),
|
|
59
|
+
filename: z
|
|
60
|
+
.string()
|
|
61
|
+
.optional()
|
|
62
|
+
.describe("Optional filename for diagnostics"),
|
|
63
|
+
},
|
|
64
|
+
async ({ source, filename }) => {
|
|
65
|
+
const result = krlCheck(source, filename);
|
|
66
|
+
return textResult(
|
|
67
|
+
jsonText({
|
|
68
|
+
ok: result.ok,
|
|
69
|
+
rid: result.rid,
|
|
70
|
+
errors: result.errors,
|
|
71
|
+
warnings: result.warnings,
|
|
72
|
+
}),
|
|
73
|
+
!result.ok
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
server.tool(
|
|
79
|
+
"query",
|
|
80
|
+
"Run a query on a channel ECI (JSON args body)",
|
|
81
|
+
{
|
|
82
|
+
eci: z.string(),
|
|
83
|
+
rid: z.string(),
|
|
84
|
+
name: z.string(),
|
|
85
|
+
args: z.record(z.any()).optional(),
|
|
86
|
+
},
|
|
87
|
+
async ({ eci, rid, name, args }) => {
|
|
88
|
+
try {
|
|
89
|
+
const data = await client.channelQuery(eci, rid, name, args || {});
|
|
90
|
+
return textResult(jsonText(data));
|
|
91
|
+
} catch (err) {
|
|
92
|
+
return textResult(String(err), true);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
server.tool(
|
|
98
|
+
"raise_event",
|
|
99
|
+
"Send an event on a channel and wait for the outcome (event-wait)",
|
|
100
|
+
{
|
|
101
|
+
eci: z.string(),
|
|
102
|
+
domain: z.string(),
|
|
103
|
+
name: z.string(),
|
|
104
|
+
attrs: z.record(z.any()).optional(),
|
|
105
|
+
},
|
|
106
|
+
async ({ eci, domain, name, attrs }) => {
|
|
107
|
+
try {
|
|
108
|
+
const data = await client.channelEventWait(
|
|
109
|
+
eci,
|
|
110
|
+
domain,
|
|
111
|
+
name,
|
|
112
|
+
attrs || {}
|
|
113
|
+
);
|
|
114
|
+
return textResult(jsonText(data));
|
|
115
|
+
} catch (err) {
|
|
116
|
+
return textResult(String(err), true);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
server.tool(
|
|
122
|
+
"eval_krl",
|
|
123
|
+
"Evaluate a read-only KRL expression in an installed ruleset context (__consoleEval)",
|
|
124
|
+
{
|
|
125
|
+
eci: z.string().describe("Channel ECI of the target pico"),
|
|
126
|
+
rid: z.string().describe("Ruleset id"),
|
|
127
|
+
expression: z.string().describe("KRL expression"),
|
|
128
|
+
},
|
|
129
|
+
async ({ eci, rid, expression }) => {
|
|
130
|
+
try {
|
|
131
|
+
const data = await client.evalKrl(eci, rid, expression);
|
|
132
|
+
if (data && typeof data === "object" && data.error != null) {
|
|
133
|
+
return textResult(jsonText(data), true);
|
|
134
|
+
}
|
|
135
|
+
return textResult(jsonText(data));
|
|
136
|
+
} catch (err) {
|
|
137
|
+
return textResult(String(err), true);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
server.tool(
|
|
143
|
+
"list_picos",
|
|
144
|
+
"List picos on this engine (forest boxes: eci, name, parent, children)",
|
|
145
|
+
emptySchema,
|
|
146
|
+
async () => {
|
|
147
|
+
try {
|
|
148
|
+
const boxes = await client.listPicoBoxes();
|
|
149
|
+
return textResult(jsonText(boxes));
|
|
150
|
+
} catch (err) {
|
|
151
|
+
return textResult(String(err), true);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
server.tool(
|
|
157
|
+
"pico_schema",
|
|
158
|
+
"Merged pico schema (wrangler:picoSchema) from the root UI channel",
|
|
159
|
+
{
|
|
160
|
+
format: z.enum(["default", "jsonschema"]).optional(),
|
|
161
|
+
},
|
|
162
|
+
async ({ format }) => {
|
|
163
|
+
try {
|
|
164
|
+
const ui = await client.resolveUiEci();
|
|
165
|
+
const args = {};
|
|
166
|
+
if (format) args.format = format;
|
|
167
|
+
const data = await client.channelQuery(
|
|
168
|
+
ui,
|
|
169
|
+
"io.picolabs.wrangler",
|
|
170
|
+
"picoSchema",
|
|
171
|
+
args
|
|
172
|
+
);
|
|
173
|
+
return textResult(jsonText(data));
|
|
174
|
+
} catch (err) {
|
|
175
|
+
return textResult(String(err), true);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
server.tool(
|
|
181
|
+
"ruleset_schema",
|
|
182
|
+
"Ruleset schema for one RID (wrangler:rulesetSchema)",
|
|
183
|
+
{ rid: z.string() },
|
|
184
|
+
async ({ rid }) => {
|
|
185
|
+
try {
|
|
186
|
+
const ui = await client.resolveUiEci();
|
|
187
|
+
const data = await client.channelQuery(
|
|
188
|
+
ui,
|
|
189
|
+
"io.picolabs.wrangler",
|
|
190
|
+
"rulesetSchema",
|
|
191
|
+
{ rid }
|
|
192
|
+
);
|
|
193
|
+
return textResult(jsonText(data));
|
|
194
|
+
} catch (err) {
|
|
195
|
+
return textResult(String(err), true);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
server.tool(
|
|
201
|
+
"list_channels",
|
|
202
|
+
"Channels and rulesets on a pico (io.picolabs.pico-engine-ui/pico query)",
|
|
203
|
+
{
|
|
204
|
+
eci: z.string().optional().describe("UI ECI; default root UI channel"),
|
|
205
|
+
},
|
|
206
|
+
async ({ eci }) => {
|
|
207
|
+
try {
|
|
208
|
+
const target = eci || (await client.resolveUiEci());
|
|
209
|
+
const data = await client.channelQuery(
|
|
210
|
+
target,
|
|
211
|
+
"io.picolabs.pico-engine-ui",
|
|
212
|
+
"pico",
|
|
213
|
+
{}
|
|
214
|
+
);
|
|
215
|
+
return textResult(jsonText(data));
|
|
216
|
+
} catch (err) {
|
|
217
|
+
return textResult(String(err), true);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
server.tool(
|
|
223
|
+
"tail_events",
|
|
224
|
+
"Recent pico logs from the Logging tab query (truncated)",
|
|
225
|
+
{
|
|
226
|
+
eci: z.string().optional().describe("Pico UI ECI; default root"),
|
|
227
|
+
limit: z.number().int().min(1).max(30).optional(),
|
|
228
|
+
},
|
|
229
|
+
async ({ eci, limit }) => {
|
|
230
|
+
try {
|
|
231
|
+
const target = eci || (await client.resolveUiEci());
|
|
232
|
+
const logs = await client.channelQuery(
|
|
233
|
+
target,
|
|
234
|
+
"io.picolabs.pico-engine-ui",
|
|
235
|
+
"logs",
|
|
236
|
+
{}
|
|
237
|
+
);
|
|
238
|
+
const max = limit ?? 10;
|
|
239
|
+
return textResult(
|
|
240
|
+
jsonText(Array.isArray(logs) ? logs.slice(0, max) : logs)
|
|
241
|
+
);
|
|
242
|
+
} catch (err) {
|
|
243
|
+
return textResult(String(err), true);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
server.tool(
|
|
249
|
+
"install_ruleset",
|
|
250
|
+
"Install a ruleset URL on a pico via engine_ui (default root UI channel)",
|
|
251
|
+
{
|
|
252
|
+
url: z.string(),
|
|
253
|
+
eci: z.string().optional().describe("Target pico UI ECI"),
|
|
254
|
+
},
|
|
255
|
+
async ({ url, eci }) => {
|
|
256
|
+
try {
|
|
257
|
+
const ui = eci || (await client.resolveUiEci());
|
|
258
|
+
const data = await client.channelEventWait(ui, "engine_ui", "install", {
|
|
259
|
+
url,
|
|
260
|
+
});
|
|
261
|
+
return textResult(jsonText(data));
|
|
262
|
+
} catch (err) {
|
|
263
|
+
return textResult(String(err), true);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
server.tool(
|
|
269
|
+
"flush_ruleset",
|
|
270
|
+
"Flush (reinstall) a ruleset by URL on a pico via engine_ui",
|
|
271
|
+
{
|
|
272
|
+
url: z.string(),
|
|
273
|
+
eci: z.string().optional(),
|
|
274
|
+
},
|
|
275
|
+
async ({ url, eci }) => {
|
|
276
|
+
try {
|
|
277
|
+
const ui = eci || (await client.resolveUiEci());
|
|
278
|
+
const data = await client.channelEventWait(ui, "engine_ui", "flush", {
|
|
279
|
+
url,
|
|
280
|
+
});
|
|
281
|
+
return textResult(jsonText(data));
|
|
282
|
+
} catch (err) {
|
|
283
|
+
return textResult(String(err), true);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
server.tool(
|
|
289
|
+
"create_pico",
|
|
290
|
+
"Create a child pico under the root (engine_ui new)",
|
|
291
|
+
{
|
|
292
|
+
name: z.string().optional(),
|
|
293
|
+
backgroundColor: z.string().optional(),
|
|
294
|
+
},
|
|
295
|
+
async (attrs) => {
|
|
296
|
+
try {
|
|
297
|
+
const data = await client.engineUiEvent("new", attrs);
|
|
298
|
+
return textResult(jsonText(data));
|
|
299
|
+
} catch (err) {
|
|
300
|
+
return textResult(String(err), true);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
server.tool(
|
|
306
|
+
"delete_pico",
|
|
307
|
+
"Delete a pico by its UI channel ECI (engine_ui del)",
|
|
308
|
+
{ eci: z.string().describe("UI ECI of the pico to delete") },
|
|
309
|
+
async ({ eci }) => {
|
|
310
|
+
try {
|
|
311
|
+
const data = await client.engineUiEvent("del", { eci });
|
|
312
|
+
return textResult(jsonText(data));
|
|
313
|
+
} catch (err) {
|
|
314
|
+
return textResult(String(err), true);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
server.tool(
|
|
320
|
+
"create_channel",
|
|
321
|
+
"Create a channel on the root pico (engine_ui new_channel)",
|
|
322
|
+
{
|
|
323
|
+
tags: z.union([z.string(), z.array(z.string())]),
|
|
324
|
+
id: z.string().optional(),
|
|
325
|
+
eventPolicy: z.record(z.any()).optional(),
|
|
326
|
+
queryPolicy: z.record(z.any()).optional(),
|
|
327
|
+
},
|
|
328
|
+
async ({ tags, id, eventPolicy, queryPolicy }) => {
|
|
329
|
+
try {
|
|
330
|
+
const tagList =
|
|
331
|
+
typeof tags === "string"
|
|
332
|
+
? tags
|
|
333
|
+
.split(",")
|
|
334
|
+
.map((t) => t.trim())
|
|
335
|
+
.filter(Boolean)
|
|
336
|
+
: tags;
|
|
337
|
+
const data = await client.engineUiEvent("new_channel", {
|
|
338
|
+
tags: tagList,
|
|
339
|
+
id,
|
|
340
|
+
eventPolicy,
|
|
341
|
+
queryPolicy,
|
|
342
|
+
});
|
|
343
|
+
return textResult(jsonText(data));
|
|
344
|
+
} catch (err) {
|
|
345
|
+
return textResult(String(err), true);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
module.exports = { registerDevTools };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fetch = require("cross-fetch");
|
|
4
|
+
|
|
5
|
+
function normalizeBaseUrl(url) {
|
|
6
|
+
return url.replace(/\/$/, "");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class EngineClient {
|
|
10
|
+
constructor(opts = {}) {
|
|
11
|
+
const port = process.env.PORT || "3000";
|
|
12
|
+
this.baseUrl = normalizeBaseUrl(
|
|
13
|
+
opts.baseUrl ||
|
|
14
|
+
process.env.PICO_ENGINE_BASE_URL ||
|
|
15
|
+
`http://localhost:${port}`
|
|
16
|
+
);
|
|
17
|
+
this.uiEciOverride = opts.uiEci || process.env.PICO_ENGINE_UI_ECI;
|
|
18
|
+
this.uiEciCached = undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async getDevLocal() {
|
|
22
|
+
const res = await fetch(`${this.baseUrl}/api/dev/local`);
|
|
23
|
+
if (!res.ok) {
|
|
24
|
+
const text = await res.text();
|
|
25
|
+
throw new Error(`GET /api/dev/local ${res.status}: ${text}`);
|
|
26
|
+
}
|
|
27
|
+
return res.json();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async getUiContext() {
|
|
31
|
+
const res = await fetch(`${this.baseUrl}/api/ui-context`);
|
|
32
|
+
if (!res.ok) {
|
|
33
|
+
throw new Error(`GET /api/ui-context ${res.status}`);
|
|
34
|
+
}
|
|
35
|
+
return res.json();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async resolveUiEci() {
|
|
39
|
+
if (this.uiEciOverride) {
|
|
40
|
+
return this.uiEciOverride;
|
|
41
|
+
}
|
|
42
|
+
if (this.uiEciCached) {
|
|
43
|
+
return this.uiEciCached;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
const dev = await this.getDevLocal();
|
|
47
|
+
if (dev.rootUiEci) {
|
|
48
|
+
this.uiEciCached = dev.rootUiEci;
|
|
49
|
+
return dev.rootUiEci;
|
|
50
|
+
}
|
|
51
|
+
} catch (_err) {
|
|
52
|
+
// fall through
|
|
53
|
+
}
|
|
54
|
+
const ctx = await this.getUiContext();
|
|
55
|
+
if (ctx.eci) {
|
|
56
|
+
this.uiEciCached = ctx.eci;
|
|
57
|
+
return ctx.eci;
|
|
58
|
+
}
|
|
59
|
+
throw new Error(
|
|
60
|
+
"Could not resolve UI channel ECI. Set PICO_ENGINE_UI_ECI, sign in via the UI (same engine), or use localhost with PICO_ENGINE_ALLOW_LOCALHOST_C unset."
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async channelQuery(eci, rid, name, args = {}) {
|
|
65
|
+
const res = await fetch(
|
|
66
|
+
`${this.baseUrl}/c/${encodeURIComponent(eci)}/query/${encodeURIComponent(rid)}/${encodeURIComponent(name)}`,
|
|
67
|
+
{
|
|
68
|
+
method: "POST",
|
|
69
|
+
headers: { "Content-Type": "application/json; charset=utf-8" },
|
|
70
|
+
body: JSON.stringify(args),
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
const text = await res.text();
|
|
74
|
+
if (!text) return null;
|
|
75
|
+
const data = JSON.parse(text);
|
|
76
|
+
if (!res.ok) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
data && typeof data === "object" && data.error != null
|
|
79
|
+
? String(data.error)
|
|
80
|
+
: text
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return data;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async channelEventWait(eci, domain, name, attrs = {}) {
|
|
87
|
+
const res = await fetch(
|
|
88
|
+
`${this.baseUrl}/c/${encodeURIComponent(eci)}/event-wait/${encodeURIComponent(domain)}/${encodeURIComponent(name)}`,
|
|
89
|
+
{
|
|
90
|
+
method: "POST",
|
|
91
|
+
headers: { "Content-Type": "application/json; charset=utf-8" },
|
|
92
|
+
body: JSON.stringify(attrs),
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
const text = await res.text();
|
|
96
|
+
if (!text) return null;
|
|
97
|
+
return JSON.parse(text);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async engineUiEvent(name, attrs = {}) {
|
|
101
|
+
const ui = await this.resolveUiEci();
|
|
102
|
+
return this.channelEventWait(ui, "engine_ui", name, attrs);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async evalKrl(targetEci, rid, expr) {
|
|
106
|
+
return this.channelQuery(targetEci, rid, "__consoleEval", { expr });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async listPicoBoxes() {
|
|
110
|
+
const ui = await this.resolveUiEci();
|
|
111
|
+
const out = [];
|
|
112
|
+
const walk = async (client, eci) => {
|
|
113
|
+
const box = await client.channelQuery(
|
|
114
|
+
eci,
|
|
115
|
+
"io.picolabs.pico-engine-ui",
|
|
116
|
+
"box"
|
|
117
|
+
);
|
|
118
|
+
out.push(box);
|
|
119
|
+
const children = (box && box.children) || [];
|
|
120
|
+
for (const child of children) {
|
|
121
|
+
await walk(client, child);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
await walk(this, ui);
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function jsonText(data) {
|
|
130
|
+
return JSON.stringify(data, null, 2);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
module.exports = { EngineClient, jsonText };
|
package/mcp/meshHttp.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { randomUUID } = require("crypto");
|
|
4
|
+
const { McpServer } = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
5
|
+
const {
|
|
6
|
+
StreamableHTTPServerTransport,
|
|
7
|
+
} = require("@modelcontextprotocol/sdk/server/streamableHttp.js");
|
|
8
|
+
const { registerMeshTools } = require("./meshTools");
|
|
9
|
+
|
|
10
|
+
const engineVersion = require("../package.json").version;
|
|
11
|
+
|
|
12
|
+
/** @type {Map<string, { transport: import("@modelcontextprotocol/sdk/server/streamableHttp.js").StreamableHTTPServerTransport, mcpServer: import("@modelcontextprotocol/sdk/server/mcp.js").McpServer, eci: string }>} */
|
|
13
|
+
const sessions = new Map();
|
|
14
|
+
|
|
15
|
+
function bodyHasInitialize(body) {
|
|
16
|
+
if (!body || typeof body !== "object") return false;
|
|
17
|
+
const batch = Array.isArray(body) ? body : [body];
|
|
18
|
+
return batch.some(
|
|
19
|
+
(m) => m && typeof m === "object" && m.method === "initialize"
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function sessionIdFromRequest(req) {
|
|
24
|
+
const raw =
|
|
25
|
+
req.headers["mcp-session-id"] ||
|
|
26
|
+
req.headers["Mcp-Session-Id"] ||
|
|
27
|
+
req.headers["MCP-Session-Id"];
|
|
28
|
+
return typeof raw === "string" && raw.trim() ? raw.trim() : undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function createMeshSession(core, eci) {
|
|
32
|
+
const mcpServer = new McpServer(
|
|
33
|
+
{
|
|
34
|
+
name: "pico-engine-mesh",
|
|
35
|
+
version: engineVersion,
|
|
36
|
+
},
|
|
37
|
+
{ capabilities: { tools: {}, resources: {} } }
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
registerMeshTools(mcpServer, core, eci);
|
|
41
|
+
|
|
42
|
+
const transport = new StreamableHTTPServerTransport({
|
|
43
|
+
sessionIdGenerator: () => randomUUID(),
|
|
44
|
+
onsessioninitialized: (sessionId) => {
|
|
45
|
+
sessions.set(sessionId, { transport, mcpServer, eci });
|
|
46
|
+
},
|
|
47
|
+
onsessionclosed: async (sessionId) => {
|
|
48
|
+
const row = sessions.get(sessionId);
|
|
49
|
+
if (row) {
|
|
50
|
+
sessions.delete(sessionId);
|
|
51
|
+
try {
|
|
52
|
+
await row.mcpServer.close();
|
|
53
|
+
} catch (_e) {
|
|
54
|
+
// ignore
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
await mcpServer.connect(transport);
|
|
61
|
+
return { transport, mcpServer, eci };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Streamable HTTP mesh MCP at `/mcp/:eci` (OAuth bearer scoped to that ECI).
|
|
66
|
+
*/
|
|
67
|
+
function registerMeshMcpRoutes(app, core, oauth) {
|
|
68
|
+
if (!oauth) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function requireMeshBearer(req, res, next) {
|
|
73
|
+
const eci = req.params.eci;
|
|
74
|
+
if (!eci) {
|
|
75
|
+
res.status(400).json({ error: "Missing channel ECI" });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
core.picoFramework.lookupChannel(eci);
|
|
80
|
+
} catch (_e) {
|
|
81
|
+
res.status(404).json({ error: "Unknown channel ECI" });
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const header = req.headers.authorization;
|
|
85
|
+
if (typeof header !== "string" || !header.startsWith("Bearer ")) {
|
|
86
|
+
res.status(401).json({ error: "Bearer token required" });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const token = header.slice("Bearer ".length).trim();
|
|
90
|
+
try {
|
|
91
|
+
const valid = await oauth.validateSkyBearerToken(token, eci);
|
|
92
|
+
if (!valid) {
|
|
93
|
+
res.status(401).json({ error: "Invalid or expired token" });
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
next();
|
|
97
|
+
} catch (err) {
|
|
98
|
+
next(err);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const handler = async (req, res, next) => {
|
|
103
|
+
try {
|
|
104
|
+
const eci = req.params.eci;
|
|
105
|
+
const sessionId = sessionIdFromRequest(req);
|
|
106
|
+
let row = sessionId ? sessions.get(sessionId) : undefined;
|
|
107
|
+
|
|
108
|
+
if (!row && req.method === "POST" && bodyHasInitialize(req.body)) {
|
|
109
|
+
row = await createMeshSession(core, eci);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!row) {
|
|
113
|
+
if (sessionId) {
|
|
114
|
+
res.status(404).json({ error: "Unknown MCP session" });
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
res.status(400).json({
|
|
118
|
+
error: "MCP session required (send initialize on POST without session id)",
|
|
119
|
+
});
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (row.eci !== eci) {
|
|
124
|
+
res.status(403).json({ error: "Session ECI mismatch" });
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await row.transport.handleRequest(req, res, req.body);
|
|
129
|
+
} catch (err) {
|
|
130
|
+
next(err);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
app.all("/mcp/:eci", requireMeshBearer, handler);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
module.exports = { registerMeshMcpRoutes, sessions, meshToolCatalog: require("./meshTools").meshToolCatalog, getChannelSchema: require("./meshTools").getChannelSchema };
|