@onlyflows/servicenow-mcp 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +56 -0
- package/CHANGELOG.md +153 -0
- package/README.md +939 -101
- package/dist/auth.d.ts +88 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +396 -0
- package/dist/auth.js.map +1 -0
- package/dist/client.d.ts +111 -17
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +554 -79
- package/dist/client.js.map +1 -1
- package/dist/config.d.ts +41 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +44 -34
- package/dist/config.js.map +1 -1
- package/dist/encoded-query-policy.d.ts +72 -0
- package/dist/encoded-query-policy.d.ts.map +1 -0
- package/dist/encoded-query-policy.js +409 -0
- package/dist/encoded-query-policy.js.map +1 -0
- package/dist/execution-context.d.ts +235 -0
- package/dist/execution-context.d.ts.map +1 -0
- package/dist/execution-context.js +439 -0
- package/dist/execution-context.js.map +1 -0
- package/dist/field-policy.d.ts +117 -0
- package/dist/field-policy.d.ts.map +1 -0
- package/dist/field-policy.js +1684 -0
- package/dist/field-policy.js.map +1 -0
- package/dist/http-auth.d.ts +131 -0
- package/dist/http-auth.d.ts.map +1 -0
- package/dist/http-auth.js +347 -0
- package/dist/http-auth.js.map +1 -0
- package/dist/http-entrypoint.d.ts +26 -0
- package/dist/http-entrypoint.d.ts.map +1 -0
- package/dist/http-entrypoint.js +255 -0
- package/dist/http-entrypoint.js.map +1 -0
- package/dist/http-observability.d.ts +147 -0
- package/dist/http-observability.d.ts.map +1 -0
- package/dist/http-observability.js +703 -0
- package/dist/http-observability.js.map +1 -0
- package/dist/http-request-policy.d.ts +72 -0
- package/dist/http-request-policy.d.ts.map +1 -0
- package/dist/http-request-policy.js +463 -0
- package/dist/http-request-policy.js.map +1 -0
- package/dist/http-request-signal.d.ts +12 -0
- package/dist/http-request-signal.d.ts.map +1 -0
- package/dist/http-request-signal.js +39 -0
- package/dist/http-request-signal.js.map +1 -0
- package/dist/http-runtime.d.ts +77 -0
- package/dist/http-runtime.d.ts.map +1 -0
- package/dist/http-runtime.js +1321 -0
- package/dist/http-runtime.js.map +1 -0
- package/dist/incident-journal-policy.d.ts +22 -0
- package/dist/incident-journal-policy.d.ts.map +1 -0
- package/dist/incident-journal-policy.js +115 -0
- package/dist/incident-journal-policy.js.map +1 -0
- package/dist/incident-write-policy.d.ts +20 -0
- package/dist/incident-write-policy.d.ts.map +1 -0
- package/dist/incident-write-policy.js +196 -0
- package/dist/incident-write-policy.js.map +1 -0
- package/dist/index.d.ts +14 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +140 -36
- package/dist/index.js.map +1 -1
- package/dist/metadata-cache.d.ts +71 -0
- package/dist/metadata-cache.d.ts.map +1 -0
- package/dist/metadata-cache.js +397 -0
- package/dist/metadata-cache.js.map +1 -0
- package/dist/profile-admin.d.ts +64 -0
- package/dist/profile-admin.d.ts.map +1 -0
- package/dist/profile-admin.js +351 -0
- package/dist/profile-admin.js.map +1 -0
- package/dist/profile-credentials.d.ts +68 -0
- package/dist/profile-credentials.d.ts.map +1 -0
- package/dist/profile-credentials.js +329 -0
- package/dist/profile-credentials.js.map +1 -0
- package/dist/profile-manager.d.ts +187 -0
- package/dist/profile-manager.d.ts.map +1 -0
- package/dist/profile-manager.js +1026 -0
- package/dist/profile-manager.js.map +1 -0
- package/dist/record-identifier-policy.d.ts +39 -0
- package/dist/record-identifier-policy.d.ts.map +1 -0
- package/dist/record-identifier-policy.js +203 -0
- package/dist/record-identifier-policy.js.map +1 -0
- package/dist/runtime-environment.d.ts +23 -0
- package/dist/runtime-environment.d.ts.map +1 -0
- package/dist/runtime-environment.js +58 -0
- package/dist/runtime-environment.js.map +1 -0
- package/dist/server.d.ts +33 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +73 -0
- package/dist/server.js.map +1 -0
- package/dist/servicenow-identifiers.d.ts +8 -0
- package/dist/servicenow-identifiers.d.ts.map +1 -0
- package/dist/servicenow-identifiers.js +22 -0
- package/dist/servicenow-identifiers.js.map +1 -0
- package/dist/setup-prompts.d.ts +7 -0
- package/dist/setup-prompts.d.ts.map +1 -0
- package/dist/setup-prompts.js +123 -0
- package/dist/setup-prompts.js.map +1 -0
- package/dist/setup.d.ts +109 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +1903 -0
- package/dist/setup.js.map +1 -0
- package/dist/startup.d.ts +43 -0
- package/dist/startup.d.ts.map +1 -0
- package/dist/startup.js +75 -0
- package/dist/startup.js.map +1 -0
- package/dist/stdio-runtime.d.ts +106 -0
- package/dist/stdio-runtime.d.ts.map +1 -0
- package/dist/stdio-runtime.js +231 -0
- package/dist/stdio-runtime.js.map +1 -0
- package/dist/structured-query.d.ts +187 -0
- package/dist/structured-query.d.ts.map +1 -0
- package/dist/structured-query.js +503 -0
- package/dist/structured-query.js.map +1 -0
- package/dist/table-defaults.d.ts +28 -0
- package/dist/table-defaults.d.ts.map +1 -0
- package/dist/table-defaults.js +85 -0
- package/dist/table-defaults.js.map +1 -0
- package/dist/table-policy.d.ts +71 -0
- package/dist/table-policy.d.ts.map +1 -0
- package/dist/table-policy.js +290 -0
- package/dist/table-policy.js.map +1 -0
- package/dist/tool-error.d.ts +48 -0
- package/dist/tool-error.d.ts.map +1 -0
- package/dist/tool-error.js +139 -0
- package/dist/tool-error.js.map +1 -0
- package/dist/tool-table-access.d.ts +19 -0
- package/dist/tool-table-access.d.ts.map +1 -0
- package/dist/tool-table-access.js +388 -0
- package/dist/tool-table-access.js.map +1 -0
- package/dist/tools/aggregate.d.ts +22 -42
- package/dist/tools/aggregate.d.ts.map +1 -1
- package/dist/tools/aggregate.js +103 -47
- package/dist/tools/aggregate.js.map +1 -1
- package/dist/tools/all-fields-cap.d.ts +45 -0
- package/dist/tools/all-fields-cap.d.ts.map +1 -0
- package/dist/tools/all-fields-cap.js +151 -0
- package/dist/tools/all-fields-cap.js.map +1 -0
- package/dist/tools/atf.d.ts +21 -60
- package/dist/tools/atf.d.ts.map +1 -1
- package/dist/tools/atf.js +180 -116
- package/dist/tools/atf.js.map +1 -1
- package/dist/tools/attach.d.ts +31 -45
- package/dist/tools/attach.d.ts.map +1 -1
- package/dist/tools/attach.js +175 -137
- package/dist/tools/attach.js.map +1 -1
- package/dist/tools/batch.d.ts +78 -42
- package/dist/tools/batch.d.ts.map +1 -1
- package/dist/tools/batch.js +172 -68
- package/dist/tools/batch.js.map +1 -1
- package/dist/tools/catalog.d.ts +6 -0
- package/dist/tools/catalog.d.ts.map +1 -0
- package/dist/tools/catalog.js +203 -0
- package/dist/tools/catalog.js.map +1 -0
- package/dist/tools/codesearch.d.ts +14 -25
- package/dist/tools/codesearch.d.ts.map +1 -1
- package/dist/tools/codesearch.js +60 -48
- package/dist/tools/codesearch.js.map +1 -1
- package/dist/tools/create-module.d.ts +48 -0
- package/dist/tools/create-module.d.ts.map +1 -0
- package/dist/tools/create-module.js +94 -0
- package/dist/tools/create-module.js.map +1 -0
- package/dist/tools/create.d.ts +2 -38
- package/dist/tools/create.d.ts.map +1 -1
- package/dist/tools/create.js +9 -41
- package/dist/tools/create.js.map +1 -1
- package/dist/tools/delete.d.ts +13 -23
- package/dist/tools/delete.d.ts.map +1 -1
- package/dist/tools/delete.js +30 -33
- package/dist/tools/delete.js.map +1 -1
- package/dist/tools/discover.d.ts +15 -27
- package/dist/tools/discover.d.ts.map +1 -1
- package/dist/tools/discover.js +103 -60
- package/dist/tools/discover.js.map +1 -1
- package/dist/tools/get-module.d.ts +85 -0
- package/dist/tools/get-module.d.ts.map +1 -0
- package/dist/tools/get-module.js +221 -0
- package/dist/tools/get-module.js.map +1 -0
- package/dist/tools/get.d.ts +2 -51
- package/dist/tools/get.d.ts.map +1 -1
- package/dist/tools/get.js +9 -49
- package/dist/tools/get.js.map +1 -1
- package/dist/tools/handler-result.d.ts +14 -0
- package/dist/tools/handler-result.d.ts.map +1 -0
- package/dist/tools/handler-result.js +177 -0
- package/dist/tools/handler-result.js.map +1 -0
- package/dist/tools/health.d.ts +12 -15
- package/dist/tools/health.d.ts.map +1 -1
- package/dist/tools/health.js +55 -36
- package/dist/tools/health.js.map +1 -1
- package/dist/tools/incident-journal-module.d.ts +12 -0
- package/dist/tools/incident-journal-module.d.ts.map +1 -0
- package/dist/tools/incident-journal-module.js +61 -0
- package/dist/tools/incident-journal-module.js.map +1 -0
- package/dist/tools/incident-write-shared.d.ts +6 -0
- package/dist/tools/incident-write-shared.d.ts.map +1 -0
- package/dist/tools/incident-write-shared.js +64 -0
- package/dist/tools/incident-write-shared.js.map +1 -0
- package/dist/tools/index.d.ts +47 -36
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +795 -35
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/nl.d.ts +10 -24
- package/dist/tools/nl.d.ts.map +1 -1
- package/dist/tools/nl.js +37 -147
- package/dist/tools/nl.js.map +1 -1
- package/dist/tools/profile.d.ts +37 -0
- package/dist/tools/profile.d.ts.map +1 -0
- package/dist/tools/profile.js +39 -0
- package/dist/tools/profile.js.map +1 -0
- package/dist/tools/query-module.d.ts +119 -0
- package/dist/tools/query-module.d.ts.map +1 -0
- package/dist/tools/query-module.js +324 -0
- package/dist/tools/query-module.js.map +1 -0
- package/dist/tools/query.d.ts +2 -72
- package/dist/tools/query.d.ts.map +1 -1
- package/dist/tools/query.js +8 -72
- package/dist/tools/query.js.map +1 -1
- package/dist/tools/relationships.d.ts +21 -42
- package/dist/tools/relationships.d.ts.map +1 -1
- package/dist/tools/relationships.js +70 -56
- package/dist/tools/relationships.js.map +1 -1
- package/dist/tools/result-envelope.d.ts +603 -0
- package/dist/tools/result-envelope.d.ts.map +1 -0
- package/dist/tools/result-envelope.js +767 -0
- package/dist/tools/result-envelope.js.map +1 -0
- package/dist/tools/schema-module.d.ts +64 -0
- package/dist/tools/schema-module.d.ts.map +1 -0
- package/dist/tools/schema-module.js +367 -0
- package/dist/tools/schema-module.js.map +1 -0
- package/dist/tools/schema.d.ts +2 -37
- package/dist/tools/schema.d.ts.map +1 -1
- package/dist/tools/schema.js +6 -57
- package/dist/tools/schema.js.map +1 -1
- package/dist/tools/script.d.ts +33 -27
- package/dist/tools/script.d.ts.map +1 -1
- package/dist/tools/script.js +43 -37
- package/dist/tools/script.js.map +1 -1
- package/dist/tools/syslog.d.ts +19 -46
- package/dist/tools/syslog.d.ts.map +1 -1
- package/dist/tools/syslog.js +101 -67
- package/dist/tools/syslog.js.map +1 -1
- package/dist/tools/tool-module.d.ts +143 -0
- package/dist/tools/tool-module.d.ts.map +1 -0
- package/dist/tools/tool-module.js +1095 -0
- package/dist/tools/tool-module.js.map +1 -0
- package/dist/tools/update-module.d.ts +51 -0
- package/dist/tools/update-module.d.ts.map +1 -0
- package/dist/tools/update-module.js +107 -0
- package/dist/tools/update-module.js.map +1 -0
- package/dist/tools/update.d.ts +2 -45
- package/dist/tools/update.d.ts.map +1 -1
- package/dist/tools/update.js +9 -41
- package/dist/tools/update.js.map +1 -1
- package/dist/utils.d.ts +68 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +114 -16
- package/dist/utils.js.map +1 -1
- package/dist/version.d.ts +10 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +13 -0
- package/dist/version.js.map +1 -0
- package/docs/ADDING-TOOLS.md +227 -0
- package/docs/CLIENT-SETUP.md +914 -0
- package/docs/CONTAINER-DEPLOYMENT.md +310 -0
- package/docs/CROSS-CLIENT-RELEASE-MATRIX.md +63 -0
- package/docs/ENTERPRISE-RELEASE-BOUNDARY.md +149 -0
- package/docs/OPERATIONS-RUNBOOK.md +383 -0
- package/docs/PRIVATE-CHATGPT-CONNECTIVITY.md +347 -0
- package/docs/PRODUCTION-SECURITY.md +368 -0
- package/docs/PROFILE-CREDENTIALS.md +126 -0
- package/docs/RELEASE-VALIDATION.md +140 -0
- package/docs/TESTING-OAUTH.md +101 -0
- package/docs/V2-MIGRATION.md +493 -0
- package/package.json +60 -13
- package/scripts/inspector.mjs +140 -0
- package/scripts/release-validate.mjs +201 -0
- package/scripts/smoke-test.mjs +274 -0
- package/tools/inspector/package-lock.json +3812 -0
- package/tools/inspector/package.json +12 -0
|
@@ -0,0 +1,1095 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Provider-neutral contract for one ServiceNow MCP tool module.
|
|
4
|
+
*
|
|
5
|
+
* Registration consumes only values created by this contract: a shared
|
|
6
|
+
* profile-bearing input schema, a profile-bearing structured-output schema,
|
|
7
|
+
* complete security/dependency declarations, an access preflight, and a
|
|
8
|
+
* handler whose runtime services contain no AI-client or transport objects.
|
|
9
|
+
*
|
|
10
|
+
* @module tools/tool-module
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.SILENT_TOOL_MODULE_LOGGER = exports.profileOutputSchema = exports.profileNameSchema = void 0;
|
|
14
|
+
exports.withRequiredProfile = withRequiredProfile;
|
|
15
|
+
exports.withResolvedProfileOutput = withResolvedProfileOutput;
|
|
16
|
+
exports.snapshotIssuedToolSchema = snapshotIssuedToolSchema;
|
|
17
|
+
exports.defineServiceNowToolModule = defineServiceNowToolModule;
|
|
18
|
+
exports.defineContextOnlyToolModule = defineContextOnlyToolModule;
|
|
19
|
+
exports.validateToolModuleCatalog = validateToolModuleCatalog;
|
|
20
|
+
exports.snapshotToolModuleCatalog = snapshotToolModuleCatalog;
|
|
21
|
+
const node_util_1 = require("node:util");
|
|
22
|
+
const zod_1 = require("zod");
|
|
23
|
+
const encoded_query_policy_js_1 = require("../encoded-query-policy.js");
|
|
24
|
+
/**
|
|
25
|
+
* Required on every published tool. Trimming makes whitespace-only selectors
|
|
26
|
+
* invalid and ensures profile lookup uses the exact value clients discover.
|
|
27
|
+
*/
|
|
28
|
+
exports.profileNameSchema = zod_1.z
|
|
29
|
+
.string()
|
|
30
|
+
.trim()
|
|
31
|
+
.min(1, "profile must not be empty")
|
|
32
|
+
.max(128, "profile must not exceed 128 characters")
|
|
33
|
+
.describe("Configured ServiceNow profile name (required; no default fallback).");
|
|
34
|
+
const ISSUED_INPUT_SCHEMAS = new WeakSet();
|
|
35
|
+
const ISSUED_OUTPUT_SCHEMAS = new WeakSet();
|
|
36
|
+
const ISSUED_SCHEMA_SNAPSHOTS = new WeakMap();
|
|
37
|
+
const HARDENED_SCHEMA_NODES = new WeakSet();
|
|
38
|
+
const MAX_TOOL_SCHEMA_FIELDS = 128;
|
|
39
|
+
const MAX_SCHEMA_GRAPH_DEPTH = 128;
|
|
40
|
+
const MAX_SCHEMA_GRAPH_NODES = 10_000;
|
|
41
|
+
const MAX_SCHEMA_GRAPH_OWN_KEYS = 50_000;
|
|
42
|
+
const MAX_SCHEMA_CONTAINER_KEYS = 2_048;
|
|
43
|
+
const INTRINSIC_MAP_SIZE = Object.getOwnPropertyDescriptor(Map.prototype, "size")?.get;
|
|
44
|
+
const INTRINSIC_MAP_ENTRIES = Map.prototype.entries;
|
|
45
|
+
const INTRINSIC_MAP_ITERATOR_NEXT = Object.getPrototypeOf(new Map().entries()).next;
|
|
46
|
+
const INTRINSIC_SET_SIZE = Object.getOwnPropertyDescriptor(Set.prototype, "size")?.get;
|
|
47
|
+
const INTRINSIC_SET_VALUES = Set.prototype.values;
|
|
48
|
+
const INTRINSIC_SET_ITERATOR_NEXT = Object.getPrototypeOf(new Set().values()).next;
|
|
49
|
+
/** Compose tool-local fields with the exact shared profile selector. */
|
|
50
|
+
function withRequiredProfile(toolSchema) {
|
|
51
|
+
const source = snapshotUntrustedObjectSchema(toolSchema, "tool-local input schema");
|
|
52
|
+
const normalized = zod_1.z.object({
|
|
53
|
+
...source.shape,
|
|
54
|
+
profile: exports.profileNameSchema,
|
|
55
|
+
});
|
|
56
|
+
const composed = source.unknownKeys === "strict"
|
|
57
|
+
? normalized.strict(encoded_query_policy_js_1.ENCODED_QUERY_MIGRATION_MESSAGE)
|
|
58
|
+
: normalized.strip();
|
|
59
|
+
return issueSchema(composed, "input");
|
|
60
|
+
}
|
|
61
|
+
/** Compose module output fields with the coordinator-owned profile envelope. */
|
|
62
|
+
function withResolvedProfileOutput(outputSchema) {
|
|
63
|
+
const source = snapshotUntrustedObjectSchema(outputSchema, "tool-local output schema");
|
|
64
|
+
const normalized = zod_1.z
|
|
65
|
+
.object({
|
|
66
|
+
...source.shape,
|
|
67
|
+
profile: exports.profileNameSchema,
|
|
68
|
+
})
|
|
69
|
+
.strict();
|
|
70
|
+
return issueSchema(normalized, "output");
|
|
71
|
+
}
|
|
72
|
+
/** Base output envelope used until a module declares additional structured data. */
|
|
73
|
+
exports.profileOutputSchema = withResolvedProfileOutput(zod_1.z.object({}));
|
|
74
|
+
exports.SILENT_TOOL_MODULE_LOGGER = Object.freeze({
|
|
75
|
+
write() { },
|
|
76
|
+
});
|
|
77
|
+
const ISSUED_TOOL_MODULES = new WeakSet();
|
|
78
|
+
const MAX_TOOL_MODULES = 128;
|
|
79
|
+
/**
|
|
80
|
+
* Treat a caller-provided ZodObject only as an untrusted shape container.
|
|
81
|
+
* No source methods, bound functions, descriptors, or object identity are
|
|
82
|
+
* retained in the normalized contract schema.
|
|
83
|
+
*/
|
|
84
|
+
function snapshotUntrustedObjectSchema(candidate, label) {
|
|
85
|
+
if (typeof candidate !== "object" ||
|
|
86
|
+
candidate === null ||
|
|
87
|
+
node_util_1.types.isProxy(candidate)) {
|
|
88
|
+
throw new TypeError(`${label} must provide a non-proxy object shape`);
|
|
89
|
+
}
|
|
90
|
+
const definitionDescriptor = Object.getOwnPropertyDescriptor(candidate, "_def");
|
|
91
|
+
if (!definitionDescriptor || !("value" in definitionDescriptor)) {
|
|
92
|
+
throw new TypeError(`${label} definition must be a data property`);
|
|
93
|
+
}
|
|
94
|
+
const definition = definitionDescriptor.value;
|
|
95
|
+
if (typeof definition !== "object" ||
|
|
96
|
+
definition === null ||
|
|
97
|
+
node_util_1.types.isProxy(definition) ||
|
|
98
|
+
Object.getPrototypeOf(definition) !== Object.prototype) {
|
|
99
|
+
throw new TypeError(`${label} definition must be a plain data object`);
|
|
100
|
+
}
|
|
101
|
+
const typeDescriptor = Object.getOwnPropertyDescriptor(definition, "typeName");
|
|
102
|
+
const shapeDescriptor = Object.getOwnPropertyDescriptor(definition, "shape");
|
|
103
|
+
const unknownKeysDescriptor = Object.getOwnPropertyDescriptor(definition, "unknownKeys");
|
|
104
|
+
const catchallDescriptor = Object.getOwnPropertyDescriptor(definition, "catchall");
|
|
105
|
+
if (!typeDescriptor ||
|
|
106
|
+
!("value" in typeDescriptor) ||
|
|
107
|
+
typeDescriptor.value !== zod_1.z.ZodFirstPartyTypeKind.ZodObject ||
|
|
108
|
+
!shapeDescriptor ||
|
|
109
|
+
!("value" in shapeDescriptor) ||
|
|
110
|
+
typeof shapeDescriptor.value !== "function" ||
|
|
111
|
+
!unknownKeysDescriptor ||
|
|
112
|
+
!("value" in unknownKeysDescriptor) ||
|
|
113
|
+
(unknownKeysDescriptor.value !== "strip" &&
|
|
114
|
+
unknownKeysDescriptor.value !== "strict") ||
|
|
115
|
+
!catchallDescriptor ||
|
|
116
|
+
!("value" in catchallDescriptor) ||
|
|
117
|
+
!(catchallDescriptor.value instanceof zod_1.z.ZodNever) ||
|
|
118
|
+
node_util_1.types.isProxy(catchallDescriptor.value)) {
|
|
119
|
+
throw new TypeError(`${label} definition is unsupported`);
|
|
120
|
+
}
|
|
121
|
+
let shapeCandidate;
|
|
122
|
+
try {
|
|
123
|
+
shapeCandidate = Reflect.apply(shapeDescriptor.value, undefined, []);
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
throw new TypeError(`${label} shape could not be materialized`);
|
|
127
|
+
}
|
|
128
|
+
if (typeof shapeCandidate !== "object" ||
|
|
129
|
+
shapeCandidate === null ||
|
|
130
|
+
Array.isArray(shapeCandidate) ||
|
|
131
|
+
node_util_1.types.isProxy(shapeCandidate) ||
|
|
132
|
+
(Object.getPrototypeOf(shapeCandidate) !== Object.prototype &&
|
|
133
|
+
Object.getPrototypeOf(shapeCandidate) !== null)) {
|
|
134
|
+
throw new TypeError(`${label} shape must be a plain data object`);
|
|
135
|
+
}
|
|
136
|
+
const keys = Reflect.ownKeys(shapeCandidate);
|
|
137
|
+
if (keys.length > MAX_TOOL_SCHEMA_FIELDS) {
|
|
138
|
+
throw new TypeError(`${label} has too many fields`);
|
|
139
|
+
}
|
|
140
|
+
const shape = Object.create(null);
|
|
141
|
+
for (const key of keys) {
|
|
142
|
+
if (typeof key !== "string" ||
|
|
143
|
+
!/^[a-z][a-z0-9_]{0,79}$/u.test(key)) {
|
|
144
|
+
throw new TypeError(`${label} contains an invalid field name`);
|
|
145
|
+
}
|
|
146
|
+
if (key === "profile") {
|
|
147
|
+
throw new TypeError(`${label} must not redefine profile`);
|
|
148
|
+
}
|
|
149
|
+
const descriptor = Object.getOwnPropertyDescriptor(shapeCandidate, key);
|
|
150
|
+
if (!descriptor || !("value" in descriptor)) {
|
|
151
|
+
throw new TypeError(`${label} shape must contain only data fields`);
|
|
152
|
+
}
|
|
153
|
+
const field = descriptor.value;
|
|
154
|
+
if (typeof field !== "object" ||
|
|
155
|
+
field === null ||
|
|
156
|
+
node_util_1.types.isProxy(field)) {
|
|
157
|
+
throw new TypeError(`${label} contains an invalid field schema`);
|
|
158
|
+
}
|
|
159
|
+
shape[key] = field;
|
|
160
|
+
}
|
|
161
|
+
const shapeSnapshot = Object.freeze({ ...shape });
|
|
162
|
+
try {
|
|
163
|
+
const probe = zod_1.z.object({ ...shapeSnapshot }).safeParse(Object.create(null));
|
|
164
|
+
if (typeof probe.success !== "boolean") {
|
|
165
|
+
throw new TypeError("invalid parse result");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
throw new TypeError(`${label} fields could not be safely validated`);
|
|
170
|
+
}
|
|
171
|
+
return Object.freeze({
|
|
172
|
+
shape: shapeSnapshot,
|
|
173
|
+
unknownKeys: unknownKeysDescriptor.value,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
function issueSchema(schema, kind) {
|
|
177
|
+
hardenSchemaGraph(schema);
|
|
178
|
+
const shape = Object.freeze({ ...schema.shape });
|
|
179
|
+
const snapshot = Object.freeze({
|
|
180
|
+
definition: schema._def,
|
|
181
|
+
shape,
|
|
182
|
+
parse: schema.parse,
|
|
183
|
+
safeParse: schema.safeParse,
|
|
184
|
+
unknownKeys: schema._def.unknownKeys,
|
|
185
|
+
});
|
|
186
|
+
ISSUED_SCHEMA_SNAPSHOTS.set(schema, snapshot);
|
|
187
|
+
(kind === "input" ? ISSUED_INPUT_SCHEMAS : ISSUED_OUTPUT_SCHEMAS).add(schema);
|
|
188
|
+
return schema;
|
|
189
|
+
}
|
|
190
|
+
function issuedSchemaSnapshot(schema, kind) {
|
|
191
|
+
const issued = kind === "input" ? ISSUED_INPUT_SCHEMAS : ISSUED_OUTPUT_SCHEMAS;
|
|
192
|
+
const snapshot = ISSUED_SCHEMA_SNAPSHOTS.get(schema);
|
|
193
|
+
if (node_util_1.types.isProxy(schema) ||
|
|
194
|
+
!issued.has(schema) ||
|
|
195
|
+
!snapshot ||
|
|
196
|
+
schema._def !== snapshot.definition ||
|
|
197
|
+
schema.parse !== snapshot.parse ||
|
|
198
|
+
schema.safeParse !== snapshot.safeParse) {
|
|
199
|
+
throw new TypeError(`tool ${kind} schema is not an issued contract-normalized schema`);
|
|
200
|
+
}
|
|
201
|
+
const currentShape = schema.shape;
|
|
202
|
+
const expectedKeys = Object.keys(snapshot.shape);
|
|
203
|
+
const currentKeys = Object.keys(currentShape);
|
|
204
|
+
if (currentKeys.length !== expectedKeys.length ||
|
|
205
|
+
expectedKeys.some((key, index) => currentKeys[index] !== key || currentShape[key] !== snapshot.shape[key])) {
|
|
206
|
+
throw new TypeError(`tool ${kind} schema changed after factory issuance`);
|
|
207
|
+
}
|
|
208
|
+
return snapshot;
|
|
209
|
+
}
|
|
210
|
+
/** Clone an issued top-level schema so later source-catalog mutation is inert. */
|
|
211
|
+
function snapshotIssuedToolSchema(schema, kind) {
|
|
212
|
+
const snapshot = issuedSchemaSnapshot(schema, kind);
|
|
213
|
+
const normalized = zod_1.z.object({ ...snapshot.shape });
|
|
214
|
+
const cloned = snapshot.unknownKeys === "strict"
|
|
215
|
+
? normalized.strict(kind === "input" ? encoded_query_policy_js_1.ENCODED_QUERY_MIGRATION_MESSAGE : undefined)
|
|
216
|
+
: normalized.strip();
|
|
217
|
+
hardenSchemaGraph(cloned);
|
|
218
|
+
return cloned;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Make every parse-relevant value reachable from a normalized schema stable.
|
|
222
|
+
* Zod definitions are replaced with contract-owned copies before the schema
|
|
223
|
+
* node is frozen, so references to an earlier definition become inert. The
|
|
224
|
+
* traversal is deliberately bounded and rejects accessors, proxies, and
|
|
225
|
+
* mutable exotic containers rather than attempting to execute them.
|
|
226
|
+
*/
|
|
227
|
+
function hardenSchemaGraph(schema) {
|
|
228
|
+
const state = {
|
|
229
|
+
seen: new WeakMap(),
|
|
230
|
+
depth: 0,
|
|
231
|
+
nodes: 0,
|
|
232
|
+
ownKeys: 0,
|
|
233
|
+
};
|
|
234
|
+
hardenZodSchemaNode(schema, state);
|
|
235
|
+
}
|
|
236
|
+
function hardenZodSchemaNode(schema, state) {
|
|
237
|
+
if (node_util_1.types.isProxy(schema)) {
|
|
238
|
+
throw new TypeError("tool schema graph must not contain a Proxy");
|
|
239
|
+
}
|
|
240
|
+
if (HARDENED_SCHEMA_NODES.has(schema))
|
|
241
|
+
return schema;
|
|
242
|
+
const seen = state.seen.get(schema);
|
|
243
|
+
if (seen)
|
|
244
|
+
return seen;
|
|
245
|
+
enterSchemaGraphValue(state, schema);
|
|
246
|
+
state.seen.set(schema, schema);
|
|
247
|
+
const definitionDescriptor = Object.getOwnPropertyDescriptor(schema, "_def");
|
|
248
|
+
if (!definitionDescriptor ||
|
|
249
|
+
!("value" in definitionDescriptor) ||
|
|
250
|
+
(!definitionDescriptor.configurable && !definitionDescriptor.writable)) {
|
|
251
|
+
leaveSchemaGraphValue(state);
|
|
252
|
+
throw new TypeError("tool schema graph contains an immutable foreign definition");
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
const definition = cloneSchemaDefinition(schema, definitionDescriptor.value, state);
|
|
256
|
+
Object.defineProperty(schema, "_def", {
|
|
257
|
+
value: definition,
|
|
258
|
+
enumerable: definitionDescriptor.enumerable,
|
|
259
|
+
writable: false,
|
|
260
|
+
configurable: false,
|
|
261
|
+
});
|
|
262
|
+
initializeZodParseCaches(schema);
|
|
263
|
+
hardenSchemaOwnProperties(schema, state);
|
|
264
|
+
shadowZodPrototypeBehavior(schema);
|
|
265
|
+
Object.freeze(schema);
|
|
266
|
+
HARDENED_SCHEMA_NODES.add(schema);
|
|
267
|
+
return schema;
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
throw error instanceof TypeError
|
|
271
|
+
? error
|
|
272
|
+
: new TypeError("tool schema graph could not be hardened");
|
|
273
|
+
}
|
|
274
|
+
finally {
|
|
275
|
+
leaveSchemaGraphValue(state);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
function cloneSchemaDefinition(schema, candidate, state) {
|
|
279
|
+
if (typeof candidate !== "object" ||
|
|
280
|
+
candidate === null ||
|
|
281
|
+
node_util_1.types.isProxy(candidate) ||
|
|
282
|
+
(Object.getPrototypeOf(candidate) !== Object.prototype &&
|
|
283
|
+
Object.getPrototypeOf(candidate) !== null)) {
|
|
284
|
+
throw new TypeError("tool schema graph definitions must be plain data objects");
|
|
285
|
+
}
|
|
286
|
+
const existing = state.seen.get(candidate);
|
|
287
|
+
if (existing)
|
|
288
|
+
return existing;
|
|
289
|
+
enterSchemaGraphValue(state, candidate);
|
|
290
|
+
const descriptors = boundedDataDescriptors(candidate, state, "definition");
|
|
291
|
+
const clone = Object.create(Object.getPrototypeOf(candidate));
|
|
292
|
+
state.seen.set(candidate, clone);
|
|
293
|
+
try {
|
|
294
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
295
|
+
const descriptor = descriptors[key];
|
|
296
|
+
let value = descriptor.value;
|
|
297
|
+
if (schema instanceof zod_1.z.ZodObject && key === "shape") {
|
|
298
|
+
if (typeof value !== "function") {
|
|
299
|
+
throw new TypeError("tool schema graph has an invalid object shape");
|
|
300
|
+
}
|
|
301
|
+
const shape = materializeZodShape(value, state);
|
|
302
|
+
value = Object.freeze(() => shape);
|
|
303
|
+
}
|
|
304
|
+
else if (schema instanceof zod_1.z.ZodLazy && key === "getter") {
|
|
305
|
+
if (typeof value !== "function") {
|
|
306
|
+
throw new TypeError("tool schema graph has an invalid lazy getter");
|
|
307
|
+
}
|
|
308
|
+
const lazyTarget = Reflect.apply(value, undefined, []);
|
|
309
|
+
if (!isZodSchemaNode(lazyTarget)) {
|
|
310
|
+
throw new TypeError("tool schema graph lazy getter returned an invalid schema");
|
|
311
|
+
}
|
|
312
|
+
const stableTarget = hardenZodSchemaNode(lazyTarget, state);
|
|
313
|
+
value = Object.freeze(() => stableTarget);
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
value = cloneAndHardenGraphValue(value, state);
|
|
317
|
+
}
|
|
318
|
+
Object.defineProperty(clone, key, immutableDataDescriptor(descriptor, value));
|
|
319
|
+
}
|
|
320
|
+
return Object.freeze(clone);
|
|
321
|
+
}
|
|
322
|
+
finally {
|
|
323
|
+
leaveSchemaGraphValue(state);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function materializeZodShape(shapeFactory, state) {
|
|
327
|
+
let candidate;
|
|
328
|
+
try {
|
|
329
|
+
candidate = Reflect.apply(shapeFactory, undefined, []);
|
|
330
|
+
}
|
|
331
|
+
catch {
|
|
332
|
+
throw new TypeError("tool schema graph object shape could not be materialized");
|
|
333
|
+
}
|
|
334
|
+
if (typeof candidate !== "object" ||
|
|
335
|
+
candidate === null ||
|
|
336
|
+
Array.isArray(candidate) ||
|
|
337
|
+
node_util_1.types.isProxy(candidate) ||
|
|
338
|
+
(Object.getPrototypeOf(candidate) !== Object.prototype &&
|
|
339
|
+
Object.getPrototypeOf(candidate) !== null)) {
|
|
340
|
+
throw new TypeError("tool schema graph object shape must be a plain data object");
|
|
341
|
+
}
|
|
342
|
+
enterSchemaGraphValue(state, candidate);
|
|
343
|
+
const descriptors = boundedDataDescriptors(candidate, state, "object shape");
|
|
344
|
+
const shape = Object.create(null);
|
|
345
|
+
try {
|
|
346
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
347
|
+
if (typeof key !== "string") {
|
|
348
|
+
throw new TypeError("tool schema graph object shape has a symbol field");
|
|
349
|
+
}
|
|
350
|
+
const field = descriptors[key].value;
|
|
351
|
+
if (!isZodSchemaNode(field)) {
|
|
352
|
+
throw new TypeError("tool schema graph object shape has an invalid field");
|
|
353
|
+
}
|
|
354
|
+
shape[key] = hardenZodSchemaNode(field, state);
|
|
355
|
+
}
|
|
356
|
+
return Object.freeze({ ...shape });
|
|
357
|
+
}
|
|
358
|
+
finally {
|
|
359
|
+
leaveSchemaGraphValue(state);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
function initializeZodParseCaches(schema) {
|
|
363
|
+
if (schema instanceof zod_1.z.ZodObject) {
|
|
364
|
+
const shape = schema._def.shape();
|
|
365
|
+
Object.defineProperty(schema, "_cached", {
|
|
366
|
+
value: { shape, keys: Object.keys(shape) },
|
|
367
|
+
enumerable: false,
|
|
368
|
+
writable: true,
|
|
369
|
+
configurable: true,
|
|
370
|
+
});
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if (schema instanceof zod_1.z.ZodEnum) {
|
|
374
|
+
Object.defineProperty(schema, "_cache", {
|
|
375
|
+
value: new Set(schema._def.values),
|
|
376
|
+
enumerable: false,
|
|
377
|
+
writable: true,
|
|
378
|
+
configurable: true,
|
|
379
|
+
});
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (schema instanceof zod_1.z.ZodNativeEnum) {
|
|
383
|
+
const values = schema._def.values;
|
|
384
|
+
const validValues = Object.keys(values)
|
|
385
|
+
.filter((key) => typeof values[String(values[key])] !== "number")
|
|
386
|
+
.map((key) => values[key]);
|
|
387
|
+
Object.defineProperty(schema, "_cache", {
|
|
388
|
+
value: new Set(validValues),
|
|
389
|
+
enumerable: false,
|
|
390
|
+
writable: true,
|
|
391
|
+
configurable: true,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
function hardenSchemaOwnProperties(schema, state) {
|
|
396
|
+
const descriptors = boundedDataDescriptors(schema, state, "schema node");
|
|
397
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
398
|
+
if (key === "_def")
|
|
399
|
+
continue;
|
|
400
|
+
const descriptor = descriptors[key];
|
|
401
|
+
const value = cloneAndHardenGraphValue(descriptor.value, state);
|
|
402
|
+
if (!descriptor.configurable &&
|
|
403
|
+
!descriptor.writable &&
|
|
404
|
+
value !== descriptor.value) {
|
|
405
|
+
throw new TypeError("tool schema graph contains immutable foreign state");
|
|
406
|
+
}
|
|
407
|
+
Object.defineProperty(schema, key, immutableDataDescriptor(descriptor, value));
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
/** Shadow parse-relevant prototype behavior before freezing the node. */
|
|
411
|
+
function shadowZodPrototypeBehavior(schema) {
|
|
412
|
+
let prototype = Object.getPrototypeOf(schema);
|
|
413
|
+
let levels = 0;
|
|
414
|
+
while (prototype && prototype !== Object.prototype) {
|
|
415
|
+
levels += 1;
|
|
416
|
+
if (levels > 16 || node_util_1.types.isProxy(prototype)) {
|
|
417
|
+
throw new TypeError("tool schema graph has an unsupported prototype chain");
|
|
418
|
+
}
|
|
419
|
+
for (const key of Reflect.ownKeys(prototype)) {
|
|
420
|
+
if (key === "constructor" || Object.hasOwn(schema, key))
|
|
421
|
+
continue;
|
|
422
|
+
const descriptor = Object.getOwnPropertyDescriptor(prototype, key);
|
|
423
|
+
if (!descriptor)
|
|
424
|
+
continue;
|
|
425
|
+
if ("value" in descriptor) {
|
|
426
|
+
if (typeof descriptor.value !== "function")
|
|
427
|
+
continue;
|
|
428
|
+
Object.freeze(descriptor.value);
|
|
429
|
+
Object.defineProperty(schema, key, {
|
|
430
|
+
value: descriptor.value,
|
|
431
|
+
enumerable: descriptor.enumerable,
|
|
432
|
+
writable: false,
|
|
433
|
+
configurable: false,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
if (descriptor.get)
|
|
438
|
+
Object.freeze(descriptor.get);
|
|
439
|
+
if (descriptor.set)
|
|
440
|
+
Object.freeze(descriptor.set);
|
|
441
|
+
Object.defineProperty(schema, key, {
|
|
442
|
+
get: descriptor.get,
|
|
443
|
+
set: descriptor.set,
|
|
444
|
+
enumerable: descriptor.enumerable,
|
|
445
|
+
configurable: false,
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
function cloneAndHardenGraphValue(value, state) {
|
|
453
|
+
if (value === null ||
|
|
454
|
+
(typeof value !== "object" && typeof value !== "function")) {
|
|
455
|
+
return value;
|
|
456
|
+
}
|
|
457
|
+
if (node_util_1.types.isProxy(value)) {
|
|
458
|
+
throw new TypeError("tool schema graph must not contain a Proxy");
|
|
459
|
+
}
|
|
460
|
+
if (isZodSchemaNode(value))
|
|
461
|
+
return hardenZodSchemaNode(value, state);
|
|
462
|
+
const existing = state.seen.get(value);
|
|
463
|
+
if (existing)
|
|
464
|
+
return existing;
|
|
465
|
+
if (typeof value === "function") {
|
|
466
|
+
Object.freeze(value);
|
|
467
|
+
state.seen.set(value, value);
|
|
468
|
+
return value;
|
|
469
|
+
}
|
|
470
|
+
if (Array.isArray(value))
|
|
471
|
+
return cloneAndFreezeArray(value, state);
|
|
472
|
+
const mapSize = intrinsicMapSize(value);
|
|
473
|
+
if (mapSize !== undefined)
|
|
474
|
+
return cloneAndFreezeMap(value, mapSize, state);
|
|
475
|
+
const setSize = intrinsicSetSize(value);
|
|
476
|
+
if (setSize !== undefined)
|
|
477
|
+
return cloneAndFreezeSet(value, setSize, state);
|
|
478
|
+
if (value instanceof RegExp &&
|
|
479
|
+
Object.getPrototypeOf(value) === RegExp.prototype) {
|
|
480
|
+
if (value.global || value.sticky) {
|
|
481
|
+
throw new TypeError("tool schema graph does not allow stateful regular expressions");
|
|
482
|
+
}
|
|
483
|
+
enterSchemaGraphValue(state, value);
|
|
484
|
+
try {
|
|
485
|
+
const clone = new RegExp(value.source, value.flags);
|
|
486
|
+
state.seen.set(value, clone);
|
|
487
|
+
// Zod writes lastIndex before every regex test, even for non-stateful
|
|
488
|
+
// expressions. Keep only that behavior-irrelevant slot writable while
|
|
489
|
+
// disabling the mutator and pinning all parse methods/prototype state.
|
|
490
|
+
Object.defineProperties(clone, {
|
|
491
|
+
compile: immutableCollectionMethod("RegExp.compile"),
|
|
492
|
+
test: immutableBoundMethod(RegExp.prototype.test, clone),
|
|
493
|
+
exec: immutableBoundMethod(RegExp.prototype.exec, clone),
|
|
494
|
+
});
|
|
495
|
+
Object.preventExtensions(clone);
|
|
496
|
+
return clone;
|
|
497
|
+
}
|
|
498
|
+
finally {
|
|
499
|
+
leaveSchemaGraphValue(state);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
if (Object.getPrototypeOf(value) === Object.prototype ||
|
|
503
|
+
Object.getPrototypeOf(value) === null) {
|
|
504
|
+
return cloneAndFreezePlainObject(value, state);
|
|
505
|
+
}
|
|
506
|
+
throw new TypeError("tool schema graph contains an unsupported mutable container");
|
|
507
|
+
}
|
|
508
|
+
function cloneAndFreezeArray(value, state) {
|
|
509
|
+
enterSchemaGraphValue(state, value);
|
|
510
|
+
const descriptors = boundedDataDescriptors(value, state, "array");
|
|
511
|
+
if (value.length > MAX_SCHEMA_CONTAINER_KEYS) {
|
|
512
|
+
leaveSchemaGraphValue(state);
|
|
513
|
+
throw new TypeError("tool schema graph array is too large");
|
|
514
|
+
}
|
|
515
|
+
const clone = [];
|
|
516
|
+
state.seen.set(value, clone);
|
|
517
|
+
try {
|
|
518
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
519
|
+
const descriptor = descriptors[String(index)];
|
|
520
|
+
if (!descriptor) {
|
|
521
|
+
throw new TypeError("tool schema graph arrays must be dense");
|
|
522
|
+
}
|
|
523
|
+
clone.push(cloneAndHardenGraphValue(descriptor.value, state));
|
|
524
|
+
}
|
|
525
|
+
const allowedKeys = new Set([
|
|
526
|
+
"length",
|
|
527
|
+
...Array.from({ length: value.length }, (_, index) => String(index)),
|
|
528
|
+
]);
|
|
529
|
+
if (Reflect.ownKeys(descriptors).some((key) => !allowedKeys.has(key))) {
|
|
530
|
+
throw new TypeError("tool schema graph arrays must not have custom properties");
|
|
531
|
+
}
|
|
532
|
+
return Object.freeze(clone);
|
|
533
|
+
}
|
|
534
|
+
finally {
|
|
535
|
+
leaveSchemaGraphValue(state);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
function cloneAndFreezePlainObject(value, state) {
|
|
539
|
+
enterSchemaGraphValue(state, value);
|
|
540
|
+
const descriptors = boundedDataDescriptors(value, state, "plain object");
|
|
541
|
+
const clone = Object.create(Object.getPrototypeOf(value));
|
|
542
|
+
state.seen.set(value, clone);
|
|
543
|
+
try {
|
|
544
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
545
|
+
const descriptor = descriptors[key];
|
|
546
|
+
Object.defineProperty(clone, key, immutableDataDescriptor(descriptor, cloneAndHardenGraphValue(descriptor.value, state)));
|
|
547
|
+
}
|
|
548
|
+
return Object.freeze(clone);
|
|
549
|
+
}
|
|
550
|
+
finally {
|
|
551
|
+
leaveSchemaGraphValue(state);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
function cloneAndFreezeMap(value, initialSize, state) {
|
|
555
|
+
enterSchemaGraphValue(state, value);
|
|
556
|
+
if (initialSize > MAX_SCHEMA_CONTAINER_KEYS) {
|
|
557
|
+
leaveSchemaGraphValue(state);
|
|
558
|
+
throw new TypeError("tool schema graph map is too large");
|
|
559
|
+
}
|
|
560
|
+
const rawEntries = intrinsicMapEntries(value, initialSize);
|
|
561
|
+
const holder = { entries: Object.freeze([]) };
|
|
562
|
+
const facade = immutableMapFacade(holder, initialSize);
|
|
563
|
+
state.seen.set(value, facade);
|
|
564
|
+
try {
|
|
565
|
+
holder.entries = Object.freeze(rawEntries.map(([key, entry]) => Object.freeze([
|
|
566
|
+
cloneAndHardenGraphValue(key, state),
|
|
567
|
+
cloneAndHardenGraphValue(entry, state),
|
|
568
|
+
])));
|
|
569
|
+
Object.freeze(holder);
|
|
570
|
+
return facade;
|
|
571
|
+
}
|
|
572
|
+
finally {
|
|
573
|
+
leaveSchemaGraphValue(state);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
function cloneAndFreezeSet(value, initialSize, state) {
|
|
577
|
+
enterSchemaGraphValue(state, value);
|
|
578
|
+
if (initialSize > MAX_SCHEMA_CONTAINER_KEYS) {
|
|
579
|
+
leaveSchemaGraphValue(state);
|
|
580
|
+
throw new TypeError("tool schema graph set is too large");
|
|
581
|
+
}
|
|
582
|
+
const rawValues = intrinsicSetValues(value, initialSize);
|
|
583
|
+
const holder = {
|
|
584
|
+
values: Object.freeze([]),
|
|
585
|
+
};
|
|
586
|
+
const facade = immutableSetFacade(holder, initialSize);
|
|
587
|
+
state.seen.set(value, facade);
|
|
588
|
+
try {
|
|
589
|
+
holder.values = Object.freeze(rawValues.map((entry) => cloneAndHardenGraphValue(entry, state)));
|
|
590
|
+
Object.freeze(holder);
|
|
591
|
+
return facade;
|
|
592
|
+
}
|
|
593
|
+
finally {
|
|
594
|
+
leaveSchemaGraphValue(state);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
function intrinsicMapSize(value) {
|
|
598
|
+
if (!INTRINSIC_MAP_SIZE)
|
|
599
|
+
return undefined;
|
|
600
|
+
try {
|
|
601
|
+
const size = Reflect.apply(INTRINSIC_MAP_SIZE, value, []);
|
|
602
|
+
return Number.isSafeInteger(size) && size >= 0
|
|
603
|
+
? size
|
|
604
|
+
: undefined;
|
|
605
|
+
}
|
|
606
|
+
catch {
|
|
607
|
+
return undefined;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
function intrinsicSetSize(value) {
|
|
611
|
+
if (!INTRINSIC_SET_SIZE)
|
|
612
|
+
return undefined;
|
|
613
|
+
try {
|
|
614
|
+
const size = Reflect.apply(INTRINSIC_SET_SIZE, value, []);
|
|
615
|
+
return Number.isSafeInteger(size) && size >= 0
|
|
616
|
+
? size
|
|
617
|
+
: undefined;
|
|
618
|
+
}
|
|
619
|
+
catch {
|
|
620
|
+
return undefined;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
function intrinsicMapEntries(value, initialSize) {
|
|
624
|
+
const iterator = Reflect.apply(INTRINSIC_MAP_ENTRIES, value, []);
|
|
625
|
+
const entries = [];
|
|
626
|
+
for (;;) {
|
|
627
|
+
const step = Reflect.apply(INTRINSIC_MAP_ITERATOR_NEXT, iterator, []);
|
|
628
|
+
if (step.done)
|
|
629
|
+
break;
|
|
630
|
+
if (entries.length >= MAX_SCHEMA_CONTAINER_KEYS) {
|
|
631
|
+
throw new TypeError("tool schema graph map iteration exceeded its bound");
|
|
632
|
+
}
|
|
633
|
+
if (!Array.isArray(step.value) || step.value.length !== 2) {
|
|
634
|
+
throw new TypeError("tool schema graph map iterator returned an invalid entry");
|
|
635
|
+
}
|
|
636
|
+
entries.push(Object.freeze([step.value[0], step.value[1]]));
|
|
637
|
+
}
|
|
638
|
+
if (entries.length !== initialSize || intrinsicMapSize(value) !== initialSize) {
|
|
639
|
+
throw new TypeError("tool schema graph map changed during traversal");
|
|
640
|
+
}
|
|
641
|
+
return Object.freeze(entries);
|
|
642
|
+
}
|
|
643
|
+
function intrinsicSetValues(value, initialSize) {
|
|
644
|
+
const iterator = Reflect.apply(INTRINSIC_SET_VALUES, value, []);
|
|
645
|
+
const values = [];
|
|
646
|
+
for (;;) {
|
|
647
|
+
const step = Reflect.apply(INTRINSIC_SET_ITERATOR_NEXT, iterator, []);
|
|
648
|
+
if (step.done)
|
|
649
|
+
break;
|
|
650
|
+
if (values.length >= MAX_SCHEMA_CONTAINER_KEYS) {
|
|
651
|
+
throw new TypeError("tool schema graph set iteration exceeded its bound");
|
|
652
|
+
}
|
|
653
|
+
values.push(step.value);
|
|
654
|
+
}
|
|
655
|
+
if (values.length !== initialSize || intrinsicSetSize(value) !== initialSize) {
|
|
656
|
+
throw new TypeError("tool schema graph set changed during traversal");
|
|
657
|
+
}
|
|
658
|
+
return Object.freeze(values);
|
|
659
|
+
}
|
|
660
|
+
function immutableMapFacade(holder, size) {
|
|
661
|
+
const facade = Object.create(null);
|
|
662
|
+
const get = Object.freeze((candidate) => {
|
|
663
|
+
for (const [key, value] of holder.entries) {
|
|
664
|
+
if (sameValueZero(key, candidate))
|
|
665
|
+
return value;
|
|
666
|
+
}
|
|
667
|
+
return undefined;
|
|
668
|
+
});
|
|
669
|
+
const has = Object.freeze((candidate) => holder.entries.some(([key]) => sameValueZero(key, candidate)));
|
|
670
|
+
const entries = Object.freeze(() => immutableIterator(holder.entries));
|
|
671
|
+
const keys = Object.freeze(() => immutableIterator(holder.entries.map(([key]) => key)));
|
|
672
|
+
const values = Object.freeze(() => immutableIterator(holder.entries.map(([, value]) => value)));
|
|
673
|
+
const forEach = Object.freeze((callback, thisArg) => {
|
|
674
|
+
if (typeof callback !== "function")
|
|
675
|
+
throw new TypeError("callback must be a function");
|
|
676
|
+
for (const [key, value] of holder.entries) {
|
|
677
|
+
Reflect.apply(callback, thisArg, [value, key, facade]);
|
|
678
|
+
}
|
|
679
|
+
});
|
|
680
|
+
Object.defineProperties(facade, {
|
|
681
|
+
size: immutableValueDescriptor(size),
|
|
682
|
+
get: immutableValueDescriptor(get),
|
|
683
|
+
has: immutableValueDescriptor(has),
|
|
684
|
+
entries: immutableValueDescriptor(entries),
|
|
685
|
+
keys: immutableValueDescriptor(keys),
|
|
686
|
+
values: immutableValueDescriptor(values),
|
|
687
|
+
forEach: immutableValueDescriptor(forEach),
|
|
688
|
+
set: immutableCollectionMethod("Map.set"),
|
|
689
|
+
delete: immutableCollectionMethod("Map.delete"),
|
|
690
|
+
clear: immutableCollectionMethod("Map.clear"),
|
|
691
|
+
[Symbol.iterator]: immutableValueDescriptor(entries),
|
|
692
|
+
});
|
|
693
|
+
return Object.freeze(facade);
|
|
694
|
+
}
|
|
695
|
+
function immutableSetFacade(holder, size) {
|
|
696
|
+
const facade = Object.create(null);
|
|
697
|
+
const has = Object.freeze((candidate) => holder.values.some((value) => sameValueZero(value, candidate)));
|
|
698
|
+
const values = Object.freeze(() => immutableIterator(holder.values));
|
|
699
|
+
const entries = Object.freeze(() => immutableIterator(holder.values.map((value) => Object.freeze([value, value]))));
|
|
700
|
+
const forEach = Object.freeze((callback, thisArg) => {
|
|
701
|
+
if (typeof callback !== "function")
|
|
702
|
+
throw new TypeError("callback must be a function");
|
|
703
|
+
for (const value of holder.values) {
|
|
704
|
+
Reflect.apply(callback, thisArg, [value, value, facade]);
|
|
705
|
+
}
|
|
706
|
+
});
|
|
707
|
+
Object.defineProperties(facade, {
|
|
708
|
+
size: immutableValueDescriptor(size),
|
|
709
|
+
has: immutableValueDescriptor(has),
|
|
710
|
+
entries: immutableValueDescriptor(entries),
|
|
711
|
+
keys: immutableValueDescriptor(values),
|
|
712
|
+
values: immutableValueDescriptor(values),
|
|
713
|
+
forEach: immutableValueDescriptor(forEach),
|
|
714
|
+
add: immutableCollectionMethod("Set.add"),
|
|
715
|
+
delete: immutableCollectionMethod("Set.delete"),
|
|
716
|
+
clear: immutableCollectionMethod("Set.clear"),
|
|
717
|
+
[Symbol.iterator]: immutableValueDescriptor(values),
|
|
718
|
+
});
|
|
719
|
+
return Object.freeze(facade);
|
|
720
|
+
}
|
|
721
|
+
function immutableIterator(values) {
|
|
722
|
+
let index = 0;
|
|
723
|
+
const iterator = Object.create(null);
|
|
724
|
+
const next = Object.freeze(() => index < values.length
|
|
725
|
+
? { done: false, value: values[index++] }
|
|
726
|
+
: { done: true, value: undefined });
|
|
727
|
+
const self = Object.freeze(() => iterator);
|
|
728
|
+
Object.defineProperties(iterator, {
|
|
729
|
+
next: immutableValueDescriptor(next),
|
|
730
|
+
[Symbol.iterator]: immutableValueDescriptor(self),
|
|
731
|
+
});
|
|
732
|
+
return Object.freeze(iterator);
|
|
733
|
+
}
|
|
734
|
+
function sameValueZero(left, right) {
|
|
735
|
+
return left === right || (left !== left && right !== right);
|
|
736
|
+
}
|
|
737
|
+
function immutableCollectionMethod(name) {
|
|
738
|
+
const method = Object.freeze(() => {
|
|
739
|
+
throw new TypeError(`${name} is disabled on a hardened tool schema`);
|
|
740
|
+
});
|
|
741
|
+
return {
|
|
742
|
+
value: method,
|
|
743
|
+
enumerable: false,
|
|
744
|
+
writable: false,
|
|
745
|
+
configurable: false,
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
function immutableValueDescriptor(value) {
|
|
749
|
+
return {
|
|
750
|
+
value,
|
|
751
|
+
enumerable: false,
|
|
752
|
+
writable: false,
|
|
753
|
+
configurable: false,
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
function immutableBoundMethod(method, receiver) {
|
|
757
|
+
return {
|
|
758
|
+
value: Object.freeze(method.bind(receiver)),
|
|
759
|
+
enumerable: false,
|
|
760
|
+
writable: false,
|
|
761
|
+
configurable: false,
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
function boundedDataDescriptors(value, state, label) {
|
|
765
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
766
|
+
const keys = Reflect.ownKeys(descriptors);
|
|
767
|
+
if (keys.length > MAX_SCHEMA_CONTAINER_KEYS) {
|
|
768
|
+
throw new TypeError(`tool schema graph ${label} has too many properties`);
|
|
769
|
+
}
|
|
770
|
+
state.ownKeys += keys.length;
|
|
771
|
+
if (state.ownKeys > MAX_SCHEMA_GRAPH_OWN_KEYS) {
|
|
772
|
+
throw new TypeError("tool schema graph property budget exceeded");
|
|
773
|
+
}
|
|
774
|
+
for (const key of keys) {
|
|
775
|
+
if (!("value" in descriptors[key])) {
|
|
776
|
+
throw new TypeError(`tool schema graph ${label} must not contain accessors`);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
return descriptors;
|
|
780
|
+
}
|
|
781
|
+
function immutableDataDescriptor(source, value) {
|
|
782
|
+
return {
|
|
783
|
+
value,
|
|
784
|
+
enumerable: source.enumerable,
|
|
785
|
+
writable: false,
|
|
786
|
+
configurable: false,
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
function enterSchemaGraphValue(state, value) {
|
|
790
|
+
if (node_util_1.types.isProxy(value)) {
|
|
791
|
+
throw new TypeError("tool schema graph must not contain a Proxy");
|
|
792
|
+
}
|
|
793
|
+
state.depth += 1;
|
|
794
|
+
state.nodes += 1;
|
|
795
|
+
if (state.depth > MAX_SCHEMA_GRAPH_DEPTH ||
|
|
796
|
+
state.nodes > MAX_SCHEMA_GRAPH_NODES) {
|
|
797
|
+
state.depth -= 1;
|
|
798
|
+
throw new TypeError("tool schema graph traversal budget exceeded");
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
function leaveSchemaGraphValue(state) {
|
|
802
|
+
state.depth -= 1;
|
|
803
|
+
}
|
|
804
|
+
function isZodSchemaNode(value) {
|
|
805
|
+
return (typeof value === "object" &&
|
|
806
|
+
value !== null &&
|
|
807
|
+
!node_util_1.types.isProxy(value) &&
|
|
808
|
+
value instanceof zod_1.z.ZodType);
|
|
809
|
+
}
|
|
810
|
+
/** Define, validate, snapshot, and freeze a profile-resolved tool module. */
|
|
811
|
+
function defineServiceNowToolModule(candidate) {
|
|
812
|
+
validateFactoryToolModule(candidate);
|
|
813
|
+
const handler = candidate.handler;
|
|
814
|
+
return freezeModule({
|
|
815
|
+
...candidate,
|
|
816
|
+
// Registration validates with this exact schema before access preflight.
|
|
817
|
+
// The resolver may add private field-policy preparation metadata, so a
|
|
818
|
+
// second Zod parse here would incorrectly strip that authorized metadata.
|
|
819
|
+
invoke: async (args, services) => handler(args, services),
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
/** Define, validate, snapshot, and freeze a client-free diagnostic module. */
|
|
823
|
+
function defineContextOnlyToolModule(candidate) {
|
|
824
|
+
validateFactoryToolModule(candidate);
|
|
825
|
+
const handler = candidate.handler;
|
|
826
|
+
return freezeModule({
|
|
827
|
+
...candidate,
|
|
828
|
+
invoke: async (args, services) => handler(args, services),
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Validate the complete catalog before registration performs its first side
|
|
833
|
+
* effect. This runtime guard also protects callers that bypass TypeScript.
|
|
834
|
+
*/
|
|
835
|
+
function validateToolModuleCatalog(modules) {
|
|
836
|
+
snapshotToolModuleCatalog(modules);
|
|
837
|
+
}
|
|
838
|
+
/**
|
|
839
|
+
* Snapshot and validate the full issued catalog before registration starts.
|
|
840
|
+
* Proxies, sparse/accessor arrays, and hand-forged module objects fail closed.
|
|
841
|
+
*/
|
|
842
|
+
function snapshotToolModuleCatalog(modules) {
|
|
843
|
+
if (!Array.isArray(modules) ||
|
|
844
|
+
node_util_1.types.isProxy(modules) ||
|
|
845
|
+
modules.length === 0 ||
|
|
846
|
+
modules.length > MAX_TOOL_MODULES) {
|
|
847
|
+
throw new TypeError("tool module catalog must not be empty");
|
|
848
|
+
}
|
|
849
|
+
const names = new Set();
|
|
850
|
+
const snapshot = [];
|
|
851
|
+
for (let index = 0; index < modules.length; index += 1) {
|
|
852
|
+
const descriptor = Object.getOwnPropertyDescriptor(modules, String(index));
|
|
853
|
+
if (!descriptor || !("value" in descriptor)) {
|
|
854
|
+
throw new TypeError("tool module catalog must contain data entries");
|
|
855
|
+
}
|
|
856
|
+
const module = descriptor.value;
|
|
857
|
+
validateRegisteredToolModule(module);
|
|
858
|
+
if (names.has(module.definition.name)) {
|
|
859
|
+
throw new TypeError(`duplicate tool module name: ${module.definition.name}`);
|
|
860
|
+
}
|
|
861
|
+
names.add(module.definition.name);
|
|
862
|
+
snapshot.push(module);
|
|
863
|
+
}
|
|
864
|
+
return Object.freeze(snapshot);
|
|
865
|
+
}
|
|
866
|
+
function validateFactoryToolModule(candidate) {
|
|
867
|
+
validateToolModuleShape(candidate, "factory");
|
|
868
|
+
if (typeof candidate.handler !== "function" || candidate.invoke !== undefined) {
|
|
869
|
+
throw new TypeError(`${candidate.definition?.name ?? "tool module"}: factory requires handler and prohibits invoke`);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
function validateRegisteredToolModule(candidate) {
|
|
873
|
+
validateToolModuleShape(candidate, "registered");
|
|
874
|
+
if (typeof candidate.invoke !== "function" || candidate.handler !== undefined) {
|
|
875
|
+
throw new TypeError(`${candidate.definition?.name ?? "tool module"}: registered module requires invoke and prohibits handler`);
|
|
876
|
+
}
|
|
877
|
+
if (!ISSUED_TOOL_MODULES.has(candidate)) {
|
|
878
|
+
throw new TypeError(`${candidate.definition?.name ?? "tool module"}: registered module was not issued by a contract factory`);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
function validateToolModuleShape(candidate, phase) {
|
|
882
|
+
assertPlainDataObject(candidate, `${phase} tool module`, [
|
|
883
|
+
"definition",
|
|
884
|
+
"inputSchema",
|
|
885
|
+
"outputSchema",
|
|
886
|
+
"requirements",
|
|
887
|
+
"resolveAccess",
|
|
888
|
+
"runtime",
|
|
889
|
+
phase === "factory" ? "handler" : "invoke",
|
|
890
|
+
]);
|
|
891
|
+
const definition = candidate.definition;
|
|
892
|
+
assertPlainDataObject(definition, "tool module definition", [
|
|
893
|
+
"name",
|
|
894
|
+
"description",
|
|
895
|
+
"annotations",
|
|
896
|
+
]);
|
|
897
|
+
if (!/^sn_[a-z][a-z0-9_]*$/u.test(String(definition.name))) {
|
|
898
|
+
throw new TypeError("tool module name must use the sn_* namespace");
|
|
899
|
+
}
|
|
900
|
+
const typedDefinition = definition;
|
|
901
|
+
if (!typedDefinition.description?.trim()) {
|
|
902
|
+
throw new TypeError(`${typedDefinition.name}: description is required`);
|
|
903
|
+
}
|
|
904
|
+
validateAnnotations(typedDefinition.name, typedDefinition.annotations);
|
|
905
|
+
validateProfileSchema(typedDefinition.name, "input", candidate.inputSchema, false);
|
|
906
|
+
validateProfileSchema(typedDefinition.name, "output", candidate.outputSchema, true);
|
|
907
|
+
validateRequirements(typedDefinition, candidate.requirements, candidate.runtime);
|
|
908
|
+
if (typeof candidate.resolveAccess !== "function") {
|
|
909
|
+
throw new TypeError(`${typedDefinition.name}: access resolver is required`);
|
|
910
|
+
}
|
|
911
|
+
if (candidate.runtime !== "servicenow" && candidate.runtime !== "context-only") {
|
|
912
|
+
throw new TypeError(`${typedDefinition.name}: runtime declaration is incomplete`);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
function validateAnnotations(name, annotations) {
|
|
916
|
+
assertPlainDataObject(annotations, `${name} annotations`, [
|
|
917
|
+
"title",
|
|
918
|
+
"readOnlyHint",
|
|
919
|
+
"destructiveHint",
|
|
920
|
+
"idempotentHint",
|
|
921
|
+
"openWorldHint",
|
|
922
|
+
]);
|
|
923
|
+
if (!annotations ||
|
|
924
|
+
!annotations.title?.trim() ||
|
|
925
|
+
typeof annotations.readOnlyHint !== "boolean" ||
|
|
926
|
+
typeof annotations.destructiveHint !== "boolean" ||
|
|
927
|
+
typeof annotations.idempotentHint !== "boolean" ||
|
|
928
|
+
typeof annotations.openWorldHint !== "boolean") {
|
|
929
|
+
throw new TypeError(`${name}: complete MCP annotations are required`);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
function validateProfileSchema(name, kind, schema, requireStrict) {
|
|
933
|
+
let issued;
|
|
934
|
+
try {
|
|
935
|
+
if (schema)
|
|
936
|
+
issued = issuedSchemaSnapshot(schema, kind);
|
|
937
|
+
}
|
|
938
|
+
catch {
|
|
939
|
+
// The stable contract error below intentionally hides exotic object detail.
|
|
940
|
+
}
|
|
941
|
+
if (!schema ||
|
|
942
|
+
!issued ||
|
|
943
|
+
schema.shape.profile !== exports.profileNameSchema ||
|
|
944
|
+
(requireStrict && schema._def.unknownKeys !== "strict")) {
|
|
945
|
+
throw new TypeError(`${name}: ${kind} schema must use the shared required profile schema and be an issued contract-normalized Zod object${requireStrict ? " in strict mode" : ""}`);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
function validateRequirements(definition, requirements, runtime) {
|
|
949
|
+
const name = definition.name;
|
|
950
|
+
if (!requirements) {
|
|
951
|
+
throw new TypeError(`${name}: permission/dependency declaration is required`);
|
|
952
|
+
}
|
|
953
|
+
assertPlainDataObject(requirements, `${name} requirements`, [
|
|
954
|
+
"permissions",
|
|
955
|
+
"tables",
|
|
956
|
+
"apis",
|
|
957
|
+
"fieldPolicies",
|
|
958
|
+
"capabilities",
|
|
959
|
+
]);
|
|
960
|
+
const permissions = uniqueEnumValues(name, "permission", requirements.permissions, ["read", "write"]);
|
|
961
|
+
const fieldPolicies = uniqueEnumValues(name, "field policy", requirements.fieldPolicies, ["read", "write"]);
|
|
962
|
+
uniqueEnumValues(name, "API", requirements.apis, [
|
|
963
|
+
"aggregate",
|
|
964
|
+
"atf",
|
|
965
|
+
"attachment",
|
|
966
|
+
"table",
|
|
967
|
+
]);
|
|
968
|
+
validateTables(name, requirements.tables);
|
|
969
|
+
if (permissions.size === 0) {
|
|
970
|
+
throw new TypeError(`${name}: at least one permission is required`);
|
|
971
|
+
}
|
|
972
|
+
if (!Array.isArray(requirements.capabilities) || requirements.capabilities.length === 0) {
|
|
973
|
+
throw new TypeError(`${name}: at least one capability is required`);
|
|
974
|
+
}
|
|
975
|
+
validateUniqueNames(name, "capability", requirements.capabilities);
|
|
976
|
+
for (const fieldPolicy of fieldPolicies) {
|
|
977
|
+
if (!permissions.has(fieldPolicy)) {
|
|
978
|
+
throw new TypeError(`${name}: field policy exceeds declared permissions`);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
if (definition.annotations.readOnlyHint && permissions.has("write")) {
|
|
982
|
+
throw new TypeError(`${name}: read-only annotation conflicts with write permission`);
|
|
983
|
+
}
|
|
984
|
+
if (!definition.annotations.readOnlyHint && !permissions.has("write")) {
|
|
985
|
+
throw new TypeError(`${name}: non-read-only tool requires write permission`);
|
|
986
|
+
}
|
|
987
|
+
if (definition.annotations.destructiveHint && !permissions.has("write")) {
|
|
988
|
+
throw new TypeError(`${name}: destructive tool requires write permission`);
|
|
989
|
+
}
|
|
990
|
+
if (runtime === "context-only" &&
|
|
991
|
+
(requirements.tables.kind !== "none" || requirements.apis.length > 0)) {
|
|
992
|
+
throw new TypeError(`${name}: context-only tool cannot declare ServiceNow dependencies`);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
function validateTables(name, tables) {
|
|
996
|
+
assertPlainDataObject(tables, `${name} table dependencies`, [
|
|
997
|
+
"kind",
|
|
998
|
+
"names",
|
|
999
|
+
"description",
|
|
1000
|
+
]);
|
|
1001
|
+
if (!tables || !["none", "static", "dynamic"].includes(tables.kind)) {
|
|
1002
|
+
throw new TypeError(`${name}: table dependency declaration is required`);
|
|
1003
|
+
}
|
|
1004
|
+
if (tables.kind === "none")
|
|
1005
|
+
return;
|
|
1006
|
+
validateUniqueNames(name, "table dependency", tables.names);
|
|
1007
|
+
if (tables.kind === "static" && tables.names.length === 0) {
|
|
1008
|
+
throw new TypeError(`${name}: static table dependencies must not be empty`);
|
|
1009
|
+
}
|
|
1010
|
+
if (tables.kind === "dynamic" && !tables.description.trim()) {
|
|
1011
|
+
throw new TypeError(`${name}: dynamic table dependencies need a description`);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
function validateUniqueNames(moduleName, label, values) {
|
|
1015
|
+
if (!Array.isArray(values)) {
|
|
1016
|
+
throw new TypeError(`${moduleName}: ${label} declaration is required`);
|
|
1017
|
+
}
|
|
1018
|
+
if (node_util_1.types.isProxy(values)) {
|
|
1019
|
+
throw new TypeError(`${moduleName}: ${label} declaration must not be a Proxy`);
|
|
1020
|
+
}
|
|
1021
|
+
for (let index = 0; index < values.length; index += 1) {
|
|
1022
|
+
const descriptor = Object.getOwnPropertyDescriptor(values, String(index));
|
|
1023
|
+
if (!descriptor || !("value" in descriptor)) {
|
|
1024
|
+
throw new TypeError(`${moduleName}: ${label} declaration must contain data entries`);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
const normalized = values.map((value) => {
|
|
1028
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
1029
|
+
throw new TypeError(`${moduleName}: invalid ${label}`);
|
|
1030
|
+
}
|
|
1031
|
+
return value.trim();
|
|
1032
|
+
});
|
|
1033
|
+
if (new Set(normalized).size !== normalized.length) {
|
|
1034
|
+
throw new TypeError(`${moduleName}: duplicate ${label}`);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
function assertPlainDataObject(candidate, label, allowedKeys) {
|
|
1038
|
+
if (typeof candidate !== "object" ||
|
|
1039
|
+
candidate === null ||
|
|
1040
|
+
node_util_1.types.isProxy(candidate) ||
|
|
1041
|
+
(Object.getPrototypeOf(candidate) !== Object.prototype &&
|
|
1042
|
+
Object.getPrototypeOf(candidate) !== null)) {
|
|
1043
|
+
throw new TypeError(`${label} must be a plain data object`);
|
|
1044
|
+
}
|
|
1045
|
+
const allowed = new Set(allowedKeys);
|
|
1046
|
+
for (const key of Reflect.ownKeys(candidate)) {
|
|
1047
|
+
if (typeof key !== "string" || !allowed.has(key)) {
|
|
1048
|
+
throw new TypeError(`${label} contains an unsupported property`);
|
|
1049
|
+
}
|
|
1050
|
+
const descriptor = Object.getOwnPropertyDescriptor(candidate, key);
|
|
1051
|
+
if (!descriptor || !("value" in descriptor)) {
|
|
1052
|
+
throw new TypeError(`${label} must not contain accessors`);
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
function uniqueEnumValues(moduleName, label, values, allowed) {
|
|
1057
|
+
validateUniqueNames(moduleName, label, values);
|
|
1058
|
+
const result = new Set();
|
|
1059
|
+
for (const value of values ?? []) {
|
|
1060
|
+
if (!allowed.includes(value)) {
|
|
1061
|
+
throw new TypeError(`${moduleName}: unsupported ${label}`);
|
|
1062
|
+
}
|
|
1063
|
+
result.add(value);
|
|
1064
|
+
}
|
|
1065
|
+
return result;
|
|
1066
|
+
}
|
|
1067
|
+
function freezeModule(module) {
|
|
1068
|
+
const tables = module.requirements.tables.kind === "none"
|
|
1069
|
+
? Object.freeze({ kind: "none" })
|
|
1070
|
+
: Object.freeze({
|
|
1071
|
+
...module.requirements.tables,
|
|
1072
|
+
names: Object.freeze([...module.requirements.tables.names]),
|
|
1073
|
+
});
|
|
1074
|
+
const frozen = Object.freeze({
|
|
1075
|
+
definition: Object.freeze({
|
|
1076
|
+
...module.definition,
|
|
1077
|
+
annotations: Object.freeze({ ...module.definition.annotations }),
|
|
1078
|
+
}),
|
|
1079
|
+
inputSchema: module.inputSchema,
|
|
1080
|
+
outputSchema: module.outputSchema,
|
|
1081
|
+
requirements: Object.freeze({
|
|
1082
|
+
permissions: Object.freeze([...module.requirements.permissions]),
|
|
1083
|
+
tables,
|
|
1084
|
+
apis: Object.freeze([...module.requirements.apis]),
|
|
1085
|
+
fieldPolicies: Object.freeze([...module.requirements.fieldPolicies]),
|
|
1086
|
+
capabilities: Object.freeze([...module.requirements.capabilities]),
|
|
1087
|
+
}),
|
|
1088
|
+
resolveAccess: module.resolveAccess,
|
|
1089
|
+
runtime: module.runtime,
|
|
1090
|
+
invoke: module.invoke,
|
|
1091
|
+
});
|
|
1092
|
+
ISSUED_TOOL_MODULES.add(frozen);
|
|
1093
|
+
return frozen;
|
|
1094
|
+
}
|
|
1095
|
+
//# sourceMappingURL=tool-module.js.map
|