@b3os/mcp 0.2.1 → 0.2.2

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.
Files changed (48) hide show
  1. package/README.md +26 -4
  2. package/dist/handlers/_common.d.ts +14 -0
  3. package/dist/handlers/_common.js +27 -0
  4. package/dist/handlers/_common.js.map +1 -1
  5. package/dist/handlers/actions.d.ts +12 -1
  6. package/dist/handlers/actions.js +51 -2
  7. package/dist/handlers/actions.js.map +1 -1
  8. package/dist/handlers/agents.d.ts +2 -0
  9. package/dist/handlers/agents.js +41 -0
  10. package/dist/handlers/agents.js.map +1 -0
  11. package/dist/handlers/platform.d.ts +32 -0
  12. package/dist/handlers/platform.js +663 -0
  13. package/dist/handlers/platform.js.map +1 -0
  14. package/dist/handlers/render.d.ts +13 -0
  15. package/dist/handlers/render.js +145 -18
  16. package/dist/handlers/render.js.map +1 -1
  17. package/dist/handlers/routines.d.ts +8 -2
  18. package/dist/handlers/routines.js +154 -15
  19. package/dist/handlers/routines.js.map +1 -1
  20. package/dist/server.js +1 -1
  21. package/dist/table.gen.js +3312 -42
  22. package/dist/table.gen.js.map +1 -1
  23. package/dist/table.js +4 -0
  24. package/dist/table.js.map +1 -1
  25. package/dist/tools.js +3 -0
  26. package/dist/tools.js.map +1 -1
  27. package/package.json +2 -2
  28. package/src/handlers/_common.ts +37 -0
  29. package/src/handlers/actions.ts +50 -2
  30. package/src/handlers/agents.ts +50 -0
  31. package/src/handlers/platform.ts +711 -0
  32. package/src/handlers/render.ts +147 -16
  33. package/src/handlers/routines.ts +164 -14
  34. package/src/server.ts +1 -1
  35. package/src/table.gen.ts +3367 -36
  36. package/src/table.ts +4 -0
  37. package/src/tools.ts +3 -0
  38. package/table/core.mjs +13 -2
  39. package/table/node-toolsets.json +668 -0
  40. package/table/requests.json +771 -0
  41. package/table/table.json +3315 -45
  42. package/table/tools/agents.mjs +230 -0
  43. package/table/tools/listeners.mjs +124 -0
  44. package/table/tools/platform.mjs +136 -3
  45. package/table/tools/render.mjs +157 -3
  46. package/table/tools/routines.mjs +37 -0
  47. package/table/tools/sandboxes.mjs +69 -0
  48. package/table/tools.mjs +3 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@b3os/mcp",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "B3OS MCP server over stdio, and the shared MCP tool table both B3OS MCP servers serve: vision, render, infer, sandboxes, routines and actions as tools.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -49,7 +49,7 @@
49
49
  "gen:check": "node scripts/gen-table.mjs --check"
50
50
  },
