@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.
Files changed (281) hide show
  1. package/.env.example +56 -0
  2. package/CHANGELOG.md +153 -0
  3. package/README.md +939 -101
  4. package/dist/auth.d.ts +88 -0
  5. package/dist/auth.d.ts.map +1 -0
  6. package/dist/auth.js +396 -0
  7. package/dist/auth.js.map +1 -0
  8. package/dist/client.d.ts +111 -17
  9. package/dist/client.d.ts.map +1 -1
  10. package/dist/client.js +554 -79
  11. package/dist/client.js.map +1 -1
  12. package/dist/config.d.ts +41 -4
  13. package/dist/config.d.ts.map +1 -1
  14. package/dist/config.js +44 -34
  15. package/dist/config.js.map +1 -1
  16. package/dist/encoded-query-policy.d.ts +72 -0
  17. package/dist/encoded-query-policy.d.ts.map +1 -0
  18. package/dist/encoded-query-policy.js +409 -0
  19. package/dist/encoded-query-policy.js.map +1 -0
  20. package/dist/execution-context.d.ts +235 -0
  21. package/dist/execution-context.d.ts.map +1 -0
  22. package/dist/execution-context.js +439 -0
  23. package/dist/execution-context.js.map +1 -0
  24. package/dist/field-policy.d.ts +117 -0
  25. package/dist/field-policy.d.ts.map +1 -0
  26. package/dist/field-policy.js +1684 -0
  27. package/dist/field-policy.js.map +1 -0
  28. package/dist/http-auth.d.ts +131 -0
  29. package/dist/http-auth.d.ts.map +1 -0
  30. package/dist/http-auth.js +347 -0
  31. package/dist/http-auth.js.map +1 -0
  32. package/dist/http-entrypoint.d.ts +26 -0
  33. package/dist/http-entrypoint.d.ts.map +1 -0
  34. package/dist/http-entrypoint.js +255 -0
  35. package/dist/http-entrypoint.js.map +1 -0
  36. package/dist/http-observability.d.ts +147 -0
  37. package/dist/http-observability.d.ts.map +1 -0
  38. package/dist/http-observability.js +703 -0
  39. package/dist/http-observability.js.map +1 -0
  40. package/dist/http-request-policy.d.ts +72 -0
  41. package/dist/http-request-policy.d.ts.map +1 -0
  42. package/dist/http-request-policy.js +463 -0
  43. package/dist/http-request-policy.js.map +1 -0
  44. package/dist/http-request-signal.d.ts +12 -0
  45. package/dist/http-request-signal.d.ts.map +1 -0
  46. package/dist/http-request-signal.js +39 -0
  47. package/dist/http-request-signal.js.map +1 -0
  48. package/dist/http-runtime.d.ts +77 -0
  49. package/dist/http-runtime.d.ts.map +1 -0
  50. package/dist/http-runtime.js +1321 -0
  51. package/dist/http-runtime.js.map +1 -0
  52. package/dist/incident-journal-policy.d.ts +22 -0
  53. package/dist/incident-journal-policy.d.ts.map +1 -0
  54. package/dist/incident-journal-policy.js +115 -0
  55. package/dist/incident-journal-policy.js.map +1 -0
  56. package/dist/incident-write-policy.d.ts +20 -0
  57. package/dist/incident-write-policy.d.ts.map +1 -0
  58. package/dist/incident-write-policy.js +196 -0
  59. package/dist/incident-write-policy.js.map +1 -0
  60. package/dist/index.d.ts +14 -4
  61. package/dist/index.d.ts.map +1 -1
  62. package/dist/index.js +140 -36
  63. package/dist/index.js.map +1 -1
  64. package/dist/metadata-cache.d.ts +71 -0
  65. package/dist/metadata-cache.d.ts.map +1 -0
  66. package/dist/metadata-cache.js +397 -0
  67. package/dist/metadata-cache.js.map +1 -0
  68. package/dist/profile-admin.d.ts +64 -0
  69. package/dist/profile-admin.d.ts.map +1 -0
  70. package/dist/profile-admin.js +351 -0
  71. package/dist/profile-admin.js.map +1 -0
  72. package/dist/profile-credentials.d.ts +68 -0
  73. package/dist/profile-credentials.d.ts.map +1 -0
  74. package/dist/profile-credentials.js +329 -0
  75. package/dist/profile-credentials.js.map +1 -0
  76. package/dist/profile-manager.d.ts +187 -0
  77. package/dist/profile-manager.d.ts.map +1 -0
  78. package/dist/profile-manager.js +1026 -0
  79. package/dist/profile-manager.js.map +1 -0
  80. package/dist/record-identifier-policy.d.ts +39 -0
  81. package/dist/record-identifier-policy.d.ts.map +1 -0
  82. package/dist/record-identifier-policy.js +203 -0
  83. package/dist/record-identifier-policy.js.map +1 -0
  84. package/dist/runtime-environment.d.ts +23 -0
  85. package/dist/runtime-environment.d.ts.map +1 -0
  86. package/dist/runtime-environment.js +58 -0
  87. package/dist/runtime-environment.js.map +1 -0
  88. package/dist/server.d.ts +33 -0
  89. package/dist/server.d.ts.map +1 -0
  90. package/dist/server.js +73 -0
  91. package/dist/server.js.map +1 -0
  92. package/dist/servicenow-identifiers.d.ts +8 -0
  93. package/dist/servicenow-identifiers.d.ts.map +1 -0
  94. package/dist/servicenow-identifiers.js +22 -0
  95. package/dist/servicenow-identifiers.js.map +1 -0
  96. package/dist/setup-prompts.d.ts +7 -0
  97. package/dist/setup-prompts.d.ts.map +1 -0
  98. package/dist/setup-prompts.js +123 -0
  99. package/dist/setup-prompts.js.map +1 -0
  100. package/dist/setup.d.ts +109 -0
  101. package/dist/setup.d.ts.map +1 -0
  102. package/dist/setup.js +1903 -0
  103. package/dist/setup.js.map +1 -0
  104. package/dist/startup.d.ts +43 -0
  105. package/dist/startup.d.ts.map +1 -0
  106. package/dist/startup.js +75 -0
  107. package/dist/startup.js.map +1 -0
  108. package/dist/stdio-runtime.d.ts +106 -0
  109. package/dist/stdio-runtime.d.ts.map +1 -0
  110. package/dist/stdio-runtime.js +231 -0
  111. package/dist/stdio-runtime.js.map +1 -0
  112. package/dist/structured-query.d.ts +187 -0
  113. package/dist/structured-query.d.ts.map +1 -0
  114. package/dist/structured-query.js +503 -0
  115. package/dist/structured-query.js.map +1 -0
  116. package/dist/table-defaults.d.ts +28 -0
  117. package/dist/table-defaults.d.ts.map +1 -0
  118. package/dist/table-defaults.js +85 -0
  119. package/dist/table-defaults.js.map +1 -0
  120. package/dist/table-policy.d.ts +71 -0
  121. package/dist/table-policy.d.ts.map +1 -0
  122. package/dist/table-policy.js +290 -0
  123. package/dist/table-policy.js.map +1 -0
  124. package/dist/tool-error.d.ts +48 -0
  125. package/dist/tool-error.d.ts.map +1 -0
  126. package/dist/tool-error.js +139 -0
  127. package/dist/tool-error.js.map +1 -0
  128. package/dist/tool-table-access.d.ts +19 -0
  129. package/dist/tool-table-access.d.ts.map +1 -0
  130. package/dist/tool-table-access.js +388 -0
  131. package/dist/tool-table-access.js.map +1 -0
  132. package/dist/tools/aggregate.d.ts +22 -42
  133. package/dist/tools/aggregate.d.ts.map +1 -1
  134. package/dist/tools/aggregate.js +103 -47
  135. package/dist/tools/aggregate.js.map +1 -1
  136. package/dist/tools/all-fields-cap.d.ts +45 -0
  137. package/dist/tools/all-fields-cap.d.ts.map +1 -0
  138. package/dist/tools/all-fields-cap.js +151 -0
  139. package/dist/tools/all-fields-cap.js.map +1 -0
  140. package/dist/tools/atf.d.ts +21 -60
  141. package/dist/tools/atf.d.ts.map +1 -1
  142. package/dist/tools/atf.js +180 -116
  143. package/dist/tools/atf.js.map +1 -1
  144. package/dist/tools/attach.d.ts +31 -45
  145. package/dist/tools/attach.d.ts.map +1 -1
  146. package/dist/tools/attach.js +175 -137
  147. package/dist/tools/attach.js.map +1 -1
  148. package/dist/tools/batch.d.ts +78 -42
  149. package/dist/tools/batch.d.ts.map +1 -1
  150. package/dist/tools/batch.js +172 -68
  151. package/dist/tools/batch.js.map +1 -1
  152. package/dist/tools/catalog.d.ts +6 -0
  153. package/dist/tools/catalog.d.ts.map +1 -0
  154. package/dist/tools/catalog.js +203 -0
  155. package/dist/tools/catalog.js.map +1 -0
  156. package/dist/tools/codesearch.d.ts +14 -25
  157. package/dist/tools/codesearch.d.ts.map +1 -1
  158. package/dist/tools/codesearch.js +60 -48
  159. package/dist/tools/codesearch.js.map +1 -1
  160. package/dist/tools/create-module.d.ts +48 -0
  161. package/dist/tools/create-module.d.ts.map +1 -0
  162. package/dist/tools/create-module.js +94 -0
  163. package/dist/tools/create-module.js.map +1 -0
  164. package/dist/tools/create.d.ts +2 -38
  165. package/dist/tools/create.d.ts.map +1 -1
  166. package/dist/tools/create.js +9 -41
  167. package/dist/tools/create.js.map +1 -1
  168. package/dist/tools/delete.d.ts +13 -23
  169. package/dist/tools/delete.d.ts.map +1 -1
  170. package/dist/tools/delete.js +30 -33
  171. package/dist/tools/delete.js.map +1 -1
  172. package/dist/tools/discover.d.ts +15 -27
  173. package/dist/tools/discover.d.ts.map +1 -1
  174. package/dist/tools/discover.js +103 -60
  175. package/dist/tools/discover.js.map +1 -1
  176. package/dist/tools/get-module.d.ts +85 -0
  177. package/dist/tools/get-module.d.ts.map +1 -0
  178. package/dist/tools/get-module.js +221 -0
  179. package/dist/tools/get-module.js.map +1 -0
  180. package/dist/tools/get.d.ts +2 -51
  181. package/dist/tools/get.d.ts.map +1 -1
  182. package/dist/tools/get.js +9 -49
  183. package/dist/tools/get.js.map +1 -1
  184. package/dist/tools/handler-result.d.ts +14 -0
  185. package/dist/tools/handler-result.d.ts.map +1 -0
  186. package/dist/tools/handler-result.js +177 -0
  187. package/dist/tools/handler-result.js.map +1 -0
  188. package/dist/tools/health.d.ts +12 -15
  189. package/dist/tools/health.d.ts.map +1 -1
  190. package/dist/tools/health.js +55 -36
  191. package/dist/tools/health.js.map +1 -1
  192. package/dist/tools/incident-journal-module.d.ts +12 -0
  193. package/dist/tools/incident-journal-module.d.ts.map +1 -0
  194. package/dist/tools/incident-journal-module.js +61 -0
  195. package/dist/tools/incident-journal-module.js.map +1 -0
  196. package/dist/tools/incident-write-shared.d.ts +6 -0
  197. package/dist/tools/incident-write-shared.d.ts.map +1 -0
  198. package/dist/tools/incident-write-shared.js +64 -0
  199. package/dist/tools/incident-write-shared.js.map +1 -0
  200. package/dist/tools/index.d.ts +47 -36
  201. package/dist/tools/index.d.ts.map +1 -1
  202. package/dist/tools/index.js +795 -35
  203. package/dist/tools/index.js.map +1 -1
  204. package/dist/tools/nl.d.ts +10 -24
  205. package/dist/tools/nl.d.ts.map +1 -1
  206. package/dist/tools/nl.js +37 -147
  207. package/dist/tools/nl.js.map +1 -1
  208. package/dist/tools/profile.d.ts +37 -0
  209. package/dist/tools/profile.d.ts.map +1 -0
  210. package/dist/tools/profile.js +39 -0
  211. package/dist/tools/profile.js.map +1 -0
  212. package/dist/tools/query-module.d.ts +119 -0
  213. package/dist/tools/query-module.d.ts.map +1 -0
  214. package/dist/tools/query-module.js +324 -0
  215. package/dist/tools/query-module.js.map +1 -0
  216. package/dist/tools/query.d.ts +2 -72
  217. package/dist/tools/query.d.ts.map +1 -1
  218. package/dist/tools/query.js +8 -72
  219. package/dist/tools/query.js.map +1 -1
  220. package/dist/tools/relationships.d.ts +21 -42
  221. package/dist/tools/relationships.d.ts.map +1 -1
  222. package/dist/tools/relationships.js +70 -56
  223. package/dist/tools/relationships.js.map +1 -1
  224. package/dist/tools/result-envelope.d.ts +603 -0
  225. package/dist/tools/result-envelope.d.ts.map +1 -0
  226. package/dist/tools/result-envelope.js +767 -0
  227. package/dist/tools/result-envelope.js.map +1 -0
  228. package/dist/tools/schema-module.d.ts +64 -0
  229. package/dist/tools/schema-module.d.ts.map +1 -0
  230. package/dist/tools/schema-module.js +367 -0
  231. package/dist/tools/schema-module.js.map +1 -0
  232. package/dist/tools/schema.d.ts +2 -37
  233. package/dist/tools/schema.d.ts.map +1 -1
  234. package/dist/tools/schema.js +6 -57
  235. package/dist/tools/schema.js.map +1 -1
  236. package/dist/tools/script.d.ts +33 -27
  237. package/dist/tools/script.d.ts.map +1 -1
  238. package/dist/tools/script.js +43 -37
  239. package/dist/tools/script.js.map +1 -1
  240. package/dist/tools/syslog.d.ts +19 -46
  241. package/dist/tools/syslog.d.ts.map +1 -1
  242. package/dist/tools/syslog.js +101 -67
  243. package/dist/tools/syslog.js.map +1 -1
  244. package/dist/tools/tool-module.d.ts +143 -0
  245. package/dist/tools/tool-module.d.ts.map +1 -0
  246. package/dist/tools/tool-module.js +1095 -0
  247. package/dist/tools/tool-module.js.map +1 -0
  248. package/dist/tools/update-module.d.ts +51 -0
  249. package/dist/tools/update-module.d.ts.map +1 -0
  250. package/dist/tools/update-module.js +107 -0
  251. package/dist/tools/update-module.js.map +1 -0
  252. package/dist/tools/update.d.ts +2 -45
  253. package/dist/tools/update.d.ts.map +1 -1
  254. package/dist/tools/update.js +9 -41
  255. package/dist/tools/update.js.map +1 -1
  256. package/dist/utils.d.ts +68 -1
  257. package/dist/utils.d.ts.map +1 -1
  258. package/dist/utils.js +114 -16
  259. package/dist/utils.js.map +1 -1
  260. package/dist/version.d.ts +10 -0
  261. package/dist/version.d.ts.map +1 -0
  262. package/dist/version.js +13 -0
  263. package/dist/version.js.map +1 -0
  264. package/docs/ADDING-TOOLS.md +227 -0
  265. package/docs/CLIENT-SETUP.md +914 -0
  266. package/docs/CONTAINER-DEPLOYMENT.md +310 -0
  267. package/docs/CROSS-CLIENT-RELEASE-MATRIX.md +63 -0
  268. package/docs/ENTERPRISE-RELEASE-BOUNDARY.md +149 -0
  269. package/docs/OPERATIONS-RUNBOOK.md +383 -0
  270. package/docs/PRIVATE-CHATGPT-CONNECTIVITY.md +347 -0
  271. package/docs/PRODUCTION-SECURITY.md +368 -0
  272. package/docs/PROFILE-CREDENTIALS.md +126 -0
  273. package/docs/RELEASE-VALIDATION.md +140 -0
  274. package/docs/TESTING-OAUTH.md +101 -0
  275. package/docs/V2-MIGRATION.md +493 -0
  276. package/package.json +60 -13
  277. package/scripts/inspector.mjs +140 -0
  278. package/scripts/release-validate.mjs +201 -0
  279. package/scripts/smoke-test.mjs +274 -0
  280. package/tools/inspector/package-lock.json +3812 -0
  281. package/tools/inspector/package.json +12 -0
