@nexum-hub/agent 0.0.0-stage → 0.0.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.
- package/README.md +133 -2
- package/contracts/v2/common.schema.json +262 -0
- package/contracts/v2/fixtures/inputs.json +244 -0
- package/contracts/v2/fixtures/wire.json +6493 -0
- package/contracts/v2/model/agent.close.input.schema.json +14 -0
- package/contracts/v2/model/agent.close.output.schema.json +101 -0
- package/contracts/v2/model/agent.list.input.schema.json +57 -0
- package/contracts/v2/model/agent.list.output.schema.json +220 -0
- package/contracts/v2/model/agent.open.input.schema.json +35 -0
- package/contracts/v2/model/agent.open.output.schema.json +41 -0
- package/contracts/v2/model/agent.run.cancel.input.schema.json +18 -0
- package/contracts/v2/model/agent.run.cancel.output.schema.json +100 -0
- package/contracts/v2/model/agent.run.get.input.schema.json +41 -0
- package/contracts/v2/model/agent.run.get.output.schema.json +100 -0
- package/contracts/v2/model/agent.run.input.input.schema.json +30 -0
- package/contracts/v2/model/agent.run.input.output.schema.json +100 -0
- package/contracts/v2/model/agent.run.list.input.schema.json +47 -0
- package/contracts/v2/model/agent.run.list.output.schema.json +41 -0
- package/contracts/v2/model/agent.run.start.input.schema.json +73 -0
- package/contracts/v2/model/agent.run.start.output.schema.json +100 -0
- package/contracts/v2/wire/bodies.schema.json +383 -0
- package/contracts/v2/wire/envelope.schema.json +2280 -0
- package/contracts/v2/wire/events.schema.json +242 -0
- package/contracts/v2/wire/maintenance.schema.json +308 -0
- package/contracts/v2/wire/operations.schema.json +1791 -0
- package/contracts/v2/wire/stream.schema.json +74 -0
- package/contracts/v2/wire/types.schema.json +2616 -0
- package/dist/adapters/mcp-http.d.ts +22 -0
- package/dist/adapters/mcp-http.js +472 -0
- package/dist/adapters/mcp-http.js.map +1 -0
- package/dist/adapters/mcp-oauth.d.ts +29 -0
- package/dist/adapters/mcp-oauth.js +653 -0
- package/dist/adapters/mcp-oauth.js.map +1 -0
- package/dist/adapters/mcp-wire.d.ts +21 -0
- package/dist/adapters/mcp-wire.js +307 -0
- package/dist/adapters/mcp-wire.js.map +1 -0
- package/dist/adapters/mcp.d.ts +56 -0
- package/dist/adapters/mcp.js +1989 -0
- package/dist/adapters/mcp.js.map +1 -0
- package/dist/adapters/openapi-http.d.ts +29 -0
- package/dist/adapters/openapi-http.js +504 -0
- package/dist/adapters/openapi-http.js.map +1 -0
- package/dist/adapters/openapi-request.d.ts +26 -0
- package/dist/adapters/openapi-request.js +597 -0
- package/dist/adapters/openapi-request.js.map +1 -0
- package/dist/adapters/openapi-schema.d.ts +22 -0
- package/dist/adapters/openapi-schema.js +471 -0
- package/dist/adapters/openapi-schema.js.map +1 -0
- package/dist/adapters/openapi-typegen.d.ts +11 -0
- package/dist/adapters/openapi-typegen.js +185 -0
- package/dist/adapters/openapi-typegen.js.map +1 -0
- package/dist/adapters/openapi-types.d.ts +99 -0
- package/dist/adapters/openapi-types.js +2 -0
- package/dist/adapters/openapi-types.js.map +1 -0
- package/dist/adapters/openapi.d.ts +7 -0
- package/dist/adapters/openapi.js +768 -0
- package/dist/adapters/openapi.js.map +1 -0
- package/dist/application/activation.d.ts +28 -0
- package/dist/application/activation.js +289 -0
- package/dist/application/activation.js.map +1 -0
- package/dist/application/attachments.d.ts +24 -0
- package/dist/application/attachments.js +87 -0
- package/dist/application/attachments.js.map +1 -0
- package/dist/application/authority.d.ts +32 -0
- package/dist/application/authority.js +213 -0
- package/dist/application/authority.js.map +1 -0
- package/dist/application/catalog.d.ts +11 -0
- package/dist/application/catalog.js +177 -0
- package/dist/application/catalog.js.map +1 -0
- package/dist/application/config.d.ts +24 -0
- package/dist/application/config.js +140 -0
- package/dist/application/config.js.map +1 -0
- package/dist/application/connection.d.ts +35 -0
- package/dist/application/connection.js +465 -0
- package/dist/application/connection.js.map +1 -0
- package/dist/application/data-plane.d.ts +35 -0
- package/dist/application/data-plane.js +437 -0
- package/dist/application/data-plane.js.map +1 -0
- package/dist/application/diagnostics.d.ts +4 -0
- package/dist/application/diagnostics.js +65 -0
- package/dist/application/diagnostics.js.map +1 -0
- package/dist/application/dispatcher.d.ts +4 -0
- package/dist/application/dispatcher.js +16 -0
- package/dist/application/dispatcher.js.map +1 -0
- package/dist/application/main.d.ts +12 -0
- package/dist/application/main.js +167 -0
- package/dist/application/main.js.map +1 -0
- package/dist/application/maintenance.d.ts +27 -0
- package/dist/application/maintenance.js +244 -0
- package/dist/application/maintenance.js.map +1 -0
- package/dist/application/packages.d.ts +25 -0
- package/dist/application/packages.js +208 -0
- package/dist/application/packages.js.map +1 -0
- package/dist/application/processes.d.ts +49 -0
- package/dist/application/processes.js +180 -0
- package/dist/application/processes.js.map +1 -0
- package/dist/application/run-view.d.ts +17 -0
- package/dist/application/run-view.js +286 -0
- package/dist/application/run-view.js.map +1 -0
- package/dist/application/runtime.d.ts +78 -0
- package/dist/application/runtime.js +1747 -0
- package/dist/application/runtime.js.map +1 -0
- package/dist/application/tasks.d.ts +38 -0
- package/dist/application/tasks.js +23 -0
- package/dist/application/tasks.js.map +1 -0
- package/dist/application/user-state.d.ts +20 -0
- package/dist/application/user-state.js +52 -0
- package/dist/application/user-state.js.map +1 -0
- package/dist/application/work-git.d.ts +14 -0
- package/dist/application/work-git.js +67 -0
- package/dist/application/work-git.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +40 -0
- package/dist/cli.js.map +1 -0
- package/dist/contracts/caller.d.ts +51 -0
- package/dist/contracts/caller.js +95 -0
- package/dist/contracts/caller.js.map +1 -0
- package/dist/contracts/error-data.d.ts +6 -0
- package/dist/contracts/error-data.js +51 -0
- package/dist/contracts/error-data.js.map +1 -0
- package/dist/contracts/errors.d.ts +10 -0
- package/dist/contracts/errors.js +28 -0
- package/dist/contracts/errors.js.map +1 -0
- package/dist/contracts/json.d.ts +9 -0
- package/dist/contracts/json.js +104 -0
- package/dist/contracts/json.js.map +1 -0
- package/dist/contracts/types.d.ts +122 -0
- package/dist/contracts/types.js +19 -0
- package/dist/contracts/types.js.map +1 -0
- package/dist/contracts/validation.d.ts +4 -0
- package/dist/contracts/validation.js +157 -0
- package/dist/contracts/validation.js.map +1 -0
- package/dist/contracts/wire-validation.d.ts +9 -0
- package/dist/contracts/wire-validation.js +399 -0
- package/dist/contracts/wire-validation.js.map +1 -0
- package/dist/contracts/wire.d.ts +1083 -0
- package/dist/contracts/wire.js +62 -0
- package/dist/contracts/wire.js.map +1 -0
- package/dist/extension-api.d.ts +12 -0
- package/dist/extension-api.js +2 -0
- package/dist/extension-api.js.map +1 -0
- package/dist/extensions/api.d.ts +72 -0
- package/dist/extensions/api.js +2 -0
- package/dist/extensions/api.js.map +1 -0
- package/dist/extensions/host-requests.d.ts +23 -0
- package/dist/extensions/host-requests.js +60 -0
- package/dist/extensions/host-requests.js.map +1 -0
- package/dist/extensions/materials.d.ts +37 -0
- package/dist/extensions/materials.js +691 -0
- package/dist/extensions/materials.js.map +1 -0
- package/dist/extensions/package-loader.d.ts +28 -0
- package/dist/extensions/package-loader.js +177 -0
- package/dist/extensions/package-loader.js.map +1 -0
- package/dist/extensions/packages.d.ts +73 -0
- package/dist/extensions/packages.js +441 -0
- package/dist/extensions/packages.js.map +1 -0
- package/dist/extensions/registry.d.ts +92 -0
- package/dist/extensions/registry.js +1540 -0
- package/dist/extensions/registry.js.map +1 -0
- package/dist/extensions/resources.d.ts +105 -0
- package/dist/extensions/resources.js +595 -0
- package/dist/extensions/resources.js.map +1 -0
- package/dist/extensions/state.d.ts +25 -0
- package/dist/extensions/state.js +10 -0
- package/dist/extensions/state.js.map +1 -0
- package/dist/extensions/transfer.d.ts +81 -0
- package/dist/extensions/transfer.js +16 -0
- package/dist/extensions/transfer.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/maintenance/configuration.d.ts +79 -0
- package/dist/maintenance/configuration.js +1503 -0
- package/dist/maintenance/configuration.js.map +1 -0
- package/dist/maintenance/credentials.d.ts +23 -0
- package/dist/maintenance/credentials.js +177 -0
- package/dist/maintenance/credentials.js.map +1 -0
- package/dist/maintenance/files.d.ts +2 -0
- package/dist/maintenance/files.js +40 -0
- package/dist/maintenance/files.js.map +1 -0
- package/dist/maintenance/npm.d.ts +71 -0
- package/dist/maintenance/npm.js +1013 -0
- package/dist/maintenance/npm.js.map +1 -0
- package/dist/maintenance/oauth.d.ts +45 -0
- package/dist/maintenance/oauth.js +686 -0
- package/dist/maintenance/oauth.js.map +1 -0
- package/dist/maintenance/requests.d.ts +347 -0
- package/dist/maintenance/requests.js +959 -0
- package/dist/maintenance/requests.js.map +1 -0
- package/dist/maintenance/work-git.d.ts +29 -0
- package/dist/maintenance/work-git.js +776 -0
- package/dist/maintenance/work-git.js.map +1 -0
- package/dist/process/backend.d.ts +54 -0
- package/dist/process/backend.js +39 -0
- package/dist/process/backend.js.map +1 -0
- package/dist/process/environment-windows.d.ts +15 -0
- package/dist/process/environment-windows.js +151 -0
- package/dist/process/environment-windows.js.map +1 -0
- package/dist/process/environment.d.ts +28 -0
- package/dist/process/environment.js +410 -0
- package/dist/process/environment.js.map +1 -0
- package/dist/process/launch.d.ts +31 -0
- package/dist/process/launch.js +281 -0
- package/dist/process/launch.js.map +1 -0
- package/dist/process/managed.d.ts +36 -0
- package/dist/process/managed.js +459 -0
- package/dist/process/managed.js.map +1 -0
- package/dist/process/output-log.d.ts +50 -0
- package/dist/process/output-log.js +552 -0
- package/dist/process/output-log.js.map +1 -0
- package/dist/process/pipes.d.ts +16 -0
- package/dist/process/pipes.js +588 -0
- package/dist/process/pipes.js.map +1 -0
- package/dist/process/pty.d.ts +22 -0
- package/dist/process/pty.js +512 -0
- package/dist/process/pty.js.map +1 -0
- package/dist/process/windows-conout-worker.d.ts +1 -0
- package/dist/process/windows-conout-worker.js +90 -0
- package/dist/process/windows-conout-worker.js.map +1 -0
- package/dist/process/windows-conout.d.ts +46 -0
- package/dist/process/windows-conout.js +140 -0
- package/dist/process/windows-conout.js.map +1 -0
- package/dist/process/windows-helper.d.ts +43 -0
- package/dist/process/windows-helper.js +255 -0
- package/dist/process/windows-helper.js.map +1 -0
- package/dist/process/windows-job.d.ts +39 -0
- package/dist/process/windows-job.js +224 -0
- package/dist/process/windows-job.js.map +1 -0
- package/dist/process/windows-pipes.d.ts +16 -0
- package/dist/process/windows-pipes.js +475 -0
- package/dist/process/windows-pipes.js.map +1 -0
- package/dist/results/artifacts.d.ts +118 -0
- package/dist/results/artifacts.js +873 -0
- package/dist/results/artifacts.js.map +1 -0
- package/dist/results/encoder.d.ts +57 -0
- package/dist/results/encoder.js +1179 -0
- package/dist/results/encoder.js.map +1 -0
- package/dist/results/mcp.d.ts +95 -0
- package/dist/results/mcp.js +656 -0
- package/dist/results/mcp.js.map +1 -0
- package/dist/results/media.d.ts +19 -0
- package/dist/results/media.js +301 -0
- package/dist/results/media.js.map +1 -0
- package/dist/results/normalize.d.ts +29 -0
- package/dist/results/normalize.js +464 -0
- package/dist/results/normalize.js.map +1 -0
- package/dist/runtime/control-scripts.d.ts +23 -0
- package/dist/runtime/control-scripts.js +242 -0
- package/dist/runtime/control-scripts.js.map +1 -0
- package/dist/runtime/engine.d.ts +177 -0
- package/dist/runtime/engine.js +1159 -0
- package/dist/runtime/engine.js.map +1 -0
- package/dist/runtime/globals.d.ts +1 -0
- package/dist/runtime/globals.js +31 -0
- package/dist/runtime/globals.js.map +1 -0
- package/dist/runtime/material-retention.d.ts +34 -0
- package/dist/runtime/material-retention.js +671 -0
- package/dist/runtime/material-retention.js.map +1 -0
- package/dist/runtime/modules.d.ts +50 -0
- package/dist/runtime/modules.js +320 -0
- package/dist/runtime/modules.js.map +1 -0
- package/dist/runtime/names.d.ts +2 -0
- package/dist/runtime/names.js +22 -0
- package/dist/runtime/names.js.map +1 -0
- package/dist/runtime/operations.d.ts +20 -0
- package/dist/runtime/operations.js +137 -0
- package/dist/runtime/operations.js.map +1 -0
- package/dist/runtime/paths.d.ts +3 -0
- package/dist/runtime/paths.js +38 -0
- package/dist/runtime/paths.js.map +1 -0
- package/dist/runtime/promises.d.ts +9 -0
- package/dist/runtime/promises.js +78 -0
- package/dist/runtime/promises.js.map +1 -0
- package/dist/runtime/retention.d.ts +88 -0
- package/dist/runtime/retention.js +735 -0
- package/dist/runtime/retention.js.map +1 -0
- package/dist/runtime/run-log.d.ts +30 -0
- package/dist/runtime/run-log.js +207 -0
- package/dist/runtime/run-log.js.map +1 -0
- package/dist/runtime/scripts.d.ts +84 -0
- package/dist/runtime/scripts.js +387 -0
- package/dist/runtime/scripts.js.map +1 -0
- package/dist/runtime/tasks.d.ts +54 -0
- package/dist/runtime/tasks.js +290 -0
- package/dist/runtime/tasks.js.map +1 -0
- package/dist/sdk/maintenance.d.ts +3 -0
- package/dist/sdk/maintenance.js +80 -0
- package/dist/sdk/maintenance.js.map +1 -0
- package/dist/sdk/output-extension.d.ts +3 -0
- package/dist/sdk/output-extension.js +44 -0
- package/dist/sdk/output-extension.js.map +1 -0
- package/dist/sdk/output.d.ts +52 -0
- package/dist/sdk/output.js +350 -0
- package/dist/sdk/output.js.map +1 -0
- package/dist/sdk/runtime.d.ts +3 -0
- package/dist/sdk/runtime.js +26 -0
- package/dist/sdk/runtime.js.map +1 -0
- package/dist/sdk/sh.d.ts +3 -0
- package/dist/sdk/sh.js +384 -0
- package/dist/sdk/sh.js.map +1 -0
- package/dist/sdk/state.d.ts +2 -0
- package/dist/sdk/state.js +69 -0
- package/dist/sdk/state.js.map +1 -0
- package/dist/sdk/transfer.d.ts +3 -0
- package/dist/sdk/transfer.js +141 -0
- package/dist/sdk/transfer.js.map +1 -0
- package/dist/services/extensions.d.ts +19 -0
- package/dist/services/extensions.js +183 -0
- package/dist/services/extensions.js.map +1 -0
- package/dist/services/state.d.ts +37 -0
- package/dist/services/state.js +125 -0
- package/dist/services/state.js.map +1 -0
- package/dist/services/transfer.d.ts +86 -0
- package/dist/services/transfer.js +948 -0
- package/dist/services/transfer.js.map +1 -0
- package/dist/storage/schema.d.ts +8 -0
- package/dist/storage/schema.js +241 -0
- package/dist/storage/schema.js.map +1 -0
- package/dist/storage/sqlite.d.ts +241 -0
- package/dist/storage/sqlite.js +1855 -0
- package/dist/storage/sqlite.js.map +1 -0
- package/dist/storage/transfers.d.ts +90 -0
- package/dist/storage/transfers.js +2 -0
- package/dist/storage/transfers.js.map +1 -0
- package/dist/transfer/io.d.ts +74 -0
- package/dist/transfer/io.js +425 -0
- package/dist/transfer/io.js.map +1 -0
- package/dist/transfer/offers.d.ts +72 -0
- package/dist/transfer/offers.js +380 -0
- package/dist/transfer/offers.js.map +1 -0
- package/dist/transfer/stream-slots.d.ts +12 -0
- package/dist/transfer/stream-slots.js +28 -0
- package/dist/transfer/stream-slots.js.map +1 -0
- package/dist/work/catalog.d.ts +43 -0
- package/dist/work/catalog.js +198 -0
- package/dist/work/catalog.js.map +1 -0
- package/dist/work/discovery.d.ts +70 -0
- package/dist/work/discovery.js +465 -0
- package/dist/work/discovery.js.map +1 -0
- package/dist/work/service.d.ts +83 -0
- package/dist/work/service.js +703 -0
- package/dist/work/service.js.map +1 -0
- package/package.json +66 -4
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { inspect, types, MIMEType } from "node:util";
|
|
3
|
+
import { AgentError, requireCondition } from "../contracts/errors.js";
|
|
4
|
+
import { authorizeCall } from "../contracts/caller.js";
|
|
5
|
+
import { canonicalJson, isPlainRecord } from "../contracts/json.js";
|
|
6
|
+
const issued = new WeakMap();
|
|
7
|
+
const methods = new Set([
|
|
8
|
+
"tools/call",
|
|
9
|
+
"tools/list",
|
|
10
|
+
"resources/list",
|
|
11
|
+
"resources/templates/list",
|
|
12
|
+
"prompts/list",
|
|
13
|
+
"prompts/get",
|
|
14
|
+
]);
|
|
15
|
+
const contentTypes = new Set([
|
|
16
|
+
"text",
|
|
17
|
+
"image",
|
|
18
|
+
"audio",
|
|
19
|
+
"resource_link",
|
|
20
|
+
"resource",
|
|
21
|
+
]);
|
|
22
|
+
const maximumProtocolBytes = 128 * 1024 * 1024;
|
|
23
|
+
function text(value, maximum = 1024) {
|
|
24
|
+
return (typeof value === "string" &&
|
|
25
|
+
value.length > 0 &&
|
|
26
|
+
Buffer.byteLength(value) <= maximum &&
|
|
27
|
+
!/[\0\r\n]/.test(value));
|
|
28
|
+
}
|
|
29
|
+
function record(value) {
|
|
30
|
+
requireCondition(isPlainRecord(value), "invalid_mcp_result", "MCP protocol data must be an ordinary data object.");
|
|
31
|
+
const result = Object.create(null);
|
|
32
|
+
const keys = Reflect.ownKeys(value);
|
|
33
|
+
requireCondition(keys.length <= 100000, "mcp_result_too_large", "MCP protocol data exceeds its field budget.");
|
|
34
|
+
for (const key of keys) {
|
|
35
|
+
const field = Object.getOwnPropertyDescriptor(value, key);
|
|
36
|
+
if (!field.enumerable)
|
|
37
|
+
continue;
|
|
38
|
+
requireCondition(typeof key === "string" && "value" in field, "invalid_mcp_result", "MCP protocol fields cannot contain accessors or enumerable Symbols.");
|
|
39
|
+
result[key] = field.value;
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
function items(value) {
|
|
44
|
+
requireCondition(!types.isProxy(value) && Array.isArray(value) && value.length <= 100000, "invalid_mcp_result", "MCP protocol collections require bounded ordinary arrays.");
|
|
45
|
+
const result = [];
|
|
46
|
+
for (const key of Object.getOwnPropertySymbols(value))
|
|
47
|
+
requireCondition(!Object.getOwnPropertyDescriptor(value, key)?.enumerable, "invalid_mcp_result", "MCP arrays cannot contain enumerable Symbols.");
|
|
48
|
+
for (let index = 0; index < value.length; index++) {
|
|
49
|
+
const field = Object.getOwnPropertyDescriptor(value, String(index));
|
|
50
|
+
requireCondition(field && "value" in field, "invalid_mcp_result", "MCP arrays cannot contain holes or accessors.");
|
|
51
|
+
result.push(field.value);
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
/** JSON-only copy with encoded-size accounting before escaping or serialization. */
|
|
56
|
+
export function copyMcpJSON(value, maxBytes = maximumProtocolBytes) {
|
|
57
|
+
requireCondition(Number.isSafeInteger(maxBytes) &&
|
|
58
|
+
maxBytes > 0 &&
|
|
59
|
+
maxBytes <= 512 * 1024 * 1024, "invalid_mcp_budget", "MCP JSON requires a bounded byte budget.");
|
|
60
|
+
let bytes = 0, nodes = 0;
|
|
61
|
+
const ancestors = new Set();
|
|
62
|
+
const charge = (amount) => {
|
|
63
|
+
bytes += amount;
|
|
64
|
+
requireCondition(bytes <= maxBytes, "mcp_result_too_large", "MCP JSON exceeds its encoded byte budget.");
|
|
65
|
+
};
|
|
66
|
+
const string = (value) => {
|
|
67
|
+
charge(2);
|
|
68
|
+
for (let index = 0; index < value.length; index++) {
|
|
69
|
+
const code = value.charCodeAt(index);
|
|
70
|
+
if (code === 34 || code === 92)
|
|
71
|
+
charge(2);
|
|
72
|
+
else if (code < 32)
|
|
73
|
+
charge([8, 9, 10, 12, 13].includes(code) ? 2 : 6);
|
|
74
|
+
else if (code < 128)
|
|
75
|
+
charge(1);
|
|
76
|
+
else if (code < 2048)
|
|
77
|
+
charge(2);
|
|
78
|
+
else if (code >= 0xd800 && code <= 0xdbff) {
|
|
79
|
+
const next = value.charCodeAt(index + 1);
|
|
80
|
+
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
81
|
+
charge(4);
|
|
82
|
+
index++;
|
|
83
|
+
}
|
|
84
|
+
else
|
|
85
|
+
charge(6);
|
|
86
|
+
}
|
|
87
|
+
else
|
|
88
|
+
charge(code >= 0xdc00 && code <= 0xdfff ? 6 : 3);
|
|
89
|
+
}
|
|
90
|
+
return value;
|
|
91
|
+
};
|
|
92
|
+
function visit(input, depth) {
|
|
93
|
+
requireCondition(++nodes <= 100000 && depth <= 64, "mcp_result_too_large", "MCP JSON exceeds its node or depth budget.");
|
|
94
|
+
if (input === null) {
|
|
95
|
+
charge(4);
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
if (typeof input === "string")
|
|
99
|
+
return string(input);
|
|
100
|
+
if (typeof input === "boolean") {
|
|
101
|
+
charge(input ? 4 : 5);
|
|
102
|
+
return input;
|
|
103
|
+
}
|
|
104
|
+
if (typeof input === "number" && Number.isFinite(input)) {
|
|
105
|
+
charge(String(input).length);
|
|
106
|
+
return input;
|
|
107
|
+
}
|
|
108
|
+
requireCondition(input &&
|
|
109
|
+
typeof input === "object" &&
|
|
110
|
+
!types.isProxy(input) &&
|
|
111
|
+
!ancestors.has(input), "invalid_mcp_result", "MCP protocol values must be acyclic JSON data.");
|
|
112
|
+
ancestors.add(input);
|
|
113
|
+
try {
|
|
114
|
+
if (Array.isArray(input)) {
|
|
115
|
+
requireCondition(input.length <= 100000 - nodes, "mcp_result_too_large", "MCP JSON exceeds its remaining item budget.");
|
|
116
|
+
charge(2 + Math.max(0, input.length - 1));
|
|
117
|
+
return items(input).map((item) => visit(item, depth + 1));
|
|
118
|
+
}
|
|
119
|
+
const source = record(input), keys = Object.keys(source), result = Object.create(null);
|
|
120
|
+
requireCondition(keys.length <= 100000 - nodes, "mcp_result_too_large", "MCP JSON exceeds its remaining field budget.");
|
|
121
|
+
charge(2 + Math.max(0, keys.length - 1));
|
|
122
|
+
for (const key of keys) {
|
|
123
|
+
string(key);
|
|
124
|
+
charge(1);
|
|
125
|
+
result[key] = visit(source[key], depth + 1);
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
ancestors.delete(input);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return visit(value, 0);
|
|
134
|
+
}
|
|
135
|
+
function frozen(value) {
|
|
136
|
+
if (value && typeof value === "object") {
|
|
137
|
+
for (const child of Object.values(value))
|
|
138
|
+
frozen(child);
|
|
139
|
+
Object.freeze(value);
|
|
140
|
+
}
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
function absoluteUri(value) {
|
|
144
|
+
requireCondition(text(value, 65536), "invalid_mcp_uri", "An MCP resource must have a bounded absolute URI.");
|
|
145
|
+
try {
|
|
146
|
+
new URL(value);
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
throw new AgentError("invalid_mcp_uri", "An MCP resource URI is invalid.");
|
|
150
|
+
}
|
|
151
|
+
return value;
|
|
152
|
+
}
|
|
153
|
+
export function mcpSource(value) {
|
|
154
|
+
const data = copyMcpJSON(value, 16384);
|
|
155
|
+
requireCondition(isPlainRecord(data) &&
|
|
156
|
+
Object.keys(data).every((key) => [
|
|
157
|
+
"agentId",
|
|
158
|
+
"profileRoot",
|
|
159
|
+
"extensionId",
|
|
160
|
+
"origin",
|
|
161
|
+
"connectionResourceId",
|
|
162
|
+
"connection",
|
|
163
|
+
].includes(key)) &&
|
|
164
|
+
text(data.agentId, 256) &&
|
|
165
|
+
typeof data.profileRoot === "string" &&
|
|
166
|
+
path.isAbsolute(data.profileRoot) &&
|
|
167
|
+
path.resolve(data.profileRoot) === data.profileRoot &&
|
|
168
|
+
text(data.extensionId, 256) &&
|
|
169
|
+
text(data.connection, 256) &&
|
|
170
|
+
text(data.connectionResourceId, 128) &&
|
|
171
|
+
/^res_[0-9a-f]{32}$/.test(data.connectionResourceId) &&
|
|
172
|
+
isPlainRecord(data.origin), "invalid_mcp_source", "MCP results require the actual registered extension and connection identity.");
|
|
173
|
+
const origin = data.origin;
|
|
174
|
+
requireCondition(["platform", "extension"].includes(origin.type) &&
|
|
175
|
+
text(origin.owner, 1024) &&
|
|
176
|
+
text(origin.namespace, 256) &&
|
|
177
|
+
text(origin.version, 256) &&
|
|
178
|
+
text(origin.revision, 1024), "invalid_mcp_source", "MCP source revision and namespace must come from the registered extension.");
|
|
179
|
+
return frozen(data);
|
|
180
|
+
}
|
|
181
|
+
export function bindMcpSource(binding, options) {
|
|
182
|
+
requireCondition(binding, "mcp_source_unavailable", "Only a registered invocation can bind an MCP result source.");
|
|
183
|
+
const fields = record(options);
|
|
184
|
+
requireCondition(Object.keys(fields).every((key) => ["connectionResourceId", "connection", "method"].includes(key)), "invalid_mcp_source", "MCP result options contain unsupported fields.");
|
|
185
|
+
return mcpSource({
|
|
186
|
+
...binding,
|
|
187
|
+
connection: fields.connection,
|
|
188
|
+
connectionResourceId: fields.connectionResourceId,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
function project(raw, method) {
|
|
192
|
+
const source = copyMcpJSON(raw);
|
|
193
|
+
requireCondition(isPlainRecord(source), "invalid_mcp_result", "An MCP result must be an object.");
|
|
194
|
+
const meta = [];
|
|
195
|
+
const removeMeta = (value, target, extra = {}) => {
|
|
196
|
+
if (Object.hasOwn(value, "_meta")) {
|
|
197
|
+
requireCondition(isPlainRecord(value._meta), "invalid_mcp_meta", "Protocol _meta must be a JSON object.");
|
|
198
|
+
meta.push({ target, meta: value._meta, ...extra });
|
|
199
|
+
delete value._meta;
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
removeMeta(source, "result");
|
|
203
|
+
if (source.resultType !== undefined)
|
|
204
|
+
requireCondition(source.resultType === "complete", "unsupported_mcp_result_type", "This result bridge cannot complete a remote continuation, input request or Task.");
|
|
205
|
+
const content = (value, index) => {
|
|
206
|
+
requireCondition(isPlainRecord(value) &&
|
|
207
|
+
typeof value.type === "string" &&
|
|
208
|
+
contentTypes.has(value.type), "invalid_mcp_content", "MCP content must use one of the five standard content kinds.");
|
|
209
|
+
const block = value;
|
|
210
|
+
removeMeta(block, "content", { index });
|
|
211
|
+
if (block.annotations !== undefined) {
|
|
212
|
+
requireCondition(isPlainRecord(block.annotations), "invalid_mcp_content", "MCP annotations must be a data object.");
|
|
213
|
+
const annotation = block.annotations;
|
|
214
|
+
requireCondition((annotation.audience === undefined ||
|
|
215
|
+
(Array.isArray(annotation.audience) &&
|
|
216
|
+
annotation.audience.every((role) => role === "user" || role === "assistant"))) &&
|
|
217
|
+
(annotation.priority === undefined ||
|
|
218
|
+
(typeof annotation.priority === "number" &&
|
|
219
|
+
annotation.priority >= 0 &&
|
|
220
|
+
annotation.priority <= 1)) &&
|
|
221
|
+
(annotation.lastModified === undefined ||
|
|
222
|
+
typeof annotation.lastModified === "string"), "invalid_mcp_content", "MCP content annotations are invalid.");
|
|
223
|
+
}
|
|
224
|
+
if (block.type === "text")
|
|
225
|
+
requireCondition(typeof block.text === "string", "invalid_mcp_content", "MCP text content needs text.");
|
|
226
|
+
else if (block.type === "image" || block.type === "audio") {
|
|
227
|
+
requireCondition(typeof block.data === "string" && text(block.mimeType, 256), "invalid_mcp_content", "MCP media needs encoded bytes and a MIME type.");
|
|
228
|
+
try {
|
|
229
|
+
new MIMEType(block.mimeType);
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
throw new AgentError("invalid_mcp_content", "The MCP media MIME type is invalid.");
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
else if (block.type === "resource_link") {
|
|
236
|
+
absoluteUri(block.uri);
|
|
237
|
+
if (block.mimeType !== undefined) {
|
|
238
|
+
requireCondition(text(block.mimeType, 256), "invalid_mcp_content", "An MCP resource MIME type must be bounded text.");
|
|
239
|
+
try {
|
|
240
|
+
new MIMEType(block.mimeType);
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
throw new AgentError("invalid_mcp_content", "An MCP resource MIME type is invalid.");
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
requireCondition(block.size === undefined ||
|
|
247
|
+
(typeof block.size === "number" &&
|
|
248
|
+
Number.isSafeInteger(block.size) &&
|
|
249
|
+
block.size >= 0), "invalid_mcp_content", "A resource link size must be a nonnegative byte count.");
|
|
250
|
+
requireCondition(text(block.name, 1024), "invalid_mcp_content", "MCP resource links need a name.");
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
requireCondition(isPlainRecord(block.resource), "invalid_mcp_content", "An embedded MCP resource needs its contents.");
|
|
254
|
+
resource(block.resource, index);
|
|
255
|
+
}
|
|
256
|
+
return block;
|
|
257
|
+
};
|
|
258
|
+
const resource = (value, index) => {
|
|
259
|
+
absoluteUri(value.uri);
|
|
260
|
+
removeMeta(value, "resource", { index });
|
|
261
|
+
if (value.mimeType !== undefined) {
|
|
262
|
+
requireCondition(text(value.mimeType, 256), "invalid_mcp_resource", "An embedded resource MIME type must be bounded text.");
|
|
263
|
+
try {
|
|
264
|
+
new MIMEType(value.mimeType);
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
throw new AgentError("invalid_mcp_resource", "An embedded resource MIME type is invalid.");
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
requireCondition(Number(Object.hasOwn(value, "text")) +
|
|
271
|
+
Number(Object.hasOwn(value, "blob")) ===
|
|
272
|
+
1 &&
|
|
273
|
+
(Object.hasOwn(value, "text")
|
|
274
|
+
? typeof value.text === "string"
|
|
275
|
+
: typeof value.blob === "string"), "invalid_mcp_resource", "MCP resources must contain exactly one actual text or encoded binary body.");
|
|
276
|
+
};
|
|
277
|
+
if (method === "resources/read") {
|
|
278
|
+
requireCondition(Array.isArray(source.contents), "invalid_mcp_resource", "MCP resources/read must return its actual contents array.");
|
|
279
|
+
source.contents.forEach((value, index) => {
|
|
280
|
+
requireCondition(isPlainRecord(value), "invalid_mcp_resource", "An MCP resource content item is invalid.");
|
|
281
|
+
resource(value, index);
|
|
282
|
+
});
|
|
283
|
+
return { model: source, meta };
|
|
284
|
+
}
|
|
285
|
+
if (method === "tools/call") {
|
|
286
|
+
requireCondition(Array.isArray(source.content) &&
|
|
287
|
+
(source.isError === undefined || typeof source.isError === "boolean"), "invalid_mcp_result", "A tool result must contain standard content and an optional business-error flag.");
|
|
288
|
+
source.content = source.content.map(content);
|
|
289
|
+
return { model: source, meta };
|
|
290
|
+
}
|
|
291
|
+
if (method === "prompts/get") {
|
|
292
|
+
requireCondition(Array.isArray(source.messages), "invalid_mcp_result", "MCP prompts/get must return its messages.");
|
|
293
|
+
const blocks = [];
|
|
294
|
+
const messages = source.messages.map((value, index) => {
|
|
295
|
+
requireCondition(isPlainRecord(value) &&
|
|
296
|
+
["user", "assistant"].includes(value.role), "invalid_mcp_result", "Prompt message roles must remain user or assistant.");
|
|
297
|
+
const message = value;
|
|
298
|
+
removeMeta(message, "message", { index });
|
|
299
|
+
blocks.push(content(message.content, index));
|
|
300
|
+
return { role: message.role, contentIndex: index };
|
|
301
|
+
});
|
|
302
|
+
const model = {
|
|
303
|
+
content: blocks,
|
|
304
|
+
structuredContent: {
|
|
305
|
+
...(source.description === undefined
|
|
306
|
+
? {}
|
|
307
|
+
: { description: source.description }),
|
|
308
|
+
messages,
|
|
309
|
+
},
|
|
310
|
+
...(source.resultType === undefined
|
|
311
|
+
? {}
|
|
312
|
+
: { resultType: source.resultType }),
|
|
313
|
+
};
|
|
314
|
+
return { model, meta };
|
|
315
|
+
}
|
|
316
|
+
const collection = {
|
|
317
|
+
"tools/list": "tools",
|
|
318
|
+
"resources/list": "resources",
|
|
319
|
+
"resources/templates/list": "resourceTemplates",
|
|
320
|
+
"prompts/list": "prompts",
|
|
321
|
+
}[method];
|
|
322
|
+
requireCondition(collection && Array.isArray(source[collection]), "invalid_mcp_result", "The MCP catalog response does not match its method.");
|
|
323
|
+
source[collection].forEach((value, index) => {
|
|
324
|
+
requireCondition(isPlainRecord(value), "invalid_mcp_result", "An MCP catalog entry must be an object.");
|
|
325
|
+
removeMeta(value, "entry", { collection, index });
|
|
326
|
+
});
|
|
327
|
+
return { model: { content: [], structuredContent: source }, meta };
|
|
328
|
+
}
|
|
329
|
+
/** Explicit finite protocol positions only; Schema/business `_meta` fields remain data. */
|
|
330
|
+
export function projectMcpProtocolResult(raw, method = "tools/call") {
|
|
331
|
+
requireCondition(methods.has(method), "invalid_mcp_method", "The MCP result projection method is unsupported.");
|
|
332
|
+
return frozen(project(raw, method).model);
|
|
333
|
+
}
|
|
334
|
+
function view(info) {
|
|
335
|
+
const value = copyMcpJSON(info.model);
|
|
336
|
+
for (const slot of info.meta) {
|
|
337
|
+
let target;
|
|
338
|
+
if (slot.target === "result")
|
|
339
|
+
target = value;
|
|
340
|
+
else if (slot.target === "content")
|
|
341
|
+
target = value.content?.[slot.index];
|
|
342
|
+
else if (slot.target === "resource") {
|
|
343
|
+
target =
|
|
344
|
+
info.kind === "resources"
|
|
345
|
+
? value.contents[slot.index]
|
|
346
|
+
: value.content[slot.index]
|
|
347
|
+
?.resource;
|
|
348
|
+
}
|
|
349
|
+
else if (slot.target === "message")
|
|
350
|
+
target = value.structuredContent?.messages?.[slot.index];
|
|
351
|
+
else
|
|
352
|
+
target = value.structuredContent?.[slot.collection]?.[slot.index];
|
|
353
|
+
if (target)
|
|
354
|
+
Object.defineProperty(target, "_meta", {
|
|
355
|
+
value: slot.meta,
|
|
356
|
+
enumerable: false,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
Object.defineProperties(value, {
|
|
360
|
+
toJSON: { value: () => mcpLogSnapshot(value) },
|
|
361
|
+
[inspect.custom]: { value: () => mcpLogSnapshot(value) },
|
|
362
|
+
});
|
|
363
|
+
issued.set(value, info);
|
|
364
|
+
return frozen(value);
|
|
365
|
+
}
|
|
366
|
+
export function wrapMcpResult(raw, source, method = "tools/call") {
|
|
367
|
+
requireCondition(methods.has(method), "invalid_mcp_method", "The MCP result method is unsupported.");
|
|
368
|
+
const known = mcpResult(raw);
|
|
369
|
+
if (known) {
|
|
370
|
+
requireCondition(known.kind === "tool" &&
|
|
371
|
+
known.method === method &&
|
|
372
|
+
canonicalJson(known.source, 16384) ===
|
|
373
|
+
canonicalJson(mcpSource(source), 16384), "mcp_source_mismatch", "A branded protocol result cannot be rebound to a different connection or method.");
|
|
374
|
+
return rebuildMcpResult(raw, known.model);
|
|
375
|
+
}
|
|
376
|
+
const projected = project(raw, method);
|
|
377
|
+
return view(Object.freeze({
|
|
378
|
+
kind: "tool",
|
|
379
|
+
method,
|
|
380
|
+
source: mcpSource(source),
|
|
381
|
+
model: frozen(projected.model),
|
|
382
|
+
meta: frozen(projected.meta),
|
|
383
|
+
}));
|
|
384
|
+
}
|
|
385
|
+
export function wrapMcpResourceResult(raw, source) {
|
|
386
|
+
const known = mcpResult(raw);
|
|
387
|
+
if (known) {
|
|
388
|
+
requireCondition(known.kind === "resources" &&
|
|
389
|
+
canonicalJson(known.source, 16384) ===
|
|
390
|
+
canonicalJson(mcpSource(source), 16384), "mcp_source_mismatch", "A branded resource result cannot be rebound to a different connection.");
|
|
391
|
+
return rebuildMcpResult(raw, known.model);
|
|
392
|
+
}
|
|
393
|
+
const projected = project(raw, "resources/read");
|
|
394
|
+
return view(Object.freeze({
|
|
395
|
+
kind: "resources",
|
|
396
|
+
method: "resources/read",
|
|
397
|
+
source: mcpSource(source),
|
|
398
|
+
model: frozen(projected.model),
|
|
399
|
+
meta: frozen(projected.meta),
|
|
400
|
+
}));
|
|
401
|
+
}
|
|
402
|
+
export function mcpResult(value) {
|
|
403
|
+
return value && typeof value === "object"
|
|
404
|
+
? (issued.get(value) ?? null)
|
|
405
|
+
: null;
|
|
406
|
+
}
|
|
407
|
+
function same(left, right) {
|
|
408
|
+
if (left === right)
|
|
409
|
+
return true;
|
|
410
|
+
if (!left ||
|
|
411
|
+
!right ||
|
|
412
|
+
typeof left !== "object" ||
|
|
413
|
+
typeof right !== "object" ||
|
|
414
|
+
Array.isArray(left) !== Array.isArray(right))
|
|
415
|
+
return false;
|
|
416
|
+
const a = Object.keys(left), b = Object.keys(right);
|
|
417
|
+
return (a.length === b.length &&
|
|
418
|
+
a.every((key) => Object.hasOwn(right, key) &&
|
|
419
|
+
same(left[key], right[key])));
|
|
420
|
+
}
|
|
421
|
+
export function rebuildMcpResult(original, copiedModel) {
|
|
422
|
+
const known = mcpResult(original);
|
|
423
|
+
requireCondition(known, "invalid_mcp_result", "Only an issued MCP result can retain its source brand.");
|
|
424
|
+
const model = copyMcpJSON(copiedModel);
|
|
425
|
+
requireCondition(same(known.model, model), "invalid_mcp_result", "An MCP result cannot be rebound to different protocol data.");
|
|
426
|
+
return view(Object.freeze({ ...known, model: frozen(model) }));
|
|
427
|
+
}
|
|
428
|
+
export function mcpLogSnapshot(value) {
|
|
429
|
+
const known = mcpResult(value);
|
|
430
|
+
if (!known)
|
|
431
|
+
return null;
|
|
432
|
+
const content = known.kind === "tool" ? known.model.content : [];
|
|
433
|
+
return {
|
|
434
|
+
mcp: {
|
|
435
|
+
connection: known.source.connection,
|
|
436
|
+
method: known.method,
|
|
437
|
+
revision: known.source.origin.revision,
|
|
438
|
+
},
|
|
439
|
+
contentTypes: known.kind === "tool"
|
|
440
|
+
? [...new Set(content.map((item) => item.type))]
|
|
441
|
+
: ["resource"],
|
|
442
|
+
contentCount: known.kind === "tool"
|
|
443
|
+
? content.length
|
|
444
|
+
: known.model.contents.length,
|
|
445
|
+
...(known.model.isError === undefined
|
|
446
|
+
? {}
|
|
447
|
+
: { isError: known.model.isError }),
|
|
448
|
+
hasStructuredContent: Object.hasOwn(known.model, "structuredContent"),
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
export class McpResultResources {
|
|
452
|
+
agentId;
|
|
453
|
+
profileRoot;
|
|
454
|
+
#resolve;
|
|
455
|
+
constructor(options) {
|
|
456
|
+
requireCondition(text(options.agentId, 256) &&
|
|
457
|
+
typeof options.profileRoot === "string" &&
|
|
458
|
+
path.isAbsolute(options.profileRoot) &&
|
|
459
|
+
typeof options.resolveResource === "function", "invalid_mcp_source", "MCP resource routing requires this actual Agent/profile resolver.");
|
|
460
|
+
this.agentId = options.agentId;
|
|
461
|
+
this.profileRoot = path.resolve(options.profileRoot);
|
|
462
|
+
this.#resolve = options.resolveResource;
|
|
463
|
+
}
|
|
464
|
+
uri(value, originalUri) {
|
|
465
|
+
const source = this.#source(value);
|
|
466
|
+
absoluteUri(originalUri);
|
|
467
|
+
const { profileRoot: _root, agentId: _agent, ...identity } = source;
|
|
468
|
+
return `nexum://agents/${encodeURIComponent(this.agentId)}/mcp/${Buffer.from(canonicalJson(identity, 16384)).toString("base64url")}/resources/${Buffer.from(originalUri).toString("base64url")}`;
|
|
469
|
+
}
|
|
470
|
+
#source(value) {
|
|
471
|
+
const source = mcpSource(value);
|
|
472
|
+
requireCondition(source.agentId === this.agentId &&
|
|
473
|
+
source.profileRoot === this.profileRoot, "mcp_profile_mismatch", "The remote source belongs to another Agent/profile.");
|
|
474
|
+
return source;
|
|
475
|
+
}
|
|
476
|
+
#route(uri) {
|
|
477
|
+
requireCondition(typeof uri === "string" && uri.length <= 131072, "invalid_mcp_uri", "The MCP proxy resource URI exceeds its budget.");
|
|
478
|
+
const match = /^nexum:\/\/agents\/([^/]+)\/mcp\/([A-Za-z0-9_-]+)\/resources\/([A-Za-z0-9_-]+)$/.exec(uri);
|
|
479
|
+
requireCondition(match, "invalid_mcp_uri", "The resource URI does not identify an MCP connection resource.");
|
|
480
|
+
try {
|
|
481
|
+
requireCondition(decodeURIComponent(match[1]) === this.agentId &&
|
|
482
|
+
match[2].length <= 24000, "mcp_profile_mismatch", "The MCP resource targets another Agent or invalid source.");
|
|
483
|
+
const payload = Buffer.from(match[2], "base64url"), raw = Buffer.from(match[3], "base64url");
|
|
484
|
+
requireCondition(payload.toString("base64url") === match[2] &&
|
|
485
|
+
raw.toString("base64url") === match[3], "invalid_mcp_uri", "The resource route is not canonically encoded.");
|
|
486
|
+
const source = this.#source({
|
|
487
|
+
...JSON.parse(payload.toString("utf8")),
|
|
488
|
+
agentId: this.agentId,
|
|
489
|
+
profileRoot: this.profileRoot,
|
|
490
|
+
});
|
|
491
|
+
const originalUri = new TextDecoder("utf-8", { fatal: true }).decode(raw);
|
|
492
|
+
absoluteUri(originalUri);
|
|
493
|
+
return { source, uri: originalUri };
|
|
494
|
+
}
|
|
495
|
+
catch (cause) {
|
|
496
|
+
if (cause instanceof AgentError)
|
|
497
|
+
throw cause;
|
|
498
|
+
throw new AgentError("invalid_mcp_uri", "The MCP resource route is invalid.");
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
authority(scope, source) {
|
|
502
|
+
source = this.#source(source);
|
|
503
|
+
requireCondition(text(scope.ownerKey, 256) &&
|
|
504
|
+
text(scope.grantId, 256) &&
|
|
505
|
+
typeof scope.authorizeTarget === "function", "mcp_authorization_unavailable", "Current verified Grant and connection authorization are required.");
|
|
506
|
+
return {
|
|
507
|
+
ownerKey: scope.ownerKey,
|
|
508
|
+
grantId: scope.grantId,
|
|
509
|
+
signal: scope.signal,
|
|
510
|
+
assertCurrent: () => {
|
|
511
|
+
authorizeCall(() => scope.assertCurrent());
|
|
512
|
+
scope.signal?.throwIfAborted();
|
|
513
|
+
},
|
|
514
|
+
authorizeConnection: (connection, uri, access = "resources.read") => {
|
|
515
|
+
authorizeCall(() => scope.assertCurrent());
|
|
516
|
+
scope.signal?.throwIfAborted();
|
|
517
|
+
requireCondition(connection === source.connection, "mcp_source_mismatch", "The connection cannot be rebound during resource access.");
|
|
518
|
+
authorizeCall(() => scope.authorizeTarget({
|
|
519
|
+
type: "connection",
|
|
520
|
+
value: connection,
|
|
521
|
+
access,
|
|
522
|
+
}));
|
|
523
|
+
if (uri !== undefined) {
|
|
524
|
+
absoluteUri(uri);
|
|
525
|
+
authorizeCall(() => scope.authorizeTarget({
|
|
526
|
+
type: "resource",
|
|
527
|
+
value: uri,
|
|
528
|
+
access: "read",
|
|
529
|
+
}));
|
|
530
|
+
}
|
|
531
|
+
},
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
async resolve(value, scope, access = "result", uri) {
|
|
535
|
+
const source = this.#source(value), authority = this.authority(scope, source);
|
|
536
|
+
authority.assertCurrent();
|
|
537
|
+
authority.authorizeConnection(source.connection, uri, access);
|
|
538
|
+
const reader = await this.#resolve(source, authority);
|
|
539
|
+
requireCondition(reader &&
|
|
540
|
+
reader.connection === source.connection &&
|
|
541
|
+
typeof reader.assertCurrent === "function" &&
|
|
542
|
+
typeof reader.readResource === "function", "mcp_source_unavailable", "The original registered MCP resource reader is unavailable.");
|
|
543
|
+
authorizeCall(() => reader.assertCurrent());
|
|
544
|
+
authority.assertCurrent();
|
|
545
|
+
authority.authorizeConnection(source.connection, uri, access);
|
|
546
|
+
return { reader, authority };
|
|
547
|
+
}
|
|
548
|
+
async readResource(uri, scope, limits = {}) {
|
|
549
|
+
const routed = this.#route(uri), { reader, authority } = await this.resolve(routed.source, scope, "resources.read", routed.uri);
|
|
550
|
+
const raw = await reader.readResource(routed.uri, authority);
|
|
551
|
+
const wrapped = wrapMcpResourceResult(raw, routed.source), info = mcpResult(wrapped);
|
|
552
|
+
const maxText = limits.textBytes ?? 65536, maxInline = limits.inlineBytes ?? 64 * 1024 * 1024, maximum = limits.responseBytes ?? maximumProtocolBytes;
|
|
553
|
+
let textBytes = 0, binaryBytes = 0;
|
|
554
|
+
const contents = info.model.contents.map((item) => {
|
|
555
|
+
const value = {
|
|
556
|
+
...item,
|
|
557
|
+
uri: this.uri(routed.source, item.uri),
|
|
558
|
+
};
|
|
559
|
+
if (typeof value.text === "string")
|
|
560
|
+
textBytes += boundedSize(value.text, maxText, "resource_read_too_large");
|
|
561
|
+
else {
|
|
562
|
+
const blob = value.blob;
|
|
563
|
+
const length = base64Size(blob);
|
|
564
|
+
const mime = typeof value.mimeType === "string"
|
|
565
|
+
? new MIMEType(value.mimeType).essence
|
|
566
|
+
: "application/octet-stream";
|
|
567
|
+
const cap = mime.startsWith("image/")
|
|
568
|
+
? (limits.imageBytes ?? 10 * 1024 * 1024)
|
|
569
|
+
: mime.startsWith("audio/")
|
|
570
|
+
? (limits.audioBytes ?? 32 * 1024 * 1024)
|
|
571
|
+
: maxInline;
|
|
572
|
+
requireCondition(length <= cap, "resource_read_too_large", "Remote resource bytes exceed this response budget; explicitly materialize a download if needed.");
|
|
573
|
+
binaryBytes += length;
|
|
574
|
+
}
|
|
575
|
+
return value;
|
|
576
|
+
});
|
|
577
|
+
requireCondition(textBytes <= maxText && binaryBytes <= maxInline, "resource_read_too_large", "Remote resource contents exceed the aggregate read budget.");
|
|
578
|
+
const response = { ...info.model, contents };
|
|
579
|
+
boundedSize({
|
|
580
|
+
contents: contents.map((item) => typeof item.blob === "string" ? { ...item, blob: "" } : item),
|
|
581
|
+
}, maxText, "resource_read_too_large");
|
|
582
|
+
// This endpoint returns a complete standard ReadResourceResult to the
|
|
583
|
+
// actual Host. Reattach only the finite resource protocol positions that
|
|
584
|
+
// the model projection separated from ordinary business data.
|
|
585
|
+
for (const slot of info.meta) {
|
|
586
|
+
if (slot.target === "resource" && Number.isSafeInteger(slot.index)) {
|
|
587
|
+
const item = contents[Number(slot.index)];
|
|
588
|
+
if (item)
|
|
589
|
+
item._meta = copyMcpJSON(slot.meta, limits.metaBytes ?? 16 * 1024 * 1024);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
applyProtocolMeta(response, info.meta, limits.metaBytes ?? 16 * 1024 * 1024);
|
|
593
|
+
requireCondition(boundedSize(response, maximum, "mcp_response_too_large") <= maximum, "mcp_response_too_large", "The remote resource response exceeds its final encoded budget.");
|
|
594
|
+
await this.resolve(routed.source, scope, "resources.read", routed.uri);
|
|
595
|
+
return response;
|
|
596
|
+
}
|
|
597
|
+
async listResources(source, scope, cursor, limits = {}) {
|
|
598
|
+
const { reader, authority } = await this.resolve(source, scope, "resources.list");
|
|
599
|
+
requireCondition(reader.listResources, "mcp_capability_unavailable", "This MCP connection does not expose resource discovery.");
|
|
600
|
+
const raw = await reader.listResources(authority, cursor), projected = project(raw, "resources/list"), data = projected.model.structuredContent;
|
|
601
|
+
const resources = data.resources.map((item) => ({
|
|
602
|
+
...item,
|
|
603
|
+
uri: this.uri(source, item.uri),
|
|
604
|
+
}));
|
|
605
|
+
const result = { ...data, resources };
|
|
606
|
+
requireCondition(boundedSize(result, limits.textBytes ?? 65536, "resource_list_too_large") <= (limits.textBytes ?? 65536), "resource_list_too_large", "The remote resource directory exceeds its metadata budget.");
|
|
607
|
+
applyProtocolMeta(result, projected.meta, limits.metaBytes ?? 16 * 1024 * 1024);
|
|
608
|
+
boundedSize(result, limits.responseBytes ?? maximumProtocolBytes, "mcp_response_too_large");
|
|
609
|
+
await this.resolve(source, scope, "resources.list");
|
|
610
|
+
return result;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
export function jsonSize(value, maximum) {
|
|
614
|
+
const copied = copyMcpJSON(value, maximum);
|
|
615
|
+
// Copy's exact accounting has already bounded this serialization.
|
|
616
|
+
return Buffer.byteLength(JSON.stringify(copied));
|
|
617
|
+
}
|
|
618
|
+
export function mcpMetaSize(value, maximum) {
|
|
619
|
+
return boundedSize(value, maximum, "mcp_meta_too_large");
|
|
620
|
+
}
|
|
621
|
+
function boundedSize(value, maximum, code) {
|
|
622
|
+
try {
|
|
623
|
+
return jsonSize(value, maximum);
|
|
624
|
+
}
|
|
625
|
+
catch (cause) {
|
|
626
|
+
if (cause instanceof AgentError && cause.code === "mcp_result_too_large")
|
|
627
|
+
throw new AgentError(code, "The remote resource metadata or body exceeds its encoded budget.");
|
|
628
|
+
throw cause;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
export function base64Size(value) {
|
|
632
|
+
requireCondition(value.length % 4 === 0, "invalid_media_encoding", "Remote resource binary text must be canonical base64.");
|
|
633
|
+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
634
|
+
const padding = value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0;
|
|
635
|
+
for (let index = 0; index < value.length - padding; index++)
|
|
636
|
+
requireCondition(alphabet.includes(value[index]), "invalid_media_encoding", "Remote resource binary text contains invalid base64.");
|
|
637
|
+
const last = padding
|
|
638
|
+
? alphabet.indexOf(value[value.length - padding - 1] ?? "")
|
|
639
|
+
: 0;
|
|
640
|
+
requireCondition(!padding || (last >= 0 && last % (padding === 2 ? 16 : 4) === 0), "invalid_media_encoding", "Remote resource base64 has invalid padding bits.");
|
|
641
|
+
return (value.length / 4) * 3 - padding;
|
|
642
|
+
}
|
|
643
|
+
export function applyProtocolMeta(target, meta, maximum) {
|
|
644
|
+
if (!meta.length)
|
|
645
|
+
return;
|
|
646
|
+
mcpMetaSize(meta, maximum);
|
|
647
|
+
const root = meta.find((slot) => slot.target === "result")?.meta;
|
|
648
|
+
const hostMeta = { ...(root ?? {}) };
|
|
649
|
+
let key = "io.nexum/mcp", suffix = 0;
|
|
650
|
+
while (Object.hasOwn(hostMeta, key))
|
|
651
|
+
key = `io.nexum/mcp/${++suffix}`;
|
|
652
|
+
hostMeta[key] = { protocolMeta: meta };
|
|
653
|
+
target._meta = hostMeta;
|
|
654
|
+
mcpMetaSize(hostMeta, maximum);
|
|
655
|
+
}
|
|
656
|
+
//# sourceMappingURL=mcp.js.map
|