@agentbus/agentbus-client 0.20.2 → 0.22.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,3 @@
1
+ import type { McpToolClient } from "./types";
2
+ /** MCP-shaped adapter for the authenticated AgentBus Unix broker. */
3
+ export declare function createBrokerMcpClient(socketPath: string, timeoutMs?: number): McpToolClient;
package/dist/broker.js ADDED
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createBrokerMcpClient = createBrokerMcpClient;
7
+ const node_net_1 = __importDefault(require("node:net"));
8
+ const node_crypto_1 = require("node:crypto");
9
+ const VERSION = "1";
10
+ const MAX_FRAME = 4 * 1024 * 1024;
11
+ /** MCP-shaped adapter for the authenticated AgentBus Unix broker. */
12
+ function createBrokerMcpClient(socketPath, timeoutMs = 30000) {
13
+ async function callTool(name, args) {
14
+ const operation = name === "agentbus_publish" ? "publish" :
15
+ name === "agentbus_poll" ? "poll" : name === "agentbus_status" ? "status" :
16
+ name === "agentbus_get_event" ? "get_event" : name === "agentbus_verify_event" ? "verify_event" : null;
17
+ if (!operation)
18
+ throw new Error(`broker_unsupported_tool:${name}`);
19
+ const body = operation === "status" ? {} : args;
20
+ const result = await request(socketPath, operation, body, timeoutMs);
21
+ return { content: [{ type: "text", text: JSON.stringify(result) }] };
22
+ }
23
+ return { callTool };
24
+ }
25
+ function request(path, operation, body, timeoutMs) {
26
+ return new Promise((resolve, reject) => {
27
+ const id = (0, node_crypto_1.randomUUID)();
28
+ const socket = node_net_1.default.createConnection({ path });
29
+ let buffer = Buffer.alloc(0);
30
+ const timer = setTimeout(() => { socket.destroy(); reject(new Error("broker_timeout")); }, timeoutMs);
31
+ const fail = (err) => { clearTimeout(timer); reject(err); };
32
+ socket.once("error", (err) => fail(new Error(`broker_unavailable:${String(err)}`)));
33
+ socket.on("data", (chunk) => {
34
+ buffer = Buffer.concat([buffer, chunk]);
35
+ if (buffer.length < 4)
36
+ return;
37
+ const size = buffer.readUInt32BE(0);
38
+ if (size < 2 || size > MAX_FRAME) {
39
+ socket.destroy();
40
+ return fail(new Error("invalid_broker_frame_size"));
41
+ }
42
+ if (buffer.length < size + 4)
43
+ return;
44
+ const response = JSON.parse(buffer.subarray(4, size + 4).toString("utf8"));
45
+ clearTimeout(timer);
46
+ socket.end();
47
+ if (response.protocol_version !== VERSION || response.request_id !== id)
48
+ return reject(new Error("invalid_broker_response"));
49
+ if (response.ok !== true)
50
+ return reject(new Error(String(response.error?.code ?? "broker_request_failed")));
51
+ resolve(response.result);
52
+ });
53
+ socket.once("connect", () => {
54
+ const raw = Buffer.from(JSON.stringify({ protocol_version: VERSION, request_id: id, operation, body }));
55
+ if (raw.length > MAX_FRAME)
56
+ return fail(new Error("invalid_broker_frame_size"));
57
+ const header = Buffer.alloc(4);
58
+ header.writeUInt32BE(raw.length, 0);
59
+ socket.write(Buffer.concat([header, raw]));
60
+ });
61
+ });
62
+ }
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import type { AgentBusOptions, BusEvent } from "./types";
3
3
  export { getDatabasePath } from "./locator";
4
4
  export { DatabaseWatcher } from "./watcher";
5
5
  export { createStdioMcpClient } from "./stdio";
6
+ export { createBrokerMcpClient } from "./broker";
6
7
  export { agentIdCanonicalSha256, canonicalizeAgentId, validateAgentIdValue, validateAgentIdAction, verifyAgentIdSignature, } from "./identity";
7
8
  export { platformKey, optionalPackageName, resolveGoWorkerPath, } from "./goBin";
8
9
  export type { AgentBusOptions, BusEvent, McpToolClient, } from "./types";
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AgentBus = exports.resolveGoWorkerPath = exports.optionalPackageName = exports.platformKey = exports.verifyAgentIdSignature = exports.validateAgentIdAction = exports.validateAgentIdValue = exports.canonicalizeAgentId = exports.agentIdCanonicalSha256 = exports.createStdioMcpClient = exports.DatabaseWatcher = exports.getDatabasePath = void 0;
3
+ exports.AgentBus = exports.resolveGoWorkerPath = exports.optionalPackageName = exports.platformKey = exports.verifyAgentIdSignature = exports.validateAgentIdAction = exports.validateAgentIdValue = exports.canonicalizeAgentId = exports.agentIdCanonicalSha256 = exports.createBrokerMcpClient = exports.createStdioMcpClient = exports.DatabaseWatcher = exports.getDatabasePath = void 0;
4
4
  exports.unwrapToolResult = unwrapToolResult;
5
5
  exports.parsePollPage = parsePollPage;
6
6
  exports.normalizePollResult = normalizePollResult;
@@ -8,12 +8,15 @@ const events_1 = require("events");
8
8
  const watcher_1 = require("./watcher");
9
9
  const locator_1 = require("./locator");
10
10
  const stdio_1 = require("./stdio");
11
+ const broker_1 = require("./broker");
11
12
  var locator_2 = require("./locator");