51
51
  "dependencies": {
52
- "@b3os/sdk": "0.2.1"
52
+ "@b3os/sdk": "0.2.2"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@types/node": "24.19.2",
@@ -182,6 +182,43 @@ export function sentence(t: string, max: number): string {
182
182
  return stop > max / 3 ? cut.slice(0, stop + 1) : `${cut.slice(0, max - 3).trimEnd()}...`;
183
183
  }
184
184
 
185
+ // The read tools' line format (traces, the request log, kept model calls, routine runs; B3-6853): a duration, a
186
+ // time, a count and a short string the same way in every answer.
187
+
188
+ /** `48.9 ms`, `1.20 s`; "" when the value is not a number. */
189
+ export const ms = (v: unknown): string =>
190
+ typeof v === "number" && Number.isFinite(v)
191
+ ? v >= 1000
192
+ ? `${(v / 1000).toFixed(2)} s`
193
+ : `${+v.toPrecision(3)} ms`
194
+ : "";
195
+
196
+ /** An RFC 3339 time without its fraction (`2026-11-16T23:01:02Z`); "" when it is not a string. */
197
+ export const when = (v: unknown): string => (typeof v === "string" ? v.replace(/\.\d+(?=Z$)/, "") : "");
198
+
199
+ /** An integer with thousands separators (`9,120`), the same in every runtime. */
200
+ export const int = (n: number): string => String(Math.round(n)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
201
+
202
+ /** At most `max` characters on one line, `...` marking a cut; "" for a non-string. */
203
+ export function short(v: unknown, max: number): string {
204
+ if (typeof v !== "string") return "";
205
+ const t = v.replace(/\s+/g, " ").trim();
206
+ return t.length <= max ? t : `${t.slice(0, max - 3).trimEnd()}...`;
207
+ }
208
+
209
+ /** The `data` of a list envelope (`{object: "list", data, next_cursor}`), objects only. */
210
+ export const listOf = (page: unknown): Obj[] =>
211
+ isObj(page) && Array.isArray(page.data) ? page.data.filter(isObj) : [];
212
+
213
+ /** The list envelope's `next_cursor`, when there is a next page. */
214
+ export const nextCursor = (page: unknown): string | undefined =>
215
+ isObj(page) && typeof page.next_cursor === "string" && page.next_cursor ? page.next_cursor : undefined;
216
+
217
+ /** A whole-number argument clamped to [min, max], or `dflt` when it is absent. */
218
+ export function intArg(args: Obj, k: string, dflt: number, min: number, max: number): number {
219
+ return Math.max(min, Math.min(max, Math.trunc(numArg(args, k, dflt))));
220
+ }
221
+
185
222
  /** A JSON Schema without UI-only `x-` keys, for a model to fill. */
186
223
  export function forModel(v: unknown): unknown {
187
224
  if (Array.isArray(v)) return v.map(forModel);
@@ -5,6 +5,7 @@
5
5
  import type { JsonSchemaObject } from "../table-types.ts";
6
6
  import {
7
7
  buildRequest,
8
+ type ContentBlock,
8
9
  clip,
9
10
  errorResult,
10
11
  forModel,
@@ -49,7 +50,7 @@ export const RUNNERS: Record<string, Runner> = {
49
50
  async actions_call(tool, args, io, ctx) {
50
51
  try {
51
52
  const r = (await io.request(buildRequest(tool, args), ctx.signal)) as Obj;
52
- return text(clip(json(r.output)), r);
53
+ return withFileLinks(text(clip(json(r.output)), r), r.output);
53
54
  } catch (e) {
54
55
  const next = connectNext(e);
55
56
  if (!next) throw e;
@@ -58,6 +59,53 @@ export const RUNNERS: Record<string, Runner> = {
58
59
  },
59
60
  };
60
61
 
62
+ /** The host B3OS serves tool file links from (the node's `[actions.files] link_base`). */
63
+ export const FILE_LINK_HOST = "usercontent.b3os.org";
64
+ const FILE_PATH = /^\/tf\/(tfile-[0-9a-z]{1,64})\/([A-Za-z0-9][A-Za-z0-9._-]{0,127})$/;
65
+
66
+ /**
67
+ * A hosted tool's output files (ToolFileOutput: `{"$link", "file", "name", "contentType", "bytes", "sha256",
68
+ * "expiresAt"}`, a top-level output field each) as MCP resource links after the text, one per file, so a client can
69
+ * offer the download. A tool writes its own output JSON, so only a link B3OS signed is passed on: https on
70
+ * usercontent.b3os.org, the path `/tf/<host id>/<name>`, whose host id is `file` with `-` for `_` and whose name is
71
+ * `name`. Any other `$link` stays in the text only. A client older than 2025-06-18 gets the text only (server.ts
72
+ * drops resource_link for it); the text still carries each `$link`.
73
+ */
74
+ export function fileLinks(output: unknown): ContentBlock[] {
75
+ if (!isObj(output)) return [];
76
+ const out: ContentBlock[] = [];
77
+ for (const [field, v] of Object.entries(output)) {
78
+ if (!isObj(v) || typeof v.$link !== "string" || typeof v.name !== "string" || typeof v.file !== "string") continue;
79
+ let u: URL;
80
+ try {
81
+ u = new URL(v.$link);
82
+ } catch {
83
+ continue;
84
+ }
85
+ if (u.protocol !== "https:" || u.username || u.password || u.port || u.hostname !== FILE_LINK_HOST) continue;
86
+ const m = FILE_PATH.exec(u.pathname);
87
+ if (!m || m[1] !== v.file.replace("_", "-") || m[2] !== v.name) continue;
88
+ const facts = [
89
+ typeof v.bytes === "number" ? `${v.bytes} bytes` : "",
90
+ typeof v.sha256 === "string" ? `sha256 ${v.sha256}` : "",
91
+ typeof v.expiresAt === "string" ? `link valid until ${v.expiresAt}` : "",
92
+ ].filter(Boolean);
93
+ out.push({
94
+ type: "resource_link",
95
+ uri: v.$link,
96
+ name: v.name,
97
+ ...(typeof v.contentType === "string" ? { mimeType: v.contentType } : {}),
98
+ description: `Output ${field}${facts.length ? `: ${facts.join(", ")}` : ""}`,
99
+ });
100
+ }
101
+ return out;
102
+ }
103
+
104
+ function withFileLinks(r: ToolResult, output: unknown): ToolResult {
105
+ const links = fileLinks(output);
106
+ return links.length ? { ...r, content: [...r.content, ...links] } : r;
107
+ }
108
+
61
109
  /** The dashboard link a `connection_required` carries (`connect_url`), from the problem or, when only the detail
62
110
  * reached the server (a ToolCallError keeps no extension members), from the detail's "add one at <url>". Only an
63
111
  * https URL with no credentials in it is passed on: an agent shows it to a person as a link to open. */
@@ -137,7 +185,7 @@ export async function runPackTool(t: PackTool, args: Obj, io: ToolIO, ctx: ToolC
137
185
  requestFor("actionsCall", [], { body: { ref: t.ref, input: args } }),
138
186
  ctx.signal,
139
187
  )) as Obj;
140
- return text(clip(json(r.output)), r);
188
+ return withFileLinks(text(clip(json(r.output)), r), r.output);
141
189
  } catch (e) {
142
190
  return errorResult(e);
143
191
  }
@@ -0,0 +1,50 @@
1
+ // Agents (Actors) tool handlers of the MCP tool runtime (owner: agents, B3-7151): what each agents tool in
2
+ // table/tools/agents.mjs sends and how it answers, beyond one plain request. src/table.ts merges every product's
3
+ // RUNNERS.
4
+
5
+ import {
6
+ buildRequest,
7
+ clip,
8
+ invalid,
9
+ isObj,
10
+ type JobLike,
11
+ json,
12
+ numArg,
13
+ type Obj,
14
+ type Runner,
15
+ startOrContinue,
16
+ text,
17
+ waitFor,
18
+ } from "./_common.ts";
19
+
20
+ export const RUNNERS: Record<string, Runner> = {
21
+ /**
22
+ * agentsSendMessage: the message body is the caller's `payload` with the reserved members (`type`, `conversation`,
23
+ * `subject`, `metadata`, `create`) beside it, which win. With `job_id`, the turn of that session is waited on.
24
+ */
25
+ async agent_send(tool, args, io, ctx) {
26
+ let ses: JobLike;
27
+ if (typeof args.job_id === "string" && args.job_id) {
28
+ ses = await startOrContinue(tool, args, io, ctx);
29
+ } else {
30
+ if (!args.agent || !args.key)
31
+ throw invalid("give agent and key (send a message) or job_id (continue a turn)", "/agent");
32
+ const waitS = Math.max(0, Math.min(60, numArg(args, "wait_s", 50)));
33
+ const req = buildRequest(tool, args, io.waitJob ? undefined : waitS);
34
+ const payload = isObj(args.payload) ? args.payload : {};
35
+ req.body = { ...payload, ...((req.body as Obj | undefined) ?? {}) };
36
+ ses = (await io.request(req, ctx.signal)) as JobLike;
37
+ if (io.waitJob) ses = await waitFor(tool, ses, waitS, io, ctx);
38
+ }
39
+ const s = ses as JobLike & Obj;
40
+ const head = [`session ${s.id}: ${s.status}`];
41
+ if (typeof s.woke_from === "string") head.push(`woke_from ${s.woke_from}${s.recovered ? " (recovered)" : ""}`);
42
+ const lines = [head.join(", ")];
43
+ if (s.result !== undefined) lines.push(json(s.result));
44
+ const err = isObj(s.error) ? s.error : undefined;
45
+ if (err) lines.push(`${err.code}: ${err.detail}`);
46
+ if (s.status === "queued" || s.status === "running")
47
+ lines.push(`The turn is still ${s.status}: call agent_send with job_id ${s.id} to keep waiting.`);
48
+ return { ...text(clip(lines.join("\n")), ses), isError: s.status === "failed" };
49
+ },
50
+ };