@nekzus/liop 2.1.0-alpha.4 → 2.1.0-alpha.5

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 (78) hide show
  1. package/dist/bin/agent.js +301 -4
  2. package/dist/bin/agent.js.map +1 -1
  3. package/dist/bridge.js +3 -1
  4. package/dist/chunk-32ADSAJS.js +104 -0
  5. package/dist/chunk-32ADSAJS.js.map +1 -0
  6. package/dist/chunk-4KIGYPIQ.js +3298 -0
  7. package/dist/chunk-4KIGYPIQ.js.map +1 -0
  8. package/dist/chunk-72MNYFR6.js +64 -0
  9. package/dist/chunk-72MNYFR6.js.map +1 -0
  10. package/dist/chunk-AL7H7DTW.js +463 -0
  11. package/dist/chunk-AL7H7DTW.js.map +1 -0
  12. package/dist/chunk-CT6NHSYP.js +30 -0
  13. package/dist/chunk-CT6NHSYP.js.map +1 -0
  14. package/dist/chunk-IJHTRIZC.js +56 -0
  15. package/dist/chunk-IJHTRIZC.js.map +1 -0
  16. package/dist/chunk-J3WPBMJ5.js +332 -0
  17. package/dist/chunk-J3WPBMJ5.js.map +1 -0
  18. package/dist/chunk-MMYZR7G7.js +815 -0
  19. package/dist/chunk-MMYZR7G7.js.map +1 -0
  20. package/dist/chunk-ODJT7ZUF.js +300 -0
  21. package/dist/chunk-ODJT7ZUF.js.map +1 -0
  22. package/dist/chunk-OUUTDSOW.js +24 -0
  23. package/dist/chunk-OUUTDSOW.js.map +1 -0
  24. package/dist/chunk-QLCOEP5J.js +68 -0
  25. package/dist/chunk-QLCOEP5J.js.map +1 -0
  26. package/dist/chunk-RDWCGZ2A.js +87 -0
  27. package/dist/chunk-RDWCGZ2A.js.map +1 -0
  28. package/dist/chunk-RWRRBYG4.js +1 -0
  29. package/dist/chunk-SSURAA3I.js +469 -0
  30. package/dist/chunk-SSURAA3I.js.map +1 -0
  31. package/dist/chunk-Y73XGYY7.js +1597 -0
  32. package/dist/chunk-Y73XGYY7.js.map +1 -0
  33. package/dist/client.js +8 -1
  34. package/dist/gateway.js +9 -1
  35. package/dist/index.js +58 -4
  36. package/dist/index.js.map +1 -1
  37. package/dist/kyber-3ULIJSE3.js +3 -0
  38. package/dist/{kyber-2WDOTUQX.js.map → kyber-3ULIJSE3.js.map} +1 -1
  39. package/dist/mesh.js +4 -1
  40. package/dist/server.js +6 -1
  41. package/dist/types.js +2 -1
  42. package/dist/verifier-3FAKCFNN.js +5 -0
  43. package/dist/{verifier-KZ4QYF5M.js.map → verifier-3FAKCFNN.js.map} +1 -1
  44. package/dist/workers/logic-execution.js +255 -1
  45. package/dist/workers/logic-execution.js.map +1 -1
  46. package/dist/workers/zk-verifier.js +173 -1
  47. package/dist/workers/zk-verifier.js.map +1 -1
  48. package/package.json +1 -1
  49. package/dist/chunk-AEWYQWVZ.js +0 -42
  50. package/dist/chunk-AEWYQWVZ.js.map +0 -1
  51. package/dist/chunk-ANFXJGMP.js +0 -2
  52. package/dist/chunk-ANFXJGMP.js.map +0 -1
  53. package/dist/chunk-CPLE5VZ5.js +0 -33
  54. package/dist/chunk-CPLE5VZ5.js.map +0 -1
  55. package/dist/chunk-DBXGYHKY.js +0 -2
  56. package/dist/chunk-DBXGYHKY.js.map +0 -1
  57. package/dist/chunk-DQ6UW6L7.js +0 -2
  58. package/dist/chunk-DQ6UW6L7.js.map +0 -1
  59. package/dist/chunk-JIUFKRVG.js +0 -13
  60. package/dist/chunk-JIUFKRVG.js.map +0 -1
  61. package/dist/chunk-PWCXZWSE.js +0 -2
  62. package/dist/chunk-PWCXZWSE.js.map +0 -1
  63. package/dist/chunk-RYYRR4N5.js +0 -31
  64. package/dist/chunk-RYYRR4N5.js.map +0 -1
  65. package/dist/chunk-S6RJHZV2.js +0 -2
  66. package/dist/chunk-S6RJHZV2.js.map +0 -1
  67. package/dist/chunk-SB5XJXKV.js +0 -2
  68. package/dist/chunk-SB5XJXKV.js.map +0 -1
  69. package/dist/chunk-T3L6OCM3.js +0 -3
  70. package/dist/chunk-T3L6OCM3.js.map +0 -1
  71. package/dist/chunk-TNBXOZNG.js +0 -2
  72. package/dist/chunk-TNBXOZNG.js.map +0 -1
  73. package/dist/chunk-V5MKJT6S.js +0 -2
  74. package/dist/chunk-V5MKJT6S.js.map +0 -1
  75. package/dist/chunk-VDNV2I4I.js +0 -3
  76. package/dist/chunk-VDNV2I4I.js.map +0 -1
  77. package/dist/kyber-2WDOTUQX.js +0 -2
  78. package/dist/verifier-KZ4QYF5M.js +0 -2
