@zuora/agent-toolkit 0.0.1-alpha.0 → 0.1.0-alpha.1

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.
@@ -1,7 +1,7 @@
1
- import { Z as ZuoraOpenAIAgentToolkit } from '../toolkit-CafRZuAV.js';
2
- export { c as createZuoraOpenAIAgentToolkit } from '../toolkit-CafRZuAV.js';
1
+ import { Z as ZuoraOpenAIAgentToolkit } from '../toolkit-Bjw46goJ.js';
2
+ export { c as createZuoraOpenAIAgentToolkit } from '../toolkit-Bjw46goJ.js';
3
3
  import { ChatCompletion, ChatCompletionToolMessageParam } from 'openai/resources/chat/completions';
4
- import { b as ZuoraToolExecutionResult, Z as ZuoraToolDefinition, c as ZuoraAsyncJobResult } from '../async-jobs-Dd8Jafv0.js';
4
+ import { b as ZuoraToolExecutionResult, Z as ZuoraToolDefinition, c as ZuoraAsyncJobResult } from '../async-jobs-DJ_GygII.js';
5
5
  import '../shared/toolkit-core.js';
6
6
 
7
7
  type ZuoraOpenAIToolExecutionContext = {
@@ -19,6 +19,10 @@ var ZuoraAgentToolkitError = class extends Error {
19
19
  };
20
20
 
21
21
  // src/shared/types.ts
22
+ var SafetyMode = {
23
+ ReadOnly: "readOnly",
24
+ WriteEnabled: "writeEnabled"
25
+ };
22
26
  var ApprovalPolicyPreset = {
23
27
  Off: "off",
24
28
  AllWrites: "all_writes",
@@ -114,15 +118,15 @@ var evaluatePreset = (preset, request) => {
114
118
  const isProduction = environment === "production";
115
119
  switch (preset) {
116
120
  case ApprovalPolicyPreset.Off:
117
- return "not_required";
121
+ return "allow";
118
122
  case ApprovalPolicyPreset.AllWrites:
119
- return isWrite ? "deny" : "not_required";
123
+ return isWrite ? "deny" : "allow";
120
124
  case ApprovalPolicyPreset.HighRisk:
121
- return isHighRisk ? "deny" : "not_required";
125
+ return isHighRisk ? "deny" : "allow";
122
126
  case ApprovalPolicyPreset.ProductionWrites:
123
- return isProduction && isWrite ? "deny" : "not_required";
127
+ return isProduction && isWrite ? "deny" : "allow";
124
128
  case ApprovalPolicyPreset.ProductionHighRisk:
125
- return isProduction && isHighRisk ? "deny" : "not_required";
129
+ return isProduction && isHighRisk ? "deny" : "allow";
126
130
  }
127
131
  };
128
132
  var throwApprovalRejected = (tool) => {
@@ -2590,26 +2594,11 @@ var readEnvAllowedHosts = () => {
2590
2594
  const hosts = raw.split(",").map((host) => host.trim()).filter(Boolean);
2591
2595
  return hosts.length > 0 ? hosts : void 0;
2592
2596
  };
2593
- var isQueryBundleConfigured = (configuration) => {
2594
- const bundles = configuration?.bundles;
2595
- if (!bundles) {
2596
- return false;
2597
- }
2598
- const query = bundles.query;
2599
- if (query === true) {
2600
- return true;
2601
- }
2602
- return typeof query === "object" && query !== null;
2603
- };
2604
2597
  var resolveNativeFileToolsInput = (config) => {
2605
2598
  const explicit = config.nativeFileTools;
2606
2599
  if (explicit?.enabled === false) {
2607
2600
  return { enabled: false };
2608
2601
  }
2609
- const autoEnable = isQueryBundleConfigured(config.configuration);
2610
- if (!autoEnable && explicit?.enabled !== true) {
2611
- return explicit;
2612
- }
2613
2602
  const rootDir = nonEmptyString(explicit?.rootDir) ?? nonEmptyString(process.env["ZUORA_NATIVE_FILE_ROOT"]) ?? DEFAULT_NATIVE_ARTIFACT_ROOT;
2614
2603
  const allowedHosts = explicit?.allowedHosts ?? readEnvAllowedHosts() ?? DEFAULT_NATIVE_ALLOWED_HOSTS;
2615
2604
  return {
@@ -3480,21 +3469,10 @@ var OAuthTokenProvider = class {
3480
3469
  category: "auth"
3481
3470
  });
3482
3471
  }
3483
- if (this.config.auth.type === "accessToken" && this.config.auth.accessToken) {
3484
- this.accessToken = this.config.auth.accessToken;
3485
- return this.config.auth.accessToken;
3486
- }
3487
3472
  const now = Date.now();
3488
3473
  if (this.accessToken && this.isAccessTokenFresh(now)) {
3489
3474
  return this.accessToken;
3490
3475
  }
3491
- if (this.config.auth.type !== "oauthClientCredentials") {
3492
- throw new ZuoraAgentToolkitError({
3493
- code: "UNSUPPORTED_AUTH",
3494
- message: "Unsupported auth configuration for Zuora HTTP MCP client.",
3495
- category: "auth"
3496
- });
3497
- }
3498
3476
  const token = await this.fetchOAuthToken();
3499
3477
  this.accessToken = token.access_token;
3500
3478
  this.tokenIssuedAt = now;
@@ -3513,10 +3491,8 @@ var OAuthTokenProvider = class {
3513
3491
  sensitiveValues() {
3514
3492
  const values = [this.accessToken];
3515
3493
  const auth = this.config.auth;
3516
- if (auth?.type === "oauthClientCredentials") {
3494
+ if (auth?.clientSecret) {
3517
3495
  values.push(auth.clientSecret);
3518
- } else if (auth?.type === "accessToken") {
3519
- values.push(auth.accessToken);
3520
3496
  }
3521
3497
  return values.filter(
3522
3498
  (value) => typeof value === "string" && value.length > 0
@@ -3580,11 +3556,8 @@ var OAuthTokenProvider = class {
3580
3556
  });
3581
3557
  }
3582
3558
  async resolveTokenEndpoints() {
3583
- if (this.config.tokenEndpoint) {
3584
- return [this.config.tokenEndpoint];
3585
- }
3586
3559
  const restEndpoint = `${this.restBaseUrl}/oauth/token`;
3587
- const oneIdEndpoint = await this.resolveOneIdTokenEndpoint();
3560
+ const oneIdEndpoint = isStagingZuoraBaseUrl(this.restBaseUrl) ? "https://one.stg.na.zuora.com/oauth2/token" : "https://one.zuora.com/oauth2/token";
3588
3561
  const endpoints = [restEndpoint, oneIdEndpoint];
3589
3562
  const uniqueEndpoints = [...new Set(endpoints)];
3590
3563
  if (!this.preferredTokenEndpoint) {
@@ -3597,29 +3570,8 @@ var OAuthTokenProvider = class {
3597
3570
  )
3598
3571
  ];
3599
3572
  }
3600
- async resolveOneIdTokenEndpoint() {
3601
- const fallback = isStagingZuoraBaseUrl(this.restBaseUrl) ? "https://one.stg.na.zuora.com/oauth2/token" : "https://one.zuora.com/oauth2/token";
3602
- try {
3603
- return await this.discoverTokenEndpoint() ?? fallback;
3604
- } catch {
3605
- return fallback;
3606
- }
3607
- }
3608
- async discoverTokenEndpoint() {
3609
- const metadataUrl = new URL(this.restBaseUrl);
3610
- metadataUrl.pathname = "/.well-known/oauth-authorization-server";
3611
- metadataUrl.search = "";
3612
- const response = await fetch(metadataUrl, {
3613
- headers: { accept: "application/json" }
3614
- });
3615
- if (!response.ok) {
3616
- return void 0;
3617
- }
3618
- const metadata = await response.json().catch(() => ({}));
3619
- return metadata.token_endpoint;
3620
- }
3621
3573
  async requestOAuthToken(tokenEndpoint) {
3622
- if (!this.config.auth || this.config.auth.type !== "oauthClientCredentials") {
3574
+ if (!this.config.auth) {
3623
3575
  throw new ZuoraAgentToolkitError({
3624
3576
  code: "UNSUPPORTED_AUTH",
3625
3577
  message: "Unsupported auth configuration for Zuora HTTP MCP client.",
@@ -3632,17 +3584,8 @@ var OAuthTokenProvider = class {
3632
3584
  accept: "application/json"
3633
3585
  };
3634
3586
  const auth = this.config.auth;
3635
- if (this.config.tokenAuthMethod === "client_secret_basic") {
3636
- headers.authorization = `Basic ${base64Encode(
3637
- `${auth.clientId}:${auth.clientSecret}`
3638
- )}`;
3639
- } else {
3640
- body.set("client_id", auth.clientId);
3641
- body.set("client_secret", auth.clientSecret);
3642
- }
3643
- if (auth.scope) {
3644
- body.set("scope", auth.scope);
3645
- }
3587
+ body.set("client_id", auth.clientId);
3588
+ body.set("client_secret", auth.clientSecret);
3646
3589
  const response = await fetch(tokenEndpoint, {
3647
3590
  method: "POST",
3648
3591
  headers,
@@ -3684,7 +3627,6 @@ var extractTenantId = (scope) => {
3684
3627
  const tenantMatch = scope?.match(/tenant\.(\d+)/);
3685
3628
  return tenantMatch?.[1];
3686
3629
  };
3687
- var base64Encode = (value) => Buffer.from(value).toString("base64");
3688
3630
 
3689
3631
  // src/shared/transport/request-headers.ts
3690
3632
  var RESERVED_HEADER_NAMES = /* @__PURE__ */ new Set([
@@ -3756,10 +3698,8 @@ var resolveZuoraConnectionConfig = (config, mcpUrl) => ({
3756
3698
  clientName: config.clientName ?? "zuora-agent-toolkit",
3757
3699
  clientVersion: config.clientVersion ?? "0.1.0",
3758
3700
  protocolVersion: config.protocolVersion ?? "2025-03-26",
3759
- tokenAuthMethod: config.tokenAuthMethod ?? "client_secret_post",
3760
3701
  url: mcpUrl,
3761
3702
  ...config.auth ? { auth: config.auth } : {},
3762
- ...config.tokenEndpoint ? { tokenEndpoint: config.tokenEndpoint } : {},
3763
3703
  ...config.headers ? { headers: config.headers } : {},
3764
3704
  ...config.tenantAuth ? { tenantAuth: config.tenantAuth } : {},
3765
3705
  ...config.entityIds ? { entityIds: config.entityIds } : {},
@@ -3940,7 +3880,7 @@ var resolveToolSelectionPolicy = (configuration, selection) => ({
3940
3880
  ...selection?.exclude ?? []
3941
3881
  ])
3942
3882
  });
3943
- var resolveToolMode = (configuration, selection) => selection?.mode ?? configuration?.mode ?? "readOnly";
3883
+ var resolveToolMode = (configuration, selection) => selection?.mode ?? configuration?.mode ?? SafetyMode.ReadOnly;
3944
3884
  var resolveExcludedToolNames = (configuration) => /* @__PURE__ */ new Set([
3945
3885
  ...resolveConfiguredExcludedToolNames(configuration),
3946
3886
  ...resolveBundleExcludedToolNames(configuration)
@@ -4202,11 +4142,19 @@ var ZuoraHttpMcpClient = class _ZuoraHttpMcpClient extends ZuoraClient {
4202
4142
 
4203
4143
  // src/shared/toolkit-core.ts
4204
4144
  var createHttpMcpClientConfig = (config) => {
4205
- if (isTenantAuthConnection(config.connection)) {
4145
+ const contextEntityIds = config.configuration?.context?.entityIds;
4146
+ const contextOrgIds = config.configuration?.context?.orgIds;
4147
+ const contextOverrides = {
4148
+ ...contextEntityIds ? { entityIds: contextEntityIds } : {},
4149
+ ...contextOrgIds ? { orgIds: contextOrgIds } : {}
4150
+ };
4151
+ const connection = "connection" in config ? config.connection : void 0;
4152
+ if (isTenantAuthConnection(connection)) {
4206
4153
  return {
4207
4154
  url: config.baseUrl,
4208
4155
  ...config.auth ? { auth: config.auth } : {},
4209
- ...config.connection
4156
+ ...connection,
4157
+ ...contextOverrides
4210
4158
  };
4211
4159
  }
4212
4160
  if (!config.auth) {
@@ -4219,13 +4167,23 @@ var createHttpMcpClientConfig = (config) => {
4219
4167
  return {
4220
4168
  url: config.baseUrl,
4221
4169
  auth: config.auth,
4222
- ...config.connection
4170
+ ...connection ?? {},
4171
+ ...contextOverrides
4223
4172
  };
4224
4173
  };
4225
4174
  var isTenantAuthConnection = (connection) => connection?.tenantAuth?.enabled === true;
4226
4175
  var filterCatalogTools = (tools, policy) => tools.filter((tool) => {
4227
- if (policy.mode === "readOnly" && tool.meta.access === "write") {
4228
- return false;
4176
+ if (policy.mode === SafetyMode.ReadOnly) {
4177
+ if (tool.meta.operationAccess) {
4178
+ const hasReadOperation = Object.values(tool.meta.operationAccess).some(
4179
+ (access) => access === "read"
4180
+ );
4181
+ if (!hasReadOperation) {
4182
+ return false;
4183
+ }
4184
+ } else if (tool.meta.access === "write") {
4185
+ return false;
4186
+ }
4229
4187
  }
4230
4188
  if (policy.bundles.size > 0 && !policy.bundles.has(tool.meta.bundle)) {
4231
4189
  return false;
@@ -4386,19 +4344,19 @@ var ToolkitCore = class {
4386
4344
  category: "configuration"
4387
4345
  });
4388
4346
  }
4389
- const mode = this.config.configuration?.mode ?? "readOnly";
4390
- if (mode === "readOnly" && tool.meta.access === "write") {
4391
- throw new ZuoraAgentToolkitError({
4392
- code: "TOOL_EXECUTION_ERROR",
4393
- message: `Write tool "${operationName}" is disabled in readOnly mode.`,
4394
- category: "tool_execution"
4395
- });
4396
- }
4347
+ const mode = this.config.configuration?.mode ?? SafetyMode.ReadOnly;
4397
4348
  const nativeRuntimeTool = this.nativeToolRegistry?.get(operationName);
4398
4349
  const nativeApprovalMetadata = nativeRuntimeTool?.approvalMetadata(input);
4399
4350
  const invokedOperation = tool.meta.operationSelector ? String(input[tool.meta.operationSelector] ?? "") : void 0;
4400
4351
  const effectiveAccess = (invokedOperation ? tool.meta.operationAccess?.[invokedOperation] : void 0) ?? tool.meta.access;
4401
4352
  const effectiveRiskLevel = (invokedOperation ? tool.meta.operationRiskLevel?.[invokedOperation] : void 0) ?? tool.meta.riskLevel;
4353
+ if (mode === SafetyMode.ReadOnly && effectiveAccess === "write") {
4354
+ throw new ZuoraAgentToolkitError({
4355
+ code: "TOOL_EXECUTION_ERROR",
4356
+ message: `Write operation "${invokedOperation ?? operationName}" is disabled in readOnly mode.`,
4357
+ category: "tool_execution"
4358
+ });
4359
+ }
4402
4360
  const effectiveTool = effectiveAccess === tool.meta.access && effectiveRiskLevel === tool.meta.riskLevel ? tool : { ...tool, meta: { ...tool.meta, access: effectiveAccess, riskLevel: effectiveRiskLevel } };
4403
4361
  await enforceApproval(
4404
4362
  effectiveTool,
@@ -4632,7 +4590,7 @@ var ToolkitCore = class {
4632
4590
  return this.catalogTools ?? [];
4633
4591
  }
4634
4592
  resolveSplitOperations() {
4635
- return this.config.configuration?.splitOperations ?? true;
4593
+ return this.config.configuration?.splitOperations ?? false;
4636
4594
  }
4637
4595
  warnIfNotInitialized() {
4638
4596
  if (!this._initialized) {
@@ -4701,13 +4659,11 @@ var ToolkitCore = class {
4701
4659
  }
4702
4660
  createEventContext() {
4703
4661
  const configurationContext = this.config.configuration?.context ?? {};
4704
- const tenantId = nonEmptyString(configurationContext.tenantId) ?? nonEmptyString(configurationContext.tenant_id);
4705
4662
  const entityIds = normalizeIdList(configurationContext.entityIds) ?? normalizeIdList(configurationContext.entityId);
4706
4663
  const orgIds = normalizeIdList(configurationContext.orgIds) ?? normalizeIdList(configurationContext.orgId);
4707
4664
  const environment = inferZuoraEnvironment(this.config.baseUrl) ?? "sandbox";
4708
4665
  return {
4709
4666
  environment,
4710
- ...tenantId ? { tenantId } : {},
4711
4667
  ...entityIds ? { entityIds } : {},
4712
4668
  ...orgIds ? { orgIds } : {}
4713
4669
  };
@@ -4799,51 +4755,21 @@ var readBooleanEnvValue = (name, rawValue) => {
4799
4755
  var readBooleanEnv = (env, name) => readBooleanEnvValue(name, env[name]);
4800
4756
  var readZuoraEndpoint = (env) => readTrimmedEnv(env, "ZUORA_BASE_URL") ?? readTrimmedEnv(env, "ZUORA_MCP_URL");
4801
4757
  var readToolkitMode = (env) => {
4802
- const mode = env.ZUORA_TOOLKIT_MODE?.trim() ?? "readOnly";
4803
- if (mode === "readOnly" || mode === "writeEnabled") {
4758
+ const mode = env.ZUORA_TOOLKIT_MODE?.trim() ?? SafetyMode.ReadOnly;
4759
+ if (mode === SafetyMode.ReadOnly || mode === SafetyMode.WriteEnabled) {
4804
4760
  return mode;
4805
4761
  }
4806
4762
  throw new Error(
4807
- `Invalid ZUORA_TOOLKIT_MODE: ${mode}. Expected readOnly or writeEnabled.`
4808
- );
4809
- };
4810
- var readOptionalUrlEnv = (env, name) => {
4811
- const value = readTrimmedEnv(env, name);
4812
- if (!value) {
4813
- return void 0;
4814
- }
4815
- try {
4816
- const url = new URL(value);
4817
- if (url.protocol === "http:" || url.protocol === "https:") {
4818
- return url.toString();
4819
- }
4820
- } catch {
4821
- }
4822
- throw new Error(`${name} must be an absolute HTTP(S) URL.`);
4823
- };
4824
- var readTokenAuthMethodEnv = (env) => {
4825
- const value = readTrimmedEnv(env, "ZUORA_TOKEN_AUTH_METHOD");
4826
- if (!value) {
4827
- return void 0;
4828
- }
4829
- if (value === "client_secret_post" || value === "client_secret_basic") {
4830
- return value;
4831
- }
4832
- throw new Error(
4833
- "Invalid ZUORA_TOKEN_AUTH_METHOD: expected client_secret_post or client_secret_basic."
4763
+ `Invalid ZUORA_TOOLKIT_MODE: ${mode}. Expected ${SafetyMode.ReadOnly} or ${SafetyMode.WriteEnabled}.`
4834
4764
  );
4835
4765
  };
4836
4766
  var readConnectionOptions = (env) => {
4837
- const tokenEndpoint = readOptionalUrlEnv(env, "ZUORA_TOKEN_ENDPOINT");
4838
- const tokenAuthMethod = readTokenAuthMethodEnv(env);
4839
4767
  const entityIds = readTrimmedEnv(env, "ZUORA_ENTITY_IDS");
4840
4768
  const orgIds = readTrimmedEnv(env, "ZUORA_ORG_IDS");
4841
4769
  const zuoraVersion = readTrimmedEnv(env, "ZUORA_VERSION");
4842
4770
  return {
4843
4771
  ...entityIds ? { entityIds } : {},
4844
4772
  ...orgIds ? { orgIds } : {},
4845
- ...tokenEndpoint ? { tokenEndpoint } : {},
4846
- ...tokenAuthMethod ? { tokenAuthMethod } : {},
4847
4773
  ...zuoraVersion ? { zuoraVersion } : {}
4848
4774
  };
4849
4775
  };
@@ -4868,28 +4794,19 @@ var zuoraAgentToolkitConfigFromEnv = (env = process.env) => {
4868
4794
  }
4869
4795
  },
4870
4796
  configuration: {
4871
- mode,
4872
- context: {
4873
- tenantId
4874
- }
4797
+ mode
4875
4798
  }
4876
4799
  };
4877
4800
  }
4878
4801
  return {
4879
4802
  baseUrl,
4880
4803
  auth: {
4881
- type: "oauthClientCredentials",
4882
4804
  clientId: requiredEnv(env, "ZUORA_CLIENT_ID"),
4883
4805
  clientSecret: requiredEnv(env, "ZUORA_CLIENT_SECRET")
4884
4806
  },
4885
4807
  ...Object.keys(connection).length > 0 ? { connection } : {},
4886
4808
  configuration: {
4887
- mode,
4888
- ...readTrimmedEnv(env, "ZUORA_TENANT_ID") ? {
4889
- context: {
4890
- tenantId: readTrimmedEnv(env, "ZUORA_TENANT_ID")
4891
- }
4892
- } : {}
4809
+ mode
4893
4810
  }
4894
4811
  };
4895
4812
  };