@openartifactsai/server-sdk 0.0.0-stage → 0.23.0-rc.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/bin/frame-host.d.ts +2 -0
- package/bin/frame-host.js +47 -0
- package/package.json +124 -4
- package/src/child.d.ts +37 -0
- package/src/child.js +64 -0
- package/src/connectors/connectors.d.ts +101 -0
- package/src/connectors/connectors.js +256 -0
- package/src/connectors/daemon-connectors.d.ts +34 -0
- package/src/connectors/daemon-connectors.js +73 -0
- package/src/connectors/index.d.ts +7 -0
- package/src/connectors/index.js +7 -0
- package/src/connectors/mcp-client.d.ts +157 -0
- package/src/connectors/mcp-client.js +483 -0
- package/src/daemon.d.ts +119 -0
- package/src/daemon.js +301 -0
- package/src/frame/access.d.ts +69 -0
- package/src/frame/access.js +168 -0
- package/src/frame/client/comments.css +1 -0
- package/src/frame/client/comments.js +58 -0
- package/src/frame/client/imports.d.ts +6 -0
- package/src/frame/client/imports.js +112 -0
- package/src/frame/client/mcp-shell.js +345 -0
- package/src/frame/client/mcp.js +253 -0
- package/src/frame/client/runtime.js +1363 -0
- package/src/frame/client/sample.js +762 -0
- package/src/frame/client/shell.css +51 -0
- package/src/frame/client/shell.html +29 -0
- package/src/frame/client/shell.js +1417 -0
- package/src/frame/comments.d.ts +102 -0
- package/src/frame/comments.js +379 -0
- package/src/frame/core.d.ts +313 -0
- package/src/frame/core.js +1757 -0
- package/src/frame/db.d.ts +162 -0
- package/src/frame/db.js +604 -0
- package/src/frame/declaration.d.ts +30 -0
- package/src/frame/declaration.js +60 -0
- package/src/frame/env.d.ts +262 -0
- package/src/frame/env.js +13 -0
- package/src/frame/files.d.ts +130 -0
- package/src/frame/files.js +472 -0
- package/src/frame/index.d.ts +35 -0
- package/src/frame/index.js +33 -0
- package/src/frame/local.d.ts +126 -0
- package/src/frame/local.js +612 -0
- package/src/frame/mcp-relay.d.ts +79 -0
- package/src/frame/mcp-relay.js +191 -0
- package/src/frame/page-api.d.ts +48 -0
- package/src/frame/page-api.js +1 -0
- package/src/frame/page-comments.d.ts +109 -0
- package/src/frame/page-comments.js +1 -0
- package/src/frame/page-db.d.ts +88 -0
- package/src/frame/page-db.js +1 -0
- package/src/frame/page-imports.d.ts +2 -0
- package/src/frame/page-imports.js +2 -0
- package/src/frame/page-services.d.ts +124 -0
- package/src/frame/page-services.js +1 -0
- package/src/frame/page-values.d.ts +90 -0
- package/src/frame/page-values.js +1 -0
- package/src/frame/sample-relay.d.ts +42 -0
- package/src/frame/sample-relay.js +186 -0
- package/src/frame/snapshot-action.d.ts +18 -0
- package/src/frame/snapshot-action.js +82 -0
- package/src/frame/wire.d.ts +198 -0
- package/src/frame/wire.js +21 -0
- package/src/index.d.ts +17 -0
- package/src/index.js +17 -0
- package/src/lan.d.ts +75 -0
- package/src/lan.js +200 -0
- package/src/legacy-env.d.ts +7 -0
- package/src/legacy-env.js +14 -0
- package/src/serve.d.ts +375 -0
- package/src/serve.js +811 -0
- package/src/snapshot.d.ts +41 -0
- package/src/snapshot.js +224 -0
- package/src/type-package.d.ts +48 -0
- package/src/type-package.js +114 -0
- package/src/ws.d.ts +50 -0
- package/src/ws.js +203 -0
- package/src/zip.d.ts +197 -0
- package/src/zip.js +662 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(() => {
|
|
3
|
+
// packages/server-sdk/src/frame/client/mcp.ts
|
|
4
|
+
(() => {
|
|
5
|
+
"use strict";
|
|
6
|
+
const register = window.__openArtifactsMcp;
|
|
7
|
+
if (typeof register !== "function") return;
|
|
8
|
+
const ANSWER_MS = 13e4;
|
|
9
|
+
const ACKED_MS = 9e5;
|
|
10
|
+
const SHORT_MS = 7e4;
|
|
11
|
+
const NAME = /^[A-Za-z0-9_.:@/-]{1,128}$/;
|
|
12
|
+
const isRecord = (v) => typeof v === "object" && v !== null;
|
|
13
|
+
const isPlain = (v) => {
|
|
14
|
+
if (!isRecord(v) || Array.isArray(v)) return false;
|
|
15
|
+
const proto = Object.getPrototypeOf(v);
|
|
16
|
+
return proto === null || proto === Object.prototype;
|
|
17
|
+
};
|
|
18
|
+
function failure(code, message, extra = {}) {
|
|
19
|
+
return Object.assign(new Error(message), { name: "McpError", ...extra, code, message });
|
|
20
|
+
}
|
|
21
|
+
function failureOf(value, server, tool) {
|
|
22
|
+
const e = isRecord(value) ? value : {};
|
|
23
|
+
const code = typeof e.code === "string" ? e.code : "upstream_error";
|
|
24
|
+
const { code: _c, message: _m, ...rest } = e;
|
|
25
|
+
return failure(code, typeof e.message === "string" ? e.message : "The connector call failed.", { ...server ? { server } : {}, ...tool ? { tool } : {}, ...rest });
|
|
26
|
+
}
|
|
27
|
+
register((host) => {
|
|
28
|
+
const declared = () => {
|
|
29
|
+
const list = host.config().servers;
|
|
30
|
+
return Array.isArray(list) ? list.filter((s) => isRecord(s) && typeof s.server === "string" && Array.isArray(s.tools)) : [];
|
|
31
|
+
};
|
|
32
|
+
const isDeclared = (server, tool) => declared().some((s) => s.server === server && (tool === void 0 || s.tools.includes(tool)));
|
|
33
|
+
function parts(server, tool, input) {
|
|
34
|
+
if (typeof server !== "string" || !NAME.test(server)) return failure("bad_request", "mcp: server is the connector's name, as declared.");
|
|
35
|
+
if (typeof tool !== "string" || !NAME.test(tool)) return failure("bad_request", "mcp: tool is the tool's name, as declared.", { server });
|
|
36
|
+
if (input !== void 0 && input !== null && !isPlain(input)) return failure("bad_request", "mcp: input is a plain object of the tool's arguments.", { server, tool });
|
|
37
|
+
if (!isDeclared(server, tool)) return failure("not_in_manifest", `The page does not declare ${tool} of ${server}: add it to capabilities.mcp.servers.`, { server, tool });
|
|
38
|
+
let copy;
|
|
39
|
+
try {
|
|
40
|
+
copy = input ?? {};
|
|
41
|
+
JSON.stringify(copy);
|
|
42
|
+
} catch {
|
|
43
|
+
return failure("bad_request", "mcp: input must be JSON (no cycles, no functions).", { server, tool });
|
|
44
|
+
}
|
|
45
|
+
return { server, tool, input: copy };
|
|
46
|
+
}
|
|
47
|
+
function cacheOf(value) {
|
|
48
|
+
if (value === void 0 || value === false) return value;
|
|
49
|
+
if (!isPlain(value)) return failure("bad_request", "cache: false, or {staleTime, gcTime, refresh}.");
|
|
50
|
+
for (const key of ["staleTime", "gcTime"]) {
|
|
51
|
+
const n = value[key];
|
|
52
|
+
if (n !== void 0 && (typeof n !== "number" || !Number.isFinite(n) || n < 0)) return failure("bad_request", `cache.${key}: ms, 0 or more.`);
|
|
53
|
+
}
|
|
54
|
+
if (value.refresh !== void 0 && typeof value.refresh !== "boolean") return failure("bad_request", "cache.refresh: true or false.");
|
|
55
|
+
return { ...value.staleTime !== void 0 ? { staleTime: value.staleTime } : {}, ...value.gcTime !== void 0 ? { gcTime: value.gcTime } : {}, ...value.refresh ? { refresh: true } : {} };
|
|
56
|
+
}
|
|
57
|
+
function settle(answer, server, tool) {
|
|
58
|
+
if (!isRecord(answer) || !Array.isArray(answer.content)) throw failure("upstream_error", "The connector call answered nothing usable.", { server, tool, retryable: true });
|
|
59
|
+
const result = answer;
|
|
60
|
+
if (result.isError === true) {
|
|
61
|
+
const text = result.content.find((c) => isRecord(c) && c.type === "text" && typeof c.text === "string");
|
|
62
|
+
const { payload: _p, cache: _c, ...envelope } = result;
|
|
63
|
+
throw failure("tool_error", `${tool} of ${server} failed${text ? `: ${String(text.text).slice(0, 500)}` : "."}`, { server, tool, result: envelope });
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
const running = /* @__PURE__ */ new Map();
|
|
68
|
+
function callTool(server, tool, input, options) {
|
|
69
|
+
const p = parts(server, tool, input);
|
|
70
|
+
if ("code" in p) return Promise.reject(p);
|
|
71
|
+
if (options !== void 0 && !isPlain(options)) return Promise.reject(failure("bad_request", "callTool options: {cache, signal}.", { server: p.server, tool: p.tool }));
|
|
72
|
+
const o = options ?? {};
|
|
73
|
+
const cache = cacheOf(o.cache);
|
|
74
|
+
if (isRecord(cache) && "code" in cache) return Promise.reject(Object.assign(cache, { server: p.server, tool: p.tool }));
|
|
75
|
+
const signal = o.signal;
|
|
76
|
+
if (signal !== void 0 && !(typeof AbortSignal !== "undefined" && signal instanceof AbortSignal)) return Promise.reject(failure("bad_request", "callTool options.signal: an AbortSignal.", { server: p.server, tool: p.tool }));
|
|
77
|
+
if (signal?.aborted) return Promise.reject(failure("cancelled", "The call was cancelled.", { server: p.server, tool: p.tool }));
|
|
78
|
+
if (!host.framed) return Promise.reject(failure("capability_disabled", "This page is not inside its frame shell.", { server: p.server, tool: p.tool }));
|
|
79
|
+
const id = host.id();
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
let timer = null;
|
|
82
|
+
let over = false;
|
|
83
|
+
const arm = (ms) => {
|
|
84
|
+
if (timer) clearTimeout(timer);
|
|
85
|
+
timer = setTimeout(() => {
|
|
86
|
+
host.send(host.id(), "cancelCall", [id]);
|
|
87
|
+
call.end(failure("upstream_error", `${p.tool} of ${p.server} did not answer in time.`, { server: p.server, tool: p.tool, retryable: true }));
|
|
88
|
+
}, ms);
|
|
89
|
+
};
|
|
90
|
+
const onAbort = () => {
|
|
91
|
+
host.send(host.id(), "cancelCall", [id]);
|
|
92
|
+
call.end(failure("cancelled", "The call was cancelled.", { server: p.server, tool: p.tool }));
|
|
93
|
+
};
|
|
94
|
+
const call = {
|
|
95
|
+
end: (error, answer) => {
|
|
96
|
+
if (over) return;
|
|
97
|
+
over = true;
|
|
98
|
+
if (timer) clearTimeout(timer);
|
|
99
|
+
running.delete(id);
|
|
100
|
+
host.route(id, null);
|
|
101
|
+
signal?.removeEventListener("abort", onAbort);
|
|
102
|
+
if (error) return reject(error);
|
|
103
|
+
try {
|
|
104
|
+
resolve(settle(answer, p.server, p.tool));
|
|
105
|
+
} catch (e) {
|
|
106
|
+
reject(e);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
running.set(id, call);
|
|
111
|
+
host.route(id, (m) => {
|
|
112
|
+
if (m.__frame_cap_ack === true) return arm(ACKED_MS);
|
|
113
|
+
if (m.__frame_cap_p === true) return arm(ACKED_MS);
|
|
114
|
+
if (m.__frame_cap_r !== true) return;
|
|
115
|
+
if (m.error != null) call.end(failureOf(m.error, p.server, p.tool));
|
|
116
|
+
else call.end(null, m.result);
|
|
117
|
+
});
|
|
118
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
119
|
+
arm(ANSWER_MS);
|
|
120
|
+
if (!host.send(id, "callTool", [p.server, p.tool, p.input, cache === void 0 ? null : cache])) {
|
|
121
|
+
call.end(failure("upstream_error", "The call could not be sent.", { server: p.server, tool: p.tool, retryable: true }));
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
function ask(method, args, server, tool) {
|
|
126
|
+
if (!host.framed) return Promise.reject(failure("capability_disabled", "This page is not inside its frame shell."));
|
|
127
|
+
const id = host.id();
|
|
128
|
+
return new Promise((resolve, reject) => {
|
|
129
|
+
const timer = setTimeout(() => {
|
|
130
|
+
host.route(id, null);
|
|
131
|
+
reject(failure("upstream_error", `mcp.${method} did not answer in time.`, { retryable: true }));
|
|
132
|
+
}, SHORT_MS);
|
|
133
|
+
host.route(id, (m) => {
|
|
134
|
+
if (m.__frame_cap_r !== true) return;
|
|
135
|
+
clearTimeout(timer);
|
|
136
|
+
host.route(id, null);
|
|
137
|
+
if (m.error != null) reject(failureOf(m.error, server, tool));
|
|
138
|
+
else resolve(m.result);
|
|
139
|
+
});
|
|
140
|
+
if (!host.send(id, method, args)) {
|
|
141
|
+
clearTimeout(timer);
|
|
142
|
+
host.route(id, null);
|
|
143
|
+
reject(failure("upstream_error", `mcp.${method} could not be sent.`, { retryable: true }));
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
function listTools(server) {
|
|
148
|
+
if (server !== void 0 && (typeof server !== "string" || !NAME.test(server))) return Promise.reject(failure("bad_request", "listTools(server?): a connector name."));
|
|
149
|
+
if (typeof server === "string" && !isDeclared(server)) return Promise.reject(failure("not_in_manifest", `The page does not declare the connector ${server}.`, { server }));
|
|
150
|
+
return ask("listTools", [server ?? null], server);
|
|
151
|
+
}
|
|
152
|
+
function describeTool(server, tool) {
|
|
153
|
+
const p = parts(server, tool, void 0);
|
|
154
|
+
if ("code" in p) return Promise.reject(p);
|
|
155
|
+
return ask("describeTool", [p.server, p.tool], p.server, p.tool);
|
|
156
|
+
}
|
|
157
|
+
const watches = /* @__PURE__ */ new Map();
|
|
158
|
+
const deliver = (w, event) => {
|
|
159
|
+
try {
|
|
160
|
+
w.handler(event);
|
|
161
|
+
} catch (e) {
|
|
162
|
+
console.error("openArtifacts.mcp.watchTool handler threw:", e);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
function startWatch(id, w) {
|
|
166
|
+
host.route(id, (m) => {
|
|
167
|
+
if (m.__frame_cap_r !== true) return;
|
|
168
|
+
host.route(id, null);
|
|
169
|
+
if (m.error != null && watches.delete(id)) deliver(w, { type: "error", error: failureOf(m.error, w.server, w.tool) });
|
|
170
|
+
});
|
|
171
|
+
if (!host.send(id, "watch", [id, w.server, w.tool, w.input, w.options])) {
|
|
172
|
+
host.route(id, null);
|
|
173
|
+
watches.delete(id);
|
|
174
|
+
deliver(w, { type: "error", error: failure("upstream_error", "The watch could not be started.", { server: w.server, tool: w.tool, retryable: true }) });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function watchTool(server, tool, input, handler, options) {
|
|
178
|
+
if (typeof handler !== "function") throw failure("bad_request", "watchTool(server, tool, input, handler, options?): handler is a function.");
|
|
179
|
+
const report = (error) => {
|
|
180
|
+
queueMicrotask(() => deliver({ handler }, { type: "error", error }));
|
|
181
|
+
return () => void 0;
|
|
182
|
+
};
|
|
183
|
+
const p = parts(server, tool, input);
|
|
184
|
+
if ("code" in p) return report(p);
|
|
185
|
+
if (options !== void 0 && !isPlain(options)) return report(failure("bad_request", "watchTool options: {cache, refetchInterval}.", { server: p.server, tool: p.tool }));
|
|
186
|
+
const o = options ?? {};
|
|
187
|
+
const cache = cacheOf(o.cache);
|
|
188
|
+
if (isRecord(cache) && "code" in cache) return report(Object.assign(cache, { server: p.server, tool: p.tool }));
|
|
189
|
+
const every = o.refetchInterval;
|
|
190
|
+
if (every !== void 0 && (typeof every !== "number" || !Number.isFinite(every) || every <= 0)) return report(failure("bad_request", "refetchInterval: ms between polls.", { server: p.server, tool: p.tool }));
|
|
191
|
+
if (!host.framed) return report(failure("capability_disabled", "This page is not inside its frame shell.", { server: p.server, tool: p.tool }));
|
|
192
|
+
const id = host.id();
|
|
193
|
+
const w = { ...p, handler, options: { ...cache !== void 0 ? { cache } : {}, ...every !== void 0 ? { refetchInterval: every } : {} } };
|
|
194
|
+
watches.set(id, w);
|
|
195
|
+
startWatch(id, w);
|
|
196
|
+
return () => {
|
|
197
|
+
if (!watches.delete(id)) return;
|
|
198
|
+
host.route(id, null);
|
|
199
|
+
host.send(host.id(), "unwatch", [id]);
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function invalidate(server, tool, input) {
|
|
203
|
+
if (server !== void 0 && typeof server !== "string") throw failure("bad_request", "invalidate(server?, tool?, input?): server is a name.");
|
|
204
|
+
if (tool !== void 0 && typeof tool !== "string") throw failure("bad_request", "invalidate(server?, tool?, input?): tool is a name.");
|
|
205
|
+
if (input !== void 0 && !isPlain(input)) throw failure("bad_request", "invalidate(server?, tool?, input?): input is a plain object.");
|
|
206
|
+
if (!host.framed) return;
|
|
207
|
+
host.send(host.id(), "invalidate", [server ?? null, tool ?? null, input ?? null]);
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
callTool,
|
|
211
|
+
listTools,
|
|
212
|
+
describeTool,
|
|
213
|
+
watchTool,
|
|
214
|
+
invalidate,
|
|
215
|
+
watchEvent: (m) => {
|
|
216
|
+
const id = typeof m.watch === "string" ? m.watch : "";
|
|
217
|
+
const w = watches.get(id);
|
|
218
|
+
const ev = isRecord(m.ev) ? m.ev : null;
|
|
219
|
+
if (!w || !ev) return;
|
|
220
|
+
if (ev.type === "error") {
|
|
221
|
+
if (ev.final === true) watches.delete(id);
|
|
222
|
+
return deliver(w, { type: "error", error: failureOf(ev.error, w.server, w.tool) });
|
|
223
|
+
}
|
|
224
|
+
if (ev.type !== "data") return;
|
|
225
|
+
try {
|
|
226
|
+
deliver(w, { type: "data", result: settle(ev.result, w.server, w.tool) });
|
|
227
|
+
} catch (error) {
|
|
228
|
+
deliver(w, { type: "error", error });
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
rewired: (message, gone) => {
|
|
232
|
+
for (const r of [...running.values()]) r.end(gone ? failure("capability_removed", "This version of the artifact no longer declares mcp.") : failure("upstream_error", message, { retryable: true }));
|
|
233
|
+
for (const [id, w] of [...watches]) {
|
|
234
|
+
if (gone) {
|
|
235
|
+
watches.delete(id);
|
|
236
|
+
deliver(w, { type: "error", error: failure("capability_removed", "This version of the artifact no longer declares mcp.", { server: w.server, tool: w.tool }) });
|
|
237
|
+
} else if (!isDeclared(w.server, w.tool)) {
|
|
238
|
+
watches.delete(id);
|
|
239
|
+
deliver(w, { type: "error", error: failure("not_in_manifest", `This version of the artifact no longer declares ${w.tool} of ${w.server}.`, { server: w.server, tool: w.tool }) });
|
|
240
|
+
} else {
|
|
241
|
+
startWatch(id, w);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
pagehide: () => {
|
|
246
|
+
for (const r of [...running.values()]) r.end(failure("cancelled", "The page was closed."));
|
|
247
|
+
for (const id of [...watches.keys()]) host.send(host.id(), "unwatch", [id]);
|
|
248
|
+
watches.clear();
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
});
|
|
252
|
+
})();
|
|
253
|
+
})();
|