@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,397 @@
1
+ "use strict";
2
+ /**
3
+ * Per-identity read-through cache for stable ServiceNow metadata table reads.
4
+ *
5
+ * Cached reads are scoped by caller-supplied cache scope and full GET
6
+ * selector. The scope must discriminate credential identity as well as
7
+ * instance: ServiceNow evaluates ACLs per user, and a cache hit skips the
8
+ * upstream read entirely, so a scope shared between identities would serve
9
+ * one caller's authorized rows to another. A cache hit performs a lightweight
10
+ * sys_updated_on probe before reuse; changed rows force a refresh. Callers can
11
+ * force refresh by passing the internal sysparm_force_recache=true parameter,
12
+ * which is stripped before upstream I/O.
13
+ *
14
+ * The freshness probe compares sys_updated_on, which ServiceNow evaluates in
15
+ * the session user's timezone, so the probe timestamp is formatted in that
16
+ * timezone rather than UTC. The zone is resolved per identity from
17
+ * sys_user.time_zone, falling back to the glide.sys.default.tz property; when
18
+ * it cannot be resolved, caching is disabled for that identity rather than
19
+ * assuming UTC and serving silently stale metadata.
20
+ *
21
+ * Deletions are not detected by the probe, which sees updates only. A row
22
+ * deleted upstream stays visible for at most one TTL: a successful probe
23
+ * refreshes recency for eviction but never extends expiresAt, so the entry
24
+ * still expires on schedule and forces a genuine refetch.
25
+ *
26
+ * @module metadata-cache
27
+ */
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.MAX_METADATA_CACHE_ENTRIES_PER_SCOPE = exports.MAX_METADATA_CACHE_SCOPES = exports.FORCE_RECACHE_PARAM = exports.DEFAULT_METADATA_CACHE_TABLES = exports.DEFAULT_METADATA_CACHE_TTL_MS = void 0;
30
+ exports.metadataCacheConfigFromEnvironment = metadataCacheConfigFromEnvironment;
31
+ exports.createMetadataCacheConfig = createMetadataCacheConfig;
32
+ exports.createCachedServiceNowOperations = createCachedServiceNowOperations;
33
+ const config_js_1 = require("./config.js");
34
+ const utils_js_1 = require("./utils.js");
35
+ exports.DEFAULT_METADATA_CACHE_TTL_MS = 24 * 60 * 60 * 1000;
36
+ /**
37
+ * Tables cached by default.
38
+ *
39
+ * The bar for inclusion is a large payload relative to the freshness probe,
40
+ * not merely "this table changes rarely" -- a cache hit still costs one
41
+ * upstream round trip (see hasChangedSince), so the saving is payload bytes,
42
+ * never request count. sys_properties is deliberately absent: its rows are
43
+ * ACL-restricted per user and routinely hold integration secrets in `value`,
44
+ * and the only reads of it here are three single-value build-info lookups
45
+ * whose payloads are far too small to be worth retaining for a day. Operators
46
+ * who want it can add it through SN_METADATA_CACHE_TABLES.
47
+ */
48
+ exports.DEFAULT_METADATA_CACHE_TABLES = Object.freeze([
49
+ "sys_glide_object",
50
+ "sys_dictionary",
51
+ "sys_db_object",
52
+ "sys_app",
53
+ "sys_plugins",
54
+ "sys_metadata*",
55
+ "sys_flow*",
56
+ ]);
57
+ const SCOPED_CACHES = new Map();
58
+ const TABLE_NAME = /^[a-z][a-z0-9_]{0,79}$/u;
59
+ exports.FORCE_RECACHE_PARAM = "sysparm_force_recache";
60
+ const MAX_CACHE_TABLE_PATTERNS = 128;
61
+ const MAX_CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
62
+ /** Bounds on process-global cache state. Scopes multiply with credential
63
+ * rotation and profiles, and entries multiply with distinct GET selectors,
64
+ * so both are capped and evicted in least-recently-used order. */
65
+ exports.MAX_METADATA_CACHE_SCOPES = 64;
66
+ exports.MAX_METADATA_CACHE_ENTRIES_PER_SCOPE = 512;
67
+ /**
68
+ * Resolved ServiceNow session timezone per cache scope.
69
+ *
70
+ * A timezone NAME is cached, never a numeric offset: an offset is wrong for
71
+ * half the year, and a cached one silently reintroduces the skew at every DST
72
+ * transition -- the longer the TTL, the longer it stays wrong. The offset is
73
+ * computed from the name for the specific instant being compared.
74
+ *
75
+ * Keyed by the same scope as the metadata cache, so it discriminates instance
76
+ * AND credential identity: two profiles on one instance can authenticate as
77
+ * accounts in different timezones, and sharing a resolution between them would
78
+ * corrupt the freshness window for both.
79
+ */
80
+ const SCOPED_TIME_ZONES = new Map();
81
+ const TIME_ZONE_WARNED_SCOPES = new Set();
82
+ const DEFAULT_TIME_ZONE_PROPERTY = "glide.sys.default.tz";
83
+ function metadataCacheConfigFromEnvironment(environment = process.env) {
84
+ const ttlMs = (0, config_js_1.parsePositiveIntegerEnv)(environment.SN_METADATA_CACHE_TTL_MS, "SN_METADATA_CACHE_TTL_MS", { max: MAX_CACHE_TTL_MS });
85
+ const tables = parseTablePatterns(environment.SN_METADATA_CACHE_TABLES);
86
+ return Object.freeze({
87
+ ...(ttlMs === undefined ? {} : { ttlMs }),
88
+ ...(tables === undefined ? {} : { tables }),
89
+ });
90
+ }
91
+ function createMetadataCacheConfig(input) {
92
+ return Object.freeze({
93
+ ttlMs: validateTtlMs(input?.ttlMs ?? exports.DEFAULT_METADATA_CACHE_TTL_MS),
94
+ tables: normalizeTablePatterns(input?.tables ?? exports.DEFAULT_METADATA_CACHE_TABLES),
95
+ });
96
+ }
97
+ /**
98
+ * @param scope Cache partition key. Must identify the ServiceNow instance
99
+ * *and* the credential identity reading from it -- see the module docstring.
100
+ * @param sessionUserName ServiceNow user_name this scope authenticates as.
101
+ * Required to resolve the session timezone; without it the freshness probe
102
+ * cannot be made correct, so caching is disabled for the scope.
103
+ */
104
+ function createCachedServiceNowOperations(operations, scope, config, sessionUserName) {
105
+ if (config === undefined && process.env.NODE_ENV === "test") {
106
+ return operations;
107
+ }
108
+ const cacheConfig = createMetadataCacheConfig(config);
109
+ const scopedCache = cacheForScope(scope);
110
+ const cached = Object.create(null);
111
+ Object.defineProperties(cached, {
112
+ get: {
113
+ value: async (path, params) => (await cachedGetWithMeta(operations, scopedCache, cacheConfig, path, params, { scope, sessionUserName })).data,
114
+ },
115
+ getWithMeta: {
116
+ value: (path, params) => cachedGetWithMeta(operations, scopedCache, cacheConfig, path, params, {
117
+ scope,
118
+ sessionUserName,
119
+ }),
120
+ },
121
+ post: { value: operations.post.bind(operations) },
122
+ patch: { value: operations.patch.bind(operations) },
123
+ delete: { value: operations.delete.bind(operations) },
124
+ postBinary: { value: operations.postBinary.bind(operations) },
125
+ getRaw: { value: operations.getRaw.bind(operations) },
126
+ });
127
+ return Object.freeze(cached);
128
+ }
129
+ async function cachedGetWithMeta(operations, cache, config, path, params, identity) {
130
+ // Strip the internal flag before any upstream call, including the
131
+ // passthrough: sysparm_force_recache is this module's own parameter and must
132
+ // never reach ServiceNow, whether or not the table is cached.
133
+ const { cleanParams, forceRecache } = stripForceRecache(params);
134
+ const match = metadataTableFromPath(path);
135
+ if (!match || !tableMatches(config.tables, match)) {
136
+ return operations.getWithMeta(path, cleanParams);
137
+ }
138
+ // The freshness probe compares sys_updated_on in the session user's
139
+ // timezone. Without knowing that timezone the probe is wrong by the offset,
140
+ // so caching is disabled for this identity rather than assuming UTC.
141
+ const timeZone = await sessionTimeZone(operations, identity);
142
+ if (timeZone === undefined) {
143
+ return operations.getWithMeta(path, cleanParams);
144
+ }
145
+ const key = cacheKey(path, cleanParams);
146
+ const now = Date.now();
147
+ const existing = cache.get(key);
148
+ if (existing && existing.expiresAt <= now)
149
+ cache.delete(key);
150
+ if (!forceRecache && existing && existing.expiresAt > now) {
151
+ const changed = await hasChangedSince(operations, match, existing.lastSyncedAt, timeZone);
152
+ if (!changed) {
153
+ // Re-insert to mark the entry most recently used for eviction order.
154
+ cache.delete(key);
155
+ cache.set(key, existing);
156
+ return cloneCachedResult(existing);
157
+ }
158
+ }
159
+ const lastSyncedAt = Date.now();
160
+ const fresh = await operations.getWithMeta(path, cleanParams);
161
+ // Only a materialized 200 body is reusable. A 204 carries no rows, and a
162
+ // failed read throws before reaching here, so nothing negative is cached.
163
+ if (fresh.status !== 200 || fresh.data === null)
164
+ return fresh;
165
+ cache.delete(key);
166
+ cache.set(key, Object.freeze({
167
+ expiresAt: lastSyncedAt + config.ttlMs,
168
+ lastSyncedAt,
169
+ data: cloneJson(fresh.data),
170
+ status: fresh.status,
171
+ headers: Object.freeze([...fresh.headers.entries()]),
172
+ }));
173
+ evictOldest(cache, exports.MAX_METADATA_CACHE_ENTRIES_PER_SCOPE);
174
+ return fresh;
175
+ }
176
+ /** Drop least-recently-used entries until the map is within `limit`. */
177
+ function evictOldest(entries, limit) {
178
+ while (entries.size > limit) {
179
+ const oldest = entries.keys().next();
180
+ if (oldest.done)
181
+ return;
182
+ entries.delete(oldest.value);
183
+ }
184
+ }
185
+ async function hasChangedSince(operations, table, lastSyncedAt, timeZone) {
186
+ try {
187
+ const response = await operations.get(`/api/now/table/${table}`, {
188
+ sysparm_query: `sys_updated_on>${serviceNowDateTime(lastSyncedAt, timeZone)}`,
189
+ sysparm_fields: "sys_id,sys_updated_on",
190
+ sysparm_limit: "1",
191
+ sysparm_no_count: "true",
192
+ sysparm_display_value: "false",
193
+ sysparm_exclude_reference_link: "true",
194
+ });
195
+ return Array.isArray(response?.result) && response.result.length > 0;
196
+ }
197
+ catch {
198
+ // A failed probe must not serve stale metadata indefinitely.
199
+ return true;
200
+ }
201
+ }
202
+ function metadataTableFromPath(path) {
203
+ const match = /^\/api\/now\/table\/([a-z][a-z0-9_]{0,79})(?:\/|$)/u.exec(path);
204
+ return match?.[1];
205
+ }
206
+ function tableMatches(patterns, table) {
207
+ return patterns.some((pattern) => pattern.endsWith("*")
208
+ ? table.startsWith(pattern.slice(0, -1))
209
+ : pattern === table);
210
+ }
211
+ function stripForceRecache(params) {
212
+ if (!params || params[exports.FORCE_RECACHE_PARAM] === undefined) {
213
+ return { cleanParams: params, forceRecache: false };
214
+ }
215
+ const { [exports.FORCE_RECACHE_PARAM]: forceValue, ...rest } = params;
216
+ return {
217
+ cleanParams: Object.keys(rest).length === 0 ? undefined : rest,
218
+ forceRecache: forceValue === "true" || forceValue === "1",
219
+ };
220
+ }
221
+ function cacheForScope(scope) {
222
+ const existing = SCOPED_CACHES.get(scope);
223
+ if (existing) {
224
+ SCOPED_CACHES.delete(scope);
225
+ SCOPED_CACHES.set(scope, existing);
226
+ return existing;
227
+ }
228
+ const next = new Map();
229
+ SCOPED_CACHES.set(scope, next);
230
+ evictOldest(SCOPED_CACHES, exports.MAX_METADATA_CACHE_SCOPES);
231
+ return next;
232
+ }
233
+ function cacheKey(path, params) {
234
+ return JSON.stringify({
235
+ path,
236
+ params: Object.fromEntries(Object.entries(params ?? {}).sort(([a], [b]) => a.localeCompare(b))),
237
+ });
238
+ }
239
+ function cloneCachedResult(entry) {
240
+ return {
241
+ data: cloneJson(entry.data),
242
+ status: entry.status,
243
+ headers: new Headers([...entry.headers]),
244
+ };
245
+ }
246
+ function cloneJson(value) {
247
+ if (value === null || value === undefined)
248
+ return value;
249
+ return JSON.parse(JSON.stringify(value));
250
+ }
251
+ /**
252
+ * Format an instant as ServiceNow's `YYYY-MM-DD HH:mm:ss` in the given zone.
253
+ *
254
+ * The offset is derived from the zone NAME for this specific instant, so a
255
+ * timestamp on either side of a DST transition resolves to the offset actually
256
+ * in force then. This is why the name is what gets cached.
257
+ */
258
+ function serviceNowDateTime(epochMs, timeZone) {
259
+ const parts = new Intl.DateTimeFormat("en-US", {
260
+ timeZone,
261
+ hourCycle: "h23",
262
+ year: "numeric",
263
+ month: "2-digit",
264
+ day: "2-digit",
265
+ hour: "2-digit",
266
+ minute: "2-digit",
267
+ second: "2-digit",
268
+ }).formatToParts(new Date(epochMs));
269
+ const field = (type) => parts.find((part) => part.type === type)?.value ?? "00";
270
+ return (`${field("year")}-${field("month")}-${field("day")} ` +
271
+ `${field("hour")}:${field("minute")}:${field("second")}`);
272
+ }
273
+ /** True when Intl recognizes the name, so a bad value never silently formats. */
274
+ function isUsableTimeZone(candidate) {
275
+ if (typeof candidate !== "string" || candidate.trim() === "")
276
+ return false;
277
+ try {
278
+ new Intl.DateTimeFormat("en-US", { timeZone: candidate.trim() });
279
+ return true;
280
+ }
281
+ catch {
282
+ return false;
283
+ }
284
+ }
285
+ /**
286
+ * Resolve, and memoize per scope, the timezone the session user's
287
+ * `sys_updated_on` comparisons are evaluated in.
288
+ *
289
+ * Chain: the user's own `sys_user.time_zone`, then the instance default in
290
+ * `glide.sys.default.tz`, then unresolved. `time_zone` is not a required field
291
+ * and is frequently empty; an empty value means "inherit the system default"
292
+ * and must never be read as UTC, which is the very error this fixes.
293
+ */
294
+ async function sessionTimeZone(operations, identity) {
295
+ const existing = SCOPED_TIME_ZONES.get(identity.scope);
296
+ if (existing) {
297
+ SCOPED_TIME_ZONES.delete(identity.scope);
298
+ SCOPED_TIME_ZONES.set(identity.scope, existing);
299
+ return existing;
300
+ }
301
+ const pending = resolveSessionTimeZone(operations, identity);
302
+ SCOPED_TIME_ZONES.set(identity.scope, pending);
303
+ evictOldest(SCOPED_TIME_ZONES, exports.MAX_METADATA_CACHE_SCOPES);
304
+ return pending;
305
+ }
306
+ async function resolveSessionTimeZone(operations, identity) {
307
+ const resolved = (await userTimeZone(operations, identity.sessionUserName)) ??
308
+ (await instanceDefaultTimeZone(operations));
309
+ if (resolved === undefined)
310
+ warnTimeZoneUnresolved(identity);
311
+ return resolved;
312
+ }
313
+ async function userTimeZone(operations, sessionUserName) {
314
+ if (typeof sessionUserName !== "string" || sessionUserName.trim() === "") {
315
+ return undefined;
316
+ }
317
+ return firstUsableValue(operations, "/api/now/table/sys_user", {
318
+ sysparm_query: `user_name=${(0, utils_js_1.escapeQueryValue)(sessionUserName.trim())}`,
319
+ sysparm_fields: "time_zone",
320
+ sysparm_limit: "1",
321
+ }, "time_zone");
322
+ }
323
+ async function instanceDefaultTimeZone(operations) {
324
+ return firstUsableValue(operations, "/api/now/table/sys_properties", {
325
+ sysparm_query: `name=${DEFAULT_TIME_ZONE_PROPERTY}`,
326
+ sysparm_fields: "value",
327
+ sysparm_limit: "1",
328
+ }, "value");
329
+ }
330
+ async function firstUsableValue(operations, path, params, field) {
331
+ try {
332
+ const response = await operations.get(path, {
333
+ ...params,
334
+ sysparm_display_value: "false",
335
+ sysparm_exclude_reference_link: "true",
336
+ });
337
+ const row = Array.isArray(response?.result) ? response.result[0] : undefined;
338
+ if (typeof row !== "object" || row === null)
339
+ return undefined;
340
+ const value = row[field];
341
+ return isUsableTimeZone(value) ? value.trim() : undefined;
342
+ }
343
+ catch {
344
+ // An unreadable lookup is indistinguishable from an empty one here, and
345
+ // both mean the same thing: the probe cannot be made correct.
346
+ return undefined;
347
+ }
348
+ }
349
+ /**
350
+ * Report the degradation once per scope. Names the account, never the scope
351
+ * key -- that carries the credential fingerprint.
352
+ */
353
+ function warnTimeZoneUnresolved(identity) {
354
+ if (TIME_ZONE_WARNED_SCOPES.has(identity.scope))
355
+ return;
356
+ TIME_ZONE_WARNED_SCOPES.add(identity.scope);
357
+ const account = typeof identity.sessionUserName === "string" && identity.sessionUserName.trim() !== ""
358
+ ? `account "${identity.sessionUserName.trim()}"`
359
+ : "an account with no configured user name";
360
+ process.stderr.write(`[servicenow-mcp] WARNING: metadata caching is disabled for ${account}: the ` +
361
+ `ServiceNow session timezone could not be resolved. Grant read access to ` +
362
+ `sys_user (time_zone) and sys_properties (${DEFAULT_TIME_ZONE_PROPERTY}), or ` +
363
+ `set the account's timezone, to re-enable it.\n`);
364
+ }
365
+ function validateTtlMs(value) {
366
+ if (typeof value !== "number" ||
367
+ !Number.isSafeInteger(value) ||
368
+ value < 1 ||
369
+ value > MAX_CACHE_TTL_MS) {
370
+ throw new TypeError(`metadata cache TTL must be between 1 and ${MAX_CACHE_TTL_MS}`);
371
+ }
372
+ return value;
373
+ }
374
+ function parseTablePatterns(value) {
375
+ if (value === undefined || value.trim() === "")
376
+ return undefined;
377
+ return value.split(",").map((entry) => entry.trim());
378
+ }
379
+ function normalizeTablePatterns(patterns) {
380
+ if (!Array.isArray(patterns) || patterns.length > MAX_CACHE_TABLE_PATTERNS) {
381
+ throw new TypeError("metadata cache tables are invalid or exceed the limit");
382
+ }
383
+ const out = new Set();
384
+ for (const candidate of patterns) {
385
+ if (typeof candidate !== "string") {
386
+ throw new TypeError("metadata cache table pattern must be a string");
387
+ }
388
+ const normalized = candidate.trim().toLowerCase();
389
+ const base = normalized.endsWith("*") ? normalized.slice(0, -1) : normalized;
390
+ if (!TABLE_NAME.test(base)) {
391
+ throw new TypeError("metadata cache table pattern must be a ServiceNow identifier or prefix*");
392
+ }
393
+ out.add(normalized);
394
+ }
395
+ return Object.freeze([...out].sort());
396
+ }
397
+ //# sourceMappingURL=metadata-cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"metadata-cache.js","sourceRoot":"","sources":["../src/metadata-cache.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;;;AAqFH,gFAaC;AAED,8DAOC;AASD,4EAwCC;AAtJD,2CAAsD;AACtD,yCAA8C;AAEjC,QAAA,6BAA6B,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACjE;;;;;;;;;;;GAW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AC,GAAG,CAAC,UAAU,CAAC,CAAC;IACtB,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;AACxC,CAAC"}
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env node
2
+ /** Out-of-band profile administration. This module is never registered as MCP. */
3
+ import type { Readable, Writable } from "node:stream";
4
+ import { type CredentialSource, type ProfileEncryptionKeyProvider, type ProfileSecretField } from "./profile-credentials.js";
5
+ import { ProfileManager, type Profile } from "./profile-manager.js";
6
+ import type { AuthType, GrantType } from "./config.js";
7
+ type Command = "create" | "inspect" | "rotate" | "remove";
8
+ type SourceMode = "encrypted" | "reference";
9
+ export declare const MAX_SENSITIVE_VALUE_CHARACTERS = 16384;
10
+ export declare const MAX_PROTECTED_INPUT_BYTES = 65536;
11
+ export declare const MAX_PROTECTED_INPUT_VALUES = 4;
12
+ export interface ProfileAdminIO {
13
+ readSensitive(label: string): Promise<string>;
14
+ write(value: string): void;
15
+ }
16
+ export interface ProfileAdminDependencies {
17
+ manager?: ProfileManager;
18
+ keyProvider?: ProfileEncryptionKeyProvider;
19
+ io?: ProfileAdminIO;
20
+ }
21
+ interface ParsedArguments {
22
+ command: Command;
23
+ values: ReadonlyMap<string, string>;
24
+ }
25
+ export declare function runProfileAdmin(argv: readonly string[], dependencies?: ProfileAdminDependencies): Promise<void>;
26
+ /** Strict parsing is the shell-history/process-list credential boundary. */
27
+ export declare function parseProfileAdminArguments(argv: readonly string[]): ParsedArguments;
28
+ export declare function createProtectedInputIO(input: Readable & {
29
+ isTTY?: boolean;
30
+ setRawMode?: (mode: boolean) => void;
31
+ }, output: Writable): ProfileAdminIO;
32
+ /** How a captured secret is stored: encrypted at rest, or a provider reference. */
33
+ export type CredentialSourceMode = SourceMode;
34
+ export interface ProfileSecretPlacement {
35
+ readonly mode: CredentialSourceMode;
36
+ /** Secret provider, required when mode is "reference". */
37
+ readonly provider?: string;
38
+ }
39
+ /**
40
+ * A secret captured from the operator but not yet stored.
41
+ *
42
+ * For "encrypted" this holds the plaintext; for "reference" it holds the
43
+ * *name* of the variable that supplies it. Both are validated on capture. The
44
+ * value is deliberately separated from persistence so a caller can verify the
45
+ * credential against the live instance before anything reaches disk — which is
46
+ * what keeps an aborted or failed setup from leaving a half-written profile.
47
+ */
48
+ export interface CapturedProfileSecret {
49
+ readonly field: ProfileSecretField;
50
+ readonly mode: CredentialSourceMode;
51
+ readonly value: string;
52
+ }
53
+ /** Read one secret through the protected prompt or bounded stdin. */
54
+ export declare function captureProfileSecret(io: ProfileAdminIO, field: ProfileSecretField, mode: CredentialSourceMode): Promise<CapturedProfileSecret>;
55
+ /** Convert a captured secret into the form persisted in the profile file. */
56
+ export declare function materializeProfileSecret(captured: CapturedProfileSecret, placement: ProfileSecretPlacement, profile: string, field: ProfileSecretField, keyProvider: ProfileEncryptionKeyProvider): CredentialSource;
57
+ /** Secret fields a profile of this shape must carry. */
58
+ export declare function requiredSecretFields(authType: AuthType, grantType: GrantType): ProfileSecretField[];
59
+ /** Non-secret metadata a profile of this shape must carry. */
60
+ export declare function validateCreateMetadata(profile: Profile, fields: readonly ProfileSecretField[]): void;
61
+ /** Non-secret profile summary: source kinds only, never a reference name. */
62
+ export declare function safeProfileView(name: string, profile: Profile): object;
63
+ export {};
64
+ //# sourceMappingURL=profile-admin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"profile-admin.d.ts","sourceRoot":"","sources":["../src/profile-admin.ts"],"names":[],"mappings":";AAEA,kFAAkF;AAElF,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAEL,KAAK,gBAAgB,EACrB,KAAK,4BAA4B,EACjC,KAAK,kBAAkB,EAIxB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,cAAc,EACd,KAAK,OAAO,EACb,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,QAAQ,EAA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