@onlyflows/servicenow-mcp 1.0.0 → 2.1.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 +214 -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 +441 -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 +711 -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/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 +98 -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/index.d.ts +47 -36
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +814 -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/record-write-shared.d.ts +7 -0
- package/dist/tools/record-write-shared.d.ts.map +1 -0
- package/dist/tools/record-write-shared.js +65 -0
- package/dist/tools/record-write-shared.js.map +1 -0
- 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 +770 -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 +111 -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/dist/write-value-policy.d.ts +56 -0
- package/dist/write-value-policy.d.ts.map +1 -0
- package/dist/write-value-policy.js +310 -0
- package/dist/write-value-policy.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
package/dist/tools/index.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* High-level ServiceNow tool registry.
|
|
4
|
+
*
|
|
5
|
+
* Tool-local Zod schemas describe handler inputs. Registration composes each
|
|
6
|
+
* one with the required profile selector, making the resulting Zod object the
|
|
7
|
+
* single source for both MCP discovery and runtime validation.
|
|
4
8
|
*
|
|
5
9
|
* @module tools
|
|
6
10
|
*/
|
|
@@ -38,26 +42,64 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
38
42
|
};
|
|
39
43
|
})();
|
|
40
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
-
exports.tools = void 0;
|
|
42
|
-
exports.
|
|
43
|
-
exports.
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
45
|
+
exports.REGISTERED_TOOL_COUNT = exports.toolModules = exports.profileTools = exports.tools = exports.withStructuredResultEnvelope = exports.resultPaginationSchema = exports.resultMetadataSchema = exports.productionToolOutputSchemas = exports.finalizeEnvelopeResult = exports.envelopeCompatibilityResult = exports.withResolvedProfileOutput = exports.withRequiredProfile = exports.validateToolModuleCatalog = exports.profileOutputSchema = exports.profileNameSchema = exports.defineServiceNowToolModule = exports.defineContextOnlyToolModule = void 0;
|
|
46
|
+
exports.registerServiceNowTools = registerServiceNowTools;
|
|
47
|
+
exports.registerServiceNowToolModules = registerServiceNowToolModules;
|
|
48
|
+
exports.enrichSuccessfulResult = enrichSuccessfulResult;
|
|
49
|
+
const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
|
|
50
|
+
const inMemory_js_1 = require("@modelcontextprotocol/sdk/inMemory.js");
|
|
51
|
+
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
52
|
+
const node_util_1 = require("node:util");
|
|
53
|
+
const client_js_1 = require("../client.js");
|
|
54
|
+
const metadata_cache_js_1 = require("../metadata-cache.js");
|
|
55
|
+
const field_policy_js_1 = require("../field-policy.js");
|
|
56
|
+
const encoded_query_policy_js_1 = require("../encoded-query-policy.js");
|
|
57
|
+
const profile_credentials_js_1 = require("../profile-credentials.js");
|
|
58
|
+
const incident_journal_policy_js_1 = require("../incident-journal-policy.js");
|
|
59
|
+
const write_value_policy_js_1 = require("../write-value-policy.js");
|
|
60
|
+
const execution_context_js_1 = require("../execution-context.js");
|
|
61
|
+
const profile_manager_js_1 = require("../profile-manager.js");
|
|
62
|
+
const table_policy_js_1 = require("../table-policy.js");
|
|
63
|
+
const structured_query_js_1 = require("../structured-query.js");
|
|
64
|
+
const tool_error_js_1 = require("../tool-error.js");
|
|
65
|
+
const utils_js_1 = require("../utils.js");
|
|
66
|
+
const catalog_js_1 = require("./catalog.js");
|
|
50
67
|
const aggregate = __importStar(require("./aggregate.js"));
|
|
51
|
-
const
|
|
52
|
-
const health = __importStar(require("./health.js"));
|
|
68
|
+
const atf = __importStar(require("./atf.js"));
|
|
53
69
|
const attach = __importStar(require("./attach.js"));
|
|
54
|
-
const
|
|
55
|
-
const syslog = __importStar(require("./syslog.js"));
|
|
70
|
+
const batch = __importStar(require("./batch.js"));
|
|
56
71
|
const codesearch = __importStar(require("./codesearch.js"));
|
|
72
|
+
const create = __importStar(require("./create.js"));
|
|
73
|
+
const del = __importStar(require("./delete.js"));
|
|
57
74
|
const discover = __importStar(require("./discover.js"));
|
|
58
|
-
const
|
|
75
|
+
const get = __importStar(require("./get.js"));
|
|
76
|
+
const health = __importStar(require("./health.js"));
|
|
59
77
|
const nl = __importStar(require("./nl.js"));
|
|
60
|
-
const
|
|
78
|
+
const profile = __importStar(require("./profile.js"));
|
|
79
|
+
const query = __importStar(require("./query.js"));
|
|
80
|
+
const relationships = __importStar(require("./relationships.js"));
|
|
81
|
+
const schema = __importStar(require("./schema.js"));
|
|
82
|
+
const syslog = __importStar(require("./syslog.js"));
|
|
83
|
+
const update = __importStar(require("./update.js"));
|
|
84
|
+
const result_envelope_js_1 = require("./result-envelope.js");
|
|
85
|
+
const handler_result_js_1 = require("./handler-result.js");
|
|
86
|
+
const tool_module_js_1 = require("./tool-module.js");
|
|
87
|
+
var tool_module_js_2 = require("./tool-module.js");
|
|
88
|
+
Object.defineProperty(exports, "defineContextOnlyToolModule", { enumerable: true, get: function () { return tool_module_js_2.defineContextOnlyToolModule; } });
|
|
89
|
+
Object.defineProperty(exports, "defineServiceNowToolModule", { enumerable: true, get: function () { return tool_module_js_2.defineServiceNowToolModule; } });
|
|
90
|
+
Object.defineProperty(exports, "profileNameSchema", { enumerable: true, get: function () { return tool_module_js_2.profileNameSchema; } });
|
|
91
|
+
Object.defineProperty(exports, "profileOutputSchema", { enumerable: true, get: function () { return tool_module_js_2.profileOutputSchema; } });
|
|
92
|
+
Object.defineProperty(exports, "validateToolModuleCatalog", { enumerable: true, get: function () { return tool_module_js_2.validateToolModuleCatalog; } });
|
|
93
|
+
Object.defineProperty(exports, "withRequiredProfile", { enumerable: true, get: function () { return tool_module_js_2.withRequiredProfile; } });
|
|
94
|
+
Object.defineProperty(exports, "withResolvedProfileOutput", { enumerable: true, get: function () { return tool_module_js_2.withResolvedProfileOutput; } });
|
|
95
|
+
var result_envelope_js_2 = require("./result-envelope.js");
|
|
96
|
+
Object.defineProperty(exports, "envelopeCompatibilityResult", { enumerable: true, get: function () { return result_envelope_js_2.envelopeCompatibilityResult; } });
|
|
97
|
+
Object.defineProperty(exports, "finalizeEnvelopeResult", { enumerable: true, get: function () { return result_envelope_js_2.finalizeEnvelopeResult; } });
|
|
98
|
+
Object.defineProperty(exports, "productionToolOutputSchemas", { enumerable: true, get: function () { return result_envelope_js_2.productionToolOutputSchemas; } });
|
|
99
|
+
Object.defineProperty(exports, "resultMetadataSchema", { enumerable: true, get: function () { return result_envelope_js_2.resultMetadataSchema; } });
|
|
100
|
+
Object.defineProperty(exports, "resultPaginationSchema", { enumerable: true, get: function () { return result_envelope_js_2.resultPaginationSchema; } });
|
|
101
|
+
Object.defineProperty(exports, "withStructuredResultEnvelope", { enumerable: true, get: function () { return result_envelope_js_2.withStructuredResultEnvelope; } });
|
|
102
|
+
/** Standard tools that resolve a client/config only after profile validation. */
|
|
61
103
|
exports.tools = [
|
|
62
104
|
query,
|
|
63
105
|
get,
|
|
@@ -75,36 +117,773 @@ exports.tools = [
|
|
|
75
117
|
discover,
|
|
76
118
|
atf,
|
|
77
119
|
nl,
|
|
78
|
-
script,
|
|
79
120
|
];
|
|
121
|
+
/** Read-only diagnostic tool; it deliberately never resolves credentials. */
|
|
122
|
+
exports.profileTools = [profile];
|
|
123
|
+
/** Contract-bearing modules consumed by the registration dispatcher. */
|
|
124
|
+
exports.toolModules = catalog_js_1.allToolModules;
|
|
125
|
+
exports.REGISTERED_TOOL_COUNT = exports.toolModules.length;
|
|
80
126
|
/**
|
|
81
|
-
*
|
|
127
|
+
* Register all 19 published tools through the SDK's high-level API.
|
|
128
|
+
*
|
|
129
|
+
* The contract factory retains each module's inferred Zod type while this
|
|
130
|
+
* dispatcher consumes its deliberately erased, runtime-validated boundary.
|
|
82
131
|
*/
|
|
83
|
-
function
|
|
84
|
-
|
|
132
|
+
async function registerServiceNowTools(mcpServer, profileManager, contextDependencies, logger = tool_module_js_1.SILENT_TOOL_MODULE_LOGGER) {
|
|
133
|
+
await registerServiceNowToolModules(mcpServer, profileManager, contextDependencies, exports.toolModules, logger);
|
|
134
|
+
}
|
|
135
|
+
/** Register an explicit catalog after an all-or-nothing contract preflight. */
|
|
136
|
+
async function registerServiceNowToolModules(mcpServer, profileManager, contextDependencies, modules, logger = tool_module_js_1.SILENT_TOOL_MODULE_LOGGER) {
|
|
137
|
+
if (!logger || typeof logger.write !== "function") {
|
|
138
|
+
throw new TypeError("tool module logger must implement write");
|
|
139
|
+
}
|
|
140
|
+
const catalog = (0, tool_module_js_1.snapshotToolModuleCatalog)(modules);
|
|
141
|
+
const plans = Object.freeze(catalog.map((module) => Object.freeze({ module, config: toolRegistrationConfig(module) })));
|
|
142
|
+
await preflightSdkToolConfigurations(plans);
|
|
143
|
+
for (const { module, config } of plans) {
|
|
144
|
+
if (module.runtime === "servicenow") {
|
|
145
|
+
registerStandardTool(mcpServer, profileManager, contextDependencies, module, logger, config);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
registerProfileDiagnostic(mcpServer, profileManager, contextDependencies, module, logger, config);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function toolRegistrationConfig(tool) {
|
|
153
|
+
return Object.freeze({
|
|
154
|
+
description: tool.definition.description,
|
|
155
|
+
inputSchema: (0, tool_module_js_1.snapshotIssuedToolSchema)(tool.inputSchema, "input"),
|
|
156
|
+
outputSchema: (0, tool_module_js_1.snapshotIssuedToolSchema)(tool.outputSchema, "output"),
|
|
157
|
+
annotations: tool.definition.annotations,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/** Materialize and discover every frozen config before target writes. */
|
|
161
|
+
async function preflightSdkToolConfigurations(plans) {
|
|
162
|
+
const preflight = new mcp_js_1.McpServer({ name: "servicenow-tool-catalog-preflight", version: "0.0.0" }, { capabilities: { tools: {} } });
|
|
163
|
+
for (const { module, config } of plans) {
|
|
164
|
+
preflight.registerTool(module.definition.name, config, async () => ({
|
|
165
|
+
content: [{ type: "text", text: "registration preflight only" }],
|
|
166
|
+
structuredContent: { profile: "registration-preflight" },
|
|
167
|
+
}));
|
|
168
|
+
}
|
|
169
|
+
const client = new index_js_1.Client({
|
|
170
|
+
name: "servicenow-tool-catalog-preflight-client",
|
|
171
|
+
version: "0.0.0",
|
|
172
|
+
});
|
|
173
|
+
const [clientTransport, serverTransport] = inMemory_js_1.InMemoryTransport.createLinkedPair();
|
|
174
|
+
try {
|
|
175
|
+
await preflight.connect(serverTransport);
|
|
176
|
+
await client.connect(clientTransport);
|
|
177
|
+
const discovered = (await client.listTools()).tools;
|
|
178
|
+
validateDiscoveredToolConfigurations(plans, discovered);
|
|
179
|
+
}
|
|
180
|
+
finally {
|
|
181
|
+
await client.close().catch(() => { });
|
|
182
|
+
if (preflight.isConnected())
|
|
183
|
+
await preflight.close().catch(() => { });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function validateDiscoveredToolConfigurations(plans, discovered) {
|
|
187
|
+
if (discovered.length !== plans.length) {
|
|
188
|
+
throw new TypeError("SDK tool catalog preflight returned an incomplete catalog");
|
|
189
|
+
}
|
|
190
|
+
const byName = new Map();
|
|
191
|
+
for (const candidate of discovered) {
|
|
192
|
+
if (typeof candidate !== "object" || candidate === null || Array.isArray(candidate)) {
|
|
193
|
+
throw new TypeError("SDK tool catalog preflight returned an invalid tool");
|
|
194
|
+
}
|
|
195
|
+
const record = candidate;
|
|
196
|
+
if (typeof record.name !== "string" || byName.has(record.name)) {
|
|
197
|
+
throw new TypeError("SDK tool catalog preflight returned an invalid tool name");
|
|
198
|
+
}
|
|
199
|
+
byName.set(record.name, record);
|
|
200
|
+
}
|
|
201
|
+
for (const { module } of plans) {
|
|
202
|
+
const discoveredTool = byName.get(module.definition.name);
|
|
203
|
+
if (!discoveredTool) {
|
|
204
|
+
throw new TypeError("SDK tool catalog preflight omitted a tool");
|
|
205
|
+
}
|
|
206
|
+
validateDiscoveryObjectSchema(module.definition.name, "input", discoveredTool.inputSchema);
|
|
207
|
+
validateDiscoveryObjectSchema(module.definition.name, "output", discoveredTool.outputSchema);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function validateDiscoveryObjectSchema(toolName, kind, candidate) {
|
|
211
|
+
if (typeof candidate !== "object" || candidate === null || Array.isArray(candidate)) {
|
|
212
|
+
throw new TypeError(`${toolName}: SDK ${kind} discovery schema is invalid`);
|
|
213
|
+
}
|
|
214
|
+
const schema = candidate;
|
|
215
|
+
const properties = schema.properties;
|
|
216
|
+
const required = schema.required;
|
|
217
|
+
if (schema.type !== "object" ||
|
|
218
|
+
schema.additionalProperties !== false ||
|
|
219
|
+
typeof properties !== "object" ||
|
|
220
|
+
properties === null ||
|
|
221
|
+
Array.isArray(properties) ||
|
|
222
|
+
!Array.isArray(required) ||
|
|
223
|
+
!required.includes("profile")) {
|
|
224
|
+
throw new TypeError(`${toolName}: SDK ${kind} discovery schema is incomplete`);
|
|
225
|
+
}
|
|
226
|
+
const profileProperty = Reflect.get(properties, "profile");
|
|
227
|
+
if (typeof profileProperty !== "object" ||
|
|
228
|
+
profileProperty === null ||
|
|
229
|
+
Reflect.get(profileProperty, "type") !== "string") {
|
|
230
|
+
throw new TypeError(`${toolName}: SDK ${kind} discovery profile is invalid`);
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
JSON.stringify(schema);
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
throw new TypeError(`${toolName}: SDK ${kind} discovery schema is not materializable`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function registerStandardTool(mcpServer, profileManager, sharedContextDependencies, tool, logger, registrationConfig) {
|
|
240
|
+
mcpServer.registerTool(tool.definition.name, registrationConfig, async (args, extra) => {
|
|
241
|
+
const contextDependencies = dependenciesForToolInvocation(sharedContextDependencies);
|
|
242
|
+
const request = await resolveRequestMetadata(tool.definition.name, extra, contextDependencies);
|
|
243
|
+
if (request.kind === "cancelled") {
|
|
244
|
+
auditPreContextCancellation(contextDependencies, tool.definition.name, request.metadata, request.reason);
|
|
245
|
+
return (0, utils_js_1.err)(`Tool ${JSON.stringify(tool.definition.name)} was cancelled. ` +
|
|
246
|
+
`Correlation ID: ${request.metadata.correlationId}.`);
|
|
247
|
+
}
|
|
248
|
+
if (request.kind === "unavailable") {
|
|
249
|
+
auditPreContextRejection(contextDependencies, tool.definition.name, request.metadata);
|
|
250
|
+
return (0, utils_js_1.err)(`Request metadata could not be initialized. Correlation ID: ${request.metadata.correlationId}.`);
|
|
251
|
+
}
|
|
252
|
+
// The SDK has already validated and transformed this composed schema.
|
|
253
|
+
// Reassert the shared selector type because the SDK's generic callback
|
|
254
|
+
// widens an extended AnyZodObject property to unknown in TypeScript.
|
|
255
|
+
const profileName = tool_module_js_1.profileNameSchema.parse(args.profile);
|
|
256
|
+
const resolution = resolveProfile(profileManager, profileName);
|
|
257
|
+
if (resolution.kind === "unknown") {
|
|
258
|
+
auditInvocation(contextDependencies, { outcome: "profile_rejected", reason: "unknown_profile" }, tool.definition.name, request.metadata);
|
|
259
|
+
return correlateErrorResult(unknownProfile(profileName), request.metadata.correlationId);
|
|
260
|
+
}
|
|
261
|
+
if (resolution.kind === "invalid") {
|
|
262
|
+
auditInvocation(contextDependencies, { outcome: "profile_rejected", reason: "invalid_profile" }, tool.definition.name, request.metadata);
|
|
263
|
+
return correlateErrorResult(invalidProfile(profileName), request.metadata.correlationId);
|
|
264
|
+
}
|
|
265
|
+
const binding = Object.freeze({
|
|
266
|
+
name: profileName,
|
|
267
|
+
instance: resolution.canonicalInstance,
|
|
268
|
+
});
|
|
269
|
+
let context;
|
|
270
|
+
try {
|
|
271
|
+
context = await (0, execution_context_js_1.createExecutionContext)(request.metadata, binding, tool.definition.name, contextDependencies.effectivePolicyProvider, contextDependencies.requestSignal);
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
const cancellation = cancellationResultIfIssued(contextDependencies, contextDependencies.requestSignal, binding, tool.definition.name, request.metadata);
|
|
275
|
+
if (cancellation)
|
|
276
|
+
return cancellation;
|
|
277
|
+
auditInvocation(contextDependencies, {
|
|
278
|
+
outcome: "context_rejected",
|
|
279
|
+
reason: "policy_context_unavailable",
|
|
280
|
+
profile: binding,
|
|
281
|
+
}, tool.definition.name, request.metadata);
|
|
282
|
+
return (0, utils_js_1.err)(`Request context could not be initialized. Correlation ID: ${request.metadata.correlationId}.`);
|
|
283
|
+
}
|
|
284
|
+
const contextCancellation = cancellationResultIfIssued(contextDependencies, context.signal, binding, tool.definition.name, request.metadata);
|
|
285
|
+
if (contextCancellation)
|
|
286
|
+
return contextCancellation;
|
|
287
|
+
// Resolve and authorize the complete table plan before configuration,
|
|
288
|
+
// credential-bearing client construction, or handler execution. The
|
|
289
|
+
// resolver also canonicalizes caller-selected table identifiers so the
|
|
290
|
+
// value evaluated by policy is the value used by the handler.
|
|
291
|
+
let authorizedArgs;
|
|
292
|
+
try {
|
|
293
|
+
const access = (0, field_policy_js_1.runWithFieldPolicyConfiguration)(context.effectivePolicy.fieldPolicy, () => tool.resolveAccess(args, context.effectivePolicy));
|
|
294
|
+
(0, table_policy_js_1.authorizeTableAccessPlan)(context.effectivePolicy.tableAccess, access.requests, tool.definition.name);
|
|
295
|
+
authorizedArgs = access.args;
|
|
296
|
+
}
|
|
297
|
+
catch (error) {
|
|
298
|
+
const cancellation = cancellationResultIfIssued(contextDependencies, context.signal, binding, tool.definition.name, request.metadata);
|
|
299
|
+
if (cancellation)
|
|
300
|
+
return cancellation;
|
|
301
|
+
const encodedQueryDenied = (0, encoded_query_policy_js_1.isEncodedQueryPolicyError)(error);
|
|
302
|
+
const structuredQueryDenied = error instanceof structured_query_js_1.StructuredQueryError;
|
|
303
|
+
const journalUpdateDenied = (0, incident_journal_policy_js_1.isIncidentJournalPolicyError)(error) &&
|
|
304
|
+
error.reason === "generic_journal_update" &&
|
|
305
|
+
error.field !== undefined;
|
|
306
|
+
// A field-policy denial is a distinct failure from a table denial and
|
|
307
|
+
// is fixed in a different configuration key. Reporting it as
|
|
308
|
+
// "table access denied" points the operator at the wrong place.
|
|
309
|
+
const fieldAccessDenied = !journalUpdateDenied && (0, field_policy_js_1.isFieldPolicyError)(error);
|
|
310
|
+
// A bounded-value denial is not a table denial: it is not fixed in
|
|
311
|
+
// tableAccess.writeTables. Reporting it as one sends the operator to
|
|
312
|
+
// edit a key that cannot change the outcome.
|
|
313
|
+
const writeValueDenied = (0, write_value_policy_js_1.isWriteValuePolicyError)(error);
|
|
314
|
+
auditInvocation(contextDependencies, {
|
|
315
|
+
outcome: "policy_rejected",
|
|
316
|
+
reason: encodedQueryDenied
|
|
317
|
+
? "encoded_query_denied"
|
|
318
|
+
: structuredQueryDenied
|
|
319
|
+
? "structured_query_denied"
|
|
320
|
+
: journalUpdateDenied
|
|
321
|
+
? "journal_update_denied"
|
|
322
|
+
: fieldAccessDenied
|
|
323
|
+
? "field_access_denied"
|
|
324
|
+
: writeValueDenied
|
|
325
|
+
? "write_value_denied"
|
|
326
|
+
: "table_access_denied",
|
|
327
|
+
profile: binding,
|
|
328
|
+
}, tool.definition.name, request.metadata);
|
|
329
|
+
return (0, utils_js_1.err)(encodedQueryDenied
|
|
330
|
+
? `${encoded_query_policy_js_1.ENCODED_QUERY_MIGRATION_MESSAGE} Correlation ID: ${context.correlationId}.`
|
|
331
|
+
: structuredQueryDenied
|
|
332
|
+
? `Structured query was denied by policy (${error.reason}). Correlation ID: ${context.correlationId}.`
|
|
333
|
+
: journalUpdateDenied
|
|
334
|
+
? `${(0, incident_journal_policy_js_1.incidentJournalMigrationMessage)(error.field, tool.definition.name)} Correlation ID: ${context.correlationId}.`
|
|
335
|
+
: fieldAccessDenied
|
|
336
|
+
? `${(0, field_policy_js_1.fieldPolicyDenialMessage)(error)} Correlation ID: ${context.correlationId}.`
|
|
337
|
+
: writeValueDenied
|
|
338
|
+
? `${(0, write_value_policy_js_1.writeValueDenialMessage)(error, tool.definition.name)} Correlation ID: ${context.correlationId}.`
|
|
339
|
+
: `Table access was denied by policy. Correlation ID: ${context.correlationId}.`);
|
|
340
|
+
}
|
|
341
|
+
// Profile existence is established before these credential-bearing
|
|
342
|
+
// paths. Initialization errors are sanitized at this trust boundary.
|
|
343
|
+
let config;
|
|
344
|
+
try {
|
|
345
|
+
config = profileManager.getConfig(context.profile.name);
|
|
346
|
+
}
|
|
347
|
+
catch {
|
|
348
|
+
return clientInitializationFailure(contextDependencies, "client_initialization_failed", binding, tool.definition.name, request.metadata, context.correlationId);
|
|
349
|
+
}
|
|
350
|
+
let configInstance;
|
|
351
|
+
try {
|
|
352
|
+
configInstance = (0, profile_manager_js_1.normalizeInstanceUrl)(config.instance);
|
|
353
|
+
}
|
|
354
|
+
catch {
|
|
355
|
+
return clientInitializationFailure(contextDependencies, "client_initialization_failed", binding, tool.definition.name, request.metadata, context.correlationId);
|
|
356
|
+
}
|
|
357
|
+
if (configInstance !== context.profile.instance) {
|
|
358
|
+
return clientInitializationFailure(contextDependencies, "profile_binding_changed", binding, tool.definition.name, request.metadata, context.correlationId);
|
|
359
|
+
}
|
|
360
|
+
let client;
|
|
361
|
+
try {
|
|
362
|
+
// Use the exact immutable configuration resolved and bound above.
|
|
363
|
+
// A second resolver read here could select different rotating secret
|
|
364
|
+
// material from the values the handler and preflight authorized.
|
|
365
|
+
client = profileManager.getClient(context.profile.name, config);
|
|
366
|
+
}
|
|
367
|
+
catch {
|
|
368
|
+
return clientInitializationFailure(contextDependencies, "client_initialization_failed", binding, tool.definition.name, request.metadata, context.correlationId);
|
|
369
|
+
}
|
|
370
|
+
let result;
|
|
371
|
+
try {
|
|
372
|
+
const services = Object.freeze({
|
|
373
|
+
serviceNow: (0, metadata_cache_js_1.createCachedServiceNowOperations)((0, client_js_1.createServiceNowOperations)(client),
|
|
374
|
+
// Scoped by instance AND credential identity. ServiceNow applies
|
|
375
|
+
// per-user ACLs, and a cache hit bypasses the upstream read, so
|
|
376
|
+
// two identities on one instance must never share a partition.
|
|
377
|
+
`${context.profile.instance}|${(0, profile_manager_js_1.credentialFingerprint)(config)}`, config.metadataCache,
|
|
378
|
+
// Needed to resolve the session timezone the freshness probe
|
|
379
|
+
// compares in; without it the cache disables itself.
|
|
380
|
+
config.user),
|
|
381
|
+
settings: Object.freeze({
|
|
382
|
+
instance: context.profile.instance,
|
|
383
|
+
displayValue: config.displayValue,
|
|
384
|
+
relDepth: config.relDepth,
|
|
385
|
+
}),
|
|
386
|
+
context,
|
|
387
|
+
policy: context.effectivePolicy,
|
|
388
|
+
logger,
|
|
389
|
+
});
|
|
390
|
+
result = await (0, client_js_1.runWithServiceNowRequestSignal)(context.signal, () => (0, field_policy_js_1.runWithFieldPolicyConfiguration)(context.effectivePolicy.fieldPolicy, () => tool.invoke(authorizedArgs, services)));
|
|
391
|
+
}
|
|
392
|
+
catch (error) {
|
|
393
|
+
const cancellation = cancellationResultIfIssued(contextDependencies, context.signal, binding, tool.definition.name, request.metadata);
|
|
394
|
+
if (cancellation)
|
|
395
|
+
return cancellation;
|
|
396
|
+
// A journal denial raised inside a handler is a policy rejection, not
|
|
397
|
+
// an internal fault. Without this it reaches the caller as "the
|
|
398
|
+
// operation failed unexpectedly", which tells them nothing about the
|
|
399
|
+
// tool they should have used.
|
|
400
|
+
const journalDenied = (0, incident_journal_policy_js_1.isIncidentJournalPolicyError)(error) &&
|
|
401
|
+
error.reason === "generic_journal_update" &&
|
|
402
|
+
error.field !== undefined;
|
|
403
|
+
if (journalDenied) {
|
|
404
|
+
auditInvocation(contextDependencies, {
|
|
405
|
+
outcome: "policy_rejected",
|
|
406
|
+
reason: "journal_update_denied",
|
|
407
|
+
profile: binding,
|
|
408
|
+
}, tool.definition.name, request.metadata);
|
|
409
|
+
return (0, utils_js_1.err)(`${(0, incident_journal_policy_js_1.incidentJournalMigrationMessage)(error.field, tool.definition.name)} ` +
|
|
410
|
+
`Correlation ID: ${context.correlationId}.`);
|
|
411
|
+
}
|
|
412
|
+
auditInvocation(contextDependencies, {
|
|
413
|
+
outcome: "handler_error",
|
|
414
|
+
reason: "handler_threw",
|
|
415
|
+
profile: binding,
|
|
416
|
+
error,
|
|
417
|
+
}, tool.definition.name, request.metadata);
|
|
418
|
+
return correlatedToolError(error, context.correlationId);
|
|
419
|
+
}
|
|
420
|
+
const completionCancellation = cancellationResultIfIssued(contextDependencies, context.signal, binding, tool.definition.name, request.metadata);
|
|
421
|
+
if (completionCancellation)
|
|
422
|
+
return completionCancellation;
|
|
423
|
+
return finalizeHandlerInvocation(result, registrationConfig.outputSchema, context.profile.name, contextDependencies, binding, tool.definition.name, request.metadata, context.correlationId);
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
function registerProfileDiagnostic(mcpServer, profileManager, sharedContextDependencies, tool, logger, registrationConfig) {
|
|
427
|
+
mcpServer.registerTool(tool.definition.name, registrationConfig, async (args, extra) => {
|
|
428
|
+
const contextDependencies = dependenciesForToolInvocation(sharedContextDependencies);
|
|
429
|
+
const request = await resolveRequestMetadata(tool.definition.name, extra, contextDependencies);
|
|
430
|
+
if (request.kind === "cancelled") {
|
|
431
|
+
auditPreContextCancellation(contextDependencies, tool.definition.name, request.metadata, request.reason);
|
|
432
|
+
return (0, utils_js_1.err)(`Tool ${JSON.stringify(tool.definition.name)} was cancelled. ` +
|
|
433
|
+
`Correlation ID: ${request.metadata.correlationId}.`);
|
|
434
|
+
}
|
|
435
|
+
if (request.kind === "unavailable") {
|
|
436
|
+
auditPreContextRejection(contextDependencies, tool.definition.name, request.metadata);
|
|
437
|
+
return (0, utils_js_1.err)(`Request metadata could not be initialized. Correlation ID: ${request.metadata.correlationId}.`);
|
|
438
|
+
}
|
|
439
|
+
const profileName = args.profile;
|
|
440
|
+
const resolution = resolveProfile(profileManager, profileName);
|
|
441
|
+
if (resolution.kind === "unknown") {
|
|
442
|
+
auditInvocation(contextDependencies, { outcome: "profile_rejected", reason: "unknown_profile" }, tool.definition.name, request.metadata);
|
|
443
|
+
return correlateErrorResult(unknownProfile(profileName), request.metadata.correlationId);
|
|
444
|
+
}
|
|
445
|
+
if (resolution.kind === "invalid") {
|
|
446
|
+
auditInvocation(contextDependencies, { outcome: "profile_rejected", reason: "invalid_profile" }, tool.definition.name, request.metadata);
|
|
447
|
+
return correlateErrorResult(invalidProfile(profileName), request.metadata.correlationId);
|
|
448
|
+
}
|
|
449
|
+
const binding = Object.freeze({
|
|
450
|
+
name: profileName,
|
|
451
|
+
instance: resolution.canonicalInstance,
|
|
452
|
+
});
|
|
453
|
+
let context;
|
|
454
|
+
try {
|
|
455
|
+
context = await (0, execution_context_js_1.createExecutionContext)(request.metadata, binding, tool.definition.name, contextDependencies.effectivePolicyProvider, contextDependencies.requestSignal);
|
|
456
|
+
}
|
|
457
|
+
catch {
|
|
458
|
+
const cancellation = cancellationResultIfIssued(contextDependencies, contextDependencies.requestSignal, binding, tool.definition.name, request.metadata);
|
|
459
|
+
if (cancellation)
|
|
460
|
+
return cancellation;
|
|
461
|
+
auditInvocation(contextDependencies, {
|
|
462
|
+
outcome: "context_rejected",
|
|
463
|
+
reason: "policy_context_unavailable",
|
|
464
|
+
profile: binding,
|
|
465
|
+
}, tool.definition.name, request.metadata);
|
|
466
|
+
return (0, utils_js_1.err)(`Request context could not be initialized. Correlation ID: ${request.metadata.correlationId}.`);
|
|
467
|
+
}
|
|
468
|
+
const contextCancellation = cancellationResultIfIssued(contextDependencies, context.signal, binding, tool.definition.name, request.metadata);
|
|
469
|
+
if (contextCancellation)
|
|
470
|
+
return contextCancellation;
|
|
471
|
+
let authorizedArgs;
|
|
472
|
+
try {
|
|
473
|
+
const access = (0, field_policy_js_1.runWithFieldPolicyConfiguration)(context.effectivePolicy.fieldPolicy, () => tool.resolveAccess(args, context.effectivePolicy));
|
|
474
|
+
(0, table_policy_js_1.authorizeTableAccessPlan)(context.effectivePolicy.tableAccess, access.requests, tool.definition.name);
|
|
475
|
+
authorizedArgs = access.args;
|
|
476
|
+
}
|
|
477
|
+
catch (error) {
|
|
478
|
+
const cancellation = cancellationResultIfIssued(contextDependencies, context.signal, binding, tool.definition.name, request.metadata);
|
|
479
|
+
if (cancellation)
|
|
480
|
+
return cancellation;
|
|
481
|
+
// Same classification as the primary dispatch path: a field-policy
|
|
482
|
+
// denial is fixed in a different configuration key than a table one,
|
|
483
|
+
// and a bounded-value denial is fixed in no configuration key at all.
|
|
484
|
+
// No context-only tool reaches the write-value policy today; the
|
|
485
|
+
// branch keeps the two paths from drifting apart again.
|
|
486
|
+
const fieldAccessDenied = (0, field_policy_js_1.isFieldPolicyError)(error);
|
|
487
|
+
const writeValueDenied = (0, write_value_policy_js_1.isWriteValuePolicyError)(error);
|
|
488
|
+
auditInvocation(contextDependencies, {
|
|
489
|
+
outcome: "policy_rejected",
|
|
490
|
+
reason: fieldAccessDenied
|
|
491
|
+
? "field_access_denied"
|
|
492
|
+
: writeValueDenied
|
|
493
|
+
? "write_value_denied"
|
|
494
|
+
: "table_access_denied",
|
|
495
|
+
profile: binding,
|
|
496
|
+
}, tool.definition.name, request.metadata);
|
|
497
|
+
return (0, utils_js_1.err)(fieldAccessDenied
|
|
498
|
+
? `${(0, field_policy_js_1.fieldPolicyDenialMessage)(error)} Correlation ID: ${context.correlationId}.`
|
|
499
|
+
: writeValueDenied
|
|
500
|
+
? `${(0, write_value_policy_js_1.writeValueDenialMessage)(error, tool.definition.name)} Correlation ID: ${context.correlationId}.`
|
|
501
|
+
: `Table access was denied by policy. Correlation ID: ${context.correlationId}.`);
|
|
502
|
+
}
|
|
503
|
+
let result;
|
|
504
|
+
try {
|
|
505
|
+
const services = Object.freeze({
|
|
506
|
+
context,
|
|
507
|
+
policy: context.effectivePolicy,
|
|
508
|
+
logger,
|
|
509
|
+
profileMetadata: Object.freeze({
|
|
510
|
+
instance: context.profile.instance,
|
|
511
|
+
authType: resolution.metadata.authType ?? "basic",
|
|
512
|
+
}),
|
|
513
|
+
});
|
|
514
|
+
result = await (0, client_js_1.runWithServiceNowRequestSignal)(context.signal, () => (0, field_policy_js_1.runWithFieldPolicyConfiguration)(context.effectivePolicy.fieldPolicy, () => tool.invoke(authorizedArgs, services)));
|
|
515
|
+
}
|
|
516
|
+
catch (error) {
|
|
517
|
+
const cancellation = cancellationResultIfIssued(contextDependencies, context.signal, binding, tool.definition.name, request.metadata);
|
|
518
|
+
if (cancellation)
|
|
519
|
+
return cancellation;
|
|
520
|
+
// A journal denial raised inside a handler is a policy rejection, not
|
|
521
|
+
// an internal fault. Without this it reaches the caller as "the
|
|
522
|
+
// operation failed unexpectedly", which tells them nothing about the
|
|
523
|
+
// tool they should have used.
|
|
524
|
+
const journalDenied = (0, incident_journal_policy_js_1.isIncidentJournalPolicyError)(error) &&
|
|
525
|
+
error.reason === "generic_journal_update" &&
|
|
526
|
+
error.field !== undefined;
|
|
527
|
+
if (journalDenied) {
|
|
528
|
+
auditInvocation(contextDependencies, {
|
|
529
|
+
outcome: "policy_rejected",
|
|
530
|
+
reason: "journal_update_denied",
|
|
531
|
+
profile: binding,
|
|
532
|
+
}, tool.definition.name, request.metadata);
|
|
533
|
+
return (0, utils_js_1.err)(`${(0, incident_journal_policy_js_1.incidentJournalMigrationMessage)(error.field, tool.definition.name)} ` +
|
|
534
|
+
`Correlation ID: ${context.correlationId}.`);
|
|
535
|
+
}
|
|
536
|
+
auditInvocation(contextDependencies, {
|
|
537
|
+
outcome: "handler_error",
|
|
538
|
+
reason: "handler_threw",
|
|
539
|
+
profile: binding,
|
|
540
|
+
error,
|
|
541
|
+
}, tool.definition.name, request.metadata);
|
|
542
|
+
return correlatedToolError(error, context.correlationId);
|
|
543
|
+
}
|
|
544
|
+
const completionCancellation = cancellationResultIfIssued(contextDependencies, context.signal, binding, tool.definition.name, request.metadata);
|
|
545
|
+
if (completionCancellation)
|
|
546
|
+
return completionCancellation;
|
|
547
|
+
return finalizeHandlerInvocation(result, registrationConfig.outputSchema, context.profile.name, contextDependencies, binding, tool.definition.name, request.metadata, context.correlationId);
|
|
548
|
+
});
|
|
85
549
|
}
|
|
86
550
|
/**
|
|
87
|
-
*
|
|
551
|
+
* Begin timing before metadata/profile/policy work and finish from the same
|
|
552
|
+
* issued audit record already used by the tool boundary. The wrapper is local
|
|
553
|
+
* to one callback, so concurrent calls cannot finish each other's span.
|
|
88
554
|
*/
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
if (!
|
|
555
|
+
function dependenciesForToolInvocation(dependencies) {
|
|
556
|
+
const observer = dependencies.toolAuditObserver;
|
|
557
|
+
if (!observer)
|
|
558
|
+
return dependencies;
|
|
559
|
+
let observation;
|
|
560
|
+
try {
|
|
561
|
+
observation = observer.begin();
|
|
562
|
+
if (!observation || typeof observation.finish !== "function") {
|
|
563
|
+
return dependencies;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
catch {
|
|
567
|
+
return dependencies;
|
|
568
|
+
}
|
|
569
|
+
return Object.freeze({
|
|
570
|
+
...dependencies,
|
|
571
|
+
auditSink: Object.freeze({
|
|
572
|
+
write(record) {
|
|
573
|
+
try {
|
|
574
|
+
suppressObserverThenable(observation.finish(record));
|
|
575
|
+
}
|
|
576
|
+
catch {
|
|
577
|
+
// Timing/telemetry is isolated from the authoritative audit sink.
|
|
578
|
+
}
|
|
579
|
+
dependencies.auditSink.write(record);
|
|
580
|
+
},
|
|
581
|
+
writePreContext(record) {
|
|
582
|
+
try {
|
|
583
|
+
suppressObserverThenable(observation.finish(record));
|
|
584
|
+
}
|
|
585
|
+
catch {
|
|
586
|
+
// Pre-context rejection behavior cannot depend on telemetry.
|
|
587
|
+
}
|
|
588
|
+
dependencies.auditSink.writePreContext(record);
|
|
589
|
+
},
|
|
590
|
+
}),
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
function suppressObserverThenable(value) {
|
|
594
|
+
if (((typeof value === "object" && value !== null) ||
|
|
595
|
+
typeof value === "function") &&
|
|
596
|
+
typeof Reflect.get(value, "then") === "function") {
|
|
597
|
+
void Promise.resolve(value).catch(() => { });
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
/** Resolve safe request identity before any profile lookup or context creation. */
|
|
601
|
+
async function resolveRequestMetadata(toolName, extra, dependencies) {
|
|
602
|
+
const invocation = Object.freeze({
|
|
603
|
+
tool: toolName,
|
|
604
|
+
requestId: extra.requestId,
|
|
605
|
+
...(extra.sessionId === undefined ? {} : { sessionId: extra.sessionId }),
|
|
606
|
+
...(extra.authInfo?.clientId === undefined
|
|
607
|
+
? {}
|
|
608
|
+
: { authenticatedClientId: extra.authInfo.clientId }),
|
|
609
|
+
});
|
|
610
|
+
try {
|
|
611
|
+
const candidate = await dependencies.requestMetadataProvider.resolve(invocation);
|
|
612
|
+
const metadata = (0, execution_context_js_1.immutableRequestMetadata)(candidate);
|
|
613
|
+
const cancellationReason = dependencies.requestSignal
|
|
614
|
+
? (0, execution_context_js_1.requestCancellationAuditReason)(dependencies.requestSignal)
|
|
615
|
+
: undefined;
|
|
616
|
+
return cancellationReason === undefined
|
|
617
|
+
? { kind: "resolved", metadata }
|
|
618
|
+
: { kind: "cancelled", metadata, reason: cancellationReason };
|
|
619
|
+
}
|
|
620
|
+
catch {
|
|
621
|
+
const cancellationReason = dependencies.requestSignal
|
|
622
|
+
? (0, execution_context_js_1.requestCancellationAuditReason)(dependencies.requestSignal)
|
|
623
|
+
: undefined;
|
|
624
|
+
if (cancellationReason !== undefined) {
|
|
625
|
+
return {
|
|
626
|
+
kind: "cancelled",
|
|
627
|
+
metadata: (0, execution_context_js_1.fallbackRequestMetadata)(),
|
|
628
|
+
reason: cancellationReason,
|
|
629
|
+
};
|
|
630
|
+
}
|
|
92
631
|
return {
|
|
93
|
-
|
|
94
|
-
|
|
632
|
+
kind: "unavailable",
|
|
633
|
+
metadata: (0, execution_context_js_1.fallbackRequestMetadata)(),
|
|
95
634
|
};
|
|
96
635
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
636
|
+
}
|
|
637
|
+
function auditInvocation(dependencies, disposition, tool, request) {
|
|
638
|
+
try {
|
|
639
|
+
const record = (0, execution_context_js_1.createAuditRecord)({
|
|
640
|
+
...disposition,
|
|
641
|
+
tool,
|
|
642
|
+
request,
|
|
643
|
+
});
|
|
644
|
+
(0, execution_context_js_1.emitAudit)(dependencies.auditSink, record);
|
|
645
|
+
}
|
|
646
|
+
catch {
|
|
647
|
+
// Even record construction is isolated from the caller's tool result.
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function cancellationResultIfIssued(dependencies, signal, profile, tool, request) {
|
|
651
|
+
if (!signal)
|
|
652
|
+
return undefined;
|
|
653
|
+
const reason = (0, execution_context_js_1.requestCancellationAuditReason)(signal);
|
|
654
|
+
if (reason === undefined)
|
|
655
|
+
return undefined;
|
|
656
|
+
auditInvocation(dependencies, { outcome: "cancelled", reason, profile }, tool, request);
|
|
657
|
+
return (0, utils_js_1.err)(`Tool ${JSON.stringify(tool)} was cancelled. ` +
|
|
658
|
+
`Correlation ID: ${request.correlationId}.`);
|
|
659
|
+
}
|
|
660
|
+
function auditPreContextRejection(dependencies, tool, request) {
|
|
661
|
+
try {
|
|
662
|
+
const record = (0, execution_context_js_1.createPreContextAuditRecord)({
|
|
663
|
+
tool,
|
|
664
|
+
request,
|
|
665
|
+
reason: "request_metadata_unavailable",
|
|
666
|
+
});
|
|
667
|
+
(0, execution_context_js_1.emitPreContextAudit)(dependencies.auditSink, record);
|
|
668
|
+
}
|
|
669
|
+
catch {
|
|
670
|
+
// Operational audit construction/delivery cannot alter safe rejection.
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
function auditPreContextCancellation(dependencies, tool, request, reason) {
|
|
674
|
+
try {
|
|
675
|
+
const record = (0, execution_context_js_1.createPreContextAuditRecord)({ tool, request, reason });
|
|
676
|
+
(0, execution_context_js_1.emitPreContextAudit)(dependencies.auditSink, record);
|
|
677
|
+
}
|
|
678
|
+
catch {
|
|
679
|
+
// Cancellation audit construction/delivery cannot alter safe rejection.
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
function clientInitializationFailure(dependencies, reason, profile, tool, request, correlationId) {
|
|
683
|
+
// Every credential source/provider/decryption/client-construction failure is
|
|
684
|
+
// intentionally identical to a final upstream authentication rejection.
|
|
685
|
+
const error = (0, tool_error_js_1.createToolError)("authentication", "retry_after_correction");
|
|
686
|
+
auditInvocation(dependencies, { outcome: "client_rejected", reason, profile, error }, tool, request);
|
|
687
|
+
return correlatedToolError(error, correlationId);
|
|
688
|
+
}
|
|
689
|
+
function finalizeHandlerInvocation(candidate, outputSchema, canonicalProfile, dependencies, profile, tool, request, correlationId) {
|
|
690
|
+
try {
|
|
691
|
+
const guarded = (0, handler_result_js_1.guardedHandlerResult)(candidate);
|
|
692
|
+
if (guarded.kind === "error") {
|
|
693
|
+
return handlerResultFailure(dependencies, profile, tool, request, correlationId);
|
|
694
|
+
}
|
|
695
|
+
const enriched = validateSuccessfulModuleResult(outputSchema, guarded.result, canonicalProfile);
|
|
696
|
+
if (!enriched) {
|
|
697
|
+
return handlerResultFailure(dependencies, profile, tool, request, correlationId);
|
|
698
|
+
}
|
|
699
|
+
auditInvocation(dependencies, { outcome: "success", reason: null, profile }, tool, request);
|
|
700
|
+
return enriched;
|
|
701
|
+
}
|
|
702
|
+
catch {
|
|
703
|
+
return handlerResultFailure(dependencies, profile, tool, request, correlationId);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
function handlerResultFailure(dependencies, profile, tool, request, correlationId) {
|
|
707
|
+
const error = (0, tool_error_js_1.createToolError)("internal", "do_not_retry");
|
|
708
|
+
auditInvocation(dependencies, {
|
|
709
|
+
outcome: "handler_error",
|
|
710
|
+
reason: "handler_returned_error",
|
|
711
|
+
profile,
|
|
712
|
+
error,
|
|
713
|
+
}, tool, request);
|
|
714
|
+
return correlatedToolError(error, correlationId);
|
|
715
|
+
}
|
|
716
|
+
function correlatedToolError(error, correlationId) {
|
|
717
|
+
return (0, utils_js_1.err)(`${(0, tool_error_js_1.formatToolError)(error)}\nCorrelation ID: ${correlationId}.`);
|
|
718
|
+
}
|
|
719
|
+
/** Preserve handler output and centrally attach the canonical profile envelope. */
|
|
720
|
+
function enrichSuccessfulResult(result, canonicalProfile) {
|
|
721
|
+
return {
|
|
722
|
+
...result,
|
|
723
|
+
structuredContent: {
|
|
724
|
+
...result.structuredContent,
|
|
725
|
+
profile: canonicalProfile,
|
|
726
|
+
},
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
/** Validate the exact canonical envelope before either MCP client can see it. */
|
|
730
|
+
function validateSuccessfulModuleResult(outputSchema, result, canonicalProfile) {
|
|
731
|
+
try {
|
|
732
|
+
const enriched = enrichSuccessfulResult(result, canonicalProfile);
|
|
733
|
+
const parsed = outputSchema.safeParse(enriched.structuredContent);
|
|
734
|
+
if (!parsed.success)
|
|
735
|
+
return undefined;
|
|
736
|
+
const validated = {
|
|
737
|
+
...enriched,
|
|
738
|
+
structuredContent: parsed.data,
|
|
739
|
+
};
|
|
740
|
+
const finalized = (0, result_envelope_js_1.finalizeEnvelopeResult)(validated);
|
|
741
|
+
if (!finalized)
|
|
742
|
+
return undefined;
|
|
743
|
+
const finalParsed = outputSchema.safeParse(finalized.structuredContent);
|
|
744
|
+
if (!finalParsed.success)
|
|
745
|
+
return undefined;
|
|
746
|
+
const block = finalized.content[0];
|
|
747
|
+
if (finalized.content.length !== 1 ||
|
|
748
|
+
!block ||
|
|
749
|
+
block.type !== "text") {
|
|
750
|
+
return undefined;
|
|
751
|
+
}
|
|
103
752
|
return {
|
|
104
|
-
content: [{ type: "text", text:
|
|
105
|
-
|
|
753
|
+
content: [{ type: "text", text: block.text }],
|
|
754
|
+
structuredContent: finalParsed.data,
|
|
106
755
|
};
|
|
107
756
|
}
|
|
108
|
-
|
|
757
|
+
catch {
|
|
758
|
+
return undefined;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
/** Add safe correlation to callback-reachable errors and drop structured data. */
|
|
762
|
+
function correlateErrorResult(result, correlationId) {
|
|
763
|
+
const safeResult = { ...result };
|
|
764
|
+
delete safeResult.structuredContent;
|
|
765
|
+
return {
|
|
766
|
+
...safeResult,
|
|
767
|
+
isError: true,
|
|
768
|
+
content: result.content.map((block) => block.type === "text"
|
|
769
|
+
? { ...block, text: `${block.text}\nCorrelation ID: ${correlationId}.` }
|
|
770
|
+
: block),
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
/** Existence-only resolution: getProfile does not resolve credentials. */
|
|
774
|
+
function resolveProfile(profileManager, profileName) {
|
|
775
|
+
let candidate;
|
|
776
|
+
try {
|
|
777
|
+
candidate = profileManager.getProfile(profileName);
|
|
778
|
+
}
|
|
779
|
+
catch {
|
|
780
|
+
return { kind: "unknown" };
|
|
781
|
+
}
|
|
782
|
+
let snapshot;
|
|
783
|
+
try {
|
|
784
|
+
snapshot = snapshotProfileRecord(candidate);
|
|
785
|
+
}
|
|
786
|
+
catch {
|
|
787
|
+
return { kind: "invalid" };
|
|
788
|
+
}
|
|
789
|
+
if (!snapshot || !isValidProfileRecord(snapshot)) {
|
|
790
|
+
return { kind: "invalid" };
|
|
791
|
+
}
|
|
792
|
+
const canonicalInstance = canonicalProfileInstance(snapshot.instance);
|
|
793
|
+
if (!canonicalInstance) {
|
|
794
|
+
return { kind: "invalid" };
|
|
795
|
+
}
|
|
796
|
+
return {
|
|
797
|
+
kind: "resolved",
|
|
798
|
+
metadata: Object.freeze({ authType: snapshot.authType }),
|
|
799
|
+
canonicalInstance,
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
const INVALID_PROFILE_FIELD = Symbol("invalid-profile-field");
|
|
803
|
+
/** Snapshot own data descriptors without invoking getters or proxy traps. */
|
|
804
|
+
function snapshotProfileRecord(candidate) {
|
|
805
|
+
if (typeof candidate !== "object" ||
|
|
806
|
+
candidate === null ||
|
|
807
|
+
node_util_1.types.isProxy(candidate)) {
|
|
808
|
+
return undefined;
|
|
809
|
+
}
|
|
810
|
+
const prototype = Object.getPrototypeOf(candidate);
|
|
811
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
812
|
+
return undefined;
|
|
813
|
+
const descriptors = Object.getOwnPropertyDescriptors(candidate);
|
|
814
|
+
const read = (key) => {
|
|
815
|
+
const descriptor = descriptors[key];
|
|
816
|
+
if (!descriptor)
|
|
817
|
+
return undefined;
|
|
818
|
+
return "value" in descriptor ? descriptor.value : INVALID_PROFILE_FIELD;
|
|
819
|
+
};
|
|
820
|
+
return Object.freeze({
|
|
821
|
+
instance: read("instance"),
|
|
822
|
+
username: read("username"),
|
|
823
|
+
credential: read("credential"),
|
|
824
|
+
scope: read("scope"),
|
|
825
|
+
scope_sys_id: read("scope_sys_id"),
|
|
826
|
+
vendor_code: read("vendor_code"),
|
|
827
|
+
description: read("description"),
|
|
828
|
+
authType: read("authType"),
|
|
829
|
+
clientId: read("clientId"),
|
|
830
|
+
clientSecret: read("clientSecret"),
|
|
831
|
+
grantType: read("grantType"),
|
|
832
|
+
apiKey: read("apiKey"),
|
|
833
|
+
apiKeyHeader: read("apiKeyHeader"),
|
|
834
|
+
timeoutMs: read("timeoutMs"),
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
/** Validate a plain snapshot without resolving or exposing credentials. */
|
|
838
|
+
function isValidProfileRecord(profile) {
|
|
839
|
+
if (!canonicalProfileInstance(profile.instance))
|
|
840
|
+
return false;
|
|
841
|
+
const timeoutMs = profile.timeoutMs;
|
|
842
|
+
if (timeoutMs !== undefined &&
|
|
843
|
+
(typeof timeoutMs !== "number" || !Number.isInteger(timeoutMs) || timeoutMs <= 0)) {
|
|
844
|
+
return false;
|
|
845
|
+
}
|
|
846
|
+
const authType = profile.authType ?? "basic";
|
|
847
|
+
switch (authType) {
|
|
848
|
+
case "basic":
|
|
849
|
+
return (isNonEmptyString(profile.username) &&
|
|
850
|
+
(0, profile_credentials_js_1.isCredentialSourceDescriptor)(profile.credential));
|
|
851
|
+
case "oauth": {
|
|
852
|
+
const grantType = profile.grantType ?? "client_credentials";
|
|
853
|
+
if (!isNonEmptyString(profile.clientId) ||
|
|
854
|
+
!(0, profile_credentials_js_1.isCredentialSourceDescriptor)(profile.clientSecret) ||
|
|
855
|
+
(grantType !== "client_credentials" && grantType !== "password")) {
|
|
856
|
+
return false;
|
|
857
|
+
}
|
|
858
|
+
return (grantType !== "password" ||
|
|
859
|
+
(isNonEmptyString(profile.username) &&
|
|
860
|
+
(0, profile_credentials_js_1.isCredentialSourceDescriptor)(profile.credential)));
|
|
861
|
+
}
|
|
862
|
+
case "apikey":
|
|
863
|
+
return ((0, profile_credentials_js_1.isCredentialSourceDescriptor)(profile.apiKey) &&
|
|
864
|
+
(profile.apiKeyHeader === undefined || isNonEmptyString(profile.apiKeyHeader)));
|
|
865
|
+
default:
|
|
866
|
+
return false;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
function isNonEmptyString(value) {
|
|
870
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
871
|
+
}
|
|
872
|
+
/** Return a safe canonical origin, rejecting URL credential/metadata channels. */
|
|
873
|
+
function canonicalProfileInstance(instance) {
|
|
874
|
+
if (typeof instance !== "string")
|
|
875
|
+
return undefined;
|
|
876
|
+
try {
|
|
877
|
+
return (0, profile_manager_js_1.normalizeInstanceUrl)(instance);
|
|
878
|
+
}
|
|
879
|
+
catch {
|
|
880
|
+
return undefined;
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
function unknownProfile(profileName) {
|
|
884
|
+
return (0, utils_js_1.err)(`Unknown profile ${JSON.stringify(profileName)}.`);
|
|
885
|
+
}
|
|
886
|
+
function invalidProfile(profileName) {
|
|
887
|
+
return (0, utils_js_1.err)(`Profile ${JSON.stringify(profileName)} is invalid or incomplete.`);
|
|
109
888
|
}
|
|
110
889
|
//# sourceMappingURL=index.js.map
|