@@ -0,0 +1,329 @@
1
+ "use strict";
2
+ /** Secure, provider-neutral credential sources for named ServiceNow profiles. */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.EnvironmentProfileEncryptionKeyProvider = exports.EnvironmentSecretResolver = exports.PROFILE_ENCRYPTION_ALGORITHM = exports.PROFILE_ENCRYPTION_VERSION = exports.PROFILE_ENCRYPTION_KEY_ENV = void 0;
5
+ exports.encryptionKeyProviderFromValue = encryptionKeyProviderFromValue;
6
+ exports.secretReference = secretReference;
7
+ exports.encryptCredential = encryptCredential;
8
+ exports.decryptCredential = decryptCredential;
9
+ exports.resolveCredentialSource = resolveCredentialSource;
10
+ exports.isCredentialSourceDescriptor = isCredentialSourceDescriptor;
11
+ exports.isSecretReference = isSecretReference;
12
+ exports.isEncryptedCredentialEnvelope = isEncryptedCredentialEnvelope;
13
+ exports.legacyEnvironmentReference = legacyEnvironmentReference;
14
+ exports.credentialSourceKind = credentialSourceKind;
15
+ const node_crypto_1 = require("node:crypto");
16
+ const node_util_1 = require("node:util");
17
+ exports.PROFILE_ENCRYPTION_KEY_ENV = "SN_PROFILE_ENCRYPTION_KEY";
18
+ exports.PROFILE_ENCRYPTION_VERSION = 1;
19
+ exports.PROFILE_ENCRYPTION_ALGORITHM = "aes-256-gcm";
20
+ const KEY_BYTES = 32;
21
+ const NONCE_BYTES = 12;
22
+ const AUTH_TAG_BYTES = 16;
23
+ class EnvironmentSecretResolver {
24
+ provider = "env";
25
+ resolve(reference) {
26
+ if (!isCanonicalEnvironmentName(reference)) {
27
+ throw new Error("Credential reference is invalid");
28
+ }
29
+ const value = process.env[reference];
30
+ if (!value) {
31
+ throw new Error("Referenced credential is unavailable");
32
+ }
33
+ return value;
34
+ }
35
+ }
36
+ exports.EnvironmentSecretResolver = EnvironmentSecretResolver;
37
+ /** Reads the one canonical profile key setting; it never persists the key. */
38
+ /**
39
+ * Key provider backed by an explicitly supplied encoded key.
40
+ *
41
+ * The environment provider below cannot serve a key this process has just
42
+ * generated but not exported, which is exactly the setup wizard's position: it
43
+ * writes the key to `server.env` and then needs it in the same run. Passing the
44
+ * value directly keeps the secret out of `process.env` as well.
45
+ *
46
+ * Decodes on every call so callers receive their own Buffer to zeroize.
47
+ */
48
+ function encryptionKeyProviderFromValue(encoded) {
49
+ // Validate eagerly so a corrupt key fails before any secret is captured,
50
+ // rather than after the operator has typed one.
51
+ decodeEncryptionKey(encoded).fill(0);
52
+ return Object.freeze({ getKey: () => decodeEncryptionKey(encoded) });
53
+ }
54
+ class EnvironmentProfileEncryptionKeyProvider {
55
+ getKey() {
56
+ const encoded = process.env[exports.PROFILE_ENCRYPTION_KEY_ENV];
57
+ if (!encoded) {
58
+ throw new Error("Profile encryption key is unavailable");
59
+ }
60
+ return decodeEncryptionKey(encoded);
61
+ }
62
+ }
63
+ exports.EnvironmentProfileEncryptionKeyProvider = EnvironmentProfileEncryptionKeyProvider;
64
+ function secretReference(provider, reference) {
65
+ const candidate = Object.freeze({ type: "secret_ref", provider, reference });
66
+ if (!isSecretReference(candidate)) {
67
+ throw new Error("Credential reference is invalid");
68
+ }
69
+ return candidate;
70
+ }
71
+ function encryptCredential(plaintext, profile, field, keyProvider = new EnvironmentProfileEncryptionKeyProvider()) {
72
+ if (typeof plaintext !== "string" || plaintext.length === 0) {
73
+ throw new Error("Credential value must not be empty");
74
+ }
75
+ assertBinding(profile, field);
76
+ const key = ownedEncryptionKey(keyProvider);
77
+ const nonce = (0, node_crypto_1.randomBytes)(NONCE_BYTES);
78
+ const input = Buffer.from(plaintext, "utf8");
79
+ try {
80
+ const cipher = (0, node_crypto_1.createCipheriv)(exports.PROFILE_ENCRYPTION_ALGORITHM, key, nonce, {
81
+ authTagLength: AUTH_TAG_BYTES,
82
+ });
83
+ cipher.setAAD(authenticatedMetadata(profile, field), {
84
+ plaintextLength: input.byteLength,
85
+ });
86
+ const ciphertext = Buffer.concat([cipher.update(input), cipher.final()]);
87
+ const authTag = cipher.getAuthTag();
88
+ return Object.freeze({
89
+ type: "encrypted",
90
+ version: exports.PROFILE_ENCRYPTION_VERSION,
91
+ algorithm: exports.PROFILE_ENCRYPTION_ALGORITHM,
92
+ nonce: nonce.toString("base64url"),
93
+ ciphertext: ciphertext.toString("base64url"),
94
+ authTag: authTag.toString("base64url"),
95
+ });
96
+ }
97
+ finally {
98
+ input.fill(0);
99
+ key.fill(0);
100
+ }
101
+ }
102
+ function decryptCredential(envelope, profile, field, keyProvider = new EnvironmentProfileEncryptionKeyProvider()) {
103
+ assertBinding(profile, field);
104
+ if (!isEncryptedCredentialEnvelope(envelope)) {
105
+ throw new Error("Encrypted credential envelope is invalid");
106
+ }
107
+ const key = ownedEncryptionKey(keyProvider);
108
+ try {
109
+ const nonce = decodeBase64Url(envelope.nonce, NONCE_BYTES);
110
+ const ciphertext = decodeBase64Url(envelope.ciphertext);
111
+ const authTag = decodeBase64Url(envelope.authTag, AUTH_TAG_BYTES);
112
+ try {
113
+ const decipher = (0, node_crypto_1.createDecipheriv)(exports.PROFILE_ENCRYPTION_ALGORITHM, key, nonce, { authTagLength: AUTH_TAG_BYTES });
114
+ decipher.setAAD(authenticatedMetadata(profile, field), {
115
+ plaintextLength: ciphertext.byteLength,
116
+ });
117
+ decipher.setAuthTag(authTag);
118
+ const plaintext = Buffer.concat([
119
+ decipher.update(ciphertext),
120
+ decipher.final(),
121
+ ]);
122
+ try {
123
+ return plaintext.toString("utf8");
124
+ }
125
+ finally {
126
+ plaintext.fill(0);
127
+ }
128
+ }
129
+ finally {
130
+ nonce.fill(0);
131
+ ciphertext.fill(0);
132
+ authTag.fill(0);
133
+ }
134
+ }
135
+ catch {
136
+ // Wrong keys, modified metadata, tags, nonces, and ciphertext deliberately
137
+ // share one redacted failure so errors do not become a credential oracle.
138
+ throw new Error("Credential decryption failed");
139
+ }
140
+ finally {
141
+ key.fill(0);
142
+ }
143
+ }
144
+ function resolveCredentialSource(source, context, resolvers, keyProvider) {
145
+ if (typeof source === "string") {
146
+ const migrated = legacyEnvironmentReference(source);
147
+ if (!migrated)
148
+ throw new Error("Legacy plaintext credentials are not supported");
149
+ const value = process.env[migrated.reference];
150
+ if (!value)
151
+ throw new Error("Credential resolution failed");
152
+ return value;
153
+ }
154
+ if (isEncryptedCredentialEnvelope(source)) {
155
+ return decryptCredential(source, context.profile, context.field, keyProvider);
156
+ }
157
+ if (!isSecretReference(source)) {
158
+ throw new Error("Credential source is invalid");
159
+ }
160
+ const resolver = resolvers.get(source.provider);
161
+ if (!resolver)
162
+ throw new Error("Credential resolver is unavailable");
163
+ try {
164
+ const resolved = resolver.resolve(source.reference, context);
165
+ if (typeof resolved !== "string" || resolved.length === 0) {
166
+ throw new Error("empty");
167
+ }
168
+ return resolved;
169
+ }
170
+ catch {
171
+ // Do not include provider names or opaque references in errors.
172
+ throw new Error("Credential resolution failed");
173
+ }
174
+ }
175
+ /** Validate untrusted descriptor data without invoking accessors or proxy traps. */
176
+ function isCredentialSourceDescriptor(value) {
177
+ if (typeof value === "string")
178
+ return legacyEnvironmentReference(value) !== undefined;
179
+ return isSecretReference(value) || isEncryptedCredentialEnvelope(value);
180
+ }
181
+ function isSecretReference(value) {
182
+ const record = ownDataRecord(value);
183
+ if (!record || record.type !== "secret_ref")
184
+ return false;
185
+ if (!hasExactKeys(record, ["type", "provider", "reference"]))
186
+ return false;
187
+ if (typeof record.provider !== "string" ||
188
+ !/^[a-z][a-z0-9_-]{0,31}$/u.test(record.provider) ||
189
+ typeof record.reference !== "string" ||
190
+ record.reference.length === 0 ||
191
+ record.reference.length > 2048 ||
192
+ /[\r\n\0]/u.test(record.reference)) {
193
+ return false;
194
+ }
195
+ return record.provider !== "env" || isCanonicalEnvironmentName(record.reference);
196
+ }
197
+ function isEncryptedCredentialEnvelope(value) {
198
+ const record = ownDataRecord(value);
199
+ if (!record || record.type !== "encrypted")
200
+ return false;
201
+ if (!hasExactKeys(record, [
202
+ "type",
203
+ "version",
204
+ "algorithm",
205
+ "nonce",
206
+ "ciphertext",
207
+ "authTag",
208
+ ]) ||
209
+ record.version !== exports.PROFILE_ENCRYPTION_VERSION ||
210
+ record.algorithm !== exports.PROFILE_ENCRYPTION_ALGORITHM ||
211
+ typeof record.nonce !== "string" ||
212
+ typeof record.ciphertext !== "string" ||
213
+ typeof record.authTag !== "string") {
214
+ return false;
215
+ }
216
+ try {
217
+ decodeBase64Url(record.nonce, NONCE_BYTES);
218
+ decodeBase64Url(record.ciphertext);
219
+ decodeBase64Url(record.authTag, AUTH_TAG_BYTES);
220
+ return true;
221
+ }
222
+ catch {
223
+ return false;
224
+ }
225
+ }
226
+ function legacyEnvironmentReference(value) {
227
+ if (!value.startsWith("env:"))
228
+ return undefined;
229
+ const reference = value.slice(4);
230
+ // Existing V1 profiles may use a non-SN_* variable. It remains readable as
231
+ // an explicit migration compatibility form, while all newly constructed
232
+ // structured env references must use the canonical SN_* namespace.
233
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(reference))
234
+ return undefined;
235
+ return Object.freeze({ type: "secret_ref", provider: "env", reference });
236
+ }
237
+ function credentialSourceKind(source) {
238
+ if (source === undefined)
239
+ return "missing";
240
+ return typeof source === "string" || isSecretReference(source)
241
+ ? "secret_ref"
242
+ : "encrypted";
243
+ }
244
+ function authenticatedMetadata(profile, field) {
245
+ return Buffer.from(JSON.stringify({
246
+ domain: "servicenow-mcp/profile-credential",
247
+ version: exports.PROFILE_ENCRYPTION_VERSION,
248
+ algorithm: exports.PROFILE_ENCRYPTION_ALGORITHM,
249
+ profile,
250
+ field,
251
+ }), "utf8");
252
+ }
253
+ function assertBinding(profile, field) {
254
+ if (typeof profile !== "string" ||
255
+ profile.trim().length === 0 ||
256
+ !["credential", "clientSecret", "apiKey"].includes(field)) {
257
+ throw new Error("Credential binding is invalid");
258
+ }
259
+ }
260
+ function decodeEncryptionKey(encoded) {
261
+ const value = encoded.trim();
262
+ if (!/^[A-Za-z0-9+/_-]+={0,2}$/u.test(value)) {
263
+ throw new Error("Profile encryption key is invalid");
264
+ }
265
+ const encoding = value.includes("-") || value.includes("_") ? "base64url" : "base64";
266
+ const key = Buffer.from(value, encoding);
267
+ if (key.byteLength !== KEY_BYTES) {
268
+ key.fill(0);
269
+ throw new Error("Profile encryption key is invalid");
270
+ }
271
+ return key;
272
+ }
273
+ /**
274
+ * Copy provider-owned key material before validation or zeroization. Providers
275
+ * may deliberately cache their Buffer; cryptographic callers must never mutate
276
+ * that storage while cleaning up an operation-local key copy.
277
+ */
278
+ function ownedEncryptionKey(provider) {
279
+ const provided = provider.getKey();
280
+ if (!Buffer.isBuffer(provided)) {
281
+ throw new Error("Profile encryption key is invalid");
282
+ }
283
+ const owned = Buffer.from(provided);
284
+ if (owned.byteLength !== KEY_BYTES) {
285
+ owned.fill(0);
286
+ throw new Error("Profile encryption key is invalid");
287
+ }
288
+ return owned;
289
+ }
290
+ function decodeBase64Url(value, expectedLength) {
291
+ if (!/^[A-Za-z0-9_-]*$/u.test(value))
292
+ throw new Error("invalid encoding");
293
+ const decoded = Buffer.from(value, "base64url");
294
+ if (decoded.toString("base64url") !== value ||
295
+ (expectedLength !== undefined && decoded.byteLength !== expectedLength)) {
296
+ throw new Error("invalid encoding");
297
+ }
298
+ return decoded;
299
+ }
300
+ function isCanonicalEnvironmentName(value) {
301
+ return /^SN_[A-Z0-9_]+$/u.test(value);
302
+ }
303
+ function ownDataRecord(value) {
304
+ if (typeof value !== "object" || value === null || node_util_1.types.isProxy(value)) {
305
+ return undefined;
306
+ }
307
+ const prototype = Object.getPrototypeOf(value);
308
+ if (prototype !== Object.prototype && prototype !== null)
309
+ return undefined;
310
+ const descriptors = Object.getOwnPropertyDescriptors(value);
311
+ const record = Object.create(null);
312
+ for (const [key, descriptor] of Object.entries(descriptors)) {
313
+ if (!("value" in descriptor) || !descriptor.enumerable)
314
+ return undefined;
315
+ record[key] = descriptor.value;
316
+ }
317
+ return record;
318
+ }
319
+ function hasExactKeys(value, expected) {
320
+ const actual = Object.keys(value).sort();
321
+ const wanted = [...expected].sort();
322
+ if (actual.length !== wanted.length)
323
+ return false;
324
+ const actualBytes = Buffer.from(actual.join("\0"));
325
+ const wantedBytes = Buffer.from(wanted.join("\0"));
326
+ return (actualBytes.byteLength === wantedBytes.byteLength &&
327
+ (0, node_crypto_1.timingSafeEqual)(actualBytes, wantedBytes));
328
+ }
329
+ //# sourceMappingURL=profile-credentials.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"profile-credentials.js","sourceRoot":"","sources":["../src/profile-credentials.ts"],"names":[],"mappings":";AAAA,iFAAiF;;;AAwFjF,wEAOC;AAcD,0CASC;AAED,8CAoCC;AAED,8CAiDC;AAED,0DAiCC;AAGD,oEAKC;AAED,8CAeC;A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K,MAAM,CAAC,GAAG,EAAE,UAAU,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE,CAAC;QAC5D,IAAI,CAAC,CAAC,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU;YAAE,OAAO,SAAS,CAAC;QACzE,MAAM,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC;IACjC,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,YAAY,CACnB,KAA8B,EAC9B,QAA2B;IAE3B,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC;IACzC,MAAM,MAAM,GAAG,CAAC,GAAG,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC;IACpC,IAAI,MAAM,CAAC,MAAM,KAAK,MAAM,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IAClD,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACnD,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACnD,OAAO,CACL,WAAW,CAAC,UAAU,KAAK,WAAW,CAAC,UAAU;QACjD,IAAA,6BAAe,EAAC,WAAW,EAAE,WAAW,CAAC,CAC1C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,187 @@
1
+ /**
2
+ * Multi-instance profile manager for ServiceNow MCP server.
3
+ *
4
+ * Supports named profiles stored in ~/.servicenow-mcp/config.json.
5
+ * When no file exists, canonical SN_* connection values are available only
6
+ * through an explicitly named out-of-band SN_PROFILE_NAME mapping. Bare SN_*
7
+ * values never create a synthetic/default routing target.
8
+ *
9
+ * @module profiles
10
+ */
11
+ import { AuthType, GrantType, ServiceNowConfig } from "./config.js";
12
+ import { type TableAccessPolicyInput } from "./table-policy.js";
13
+ import { type MetadataCacheConfigInput } from "./metadata-cache.js";
14
+ import { ServiceNowClient } from "./client.js";
15
+ import { type CredentialSource, type ProfileEncryptionKeyProvider, type ProfileSecretField, type SecretResolver } from "./profile-credentials.js";
16
+ export interface Profile {
17
+ /** ServiceNow instance URL (e.g. "https://myinstance.service-now.com") */
18
+ instance: string;
19
+ /** ServiceNow username (basic auth and OAuth "password" grant) */
20
+ username?: string;
21
+ /** Password credential source for basic auth or OAuth password grant. */
22
+ credential?: CredentialSource;
23
+ /** Application scope (e.g. "x_knowd_know_drago") */
24
+ scope?: string;
25
+ /** Scope sys_id */
26
+ scope_sys_id?: string;
27
+ /** Vendor code (e.g. "knowd", "1105906") */
28
+ vendor_code?: string;
29
+ /** Human-readable description of this profile */
30
+ description?: string;
31
+ /** Auth scheme: "basic" (default), "oauth", or "apikey" */
32
+ authType?: AuthType;
33
+ /** OAuth client id (authType "oauth") */
34
+ clientId?: string;
35
+ /** OAuth client-secret source (authType "oauth"). */
36
+ clientSecret?: CredentialSource;
37
+ /** OAuth grant type: "client_credentials" (default) or "password" */
38
+ grantType?: GrantType;
39
+ /** API-key source (authType "apikey"). */
40
+ apiKey?: CredentialSource;
41
+ /** Header the API key is sent in (default "x-sn-apikey") */
42
+ apiKeyHeader?: string;
43
+ /** Per-request timeout in ms (default 30000; env fallback SN_TIMEOUT_MS) */
44
+ timeoutMs?: number;
45
+ /** Per-instance upstream REST concurrency cap (default 4; env fallback SN_MAX_CONCURRENT_REQUESTS) */
46
+ maxConcurrentRequests?: number;
47
+ /** Schema cache TTL in ms (default 300000; env fallback SN_SCHEMA_CACHE_TTL_MS) */
48
+ schemaCacheTtlMs?: number;
49
+ /** Per-profile table access rules. Missing rules intentionally deny all tables. */
50
+ tableAccess?: TableAccessPolicyInput;
51
+ /** Per-profile legacy field policy object, matching SN_FIELD_POLICY_DEFINITIONS. */
52
+ fieldPolicy?: unknown;
53
+ /** Per-profile readable fields: [{ table, fields }]. Missing readable fields means all fields are readable. */
54
+ readableTableFields?: unknown;
55
+ /** Per-profile writable fields: [{ table, fields }]. Missing writable fields means all fields are writable. */
56
+ writableTableFields?: unknown;
57
+ /** Per-instance metadata cache configuration (default TTL 24h). */
58
+ metadataCache?: MetadataCacheConfigInput;
59
+ }
60
+ export interface ProfileConfig {
61
+ /** Config file schema version */
62
+ version: number;
63
+ /** Map of profile name to profile definition */
64
+ profiles: Record<string, Profile>;
65
+ }
66
+ export interface ProfileListEntry {
67
+ name: string;
68
+ instance: string;
69
+ authType: AuthType;
70
+ description?: string;
71
+ }
72
+ export interface ProfileManagerOptions {
73
+ configFilePath?: string;
74
+ secretResolvers?: readonly SecretResolver[];
75
+ encryptionKeyProvider?: ProfileEncryptionKeyProvider;
76
+ }
77
+ export declare class ProfileManager {
78
+ private config;
79
+ private clientCache;
80
+ private configFilePath;
81
+ private readonly secretResolvers;
82
+ private readonly encryptionKeyProvider;
83
+ constructor(options?: ProfileManagerOptions);
84
+ /**
85
+ * Get a profile by its explicit configured name.
86
+ * Throws if the profile does not exist.
87
+ */
88
+ getProfile(name: string): Profile;
89
+ /**
90
+ * Resolve the credential for a profile.
91
+ * If the credential starts with "env:", reads the value from the named
92
+ * environment variable. Otherwise returns the plain string and emits
93
+ * a warning to stderr about storing credentials in plain text.
94
+ */
95
+ resolveCredential(profile: Profile): string;
96
+ /**
97
+ * Resolve a secret value that supports "env:VAR_NAME" indirection
98
+ * (credential, clientSecret, apiKey). Plain values are returned as-is
99
+ * with a stderr warning.
100
+ */
101
+ private resolveSecret;
102
+ /** List all configured profiles without selecting or activating one. */
103
+ listProfiles(): ProfileListEntry[];
104
+ /**
105
+ * Names of configured profiles that authenticate with basic auth.
106
+ * Used for the startup deprecation warning (ServiceNow's inbound
107
+ * Basic Auth restriction program). Unconfigured placeholder profiles
108
+ * (empty instance) are skipped.
109
+ */
110
+ getBasicAuthProfileNames(): string[];
111
+ /**
112
+ * Return a ServiceNowClient for an explicitly named profile.
113
+ * Clients are cached per profile name -- the same instance is returned on
114
+ * subsequent calls for the same profile.
115
+ */
116
+ getClient(name: string, resolvedConfig?: Readonly<ServiceNowConfig>): ServiceNowClient;
117
+ /** Return a ServiceNowConfig for an explicitly named profile. */
118
+ getConfig(name: string): ServiceNowConfig;
119
+ /**
120
+ * Add a new profile at runtime and persist the updated config to disk.
121
+ * If a profile with the same name already exists, it is overwritten.
122
+ */
123
+ addProfile(name: string, profile: Profile): void;
124
+ /** Replace one credential source without exposing it through an MCP tool. */
125
+ rotateCredential(name: string, field: ProfileSecretField, source: CredentialSource): void;
126
+ /** Remove one named profile through the out-of-band administration boundary. */
127
+ removeProfile(name: string): void;
128
+ /**
129
+ * Return the path to the config file on disk.
130
+ */
131
+ getConfigPath(): string;
132
+ /**
133
+ * Attempt to load the config file from disk.
134
+ * Returns null if the file does not exist or is not valid JSON.
135
+ */
136
+ private loadConfigFile;
137
+ /**
138
+ * Build one explicitly named, process-local profile from canonical SN_*
139
+ * variables. SN_PROFILE_NAME is the required out-of-band mapping: without
140
+ * it, even fully populated connection variables produce no profile. This
141
+ * preserves SN_* support without a default/active/omitted routing path.
142
+ * Additional env vars configure the auth scheme:
143
+ * SN_AUTH_TYPE, SN_CLIENT_ID, SN_CLIENT_SECRET, SN_GRANT_TYPE,
144
+ * SN_API_KEY, SN_API_KEY_HEADER, SN_TIMEOUT_MS.
145
+ */
146
+ private buildExplicitEnvironmentProfile;
147
+ /**
148
+ * Persist the current config to disk, creating the directory if needed.
149
+ */
150
+ private persistConfig;
151
+ private mutateConfig;
152
+ /**
153
+ * Find the profile name for a given Profile object.
154
+ * Used for warning messages.
155
+ */
156
+ private getProfileNameFor;
157
+ }
158
+ /**
159
+ * Validate the instance URL of a profile that is being added at runtime
160
+ * (sn_profile add). Returns an error message, or undefined when the URL
161
+ * is acceptable.
162
+ *
163
+ * Secrets configured for one instance must not be redirectable to an
164
+ * arbitrary host: a profile stores env-var REFERENCES, and the resolved
165
+ * secret is sent to the profile's instance on the next authenticated
166
+ * call. So runtime adds require https and a *.service-now.com /
167
+ * *.servicenowservices.com host. Self-hosted or custom-domain instances
168
+ * are supported via the SN_ALLOWED_INSTANCE_HOSTS environment variable
169
+ * (comma-separated hostnames; a "*.example.com" entry allows any
170
+ * subdomain) -- set by the operator, never by the model.
171
+ *
172
+ * Profiles hand-written into config.json are not affected: the operator
173
+ * edits that file directly and this check only runs in addProfile.
174
+ */
175
+ export declare function instanceHostError(instance: string): string | undefined;
176
+ /**
177
+ * Normalize an instance URL: strip trailing slashes, ensure https://.
178
+ */
179
+ export declare function normalizeInstanceUrl(instance: string): string;
180
+ /**
181
+ * Stable, non-reversible discriminator for the exact credential material a
182
+ * resolved configuration will present to ServiceNow. Two configurations that
183
+ * differ in any credential input produce different fingerprints, so it is
184
+ * usable wherever per-identity state must not be shared. Never log it.
185
+ */
186
+ export declare function credentialFingerprint(config: ServiceNowConfig): string;
187
+ //# sourceMappingURL=profile-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"profile-manager.d.ts","sourceRoot":"","sources":["../src/profile-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAMH,OAAO,EACL,QAAQ,EACR,SAAS,EACT,gBAAgB,EAKjB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAyC,KAAK,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AACvG,OAAO,EAAsC,KAAK,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AACxG,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAGL,KAAK,gBAAgB,EACrB,KAAK,4BAA4B,EACjC,KAAK,kBAAkB,EACvB,KAAK,cAAc,EAIpB,MAAM,0BAA0B,CAAC;AAIlC,MAAM,WAAW,OAAO;IACtB,0EAA0E;IAC1E,QAAQ,EAAE,MAAM,CAAC;IACjB,kEAAkE;IAClE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,yEAAyE;IACzE,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAC9B,oDAA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