@@ -0,0 +1,1597 @@
1
+ import { LiopVerifier } from './chunk-32ADSAJS.js';
2
+ import { Kyber768Wrapper } from './chunk-QLCOEP5J.js';
3
+ import { authorizeRequest } from './chunk-IJHTRIZC.js';
4
+ import { liopV1, createChannelCredentials } from './chunk-RDWCGZ2A.js';
5
+ import { log } from './chunk-72MNYFR6.js';
6
+ import { metrics } from '@opentelemetry/api';
7
+ import * as crypto2 from 'crypto';
8
+ import * as grpc from '@grpc/grpc-js';
9
+
10
+ // src/economy/estimator.ts
11
+ var RealTokenEstimator = class {
12
+ name = "o200k_base";
13
+ countFn;
14
+ constructor(countFn, setMergeCacheSizeFn) {
15
+ this.countFn = countFn;
16
+ if (setMergeCacheSizeFn) {
17
+ setMergeCacheSizeFn(1e4);
18
+ }
19
+ }
20
+ countTokens(text) {
21
+ if (text.length === 0) return 0;
22
+ return this.countFn(text);
23
+ }
24
+ };
25
+ var HeuristicTokenEstimator = class {
26
+ name = "heuristic (chars/4)";
27
+ countTokens(text) {
28
+ if (text.length === 0) return 0;
29
+ return Math.ceil(text.length / 4);
30
+ }
31
+ };
32
+ async function createTokenEstimator() {
33
+ try {
34
+ const mod = await import('gpt-tokenizer');
35
+ const estimator = new RealTokenEstimator(
36
+ mod.countTokens,
37
+ mod.setMergeCacheSize
38
+ );
39
+ log.debug("[LIOP-Economy] Token estimator initialized: o200k_base");
40
+ return estimator;
41
+ } catch {
42
+ log.info(
43
+ "[LIOP-Economy] gpt-tokenizer unavailable, falling back to heuristic estimator"
44
+ );
45
+ return new HeuristicTokenEstimator();
46
+ }
47
+ }
48
+ function createSyncTokenEstimator() {
49
+ return new HeuristicTokenEstimator();
50
+ }
51
+ var METER_NAME = "@nekzus/liop";
52
+ var METER_VERSION = "1.2.0-alpha.9";
53
+ var TOKEN_USAGE_BUCKETS = [
54
+ 1,
55
+ 4,
56
+ 16,
57
+ 64,
58
+ 256,
59
+ 1024,
60
+ 4096,
61
+ 16384,
62
+ 65536,
63
+ 262144,
64
+ 1048576,
65
+ 4194304,
66
+ 16777216,
67
+ 67108864
68
+ ];
69
+ var DURATION_BUCKETS = [
70
+ 0.01,
71
+ 0.02,
72
+ 0.04,
73
+ 0.08,
74
+ 0.16,
75
+ 0.32,
76
+ 0.64,
77
+ 1.28,
78
+ 2.56,
79
+ 5.12,
80
+ 10.24,
81
+ 20.48,
82
+ 40.96,
83
+ 81.92
84
+ ];
85
+ var LiopOTelBridge = class {
86
+ tokenUsage;
87
+ operationDuration;
88
+ active = false;
89
+ constructor() {
90
+ try {
91
+ const meter = metrics.getMeter(METER_NAME, METER_VERSION);
92
+ this.tokenUsage = meter.createHistogram("gen_ai.client.token.usage", {
93
+ description: "Number of tokens used in LIOP Logic-on-Origin operations",
94
+ unit: "{token}",
95
+ advice: { explicitBucketBoundaries: TOKEN_USAGE_BUCKETS }
96
+ });
97
+ this.operationDuration = meter.createHistogram(
98
+ "gen_ai.client.operation.duration",
99
+ {
100
+ description: "Duration of LIOP operations",
101
+ unit: "s",
102
+ advice: { explicitBucketBoundaries: DURATION_BUCKETS }
103
+ }
104
+ );
105
+ this.active = true;
106
+ log.debug("[LIOP-OTel] gen_ai.* metrics bridge initialized");
107
+ } catch (err) {
108
+ log.debug(
109
+ `[LIOP-OTel] Bridge disabled: ${err instanceof Error ? err.message : String(err)}`
110
+ );
111
+ const noopHistogram = {
112
+ record: () => {
113
+ }
114
+ };
115
+ this.tokenUsage = noopHistogram;
116
+ this.operationDuration = noopHistogram;
117
+ }
118
+ }
119
+ /**
120
+ * Record token usage with gen_ai.* standard attributes.
121
+ *
122
+ * @param tokens - Number of tokens consumed
123
+ * @param tokenType - "input" or "output" (gen_ai.token.type)
124
+ * @param operationName - gen_ai.operation.name (e.g., "execute_tool", "chat")
125
+ * @param toolName - Optional LIOP-specific tool name for attribution
126
+ */
127
+ recordTokens(tokens, tokenType, operationName, toolName) {
128
+ this.tokenUsage.record(tokens, {
129
+ "gen_ai.system": "liop",
130
+ "gen_ai.operation.name": operationName,
131
+ "gen_ai.token.type": tokenType,
132
+ "gen_ai.request.model": "liop-mesh",
133
+ ...toolName ? { "liop.tool.name": toolName } : {}
134
+ });
135
+ }
136
+ /**
137
+ * Record operation duration with gen_ai.* standard attributes.
138
+ *
139
+ * @param durationMs - Duration in milliseconds (converted to seconds for OTel)
140
+ * @param operationName - gen_ai.operation.name
141
+ * @param error - Optional error type string if the operation failed
142
+ */
143
+ recordDuration(durationMs, operationName, error) {
144
+ this.operationDuration.record(durationMs / 1e3, {
145
+ "gen_ai.system": "liop",
146
+ "gen_ai.operation.name": operationName,
147
+ ...error ? { "error.type": error } : {}
148
+ });
149
+ }
150
+ /** Whether the OTel bridge is actively connected to a MeterProvider */
151
+ isActive() {
152
+ return this.active;
153
+ }
154
+ };
155
+
156
+ // src/economy/telemetry.ts
157
+ var OTEL_OPERATION_MAP = {
158
+ tools_list: "chat",
159
+ tool_call: "execute_tool",
160
+ resource_read: "chat",
161
+ resource_list: "chat",
162
+ prompt_get: "chat",
163
+ prompt_list: "chat",
164
+ diagnostic: "chat"
165
+ };
166
+ var TokenTelemetryEngine = class _TokenTelemetryEngine {
167
+ static instance = null;
168
+ operations = [];
169
+ sessionId;
170
+ startedAt;
171
+ estimator;
172
+ otelBridge;
173
+ constructor() {
174
+ this.sessionId = crypto.randomUUID();
175
+ this.startedAt = Date.now();
176
+ this.estimator = createSyncTokenEstimator();
177
+ this.otelBridge = new LiopOTelBridge();
178
+ this.initRealEstimator();
179
+ }
180
+ /** Async upgrade from heuristic to real BPE tokenizer */
181
+ initRealEstimator() {
182
+ createTokenEstimator().then((real) => {
183
+ this.estimator = real;
184
+ }).catch(() => {
185
+ });
186
+ }
187
+ static getInstance() {
188
+ if (!_TokenTelemetryEngine.instance) {
189
+ _TokenTelemetryEngine.instance = new _TokenTelemetryEngine();
190
+ }
191
+ return _TokenTelemetryEngine.instance;
192
+ }
193
+ /**
194
+ * Count tokens in a string using the active estimator.
195
+ * Delegates to o200k_base BPE tokenizer (or heuristic fallback).
196
+ */
197
+ countTokens(content) {
198
+ try {
199
+ return this.estimator.countTokens(content);
200
+ } catch {
201
+ return Math.ceil(content.length / 4);
202
+ }
203
+ }
204
+ /**
205
+ * Record a single MCP operation's token footprint.
206
+ * Emits both internal metrics and OTel gen_ai.* histograms.
207
+ */
208
+ record(metric) {
209
+ const fullMetric = {
210
+ ...metric,
211
+ timestamp: Date.now()
212
+ };
213
+ this.operations.push(fullMetric);
214
+ try {
215
+ const otelOp = OTEL_OPERATION_MAP[metric.type] || "chat";
216
+ if (metric.estimatedInputTokens > 0) {
217
+ this.otelBridge.recordTokens(
218
+ metric.estimatedInputTokens,
219
+ "input",
220
+ otelOp,
221
+ metric.toolName
222
+ );
223
+ }
224
+ if (metric.estimatedOutputTokens > 0) {
225
+ this.otelBridge.recordTokens(
226
+ metric.estimatedOutputTokens,
227
+ "output",
228
+ otelOp,
229
+ metric.toolName
230
+ );
231
+ }
232
+ if (metric.durationMs !== void 0) {
233
+ this.otelBridge.recordDuration(metric.durationMs, otelOp);
234
+ }
235
+ } catch {
236
+ }
237
+ }
238
+ /**
239
+ * @deprecated Use countTokens() instead. Kept for backward compatibility.
240
+ */
241
+ estimateTokens(content) {
242
+ return this.countTokens(content);
243
+ }
244
+ /** Generate the full session report */
245
+ getReport() {
246
+ return {
247
+ sessionId: this.sessionId,
248
+ operations: [...this.operations],
249
+ totalInputTokens: this.operations.reduce(
250
+ (sum, op) => sum + op.estimatedInputTokens,
251
+ 0
252
+ ),
253
+ totalOutputTokens: this.operations.reduce(
254
+ (sum, op) => sum + op.estimatedOutputTokens,
255
+ 0
256
+ ),
257
+ estimatorName: this.estimator.name,
258
+ sessionUptimeMs: Date.now() - this.startedAt
259
+ };
260
+ }
261
+ /** Get per-tool token breakdown for diagnostic display */
262
+ getPerToolReport() {
263
+ const breakdown = /* @__PURE__ */ new Map();
264
+ for (const op of this.operations) {
265
+ const key = op.toolName || op.method;
266
+ const existing = breakdown.get(key) || {
267
+ input: 0,
268
+ output: 0,
269
+ calls: 0,
270
+ avgDurationMs: 0
271
+ };
272
+ const totalDuration = existing.avgDurationMs * existing.calls + (op.durationMs || 0);
273
+ const newCalls = existing.calls + 1;
274
+ breakdown.set(key, {
275
+ input: existing.input + op.estimatedInputTokens,
276
+ output: existing.output + op.estimatedOutputTokens,
277
+ calls: newCalls,
278
+ avgDurationMs: newCalls > 0 ? totalDuration / newCalls : 0
279
+ });
280
+ }
281
+ return breakdown;
282
+ }
283
+ /**
284
+ * Format a rich, human-readable summary block for LiopMeshStatus diagnostic.
285
+ * Returns empty string when no operations have been recorded.
286
+ */
287
+ formatStatusBlock() {
288
+ const report = this.getReport();
289
+ if (report.operations.length === 0) return "";
290
+ const uptimeStr = this.formatUptime(report.sessionUptimeMs);
291
+ const totalCombined = report.totalInputTokens + report.totalOutputTokens;
292
+ const byType = /* @__PURE__ */ new Map();
293
+ for (const op of report.operations) {
294
+ const key = op.type;
295
+ const existing = byType.get(key) || {
296
+ count: 0,
297
+ input: 0,
298
+ output: 0
299
+ };
300
+ byType.set(key, {
301
+ count: existing.count + 1,
302
+ input: existing.input + op.estimatedInputTokens,
303
+ output: existing.output + op.estimatedOutputTokens
304
+ });
305
+ }
306
+ const typeEntries = Array.from(byType.entries());
307
+ const typeLines = typeEntries.map(([type, data], idx) => {
308
+ const prefix = idx === typeEntries.length - 1 ? "\u2502 \u2514\u2500" : "\u2502 \u251C\u2500";
309
+ const outputPart = data.output > 0 ? ` / ${data.output.toLocaleString()} out` : "";
310
+ return `${prefix} ${type} \xD7${data.count} \u2192 ${data.input.toLocaleString()} in${outputPart}`;
311
+ });
312
+ const toolReport = this.getPerToolReport();
313
+ const toolEntries = Array.from(toolReport.entries()).filter(
314
+ ([key]) => key !== "tools/list" && key !== "LiopMeshStatus"
315
+ );
316
+ const toolLines = [];
317
+ if (toolEntries.length > 0) {
318
+ toolLines.push("\u251C\u2500 By Tool:");
319
+ toolEntries.forEach(([name, data], idx) => {
320
+ const prefix = idx === toolEntries.length - 1 ? "\u2502 \u2514\u2500" : "\u2502 \u251C\u2500";
321
+ const outputPart = data.output > 0 ? ` / ${data.output.toLocaleString()} out` : "";
322
+ const durationPart = data.avgDurationMs > 0 ? ` ~${Math.round(data.avgDurationMs)}ms` : "";
323
+ toolLines.push(
324
+ `${prefix} ${name}: ${data.input.toLocaleString()} in${outputPart} (\xD7${data.calls})${durationPart}`
325
+ );
326
+ });
327
+ }
328
+ const timedOps = report.operations.filter(
329
+ (op) => op.durationMs !== void 0
330
+ );
331
+ const avgLatency = timedOps.length > 0 ? Math.round(
332
+ timedOps.reduce((sum, op) => sum + (op.durationMs || 0), 0) / timedOps.length
333
+ ) : 0;
334
+ const otelStatus = this.otelBridge.isActive() ? "gen_ai.client.token.usage \u2192 active" : "disabled";
335
+ const lines = [
336
+ "\nToken Economy:",
337
+ `\u251C\u2500 Session: ${report.sessionId.slice(0, 8)} (${uptimeStr})`,
338
+ `\u251C\u2500 Estimator: ${report.estimatorName}`,
339
+ `\u251C\u2500 Operations: ${report.operations.length}`,
340
+ ...typeLines,
341
+ `\u251C\u2500 Total: ${report.totalInputTokens.toLocaleString()} in / ${report.totalOutputTokens.toLocaleString()} out (${totalCombined.toLocaleString()} combined)`,
342
+ ...toolLines,
343
+ ...avgLatency > 0 ? [`\u251C\u2500 Avg Latency: ${avgLatency}ms`] : [],
344
+ `\u2514\u2500 OTel: ${otelStatus}`
345
+ ];
346
+ return lines.join("\n");
347
+ }
348
+ /** Format milliseconds into human-readable uptime string */
349
+ formatUptime(ms) {
350
+ const seconds = Math.floor(ms / 1e3);
351
+ if (seconds < 60) return `${seconds}s`;
352
+ const minutes = Math.floor(seconds / 60);
353
+ const remainingSeconds = seconds % 60;
354
+ if (minutes < 60) return `${minutes}m ${remainingSeconds}s`;
355
+ const hours = Math.floor(minutes / 60);
356
+ const remainingMinutes = minutes % 60;
357
+ return `${hours}h ${remainingMinutes}m`;
358
+ }
359
+ /** Reset all recorded metrics (used in tests) */
360
+ reset() {
361
+ this.operations = [];
362
+ }
363
+ /** Destroy the singleton (used in tests to guarantee isolation) */
364
+ static destroy() {
365
+ _TokenTelemetryEngine.instance = null;
366
+ }
367
+ };
368
+
369
+ // src/utils/mcpCompact.ts
370
+ function mcpCompactToolDescriptions() {
371
+ const v = process.env.LIOP_MCP_COMPACT_TOOL_DESCRIPTIONS?.toLowerCase().trim();
372
+ if (v === "0" || v === "false" || v === "no") return false;
373
+ return true;
374
+ }
375
+ function stripVerboseLiopToolDescription(description) {
376
+ let d = description;
377
+ const markers = [
378
+ "\n\n[LIOP-PROTO-V1:",
379
+ "\r\n\r\n[LIOP-PROTO-V1:",
380
+ "\n[LIOP-PROTO-V1:"
381
+ // rare
382
+ ];
383
+ for (const m of markers) {
384
+ const i = d.indexOf(m);
385
+ if (i !== -1) {
386
+ d = d.slice(0, i);
387
+ break;
388
+ }
389
+ }
390
+ return d.trimEnd();
391
+ }
392
+
393
+ // src/gateway/router.ts
394
+ var MANIFEST_CACHE_TTL_S = 300;
395
+ var MANIFEST_DISCOVERY_RETRIES = 5;
396
+ var LiopMcpRouter = class _LiopMcpRouter {
397
+ constructor(liopServer, meshNode = null, defaultRpcPort = 50051) {
398
+ this.liopServer = liopServer;
399
+ this.meshNode = meshNode;
400
+ this.defaultRpcPort = defaultRpcPort;
401
+ if (this.meshNode) {
402
+ this.meshNode.registerManifestHandler(() => {
403
+ const remoteTools = this.liopServer.listTools().map((t) => ({
404
+ name: t.name,
405
+ description: t.description,
406
+ inputSchema: t.inputSchema
407
+ }));
408
+ const resources = this.liopServer.listResources().map((r) => ({
409
+ name: r.name,
410
+ uri: r.uri,
411
+ description: r.description,
412
+ mimeType: r.mimeType
413
+ }));
414
+ const serverConfig = this.liopServer.config;
415
+ return {
416
+ peerId: this.meshNode?.getPeerId() || "unknown",
417
+ grpcPort: this.defaultRpcPort,
418
+ tools: [...remoteTools],
419
+ resources,
420
+ serverInfo: this.liopServer.getServerInfo(),
421
+ authRequired: this.liopServer.jwtValidator !== void 0,
422
+ tokenSlug: serverConfig?.tokenSlug,
423
+ taxonomy: serverConfig?.taxonomy ? {
424
+ domain: serverConfig.taxonomy.domain || "Unknown Domain",
425
+ clearanceTier: serverConfig.taxonomy.clearanceTier ?? 0,
426
+ executionTypes: serverConfig.taxonomy.executionTypes || []
427
+ } : void 0
428
+ };
429
+ });
430
+ this.meshNode.announceManifest().catch((err) => {
431
+ log.info(
432
+ `[LIOP-Router] Failed to announce manifest: ${err instanceof Error ? err.message : String(err)}`
433
+ );
434
+ });
435
+ }
436
+ if (process.env.LIOP_DIAGNOSTIC_LEVEL === "full") {
437
+ process.stderr.write(
438
+ "\u26A0\uFE0F [LIOP-Security] Diagnostic level set to FULL \u2014 PeerIDs and network topology are exposed. Do NOT use in production.\n"
439
+ );
440
+ }
441
+ }
442
+ /** Cached manifests from remote peers. Key = PeerID */
443
+ manifestCache = /* @__PURE__ */ new Map();
444
+ /** Guards against concurrent discovery storms */
445
+ currentDiscovery = null;
446
+ /** Verifier for Tier-0 integrity checks */
447
+ verifier = new LiopVerifier();
448
+ /** Callback when new remote tools are discovered */
449
+ onToolsChanged;
450
+ /** Circuit-breaker state for peers that repeatedly fail manifest queries. */
451
+ manifestFailureState = /* @__PURE__ */ new Map();
452
+ static MANIFEST_FAILURE_BASE_COOLDOWN_MS = 15e3;
453
+ static MANIFEST_FAILURE_MAX_COOLDOWN_MS = 5 * 6e4;
454
+ static MANIFEST_SKIP_LOG_THROTTLE_MS = 3e4;
455
+ shouldSkipManifestQuery(peerId) {
456
+ const state = this.manifestFailureState.get(peerId);
457
+ if (!state) return false;
458
+ const now = Date.now();
459
+ if (now >= state.cooldownUntil) return false;
460
+ if (now - state.lastSkipLogAt > _LiopMcpRouter.MANIFEST_SKIP_LOG_THROTTLE_MS) {
461
+ log.info(
462
+ `[LIOP-Router] Skipping manifest query for ${peerId} during cooldown (${Math.ceil((state.cooldownUntil - now) / 1e3)}s remaining)`
463
+ );
464
+ state.lastSkipLogAt = now;
465
+ }
466
+ return true;
467
+ }
468
+ recordManifestQuerySuccess(peerId) {
469
+ this.manifestFailureState.delete(peerId);
470
+ }
471
+ recordManifestQueryFailure(peerId) {
472
+ const now = Date.now();
473
+ const prev = this.manifestFailureState.get(peerId);
474
+ const failures = (prev?.failures || 0) + 1;
475
+ const backoff = Math.min(
476
+ _LiopMcpRouter.MANIFEST_FAILURE_BASE_COOLDOWN_MS * 2 ** Math.max(0, failures - 1),
477
+ _LiopMcpRouter.MANIFEST_FAILURE_MAX_COOLDOWN_MS
478
+ );
479
+ this.manifestFailureState.set(peerId, {
480
+ failures,
481
+ cooldownUntil: now + backoff,
482
+ lastSkipLogAt: 0
483
+ });
484
+ }
485
+ async dispatch(request, authInfo) {
486
+ const { method, params, id } = request;
487
+ log.info(`[LIOP-Router] Processing: ${method}`);
488
+ if (this.liopServer.jwtValidator) {
489
+ const authResult = authorizeRequest(method, authInfo ?? null);
490
+ if (!authResult.allowed) {
491
+ log.info(
492
+ `[LIOP-Router] RBAC Access Denied for method '${method}': ${authResult.reason}`
493
+ );
494
+ return {
495
+ jsonrpc: "2.0",
496
+ id,
497
+ error: {
498
+ code: -32099,
499
+ // Custom authentication/authorization failure code
500
+ message: authResult.reason || "Access Denied"
501
+ }
502
+ };
503
+ }
504
+ }
505
+ switch (method) {
506
+ case "initialize":
507
+ return {
508
+ jsonrpc: "2.0",
509
+ id,
510
+ result: {
511
+ protocolVersion: "2025-11-25",
512
+ capabilities: {
513
+ tools: { listChanged: true },
514
+ resources: { listChanged: true },
515
+ prompts: { listChanged: true }
516
+ },
517
+ serverInfo: this.liopServer.getServerInfo()
518
+ }
519
+ };
520
+ case "notifications/initialized":
521
+ this.kickDiscoveryAfterInitialized().catch(() => {
522
+ });
523
+ return null;
524
+ case "notifications/cancelled":
525
+ return null;
526
+ // No-op for MCP spec compliance
527
+ case "ping":
528
+ return { jsonrpc: "2.0", id, result: {} };
529
+ case "tools/list": {
530
+ const localTools = this.liopServer.listTools();
531
+ const remoteTools = await this.getRemoteTools();
532
+ const listedLocals = mcpCompactToolDescriptions() ? localTools.map((t) => ({
533
+ ...t,
534
+ description: stripVerboseLiopToolDescription(t.description ?? "")
535
+ })) : localTools;
536
+ log.info(
537
+ `[LIOP-Router] tools/list: ${localTools.length} local, ${remoteTools.length} remote tools found`
538
+ );
539
+ const diagnosticTool = {
540
+ name: "LiopMeshStatus",
541
+ description: "LiopMeshStatus: Returns the current dynamic diagnostic status of the Zero-Trust Neural Mesh.",
542
+ inputSchema: {
543
+ type: "object",
544
+ properties: {},
545
+ additionalProperties: false
546
+ }
547
+ };
548
+ const allTools = [diagnosticTool, ...listedLocals, ...remoteTools];
549
+ const telemetry = TokenTelemetryEngine.getInstance();
550
+ const toolsPayload = JSON.stringify(allTools);
551
+ const toolsResponsePayload = JSON.stringify({ tools: allTools });
552
+ telemetry.record({
553
+ type: "tools_list",
554
+ method: "tools/list",
555
+ estimatedInputTokens: telemetry.countTokens(toolsPayload),
556
+ estimatedOutputTokens: telemetry.countTokens(toolsResponsePayload)
557
+ });
558
+ return {
559
+ jsonrpc: "2.0",
560
+ id,
561
+ result: {
562
+ tools: allTools
563
+ }
564
+ };
565
+ }
566
+ case "tools/call":
567
+ return this.transcodeMcpToLiop(
568
+ id,
569
+ params,
570
+ authInfo?.token
571
+ );
572
+ case "resources/list": {
573
+ const localResources = this.liopServer.listResources();
574
+ const remoteResources = await this.getRemoteResources();
575
+ const allResources = [...localResources, ...remoteResources];
576
+ const rlTelemetry = TokenTelemetryEngine.getInstance();
577
+ const rlPayload = JSON.stringify(allResources);
578
+ rlTelemetry.record({
579
+ type: "resource_list",
580
+ method: "resources/list",
581
+ estimatedInputTokens: 0,
582
+ estimatedOutputTokens: rlTelemetry.countTokens(rlPayload)
583
+ });
584
+ return {
585
+ jsonrpc: "2.0",
586
+ id,
587
+ result: { resources: allResources }
588
+ };
589
+ }
590
+ case "resources/read": {
591
+ const typedParams = params;
592
+ if (!typedParams?.uri)
593
+ return {
594
+ jsonrpc: "2.0",
595
+ id,
596
+ error: { code: -32602, message: "Missing resource uri" }
597
+ };
598
+ try {
599
+ const rrStartTime = Date.now();
600
+ const result = await this.liopServer.readResource(typedParams.uri);
601
+ const rrTelemetry = TokenTelemetryEngine.getInstance();
602
+ const rrOutputPayload = JSON.stringify(result);
603
+ rrTelemetry.record({
604
+ type: "resource_read",
605
+ method: "resources/read",
606
+ toolName: typedParams.uri,
607
+ estimatedInputTokens: rrTelemetry.countTokens(typedParams.uri),
608
+ estimatedOutputTokens: rrTelemetry.countTokens(rrOutputPayload),
609
+ durationMs: Date.now() - rrStartTime
610
+ });
611
+ return { jsonrpc: "2.0", id, result };
612
+ } catch (err) {
613
+ const targetUri = typedParams.uri;
614
+ for (const { manifest } of this.manifestCache.values()) {
615
+ const remoteResource = manifest.resources.find(
616
+ (r) => r.uri === targetUri
617
+ );
618
+ if (remoteResource) {
619
+ log.info(
620
+ `[LIOP-Router] Resolved resource ${targetUri} from cache (Peer: ${manifest.peerId})`
621
+ );
622
+ return {
623
+ jsonrpc: "2.0",
624
+ id,
625
+ result: {
626
+ contents: [
627
+ {
628
+ uri: remoteResource.uri,
629
+ mimeType: remoteResource.mimeType || "text/plain",
630
+ text: remoteResource.text || remoteResource.description || "No content provided"
631
+ }
632
+ ]
633
+ }
634
+ };
635
+ }
636
+ }
637
+ return {
638
+ jsonrpc: "2.0",
639
+ id,
640
+ error: {
641
+ code: -32e3,
642
+ message: err instanceof Error ? err.message : String(err)
643
+ }
644
+ };
645
+ }
646
+ }
647
+ case "prompts/list": {
648
+ const promptsList = this.liopServer.listPrompts();
649
+ const plTelemetry = TokenTelemetryEngine.getInstance();
650
+ const plPayload = JSON.stringify(promptsList);
651
+ plTelemetry.record({
652
+ type: "prompt_list",
653
+ method: "prompts/list",
654
+ estimatedInputTokens: 0,
655
+ estimatedOutputTokens: plTelemetry.countTokens(plPayload)
656
+ });
657
+ return {
658
+ jsonrpc: "2.0",
659
+ id,
660
+ result: { prompts: promptsList }
661
+ };
662
+ }
663
+ case "prompts/get": {
664
+ const typedParams = params;
665
+ if (!typedParams?.name)
666
+ return {
667
+ jsonrpc: "2.0",
668
+ id,
669
+ error: { code: -32602, message: "Missing prompt name" }
670
+ };
671
+ try {
672
+ const pgStartTime = Date.now();
673
+ const result = await this.liopServer.getPrompt({
674
+ name: typedParams.name,
675
+ arguments: typedParams.arguments || {}
676
+ });
677
+ const pgTelemetry = TokenTelemetryEngine.getInstance();
678
+ const pgInputPayload = JSON.stringify({
679
+ name: typedParams.name,
680
+ arguments: typedParams.arguments
681
+ });
682
+ const pgOutputPayload = JSON.stringify(result);
683
+ pgTelemetry.record({
684
+ type: "prompt_get",
685
+ method: "prompts/get",
686
+ toolName: typedParams.name,
687
+ estimatedInputTokens: pgTelemetry.countTokens(pgInputPayload),
688
+ estimatedOutputTokens: pgTelemetry.countTokens(pgOutputPayload),
689
+ durationMs: Date.now() - pgStartTime
690
+ });
691
+ return { jsonrpc: "2.0", id, result };
692
+ } catch (err) {
693
+ return {
694
+ jsonrpc: "2.0",
695
+ id,
696
+ error: {
697
+ code: -32e3,
698
+ message: err instanceof Error ? err.message : String(err)
699
+ }
700
+ };
701
+ }
702
+ }
703
+ default:
704
+ return {
705
+ jsonrpc: "2.0",
706
+ id,
707
+ error: { code: -32601, message: `Method not found: ${method}` }
708
+ };
709
+ }
710
+ }
711
+ /**
712
+ * MCP clients often send notifications/initialized then immediately tools/list.
713
+ * Start manifest discovery without blocking the notification handler.
714
+ */
715
+ kickDiscoveryAfterInitialized() {
716
+ return (async () => {
717
+ await new Promise((r) => setTimeout(r, 250));
718
+ await Promise.race([
719
+ this.refreshManifestCache(true),
720
+ new Promise((r) => setTimeout(r, 15e3))
721
+ ]).catch(() => {
722
+ });
723
+ })();
724
+ }
725
+ /**
726
+ * Discovers and caches manifests from all remote LIOP providers in the mesh.
727
+ * Uses Kademlia DHT to find "liop:manifest" providers, then opens
728
+ * /liop/manifest/1.0.0 protocol streams to retrieve their full metadata.
729
+ */
730
+ async refreshManifestCache(silent = false) {
731
+ if (!this.meshNode) return;
732
+ if (this.currentDiscovery) return this.currentDiscovery;
733
+ if (!silent && this.manifestCache.size > 0) {
734
+ const now = Date.now();
735
+ const allFresh = Array.from(this.manifestCache.values()).every(
736
+ ({ cachedAt }) => now - cachedAt < MANIFEST_CACHE_TTL_S * 1e3
737
+ );
738
+ if (allFresh) return;
739
+ }
740
+ this.currentDiscovery = (async () => {
741
+ try {
742
+ const prevCount = Array.from(this.manifestCache.values()).reduce(
743
+ (acc, { manifest }) => acc + manifest.tools.length,
744
+ 0
745
+ );
746
+ if (this.manifestCache.size === 0) {
747
+ for (let i = 0; i < 3; i++) {
748
+ const connections = (
749
+ // biome-ignore lint/suspicious/noExplicitAny: access internal nodes for connection count
750
+ this.meshNode.node?.getConnections().length || 0
751
+ );
752
+ if (connections > 0) {
753
+ log.info(
754
+ `[LIOP-Router] P2P Connection established. Starting discovery...`
755
+ );
756
+ break;
757
+ }
758
+ log.info(
759
+ `[LIOP-Router] Waiting for P2P connections (attempt ${i + 1}/10)...`
760
+ );
761
+ await new Promise((r) => setTimeout(r, 1e3));
762
+ }
763
+ }
764
+ let providerIds = [];
765
+ const MAX_COLD_ATTEMPTS = this.manifestCache.size === 0 ? 5 : 1;
766
+ for (let coldAttempt = 0; coldAttempt < MAX_COLD_ATTEMPTS; coldAttempt++) {
767
+ for (let attempt = 0; attempt < MANIFEST_DISCOVERY_RETRIES; attempt++) {
768
+ providerIds = await this.meshNode?.discoverManifestProviders() || [];
769
+ const selfId2 = this.meshNode?.getPeerId();
770
+ const remoteIds = providerIds.filter((id) => id !== selfId2);
771
+ if (remoteIds.length > 0) break;
772
+ if (attempt < MANIFEST_DISCOVERY_RETRIES - 1) {
773
+ log.info(
774
+ `[LIOP-Router] DHT discovery attempt ${attempt + 1}/${MANIFEST_DISCOVERY_RETRIES}...`
775
+ );
776
+ await new Promise((r) => setTimeout(r, 1e3));
777
+ }
778
+ }
779
+ const activePeers = (
780
+ // biome-ignore lint/suspicious/noExplicitAny: access internal nodes
781
+ this.meshNode.node?.getConnections().map(
782
+ (c) => c.remotePeer.toString()
783
+ ) || []
784
+ );
785
+ if (activePeers.length > 0) {
786
+ providerIds = Array.from(/* @__PURE__ */ new Set([...providerIds, ...activePeers]));
787
+ }
788
+ const selfIdEnd = this.meshNode?.getPeerId();
789
+ const remoteIdsEnd = providerIds.filter((id) => id !== selfIdEnd);
790
+ if (remoteIdsEnd.length > 0) break;
791
+ if (coldAttempt < MAX_COLD_ATTEMPTS - 1) {
792
+ log.info(
793
+ `[LIOP-Router] Initial discovery failed (0 providers). Retrying in 1s (${coldAttempt + 1}/${MAX_COLD_ATTEMPTS})...`
794
+ );
795
+ await new Promise((r) => setTimeout(r, 1e3));
796
+ }
797
+ }
798
+ if (providerIds.length === 0) {
799
+ log.info(
800
+ `[LIOP-Router] No manifest providers found after all attempts.`
801
+ );
802
+ return;
803
+ }
804
+ if (!silent) {
805
+ log.info(
806
+ `[LIOP-Router] Discovered ${providerIds.length} candidate manifest providers`
807
+ );
808
+ }
809
+ const connectedPeers = new Set(
810
+ // biome-ignore lint/suspicious/noExplicitAny: internal node access for fast peer ordering
811
+ (this.meshNode.node?.getConnections?.() || []).map(
812
+ (c) => c.remotePeer.toString()
813
+ )
814
+ );
815
+ providerIds = [...providerIds].sort((a, b) => {
816
+ const aConnected = connectedPeers.has(a) ? 1 : 0;
817
+ const bConnected = connectedPeers.has(b) ? 1 : 0;
818
+ return bConnected - aConnected;
819
+ });
820
+ let successCount = 0;
821
+ let errorCount = 0;
822
+ let cacheUpdated = false;
823
+ const selfId = this.meshNode?.getPeerId();
824
+ const eligiblePeers = providerIds.filter((peerId) => {
825
+ if (!this.meshNode) return false;
826
+ if (peerId === selfId) return false;
827
+ if (this.shouldSkipManifestQuery(peerId)) return false;
828
+ const cached = this.manifestCache.get(peerId);
829
+ if (cached && Date.now() - cached.cachedAt < MANIFEST_CACHE_TTL_S * 1e3) {
830
+ successCount++;
831
+ return false;
832
+ }
833
+ return true;
834
+ });
835
+ const queryResults = await Promise.allSettled(
836
+ eligiblePeers.map(async (peerId) => {
837
+ if (!this.meshNode) return null;
838
+ log.info(`[LIOP-Router] Querying manifest from: ${peerId}`);
839
+ return {
840
+ peerId,
841
+ manifest: await this.meshNode.queryManifest(peerId)
842
+ };
843
+ })
844
+ );
845
+ for (const result of queryResults) {
846
+ if (result.status === "fulfilled" && result.value?.manifest) {
847
+ const { peerId, manifest } = result.value;
848
+ this.manifestCache.set(peerId, {
849
+ manifest,
850
+ cachedAt: Date.now()
851
+ });
852
+ this.recordManifestQuerySuccess(peerId);
853
+ cacheUpdated = true;
854
+ successCount++;
855
+ log.info(
856
+ `[LIOP-Router] Manifest received from ${peerId} (${manifest.tools.length} tools)`
857
+ );
858
+ } else if (result.status === "fulfilled" && result.value) {
859
+ this.recordManifestQueryFailure(result.value.peerId);
860
+ errorCount++;
861
+ log.info(
862
+ `[LIOP-Router] Manifest query returned NULL for ${result.value.peerId}`
863
+ );
864
+ } else if (result.status === "rejected") {
865
+ errorCount++;
866
+ log.info(
867
+ `[LIOP-Router] Fatal error querying manifest:`,
868
+ result.reason instanceof Error ? result.reason.message : String(result.reason)
869
+ );
870
+ }
871
+ }
872
+ this._discoveryStats = {
873
+ candidates: providerIds.length,
874
+ success: successCount,
875
+ failures: errorCount,
876
+ lastDiscovery: Date.now()
877
+ };
878
+ if (cacheUpdated) {
879
+ const newCount = Array.from(this.manifestCache.values()).reduce(
880
+ (acc, { manifest }) => acc + manifest.tools.length,
881
+ 0
882
+ );
883
+ if (newCount !== prevCount && this.onToolsChanged) {
884
+ process.stderr.write(
885
+ "[LIOP-Router] Mesh topology updated! Emitting notifications/tools/list_changed.\n"
886
+ );
887
+ this.onToolsChanged();
888
+ }
889
+ }
890
+ } finally {
891
+ this.currentDiscovery = null;
892
+ }
893
+ })();
894
+ return this.currentDiscovery;
895
+ }
896
+ /**
897
+ * Returns the current manifest cache size for external telemetry.
898
+ * Used by the adaptive polling system to detect topology stabilization.
899
+ */
900
+ getCacheSize() {
901
+ return this.manifestCache.size;
902
+ }
903
+ /**
904
+ * Returns all remote tools discovered via the manifest protocol.
905
+ */
906
+ async getRemoteTools() {
907
+ const EXPECTED_PROVIDERS = Number.parseInt(
908
+ process.env.LIOP_EXPECTED_PROVIDERS ?? "1",
909
+ 10
910
+ );
911
+ if (this.manifestCache.size < EXPECTED_PROVIDERS && this.meshNode) {
912
+ const initialTimeoutMs = Number.parseInt(
913
+ process.env.LIOP_INITIAL_DISCOVERY_TIMEOUT_MS ?? "8000",
914
+ 10
915
+ );
916
+ const boundedTimeoutMs = Number.isFinite(initialTimeoutMs) && initialTimeoutMs > 0 ? initialTimeoutMs : 12e3;
917
+ const deadline = Date.now() + boundedTimeoutMs;
918
+ let stableCount = 0;
919
+ let lastCacheSize = -1;
920
+ while (Date.now() < deadline) {
921
+ if (this.manifestCache.size >= EXPECTED_PROVIDERS) break;
922
+ await Promise.race([
923
+ this.refreshManifestCache(true),
924
+ new Promise((resolve) => setTimeout(resolve, 3e3))
925
+ ]).catch(() => {
926
+ });
927
+ if (this.manifestCache.size >= EXPECTED_PROVIDERS) break;
928
+ if (this.manifestCache.size === lastCacheSize) {
929
+ stableCount++;
930
+ if (stableCount >= 3 && this.manifestCache.size > 0) {
931
+ log.info(
932
+ `[LIOP-Router] Provider count stabilized at ${this.manifestCache.size}/${EXPECTED_PROVIDERS}. Proceeding with available mesh.`
933
+ );
934
+ break;
935
+ }
936
+ } else {
937
+ stableCount = 0;
938
+ lastCacheSize = this.manifestCache.size;
939
+ }
940
+ await new Promise((r) => setTimeout(r, 1e3));
941
+ }
942
+ if (this.manifestCache.size < EXPECTED_PROVIDERS) {
943
+ log.info(
944
+ `[LIOP-Router] \u26A0\uFE0F Mesh partially available: ${this.manifestCache.size}/${EXPECTED_PROVIDERS} providers. Some tools may be unavailable. Check Docker containers.`
945
+ );
946
+ this.refreshManifestCache(true).catch(() => {
947
+ });
948
+ }
949
+ }
950
+ const tools = [];
951
+ const seenNames = /* @__PURE__ */ new Set();
952
+ const localToolNames = new Set(
953
+ this.liopServer.listTools().map((t) => t.name)
954
+ );
955
+ for (const [peerId, { manifest }] of this.manifestCache.entries()) {
956
+ for (const tool of manifest.tools) {
957
+ if (tool.name === "LiopMeshStatus") continue;
958
+ let finalName = tool.name;
959
+ if (seenNames.has(tool.name) || localToolNames.has(tool.name)) {
960
+ finalName = `${tool.name}_${peerId.slice(-4)}`;
961
+ }
962
+ seenNames.add(finalName);
963
+ const providerName = manifest.serverInfo?.name || "Unknown Provider";
964
+ const baseDesc = tool.description || `Remote tool from ${providerName}`;
965
+ const cleanTool = {
966
+ name: finalName,
967
+ description: mcpCompactToolDescriptions() ? stripVerboseLiopToolDescription(baseDesc) : baseDesc,
968
+ inputSchema: tool.inputSchema || {
969
+ type: "object",
970
+ properties: {}
971
+ }
972
+ };
973
+ if (typeof cleanTool.inputSchema === "object" && !cleanTool.inputSchema.type) {
974
+ cleanTool.inputSchema.type = "object";
975
+ }
976
+ if (typeof cleanTool.inputSchema === "object" && !cleanTool.inputSchema.properties) {
977
+ cleanTool.inputSchema.properties = {};
978
+ }
979
+ let blueprint = "";
980
+ if (manifest.taxonomy) {
981
+ blueprint = `
982
+ [LIOP-DOMAIN: ${manifest.taxonomy.domain}]`;
983
+ }
984
+ const properties = cleanTool.inputSchema.properties || {};
985
+ let envelopeDoc = "";
986
+ if (!mcpCompactToolDescriptions() && properties.payload) {
987
+ envelopeDoc = `
988
+ [REQUIRES: LIOP-PROTO-V1 ENVELOPE]`;
989
+ }
990
+ if (!mcpCompactToolDescriptions() && cleanTool.description.includes("STRICT SCHEMA ADHERENCE")) {
991
+ cleanTool.description = cleanTool.description.replace(
992
+ "STRICT SCHEMA ADHERENCE:",
993
+ "[INDUSTRIAL-REQUISITE] STRICT SCHEMA ADHERENCE (MANDATORY):"
994
+ );
995
+ }
996
+ const originStamp = mcpCompactToolDescriptions() ? `
997
+ (Peer: ${peerId.slice(-8)})${blueprint}` : `
998
+ (Origin: ${peerId.slice(-8)})${blueprint}${envelopeDoc}`;
999
+ cleanTool.description = `${cleanTool.description}${originStamp}`;
1000
+ tools.push(cleanTool);
1001
+ }
1002
+ }
1003
+ return tools;
1004
+ }
1005
+ /**
1006
+ * Returns all remote resources discovered via the manifest protocol.
1007
+ */
1008
+ async getRemoteResources() {
1009
+ if (!this.currentDiscovery) {
1010
+ this.refreshManifestCache(true).catch(() => {
1011
+ });
1012
+ }
1013
+ const resources = [];
1014
+ const seenUris = new Set(this.liopServer.listResources().map((r) => r.uri));
1015
+ for (const [peerId, { manifest }] of this.manifestCache.entries()) {
1016
+ for (const resource of manifest.resources) {
1017
+ if (!seenUris.has(resource.uri)) {
1018
+ const augmentedResource = { ...resource };
1019
+ const providerName = manifest.serverInfo?.name || "Unknown Provider";
1020
+ let blueprint = "";
1021
+ if (manifest.taxonomy) {
1022
+ blueprint = `
1023
+
1024
+ [LIOP Zero-Trust Blueprint]
1025
+ Domain: ${manifest.taxonomy.domain}
1026
+ Clearance Tier: ${manifest.taxonomy.clearanceTier}`;
1027
+ if (manifest.taxonomy.executionTypes && manifest.taxonomy.executionTypes.length > 0) {
1028
+ blueprint += `
1029
+ Execution Types: ${manifest.taxonomy.executionTypes.join(", ")}`;
1030
+ }
1031
+ }
1032
+ const originStamp = `
1033
+
1034
+ [LIOP Zero-Trust Origin]
1035
+ Provider: ${providerName}
1036
+ Network ID: ${peerId}${blueprint}`;
1037
+ if (augmentedResource.uri.startsWith("liop://schema/")) {
1038
+ augmentedResource.name = `[SCHEMA] ${augmentedResource.name}`;
1039
+ augmentedResource.description = `[CRITICAL SCHEMA] ${augmentedResource.description || "Data Dictionary for Zero-Shot Autonomy"}${originStamp}`;
1040
+ } else {
1041
+ augmentedResource.description = augmentedResource.description ? `${augmentedResource.description}${originStamp}` : originStamp.trim();
1042
+ }
1043
+ resources.push(augmentedResource);
1044
+ seenUris.add(resource.uri);
1045
+ }
1046
+ }
1047
+ }
1048
+ return resources;
1049
+ }
1050
+ /**
1051
+ * Resolves the gRPC target (host:port) AND the peerId for a given tool name
1052
+ * by searching the manifest cache. Supports exact names and suffixed names.
1053
+ */
1054
+ resolveManifestTarget(toolName) {
1055
+ for (const [peerId, { manifest }] of this.manifestCache.entries()) {
1056
+ const tool = manifest.tools.find((t) => t.name === toolName);
1057
+ if (tool) {
1058
+ return {
1059
+ peerId,
1060
+ originalToolName: toolName
1061
+ };
1062
+ }
1063
+ }
1064
+ const parts = toolName.split("_");
1065
+ if (parts.length > 1) {
1066
+ const suffix = parts.pop();
1067
+ const baseName = parts.join("_");
1068
+ for (const [peerId, { manifest }] of this.manifestCache.entries()) {
1069
+ if (peerId.endsWith(suffix || "")) {
1070
+ const tool = manifest.tools.find((t) => t.name === baseName);
1071
+ if (tool) {
1072
+ return {
1073
+ peerId,
1074
+ originalToolName: baseName
1075
+ };
1076
+ }
1077
+ }
1078
+ }
1079
+ }
1080
+ return null;
1081
+ }
1082
+ /**
1083
+ * Redacts a PeerID for external-facing diagnostics.
1084
+ * LIOP_DIAGNOSTIC_LEVEL controls verbosity:
1085
+ * - "redacted" (default): truncated to last 8 chars
1086
+ * - "full": complete PeerID (development only)
1087
+ */
1088
+ redactPeerId(peerId) {
1089
+ const level = process.env.LIOP_DIAGNOSTIC_LEVEL || "redacted";
1090
+ if (level === "full") return peerId;
1091
+ return `***${peerId.slice(-8)}`;
1092
+ }
1093
+ async transcodeMcpToLiop(id, params, token) {
1094
+ const toolName = params.name;
1095
+ if (toolName === "LiopMeshStatus") {
1096
+ this.refreshManifestCache(true).catch(() => {
1097
+ });
1098
+ const stats = this._discoveryStats || {
1099
+ candidates: 0,
1100
+ success: 0,
1101
+ failures: 0
1102
+ };
1103
+ const providerCount = this.manifestCache.size;
1104
+ const meshState = this.meshNode ? "Active" : "Offline";
1105
+ const cachedTools = Array.from(this.manifestCache.values()).reduce(
1106
+ (acc, { manifest }) => acc + manifest.tools.length,
1107
+ 0
1108
+ );
1109
+ const connections = this.meshNode ? (
1110
+ // biome-ignore lint/suspicious/noExplicitAny: access internal nodes
1111
+ this.meshNode.node?.getConnections().length
1112
+ ) : 0;
1113
+ const bootstrapNodes = this.meshNode && // biome-ignore lint/suspicious/noExplicitAny: access internal config
1114
+ this.meshNode.config?.bootstrapNodes ? (
1115
+ // biome-ignore lint/suspicious/noExplicitAny: access internal config
1116
+ this.meshNode.config.bootstrapNodes
1117
+ ) : [];
1118
+ const bootstrapCount = bootstrapNodes.length;
1119
+ const diagLevel = process.env.LIOP_DIAGNOSTIC_LEVEL || "redacted";
1120
+ const showBootstraps = diagLevel !== "minimal";
1121
+ const bootstrapList = showBootstraps ? bootstrapNodes.map((addr) => {
1122
+ const parts = addr.split("/");
1123
+ const id2 = parts[parts.length - 1];
1124
+ return ` \u2022 ${id2 ? id2.slice(-8) : "Unknown"} (bootstrap)`;
1125
+ }).join("\n") : "";
1126
+ const routingTableSize = this.meshNode ? (
1127
+ // biome-ignore lint/suspicious/noExplicitAny: access internal nodes
1128
+ this.meshNode.getRoutingTableSize()
1129
+ ) : 0;
1130
+ const rawPeerId = this.meshNode?.getPeerId() || "Offline";
1131
+ const localPeerId = rawPeerId === "Offline" ? rawPeerId : this.redactPeerId(rawPeerId);
1132
+ const cachedToolList = Array.from(this.manifestCache.entries()).flatMap(
1133
+ ([peerId, { manifest }]) => manifest.tools.map(
1134
+ (t) => ` \u2022 ${t.name} (from origin: ${this.redactPeerId(peerId)})`
1135
+ )
1136
+ ).join("\n");
1137
+ const statusText = [
1138
+ `LIOP Mesh Status: ${meshState === "Active" ? "Active" : "Offline"}`,
1139
+ `Local Agent Identity: ${localPeerId}`,
1140
+ `Network: ${connections} Conns | ${routingTableSize} Mesh Nodes | ${bootstrapCount} Bootstraps`,
1141
+ showBootstraps && bootstrapCount > 0 ? `
1142
+ Active Bootstraps:
1143
+ ${bootstrapList}
1144
+ ` : "",
1145
+ `Discovery: ${stats.candidates} Candidates | ${stats.success} OK | ${stats.failures} FAIL`,
1146
+ `Tooling: ${providerCount} Providers | ${cachedTools} Total Remote Tools`,
1147
+ cachedTools > 0 ? `
1148
+ Discovered Remote Tools (Zero-Trust Origins):
1149
+ ${cachedToolList}` : "\nNo remote tools discovered yet.",
1150
+ // [Token Economy] Telemetry block (only appears when operations exist)
1151
+ TokenTelemetryEngine.getInstance().formatStatusBlock()
1152
+ ].filter((line) => line !== "").join("\n");
1153
+ const diagTelemetry = TokenTelemetryEngine.getInstance();
1154
+ diagTelemetry.record({
1155
+ type: "diagnostic",
1156
+ method: "tools/call",
1157
+ toolName: "LiopMeshStatus",
1158
+ estimatedInputTokens: 0,
1159
+ estimatedOutputTokens: diagTelemetry.countTokens(statusText)
1160
+ });
1161
+ return {
1162
+ jsonrpc: "2.0",
1163
+ id,
1164
+ result: {
1165
+ content: [
1166
+ {
1167
+ type: "text",
1168
+ text: statusText
1169
+ }
1170
+ ]
1171
+ }
1172
+ };
1173
+ }
1174
+ const isLocal = this.liopServer.listTools().some((t) => t.name === toolName);
1175
+ if (!isLocal && this.meshNode) {
1176
+ let target = this.resolveManifestTarget(toolName);
1177
+ if (!target) {
1178
+ await this.refreshManifestCache();
1179
+ target = this.resolveManifestTarget(toolName);
1180
+ }
1181
+ if (target) {
1182
+ log.info(
1183
+ `[LIOP-Router] Resolved ${toolName} via manifest cache (Peer: ${target.peerId}, Original: ${target.originalToolName})`
1184
+ );
1185
+ const manifestEntry = this.manifestCache.get(target.peerId);
1186
+ let effectiveToken = token;
1187
+ if (manifestEntry?.manifest.authRequired) {
1188
+ const resolvedToken = token || await this.getOrAcquireMeshAgentToken(target.peerId);
1189
+ if (!resolvedToken) {
1190
+ const providerName = manifestEntry.manifest.serverInfo?.name?.toLowerCase() || "unknown";
1191
+ const slug = manifestEntry.manifest.tokenSlug;
1192
+ const shortId = target.peerId.slice(-8).toUpperCase();
1193
+ const primaryVar = slug ? `LIOP_TOKEN_${slug}` : `LIOP_TOKEN_${providerName.toUpperCase().replace(/[^A-Z0-9_]/g, "_")}`;
1194
+ return {
1195
+ jsonrpc: "2.0",
1196
+ id,
1197
+ result: {
1198
+ content: [
1199
+ {
1200
+ type: "text",
1201
+ text: `Authentication Required: The restricted node (${providerName}) requires an access token. Please define the ${primaryVar} or LIOP_TOKEN_${shortId} environment variable on your agent/client host.`
1202
+ }
1203
+ ],
1204
+ isError: true
1205
+ }
1206
+ };
1207
+ }
1208
+ effectiveToken = resolvedToken;
1209
+ }
1210
+ return this.routeToRemoteProvider(
1211
+ id,
1212
+ target.originalToolName,
1213
+ target.peerId,
1214
+ params,
1215
+ effectiveToken
1216
+ );
1217
+ }
1218
+ let providers = [];
1219
+ for (let i = 0; i < 3; i++) {
1220
+ providers = await this.meshNode.findProviders(toolName);
1221
+ if (providers.length > 0) break;
1222
+ if (i < 2) await new Promise((r) => setTimeout(r, 1e3));
1223
+ }
1224
+ if (providers.length > 0) {
1225
+ return this.routeToRemoteProvider(
1226
+ id,
1227
+ toolName,
1228
+ providers[0],
1229
+ params,
1230
+ token
1231
+ );
1232
+ }
1233
+ }
1234
+ if (isLocal) {
1235
+ try {
1236
+ const localStartTime = Date.now();
1237
+ const result = await this.liopServer.callTool({
1238
+ name: toolName,
1239
+ arguments: params.arguments || {}
1240
+ });
1241
+ const localTelemetry = TokenTelemetryEngine.getInstance();
1242
+ const localInputPayload = JSON.stringify(params.arguments || {});
1243
+ const localOutputPayload = JSON.stringify(result);
1244
+ localTelemetry.record({
1245
+ type: "tool_call",
1246
+ method: "tools/call",
1247
+ toolName,
1248
+ estimatedInputTokens: localTelemetry.countTokens(localInputPayload),
1249
+ estimatedOutputTokens: localTelemetry.countTokens(localOutputPayload),
1250
+ durationMs: Date.now() - localStartTime
1251
+ });
1252
+ return { jsonrpc: "2.0", id, result };
1253
+ } catch (err) {
1254
+ return {
1255
+ jsonrpc: "2.0",
1256
+ id,
1257
+ error: {
1258
+ code: -32e3,
1259
+ message: err instanceof Error ? err.message : String(err)
1260
+ }
1261
+ };
1262
+ }
1263
+ }
1264
+ return {
1265
+ jsonrpc: "2.0",
1266
+ id,
1267
+ error: {
1268
+ code: -32002,
1269
+ message: `No provider found for tool: ${toolName}. Ensure the provider node is active and connected to the mesh.`
1270
+ }
1271
+ };
1272
+ }
1273
+ async routeToRemoteProvider(id, toolName, peerId, params, token) {
1274
+ if (!this.meshNode)
1275
+ return {
1276
+ jsonrpc: "2.0",
1277
+ id,
1278
+ error: { code: -32603, message: "Mesh Node inactive" }
1279
+ };
1280
+ let manifestEntry = this.manifestCache.get(peerId);
1281
+ let grpcPort = this.defaultRpcPort;
1282
+ if (manifestEntry) {
1283
+ grpcPort = manifestEntry.manifest.grpcPort;
1284
+ } else {
1285
+ const manifest = await this.meshNode.queryManifest(peerId);
1286
+ if (manifest) {
1287
+ grpcPort = manifest.grpcPort;
1288
+ this.manifestCache.set(peerId, {
1289
+ manifest,
1290
+ cachedAt: Date.now()
1291
+ });
1292
+ manifestEntry = this.manifestCache.get(peerId);
1293
+ }
1294
+ }
1295
+ const shouldRemapGrpcPorts = process.env.LIOP_USE_PUBLISHED_GRPC_PORTS === "1" || process.env.LIOP_DOCKER_MAP === "true" || process.env.LIOP_DEV_MODE === "true" || process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test";
1296
+ if (shouldRemapGrpcPorts && manifestEntry) {
1297
+ const providerName = manifestEntry.manifest.serverInfo?.name?.toLowerCase() || "";
1298
+ if (providerName.includes("vault")) grpcPort = 13011;
1299
+ else if (providerName.includes("bank")) grpcPort = 13021;
1300
+ else if (providerName.includes("oracle")) grpcPort = 13031;
1301
+ }
1302
+ const addrs = await this.meshNode.resolvePeer(peerId);
1303
+ let targetAddr = null;
1304
+ const os = await import('os');
1305
+ const localInterfaces = Object.values(os.networkInterfaces()).flat().filter((i) => i?.family === "IPv4").map((i) => i?.address);
1306
+ for (const addr of addrs) {
1307
+ const parts = addr.split("/");
1308
+ const ipIdx = parts.indexOf("ip4");
1309
+ if (ipIdx !== -1) {
1310
+ const advertisedIp = parts[ipIdx + 1];
1311
+ if (advertisedIp === "127.0.0.1" || localInterfaces.includes(advertisedIp)) {
1312
+ targetAddr = `127.0.0.1:${grpcPort}`;
1313
+ break;
1314
+ }
1315
+ if (!targetAddr) {
1316
+ targetAddr = `${advertisedIp}:${grpcPort}`;
1317
+ }
1318
+ }
1319
+ }
1320
+ if (!targetAddr) {
1321
+ targetAddr = `127.0.0.1:${grpcPort}`;
1322
+ }
1323
+ log.info(
1324
+ `[LIOP-Router] Dynamic route: ${toolName} -> ${targetAddr} (PeerID: ${peerId})`
1325
+ );
1326
+ const remoteClient = new liopV1.LogicMesh(
1327
+ targetAddr,
1328
+ createChannelCredentials()
1329
+ );
1330
+ return this.performTranscoding(
1331
+ id,
1332
+ remoteClient,
1333
+ toolName,
1334
+ params,
1335
+ peerId,
1336
+ token
1337
+ );
1338
+ }
1339
+ /** Cached M2M token for dynamic gateway-to-node routing */
1340
+ meshAgentToken;
1341
+ /**
1342
+ * Dynamically acquires an M2M access token from the Nexus Authorization Server.
1343
+ * If peerId is provided, checks if there are node-specific environment tokens
1344
+ * before falling back to the global static token or Nexus acquisition.
1345
+ */
1346
+ async getOrAcquireMeshAgentToken(peerId) {
1347
+ if (peerId) {
1348
+ const manifestEntry = this.manifestCache.get(peerId);
1349
+ const providerName = manifestEntry?.manifest.serverInfo?.name?.toLowerCase() || "";
1350
+ let nodeToken;
1351
+ const slug = manifestEntry?.manifest.tokenSlug;
1352
+ if (slug) {
1353
+ const envKey = `LIOP_TOKEN_${slug}`;
1354
+ nodeToken = process.env[envKey] || process.env[`LIOP_OAUTH_TOKEN_${slug}`];
1355
+ log.info(
1356
+ `[LIOP-Router] Step0 tokenSlug=${slug} envKey=${envKey} found=${!!nodeToken} peer=${peerId.slice(-8)}`
1357
+ );
1358
+ } else {
1359
+ log.info(
1360
+ `[LIOP-Router] Step0 tokenSlug=MISSING (manifest has no tokenSlug) peer=${peerId.slice(-8)} provider=${providerName}`
1361
+ );
1362
+ }
1363
+ if (!nodeToken) {
1364
+ const shortId = peerId.slice(-8).toUpperCase();
1365
+ nodeToken = process.env[`LIOP_TOKEN_${shortId}`] || process.env[`LIOP_OAUTH_TOKEN_${shortId}`];
1366
+ }
1367
+ if (!nodeToken && providerName) {
1368
+ const cleanName = providerName.toUpperCase().replace(/[^A-Z0-9_]/g, "_");
1369
+ nodeToken = process.env[`LIOP_TOKEN_${cleanName}`] || process.env[`LIOP_OAUTH_TOKEN_${cleanName}`];
1370
+ }
1371
+ if (nodeToken) {
1372
+ log.info(
1373
+ `[LIOP-Router] Resolved node-specific token for peer ${peerId.slice(-8)} (${providerName || "unknown"})`
1374
+ );
1375
+ return nodeToken;
1376
+ }
1377
+ }
1378
+ if (this.meshAgentToken) return this.meshAgentToken;
1379
+ const staticToken = process.env.LIOP_OAUTH_TOKEN || process.env.LIOP_TOKEN;
1380
+ if (staticToken) {
1381
+ this.meshAgentToken = staticToken;
1382
+ return this.meshAgentToken;
1383
+ }
1384
+ const nexusUrl = process.env.LIOP_NEXUS_URL;
1385
+ if (!nexusUrl) return void 0;
1386
+ const clientId = process.env.LIOP_OAUTH_CLIENT_ID || process.env.LIOP_CLIENT_ID || "liop-mesh-agent";
1387
+ const clientSecret = process.env.LIOP_OAUTH_CLIENT_SECRET || process.env.LIOP_CLIENT_SECRET || "dev-secret-change-me";
1388
+ const audience = process.env.LIOP_OAUTH_AUDIENCE || process.env.LIOP_AUDIENCE || "urn:liop:mesh:api";
1389
+ try {
1390
+ const baseUrl = nexusUrl.endsWith("/oidc") ? nexusUrl : `${nexusUrl}/oidc`;
1391
+ const tokenUrl = `${baseUrl}/token`;
1392
+ log.info(
1393
+ `[LIOP-Router] Proactively acquiring M2M token from Nexus: ${tokenUrl}`
1394
+ );
1395
+ const params = new URLSearchParams({
1396
+ grant_type: "client_credentials",
1397
+ scope: "liop:tools:call liop:tools:list liop:resources:read liop:schema:read liop:mesh:query",
1398
+ resource: audience,
1399
+ client_id: clientId,
1400
+ client_secret: clientSecret
1401
+ });
1402
+ const response = await fetch(tokenUrl, {
1403
+ method: "POST",
1404
+ headers: {
1405
+ "Content-Type": "application/x-www-form-urlencoded"
1406
+ },
1407
+ body: params.toString()
1408
+ });
1409
+ if (!response.ok) {
1410
+ const text = await response.text();
1411
+ log.warn(
1412
+ `[LIOP-Router] M2M Token acquisition failed: ${response.status} ${text}`
1413
+ );
1414
+ return void 0;
1415
+ }
1416
+ const data = await response.json();
1417
+ if (data.access_token) {
1418
+ this.meshAgentToken = data.access_token;
1419
+ log.info(
1420
+ "[LIOP-Router] M2M Token acquired successfully for router routing."
1421
+ );
1422
+ return this.meshAgentToken;
1423
+ }
1424
+ } catch (err) {
1425
+ log.warn(
1426
+ `[LIOP-Router] Failed to acquire M2M token: ${err instanceof Error ? err.message : String(err)}`
1427
+ );
1428
+ }
1429
+ return void 0;
1430
+ }
1431
+ async performTranscoding(id, client, toolName, params, peerId, token) {
1432
+ const capabilityHash = toolName;
1433
+ const proofOfIntent = this.meshNode ? await this.meshNode.sign(Buffer.from(capabilityHash)) : Buffer.from([]);
1434
+ const transcodingStartTime = Date.now();
1435
+ let activeToken = token;
1436
+ if (!activeToken) {
1437
+ activeToken = await this.getOrAcquireMeshAgentToken(peerId);
1438
+ }
1439
+ if (peerId) {
1440
+ const manifestEntry = this.manifestCache.get(peerId);
1441
+ if (manifestEntry?.manifest.authRequired && !activeToken) {
1442
+ const providerName = manifestEntry.manifest.serverInfo?.name?.toLowerCase() || "unknown";
1443
+ const slug = manifestEntry.manifest.tokenSlug;
1444
+ const shortId = peerId.slice(-8).toUpperCase();
1445
+ const primaryVar = slug ? `LIOP_TOKEN_${slug}` : `LIOP_TOKEN_${providerName.toUpperCase().replace(/[^A-Z0-9_]/g, "_")}`;
1446
+ return {
1447
+ jsonrpc: "2.0",
1448
+ id,
1449
+ result: {
1450
+ content: [
1451
+ {
1452
+ type: "text",
1453
+ text: `Authentication Required: The restricted node (${providerName}) requires an access token. Please define the ${primaryVar} or LIOP_TOKEN_${shortId} environment variable on your agent/client host.`
1454
+ }
1455
+ ],
1456
+ isError: true
1457
+ }
1458
+ };
1459
+ }
1460
+ }
1461
+ return new Promise((resolve) => {
1462
+ const metadata = new grpc.Metadata();
1463
+ if (activeToken) {
1464
+ metadata.add("authorization", `Bearer ${activeToken}`);
1465
+ }
1466
+ client.negotiateIntent(
1467
+ {
1468
+ agent_did: `did:liop:${this.meshNode?.getPeerId() || "mcp-proxy"}`,
1469
+ capability_hash: capabilityHash,
1470
+ proof_of_intent: proofOfIntent
1471
+ },
1472
+ metadata,
1473
+ async (err, response) => {
1474
+ if (err || !response.accepted) {
1475
+ return resolve({
1476
+ jsonrpc: "2.0",
1477
+ id,
1478
+ result: {
1479
+ content: [
1480
+ {
1481
+ type: "text",
1482
+ text: `PQC Handshake Failed: ${err?.message || "Rejected"}`
1483
+ }
1484
+ ],
1485
+ isError: true
1486
+ }
1487
+ });
1488
+ }
1489
+ const { ciphertext, sharedSecret } = await Kyber768Wrapper.encapsulateAsymmetric(
1490
+ response.kyber_public_key
1491
+ );
1492
+ const embeddedArgsJson = JSON.stringify(params.arguments || {});
1493
+ const proxyLogic = `return { "__liop_proxy_tool": "${toolName}", "__liop_proxy_args": ${embeddedArgsJson} };`;
1494
+ const nonce = crypto2.randomBytes(12);
1495
+ const sealedLogic = this.encryptWithNonce(
1496
+ Buffer.from(proxyLogic),
1497
+ sharedSecret,
1498
+ nonce
1499
+ );
1500
+ const metadataCall = new grpc.Metadata();
1501
+ if (activeToken) {
1502
+ metadataCall.add("authorization", `Bearer ${activeToken}`);
1503
+ }
1504
+ const call = client.executeLogic(
1505
+ {
1506
+ session_token: response.session_token,
1507
+ wasm_binary: new Uint8Array(sealedLogic),
1508
+ inputs: {},
1509
+ pqc_ciphertext: ciphertext,
1510
+ aes_nonce: nonce
1511
+ },
1512
+ metadataCall
1513
+ );
1514
+ let resultBody = "";
1515
+ let lastResponse = null;
1516
+ call.on("data", (grpcRes) => {
1517
+ resultBody += grpcRes.semantic_evidence;
1518
+ lastResponse = grpcRes;
1519
+ });
1520
+ call.on("end", async () => {
1521
+ try {
1522
+ if (lastResponse) {
1523
+ if (!lastResponse.is_error) {
1524
+ const proofHex = Buffer.from(
1525
+ lastResponse.cryptographic_proof
1526
+ ).toString("hex");
1527
+ const isValid = await this.verifier.verifyZkReceipt(
1528
+ Buffer.from(proxyLogic),
1529
+ proofHex,
1530
+ Buffer.from(lastResponse.zk_receipt),
1531
+ Buffer.from(sharedSecret),
1532
+ resultBody
1533
+ );
1534
+ if (!isValid) {
1535
+ return resolve({
1536
+ jsonrpc: "2.0",
1537
+ id,
1538
+ result: {
1539
+ content: [
1540
+ {
1541
+ type: "text",
1542
+ text: "SECURITY ALERT: Remote response failed cryptographic integrity audit."
1543
+ }
1544
+ ],
1545
+ isError: true
1546
+ }
1547
+ });
1548
+ }
1549
+ }
1550
+ }
1551
+ const parsedResult = JSON.parse(resultBody);
1552
+ const remoteTelemetry = TokenTelemetryEngine.getInstance();
1553
+ remoteTelemetry.record({
1554
+ type: "tool_call",
1555
+ method: "tools/call",
1556
+ toolName,
1557
+ peerId,
1558
+ estimatedInputTokens: remoteTelemetry.countTokens(embeddedArgsJson),
1559
+ estimatedOutputTokens: remoteTelemetry.countTokens(resultBody),
1560
+ durationMs: Date.now() - transcodingStartTime
1561
+ });
1562
+ resolve({ jsonrpc: "2.0", id, result: parsedResult });
1563
+ } catch (_e) {
1564
+ resolve({
1565
+ jsonrpc: "2.0",
1566
+ id,
1567
+ result: { content: [{ type: "text", text: resultBody }] }
1568
+ });
1569
+ }
1570
+ });
1571
+ call.on(
1572
+ "error",
1573
+ (e) => resolve({
1574
+ jsonrpc: "2.0",
1575
+ id,
1576
+ result: {
1577
+ content: [
1578
+ { type: "text", text: `LIOP gRPC Error: ${e.message}` }
1579
+ ],
1580
+ isError: true
1581
+ }
1582
+ })
1583
+ );
1584
+ }
1585
+ );
1586
+ });
1587
+ }
1588
+ encryptWithNonce(payload, key, nonce) {
1589
+ const cipher = crypto2.createCipheriv("aes-256-gcm", key, nonce);
1590
+ const encrypted = Buffer.concat([cipher.update(payload), cipher.final()]);
1591
+ return Buffer.concat([encrypted, cipher.getAuthTag()]);
1592
+ }
1593
+ };
1594
+
1595
+ export { HeuristicTokenEstimator, LiopMcpRouter, LiopOTelBridge, RealTokenEstimator, TokenTelemetryEngine, createSyncTokenEstimator, createTokenEstimator };
1596
+ //# sourceMappingURL=chunk-Y73XGYY7.js.map
1597
+ //# sourceMappingURL=chunk-Y73XGYY7.js.map