@onlyflows/servicenow-mcp 2.3.0-dev.2 → 2.3.0-dev.4

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 (89) hide show
  1. package/CHANGELOG.md +235 -26
  2. package/README.md +174 -36
  3. package/dist/client.d.ts +5 -13
  4. package/dist/client.d.ts.map +1 -1
  5. package/dist/client.js +11 -26
  6. package/dist/client.js.map +1 -1
  7. package/dist/code-search-snippet.d.ts +25 -0
  8. package/dist/code-search-snippet.d.ts.map +1 -0
  9. package/dist/code-search-snippet.js +67 -0
  10. package/dist/code-search-snippet.js.map +1 -0
  11. package/dist/code-search-targets.d.ts +66 -0
  12. package/dist/code-search-targets.d.ts.map +1 -0
  13. package/dist/code-search-targets.js +82 -0
  14. package/dist/code-search-targets.js.map +1 -0
  15. package/dist/config.d.ts +1 -1
  16. package/dist/config.d.ts.map +1 -1
  17. package/dist/field-policy.d.ts.map +1 -1
  18. package/dist/field-policy.js +17 -5
  19. package/dist/field-policy.js.map +1 -1
  20. package/dist/http-observability.js +1 -0
  21. package/dist/http-observability.js.map +1 -1
  22. package/dist/metadata-cache.d.ts +2 -2
  23. package/dist/metadata-cache.d.ts.map +1 -1
  24. package/dist/metadata-cache.js +3 -2
  25. package/dist/metadata-cache.js.map +1 -1
  26. package/dist/policy-card.js +1 -1
  27. package/dist/policy-card.js.map +1 -1
  28. package/dist/profile-admin.d.ts +18 -0
  29. package/dist/profile-admin.d.ts.map +1 -1
  30. package/dist/profile-admin.js +26 -1
  31. package/dist/profile-admin.js.map +1 -1
  32. package/dist/profile-manager.d.ts +1 -1
  33. package/dist/profile-manager.d.ts.map +1 -1
  34. package/dist/response-too-large.d.ts +23 -0
  35. package/dist/response-too-large.d.ts.map +1 -0
  36. package/dist/response-too-large.js +42 -0
  37. package/dist/response-too-large.js.map +1 -0
  38. package/dist/risky-grants.d.ts.map +1 -1
  39. package/dist/risky-grants.js +1 -0
  40. package/dist/risky-grants.js.map +1 -1
  41. package/dist/setup.d.ts +4 -0
  42. package/dist/setup.d.ts.map +1 -1
  43. package/dist/setup.js +165 -70
  44. package/dist/setup.js.map +1 -1
  45. package/dist/table-policy.d.ts +42 -2
  46. package/dist/table-policy.d.ts.map +1 -1
  47. package/dist/table-policy.js +90 -6
  48. package/dist/table-policy.js.map +1 -1
  49. package/dist/tool-error.d.ts +40 -4
  50. package/dist/tool-error.d.ts.map +1 -1
  51. package/dist/tool-error.js +81 -4
  52. package/dist/tool-error.js.map +1 -1
  53. package/dist/tool-table-access.d.ts +9 -2
  54. package/dist/tool-table-access.d.ts.map +1 -1
  55. package/dist/tool-table-access.js +93 -30
  56. package/dist/tool-table-access.js.map +1 -1
  57. package/dist/tools/batch.d.ts.map +1 -1
  58. package/dist/tools/batch.js +21 -5
  59. package/dist/tools/batch.js.map +1 -1
  60. package/dist/tools/catalog.d.ts.map +1 -1
  61. package/dist/tools/catalog.js +2 -1
  62. package/dist/tools/catalog.js.map +1 -1
  63. package/dist/tools/codesearch.d.ts +2 -2
  64. package/dist/tools/codesearch.d.ts.map +1 -1
  65. package/dist/tools/codesearch.js +535 -86
  66. package/dist/tools/codesearch.js.map +1 -1
  67. package/dist/tools/flow-view-contracts.d.ts +2 -2
  68. package/dist/tools/index.d.ts.map +1 -1
  69. package/dist/tools/index.js +13 -2
  70. package/dist/tools/index.js.map +1 -1
  71. package/dist/tools/query-module.d.ts.map +1 -1
  72. package/dist/tools/query-module.js +25 -4
  73. package/dist/tools/query-module.js.map +1 -1
  74. package/dist/tools/result-envelope.d.ts +2 -2
  75. package/dist/tools/result-envelope.d.ts.map +1 -1
  76. package/dist/tools/result-envelope.js +20 -2
  77. package/dist/tools/result-envelope.js.map +1 -1
  78. package/dist/unknown-filter-fields.d.ts +52 -0
  79. package/dist/unknown-filter-fields.d.ts.map +1 -0
  80. package/dist/unknown-filter-fields.js +87 -0
  81. package/dist/unknown-filter-fields.js.map +1 -0
  82. package/dist/version.d.ts +1 -1
  83. package/dist/version.js +1 -1
  84. package/docs/CLIENT-SETUP.md +23 -10
  85. package/docs/PRODUCTION-SECURITY.md +10 -6
  86. package/docs/PROFILE-CREDENTIALS.md +10 -4
  87. package/docs/V2-MIGRATION.md +1 -1
  88. package/package.json +1 -1
  89. package/server.json +2 -2