12
13
  Object.defineProperty(exports, "getDatabasePath", { enumerable: true, get: function () { return locator_2.getDatabasePath; } });
13
14
  var watcher_2 = require("./watcher");
14
15
  Object.defineProperty(exports, "DatabaseWatcher", { enumerable: true, get: function () { return watcher_2.DatabaseWatcher; } });
15
16
  var stdio_2 = require("./stdio");
16
17
  Object.defineProperty(exports, "createStdioMcpClient", { enumerable: true, get: function () { return stdio_2.createStdioMcpClient; } });
18
+ var broker_2 = require("./broker");
19
+ Object.defineProperty(exports, "createBrokerMcpClient", { enumerable: true, get: function () { return broker_2.createBrokerMcpClient; } });
17
20
  var identity_1 = require("./identity");
18
21
  Object.defineProperty(exports, "agentIdCanonicalSha256", { enumerable: true, get: function () { return identity_1.agentIdCanonicalSha256; } });
19
22
  Object.defineProperty(exports, "canonicalizeAgentId", { enumerable: true, get: function () { return identity_1.canonicalizeAgentId; } });
@@ -57,29 +60,35 @@ class AgentBus extends events_1.EventEmitter {
57
60
  const spawnedMcpHere = !this.mcp;
58
61
  try {
59
62
  if (!this.mcp) {
60
- const producerId = this.options.producerId ?? process.env.AGENTBUS_PRODUCER_ID;
61
- const env = {
62
- ...(this.options.stdio?.env ?? {}),
63
- };
64
- if (producerId) {
65
- env.AGENTBUS_PRODUCER_ID = producerId;
63
+ const brokerSocket = this.options.brokerSocket ?? process.env.AGENTBUS_BROKER_SOCKET;
64
+ if (brokerSocket) {
65
+ this.mcp = (0, broker_1.createBrokerMcpClient)(brokerSocket);
66
66
  }
67
- this.mcp = await (0, stdio_1.createStdioMcpClient)({
68
- workspace: this.options.workspace,
69
- command: this.options.stdio?.command,
70
- args: this.options.stdio?.args,
71
- cwd: this.options.stdio?.cwd,
72
- env,
73
- });
74
- this.ownsMcp = true;
67
+ else {
68
+ const producerId = this.options.producerId ?? process.env.AGENTBUS_PRODUCER_ID;
69
+ const env = {
70
+ ...(this.options.stdio?.env ?? {}),
71
+ };
72
+ if (producerId) {
73
+ env.AGENTBUS_PRODUCER_ID = producerId;
74
+ }
75
+ this.mcp = await (0, stdio_1.createStdioMcpClient)({
76
+ workspace: this.options.workspace,
77
+ command: this.options.stdio?.command,
78
+ args: this.options.stdio?.args,
79
+ cwd: this.options.stdio?.cwd,
80
+ env,
81
+ });
82
+ this.ownsMcp = true;
83
+ }
84
+ }
85
+ if (!this.options.brokerSocket && !process.env.AGENTBUS_BROKER_SOCKET) {
86
+ const dbPath = (0, locator_1.getDatabasePath)(this.options.workspace);
87
+ this.watcher = new watcher_1.DatabaseWatcher(dbPath, () => {
88
+ void this.poll().catch((err) => this.reportBackgroundError(err));
89
+ }, this.options.fallbackMs ?? 5000);
90
+ this.watcher.start();
75
91
  }
76
- const dbPath = (0, locator_1.getDatabasePath)(this.options.workspace);
77
- this.watcher = new watcher_1.DatabaseWatcher(dbPath, () => {
78
- void this.poll().catch((err) => {
79
- this.reportBackgroundError(err);
80
- });
81
- }, this.options.fallbackMs ?? 5000);
82
- this.watcher.start();
83
92
  // Initial catch-up poll — must succeed before we mark connected.
84
93
  await this.poll();
85
94
  this.connected = true;
package/dist/types.d.ts CHANGED
@@ -12,6 +12,8 @@ export type BusEvent = {
12
12
  payload: unknown;
13
13
  };
14
14
  export type AgentBusOptions = {
15
+ /** Authenticated broker Unix socket; disables SQLite watcher and stdio MCP. */
16
+ brokerSocket?: string;
15
17
  /** Override workspace (defaults to AGENTBUS_WORKSPACE). */
16
18
  workspace?: string;
17
19
  /** Fallback poll interval when fs.watch is quiet (ms). */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbus/agentbus-client",
3
- "version": "0.20.2",
3
+ "version": "0.22.0",
4
4
  "description": "TypeScript/Node EventEmitter client for AgentBus (MCP + fs.watch poll)",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -9,11 +9,11 @@
9
9
  "README.md"
10
10
  ],
11
11
  "optionalDependencies": {
12
- "@agentbus/go-worker-linux-x64": "0.20.2",
13
- "@agentbus/go-worker-linux-arm64": "0.20.2",
14
- "@agentbus/go-worker-darwin-x64": "0.20.2",
15
- "@agentbus/go-worker-darwin-arm64": "0.20.2",
16
- "@agentbus/go-worker-win32-x64": "0.20.2"
12
+ "@agentbus/go-worker-linux-x64": "0.22.0",
13
+ "@agentbus/go-worker-linux-arm64": "0.22.0",
14
+ "@agentbus/go-worker-darwin-x64": "0.22.0",
15
+ "@agentbus/go-worker-darwin-arm64": "0.22.0",
16
+ "@agentbus/go-worker-win32-x64": "0.22.0"
17
17
  },
18
18
  "scripts": {
19
19
  "build": "tsc",