package/dist/setup.js CHANGED
@@ -118,10 +118,13 @@ const MINIMUM_NODE_MAJOR = 20;
118
118
  * Tools bound to a table by a read grant when `--tools` is not given.
119
119
  *
120
120
  * `sn_codesearch` is authorized per code table it searches (sys_script,
121
- * sys_script_include, sys_ui_script, sys_script_client, sys_ws_operation), so
122
- * binding it here is what lets a plain read grant on one of those tables run a
123
- * code search on it. On any other table it is inert: a search names only code
124
- * tables, so the binding grants nothing there.
121
+ * sys_script_include, sys_ui_script, sys_script_client, sys_ws_operation and
122
+ * sys_module), so binding it here is what lets a plain read grant on one of
123
+ * those tables run a code search on it. ATF step scripts are searched through
124
+ * three tables (sys_variable_value, sys_atf_step and sys_atf_test), and a plain
125
+ * read grant on all three allows that search the same way. On any other table
126
+ * it is inert: a search names only those tables, so the binding grants nothing
127
+ * there.
125
128
  */
126
129
  const DEFAULT_READ_TOOLS = Object.freeze([
127
130
  "sn_query",
@@ -235,66 +238,16 @@ async function runWizard(options, home, env, dependencies, out) {
235
238
  }
236
239
  const name = await askProfileName(prompter, existing);
237
240
  const instance = await askInstance(prompter);
238
- const authType = await askAuthType(prompter);
239
- const grantType = authType === "oauth"
240
- ? (await prompter.ask("OAuth grant type", {
241
- default: "client_credentials",
242
- choices: ["client_credentials", "password"],
243
- }))
244
- : "client_credentials";
245
- // Asked here, before any secret is in hand. A mistyped answer to a
246
- // question adjacent to a credential prompt echoes the credential, so the
247
- // last thing before the hidden prompts is deliberately not this question.
248
- const mode = (await prompter.ask("How should the credential be stored?", {
249
- default: "encrypted",
250
- choices: ["encrypted", "reference"],
251
- }));
252
- const provider = mode === "reference"
253
- ? await prompter.ask("Secret provider", { default: "env", choices: ["env"] })
254
- : undefined;
255
- const profile = { instance, authType };
256
- if (authType === "basic" || grantType === "password") {
257
- profile.username = await prompter.ask("ServiceNow username", {
258
- validate: (value) => value.trim() === "" ? "A username is required for this auth mode." : undefined,
259
- });
260
- }
261
- if (authType === "oauth") {
262
- profile.grantType = grantType;
263
- profile.clientId = await prompter.ask("OAuth client id (not a secret)", {
264
- validate: (value) => value.trim() === "" ? "A client id is required for OAuth." : undefined,
265
- });
266
- }
267
- if (authType === "apikey") {
268
- profile.apiKeyHeader = await prompter.ask("API key header", {
269
- default: "x-sn-apikey",
270
- });
271
- }
272
- const fields = (0, profile_admin_js_1.requiredSecretFields)(authType, grantType);
273
- (0, profile_admin_js_1.validateCreateMetadata)(profile, fields);
274
- // 3. Capture the secrets. Never echoed, never in argv, never logged.
275
- prompter.note(mode === "reference"
276
- ? "\nEnter the NAME of the environment variable holding each secret.\n" +
277
- "Input is hidden."
278
- : "\nEnter each secret now. Input is hidden — nothing you type from here\n" +
279
- "is displayed.");
280
- const captured = [];
281
- for (const field of fields) {
282
- captured.push(Object.freeze({ field, mode, value: await prompter.secret(field) }));
283
- }
284
- // 4. Verify against the live instance before anything is persisted.
285
- const config = wizardConfig(profile, captured, mode, env);
286
- const verify = dependencies.verifyCredential ?? verifyCredentialAgainstInstance;
287
- prompter.note("\nVerifying against the instance...");
288
- let verification = await verify(config);
289
- while (!verification.ok) {
290
- prompter.note(`\n FAILED ${verification.detail}`);
291
- if (verification.remedy)
292
- prompter.note(` -> ${verification.remedy}`);
293
- if (!(await prompter.confirm("\nRetry with the same settings?", true))) {
294
- throw new WizardAbort();
295
- }
296
- verification = await verify(config);
241
+ // 3-4. Authentication, the credential, and its verification. Collected in
242
+ // one place so a Basic 401 can send the operator back to OAuth
243
+ // without re-asking the profile name or the instance.
244
+ let forcedAuthType;
245
+ let collected;
246
+ while (collected === undefined) {
247
+ collected = await collectVerifiedCredential(prompter, instance, env, dependencies, forcedAuthType);
248
+ forcedAuthType = "oauth";
297
249
  }
250
+ const { profile, captured, mode, provider, config, verification } = collected;
298
251
  prompter.note(` ok ${verification.detail}`);
299
252
  if (verification.authenticatedButDenied) {
300
253
  prompter.note(` note this account cannot read ${WIZARD_PROBE_TABLE}, so metadata\n` +
@@ -344,6 +297,99 @@ async function runWizard(options, home, env, dependencies, out) {
344
297
  prompter.close();
345
298
  }
346
299
  }
300
+ /**
301
+ * Ask for the authentication type and its credential, then verify it against
302
+ * the instance. Returns `undefined` when the operator, after a 401 on a Basic
303
+ * credential, chose to switch to OAuth: the caller asks again with OAuth
304
+ * forced. Nothing is persisted here, and a discarded Basic credential is not
305
+ * kept anywhere.
306
+ */
307
+ async function collectVerifiedCredential(prompter, instance, env, dependencies, forcedAuthType) {
308
+ const authType = forcedAuthType ?? (await askAuthType(prompter));
309
+ if (authType === "oauth")
310
+ prompter.note(OAUTH_REQUIREMENTS_NOTE);
311
+ const grantType = authType === "oauth"
312
+ ? (await prompter.ask("OAuth grant type", {
313
+ default: "client_credentials",
314
+ choices: ["client_credentials", "password"],
315
+ }))
316
+ : "client_credentials";
317
+ // Asked here, before any secret is in hand. A mistyped answer to a
318
+ // question adjacent to a credential prompt echoes the credential, so the
319
+ // last thing before the hidden prompts is deliberately not this question.
320
+ const mode = (await prompter.ask("How should the credential be stored?", {
321
+ default: "encrypted",
322
+ choices: ["encrypted", "reference"],
323
+ }));
324
+ const provider = mode === "reference"
325
+ ? await prompter.ask("Secret provider", { default: "env", choices: ["env"] })
326
+ : undefined;
327
+ const profile = { instance, authType };
328
+ if (authType === "basic" || grantType === "password") {
329
+ profile.username = await prompter.ask("ServiceNow username", {
330
+ validate: (value) => value.trim() === "" ? "A username is required for this auth mode." : undefined,
331
+ });
332
+ }
333
+ if (authType === "oauth") {
334
+ profile.grantType = grantType;
335
+ profile.clientId = await prompter.ask("OAuth client id (not a secret)", {
336
+ validate: (value) => value.trim() === "" ? "A client id is required for OAuth." : undefined,
337
+ });
338
+ }
339
+ if (authType === "apikey") {
340
+ profile.apiKeyHeader = await prompter.ask("API key header", {
341
+ default: "x-sn-apikey",
342
+ });
343
+ }
344
+ const fields = (0, profile_admin_js_1.requiredSecretFields)(authType, grantType);
345
+ (0, profile_admin_js_1.validateCreateMetadata)(profile, fields);
346
+ // Capture the secrets. Never echoed, never in argv, never logged.
347
+ prompter.note(mode === "reference"
348
+ ? "\nEnter the NAME of the environment variable holding each secret.\n" +
349
+ "Input is hidden."
350
+ : "\nEnter each secret now. Input is hidden — nothing you type from here\n" +
351
+ "is displayed.");
352
+ const captured = [];
353
+ for (const field of fields) {
354
+ captured.push(Object.freeze({ field, mode, value: await prompter.secret(field) }));
355
+ }
356
+ // Verify against the live instance before anything is persisted.
357
+ const config = wizardConfig(profile, captured, mode, env);
358
+ const verify = dependencies.verifyCredential ?? verifyCredentialAgainstInstance;
359
+ prompter.note("\nVerifying against the instance...");
360
+ let verification = await verify(config);
361
+ while (!verification.ok) {
362
+ prompter.note(`\n FAILED ${verification.detail}`);
363
+ const offersSwitch = authType === "basic" && verification.authenticationRejected === true;
364
+ if (verification.remedy) {
365
+ prompter.note(` -> ${verification.remedy}${offersSwitch ? BASIC_AUTH_SWITCH_IN_WIZARD : ""}`);
366
+ }
367
+ if (offersSwitch) {
368
+ // A 401 on Basic often means the instance no longer accepts Basic for
369
+ // REST, which retrying cannot fix. Offer the way out next to the retry.
370
+ const next = await prompter.ask("\nWhat next?", {
371
+ default: "retry",
372
+ choices: ["retry", "oauth", "stop"],
373
+ hints: {
374
+ retry: "try the same settings again",
375
+ oauth: "discard this credential and set up OAuth client credentials",
376
+ stop: "cancel; nothing is saved",
377
+ },
378
+ });
379
+ if (next === "stop")
380
+ throw new WizardAbort();
381
+ if (next === "oauth") {
382
+ prompter.note("\nSwitching to OAuth. The Basic credential is discarded.");
383
+ return undefined;
384
+ }
385
+ }
386
+ else if (!(await prompter.confirm("\nRetry with the same settings?", true))) {
387
+ throw new WizardAbort();
388
+ }
389
+ verification = await verify(config);
390
+ }
391
+ return { profile, captured, mode, provider, config, verification };
392
+ }
347
393
  /**
348
394
  * The checks that decide whether this install actually works.
349
395
  *
@@ -430,10 +476,30 @@ function instanceProblem(value) {
430
476
  "*.servicenowservices.com. For a self-hosted or custom-domain instance, set " +
431
477
  "SN_ALLOWED_INSTANCE_HOSTS before running setup.");
432
478
  }
479
+ /**
480
+ * OAuth client credentials is the default and listed first. Basic stays
481
+ * available for an instance that still accepts it, and is listed last and
482
+ * marked legacy: ServiceNow is retiring it for REST calls (KB3096078).
483
+ */
484
+ const AUTH_TYPE_CHOICES = Object.freeze(["oauth", "apikey", "basic"]);
485
+ const AUTH_TYPE_HINTS = Object.freeze({
486
+ oauth: "client credentials, recommended",
487
+ apikey: "inbound API key",
488
+ basic: "legacy, not recommended: ServiceNow is retiring it",
489
+ });
490
+ /** What OAuth client credentials need on the instance. Fixed text, no secret. */
491
+ const OAUTH_REQUIREMENTS_NOTE = "\nOAuth client credentials need these on the instance:\n" +
492
+ " - an Application Registry entry (System OAuth > Application Registry)\n" +
493
+ " with a client ID and client secret\n" +
494
+ " - the client credentials grant enabled for it\n" +
495
+ " - an OAuth Application User: the integration user this server acts as,\n" +
496
+ " with only the roles it needs, not an administrator\n" +
497
+ "Steps: README, section Authentication > OAuth 2.0.\n";
433
498
  async function askAuthType(prompter) {
434
499
  return (await prompter.ask("Authentication", {
435
- default: "basic",
436
- choices: ["basic", "oauth", "apikey"],
500
+ default: "oauth",
501
+ choices: AUTH_TYPE_CHOICES,
502
+ hints: AUTH_TYPE_HINTS,
437
503
  }));
438
504
  }
439
505
  /** Build a throwaway config for verification. Never persisted. */
@@ -493,6 +559,26 @@ async function verifyCredentialAgainstInstance(config) {
493
559
  return classifyVerificationFailure(error, config);
494
560
  }
495
561
  }
562
+ /** Wizard remedy for a 401 on a basic-auth profile. Fixed text only. */
563
+ const BASIC_AUTH_401_REMEDY = "Check the username and password. A ServiceNow account locked out or " +
564
+ "requiring a password reset also returns 401. If they are correct, this " +
565
+ "instance may be rejecting Basic Auth for REST API calls (KB3096078): " +
566
+ "current instances require the role snc_basic_auth_api_access for REST " +
567
+ "Basic auth, or a Web-Service-Access-Only account. ServiceNow is retiring " +
568
+ "Basic auth, so OAuth client credentials are recommended.";
569
+ /**
570
+ * How to act on the OAuth recommendation, which depends on where the advice is
571
+ * read. The wizard has a menu right below it; `doctor` has none, so it names
572
+ * the command instead. Kept out of the shared remedy so neither points at a
573
+ * choice that is not on the screen.
574
+ */
575
+ const BASIC_AUTH_SWITCH_IN_WIZARD = " Choose oauth below to switch this profile.";
576
+ const BASIC_AUTH_SWITCH_IN_DOCTOR = " To switch this profile, re-run servicenow-mcp-setup and choose oauth.";
577
+ /** True for a 401 on a Basic credential, the case the OAuth advice is for. */
578
+ function isBasicAuthRejection(verification, config) {
579
+ return (verification.authenticationRejected === true &&
580
+ (config.authType === undefined || config.authType === "basic"));
581
+ }
496
582
  /** Map a probe failure onto the thing the operator must actually change. */
497
583
  function classifyVerificationFailure(error, config) {
498
584
  const descriptor = (0, tool_error_js_1.trustedToolErrorDescriptor)(error);
@@ -528,10 +614,13 @@ function classifyVerificationFailure(error, config) {
528
614
  if (descriptor?.category === "authentication") {
529
615
  return Object.freeze({
530
616
  ok: false,
617
+ authenticationRejected: true,
531
618
  detail: `${host} rejected the credential (HTTP 401).`,
532
619
  remedy: config.authType === "oauth"
533
620
  ? "Check the OAuth client id and secret, and that the client is active on the instance."
534
- : "Check the username and password. A ServiceNow account locked out or requiring a password reset also returns 401.",
621
+ : config.authType === undefined || config.authType === "basic"
622
+ ? BASIC_AUTH_401_REMEDY
623
+ : "Check the username and password. A ServiceNow account locked out or requiring a password reset also returns 401.",
535
624
  });
536
625
  }
537
626
  if (descriptor?.category === "authorization") {
@@ -882,7 +971,8 @@ function renderQuestion(question, options) {
882
971
  const lines = [`${question}:`];
883
972
  options.choices.forEach((choice, index) => {
884
973
  const marker = choice === options.default ? " (default)" : "";
885
- lines.push(` ${index + 1}) ${choice}${marker}`);
974
+ const hint = options.hints?.[choice];
975
+ lines.push(` ${index + 1}) ${choice}${marker}${hint ? ` - ${hint}` : ""}`);
886
976
  });
887
977
  const defaultIndex = options.default ? options.choices.indexOf(options.default) + 1 : 0;
888
978
  lines.push(defaultIndex > 0 ? `Choice [${defaultIndex}]: ` : "Choice: ");
@@ -1379,11 +1469,14 @@ async function runLiveChecks(paths, home, dependencies, only) {
1379
1469
  }
1380
1470
  try {
1381
1471
  const result = await verify(config);
1472
+ const remedy = result.remedy !== undefined && isBasicAuthRejection(result, config)
1473
+ ? `${result.remedy}${BASIC_AUTH_SWITCH_IN_DOCTOR}`
1474
+ : result.remedy;
1382
1475
  return {
1383
1476
  name: `profile ${name}: live instance`,
1384
1477
  ok: result.ok,
1385
1478
  detail: result.detail,
1386
- ...(result.remedy === undefined ? {} : { remedy: result.remedy }),
1479
+ ...(remedy === undefined ? {} : { remedy }),
1387
1480
  };
1388
1481
  }
1389
1482
  catch {
@@ -2218,10 +2311,12 @@ function renderGrantHelp() {
2218
2311
  " --read TABLES Comma-separated tables to allow for reads",
2219
2312
  " --write TABLES Comma-separated tables to allow for writes",
2220
2313
  " --atf Grant the ATF authoring tables to sn_atf_author only",
2221
- " Does not enable execution or script-step authoring",
2314
+ " Does not enable execution or script-step authoring, or code",
2315
+ " search of ATF step scripts (see README, Code search)",
2222
2316
  " --flows Grant read on the 15 sn_flow_view tables to sn_flow_view only",
2223
2317
  " (see docs/FLOW-VIEW.md)",
2224
- ` --tools LIST Tools permitted on these targets`,
2318
+ ` --tools LIST Tools permitted on these targets; adds to the tools a table`,
2319
+ ` already has (for example --tools sn_codesearch)`,
2225
2320
  ` (default read: ${DEFAULT_READ_TOOLS.join(",")};`,
2226
2321
  ` default write adds: ${DEFAULT_WRITE_TOOLS.join(",")};`,
2227
2322
  ` default write on incident also adds:`,