@salesforce/b2c-dx-mcp 1.5.0 → 1.6.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.
@@ -40,7 +40,7 @@ import fs from 'node:fs';
40
40
  import type { B2CInstance } from '@salesforce/b2c-tooling-sdk';
41
41
  import type { AuthStrategy } from '@salesforce/b2c-tooling-sdk/auth';
42
42
  import type { ResolvedB2CConfig } from '@salesforce/b2c-tooling-sdk/config';
43
- import { WebDavClient, type CustomApisClient, type ScapiSchemasClient } from '@salesforce/b2c-tooling-sdk/clients';
43
+ import { WebDavClient, type CustomApisClient, type MetricsClient, type ScapiSchemasClient } from '@salesforce/b2c-tooling-sdk/clients';
44
44
  /**
45
45
  * MRT (Managed Runtime) configuration.
46
46
  * Groups auth, project, environment, and origin settings.
@@ -147,6 +147,14 @@ export declare class Services {
147
147
  * Get the user's home directory.
148
148
  */
149
149
  getHomeDir(): string;
150
+ /**
151
+ * Get Metrics client for accessing SCAPI observability metrics.
152
+ * Requires shortCode, tenantId, and OAuth credentials to be configured.
153
+ *
154
+ * @throws Error if shortCode, tenantId, or OAuth credentials are missing
155
+ * @returns Typed Metrics client
156
+ */
157
+ getMetricsClient(): MetricsClient;
150
158
  /**
151
159
  * Get organization ID for SCAPI API calls.
152
160
  * Ensures the tenant ID has the required f_ecom_ prefix.
package/dist/services.js CHANGED
@@ -44,7 +44,7 @@
44
44
  import fs from 'node:fs';
45
45
  import path from 'node:path';
46
46
  import os from 'node:os';
47
- import { createCustomApisClient, createScapiSchemasClient, toOrganizationId, WebDavClient, } from '@salesforce/b2c-tooling-sdk/clients';
47
+ import { createCustomApisClient, createMetricsClient, createScapiSchemasClient, toOrganizationId, WebDavClient, } from '@salesforce/b2c-tooling-sdk/clients';
48
48
  /**
49
49
  * Services class that provides utilities for MCP tools.
50
50
  *
@@ -167,6 +167,25 @@ export class Services {
167
167
  getHomeDir() {
168
168
  return os.homedir();
169
169
  }
170
+ /**
171
+ * Get Metrics client for accessing SCAPI observability metrics.
172
+ * Requires shortCode, tenantId, and OAuth credentials to be configured.
173
+ *
174
+ * @throws Error if shortCode, tenantId, or OAuth credentials are missing
175
+ * @returns Typed Metrics client
176
+ */
177
+ getMetricsClient() {
178
+ const { shortCode, tenantId } = this.resolvedConfig.values;
179
+ if (!shortCode) {
180
+ throw new Error('SCAPI short code required. Provide --short-code, set SFCC_SHORTCODE, or configure short-code in dw.json.');
181
+ }
182
+ if (!tenantId) {
183
+ throw new Error('Tenant ID required. Provide --tenant-id, set SFCC_TENANT_ID, or configure tenant-id in dw.json.');
184
+ }
185
+ // This will throw if OAuth credentials are missing
186
+ const oauthStrategy = this.getOAuthStrategy();
187
+ return createMetricsClient({ shortCode, tenantId }, oauthStrategy);
188
+ }
170
189
  /**
171
190
  * Get organization ID for SCAPI API calls.
172
191
  * Ensures the tenant ID has the required f_ecom_ prefix.
@@ -3,9 +3,10 @@
3
3
  * SPDX-License-Identifier: Apache-2
4
4
  * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
5
5
  */
6
- import { createScapiSchemasListTool } from './scapi-schemas-list.js';
7
- import { createScapiCustomApisStatusTool } from './scapi-custom-apis-get-status.js';
6
+ import { createMetricsGetTool } from './metrics-get.js';
8
7
  import { createScaffoldCustomApiTool } from './scapi-custom-api-generate-scaffold.js';
8
+ import { createScapiCustomApisStatusTool } from './scapi-custom-apis-get-status.js';
9
+ import { createScapiSchemasListTool } from './scapi-schemas-list.js';
9
10
  /**
10
11
  * Creates all tools for the SCAPI toolset.
11
12
  *
@@ -14,9 +15,10 @@ import { createScaffoldCustomApiTool } from './scapi-custom-api-generate-scaffol
14
15
  */
15
16
  export function createScapiTools(loadServices) {
16
17
  return [
17
- createScapiSchemasListTool(loadServices),
18
- createScapiCustomApisStatusTool(loadServices),
18
+ createMetricsGetTool(loadServices),
19
19
  createScaffoldCustomApiTool(loadServices),
20
+ createScapiCustomApisStatusTool(loadServices),
21
+ createScapiSchemasListTool(loadServices),
20
22
  ];
21
23
  }
22
24
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,25 @@
1
+ import type { Services } from '../../services.js';
2
+ import type { McpTool } from '../../utils/index.js';
3
+ /**
4
+ * Creates the metrics_get tool.
5
+ *
6
+ * Retrieves observability metrics time-series for a B2C Commerce tenant by category:
7
+ * - **overall**: Aggregate site metrics (requests, response times, errors)
8
+ * - **sales**: Sales and order metrics
9
+ * - **ecdn**: Edge CDN performance metrics
10
+ * - **third-party**: External service integration metrics (filter by thirdPartyServiceId)
11
+ * - **scapi**: SCAPI endpoint metrics (filter by apiFamily, apiName)
12
+ * - **scapi-hooks**: SCAPI hooks execution metrics
13
+ * - **mrt**: Managed Runtime (PWA Kit) metrics
14
+ * - **controller**: Controller execution metrics
15
+ * - **ocapi**: OCAPI endpoint metrics (filter by ocapiCategory, ocapiApi)
16
+ *
17
+ * Returns time-series data with metric metadata (title, description, unit) and data points
18
+ * (timestamp, value) grouped by series (e.g., 2xx, 4xx, 5xx response codes).
19
+ *
20
+ * **Requirements:** OAuth with `sfcc.metrics` scope.
21
+ *
22
+ * @param loadServices - Function that loads configuration and returns Services instance
23
+ * @returns MCP tool for retrieving metrics
24
+ */
25
+ export declare function createMetricsGetTool(loadServices: () => Promise<Services> | Services): McpTool;
@@ -0,0 +1,144 @@
1
+ /*
2
+ * Copyright (c) 2025, Salesforce, Inc.
3
+ * SPDX-License-Identifier: Apache-2
4
+ * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
5
+ */
6
+ /**
7
+ * Metrics Get tool.
8
+ *
9
+ * Retrieves observability metrics time-series data for B2C Commerce tenants via SCAPI
10
+ * observability/metrics/v1. Returns metrics data grouped by category (overall, sales, ecdn,
11
+ * third-party, scapi, scapi-hooks, mrt, controller, ocapi).
12
+ *
13
+ * @module tools/scapi/metrics-get
14
+ */
15
+ import { z } from 'zod';
16
+ import { createToolAdapter, jsonResult } from '../adapter.js';
17
+ import { getMetricsByCategory, resolveMetricsWindow, enrichMetricsTags, } from '@salesforce/b2c-tooling-sdk';
18
+ /**
19
+ * Creates the metrics_get tool.
20
+ *
21
+ * Retrieves observability metrics time-series for a B2C Commerce tenant by category:
22
+ * - **overall**: Aggregate site metrics (requests, response times, errors)
23
+ * - **sales**: Sales and order metrics
24
+ * - **ecdn**: Edge CDN performance metrics
25
+ * - **third-party**: External service integration metrics (filter by thirdPartyServiceId)
26
+ * - **scapi**: SCAPI endpoint metrics (filter by apiFamily, apiName)
27
+ * - **scapi-hooks**: SCAPI hooks execution metrics
28
+ * - **mrt**: Managed Runtime (PWA Kit) metrics
29
+ * - **controller**: Controller execution metrics
30
+ * - **ocapi**: OCAPI endpoint metrics (filter by ocapiCategory, ocapiApi)
31
+ *
32
+ * Returns time-series data with metric metadata (title, description, unit) and data points
33
+ * (timestamp, value) grouped by series (e.g., 2xx, 4xx, 5xx response codes).
34
+ *
35
+ * **Requirements:** OAuth with `sfcc.metrics` scope.
36
+ *
37
+ * @param loadServices - Function that loads configuration and returns Services instance
38
+ * @returns MCP tool for retrieving metrics
39
+ */
40
+ export function createMetricsGetTool(loadServices) {
41
+ return createToolAdapter({
42
+ name: 'metrics_get',
43
+ description: `CLOSED BETA: the Metrics API must be enabled for your organization, and its behavior, output, and OAuth scopes may change without notice.
44
+
45
+ Retrieve observability metrics time-series for a B2C Commerce tenant. Returns metrics data grouped by category with time-series data points.
46
+
47
+ **Categories:**
48
+ - overall: Aggregate site metrics (requests, response times, errors)
49
+ - sales: Sales and order metrics
50
+ - ecdn: Edge CDN performance metrics
51
+ - third-party: External service metrics (use thirdPartyServiceId filter)
52
+ - scapi: SCAPI endpoint metrics (use apiFamily/apiName filters)
53
+ - scapi-hooks: SCAPI hooks execution metrics
54
+ - mrt: Managed Runtime (PWA Kit) metrics
55
+ - controller: Controller execution metrics
56
+ - ocapi: OCAPI endpoint metrics (use ocapiCategory/ocapiApi filters)
57
+
58
+ **Time window:** Provide "from" and/or "to" as a relative duration ("1h", "7d" — interpreted as ago) or an ISO 8601 timestamp, and/or "window" as a duration ("1h", "30m"). The tool always sends an explicit from+to range, defaulting to a 24-hour window: from + window → to = from + window; to + window → from = to - window; window alone → the last <window>; from alone → 24h forward from it (capped at now); to alone → 24h back from it; nothing → the last 24h. Do not supply from, to, and window together. The API caps a window at 24h and retains 30 days; an explicit range wider than 24h is sent as-is and the API returns a clear error.
59
+
60
+ **Response:** { query, data } — "query" echoes the resolved from/to (ISO + epoch seconds), filters, and defaultedWindow/clampedFrom flags; "data[]" contains metricId, title, description, unit, and dataSeries[] with time-series points (timestamp in epoch milliseconds, value). Each series also carries a structured "tags" object (realm, environment, any applied filters, and per-series dimensions like apiFamily/host/cacheStatus) parsed client-side from the packed series id — use these to group/filter rather than parsing the series id string.
61
+
62
+ **Requirements:** OAuth with sfcc.metrics scope.`,
63
+ toolsets: ['SCAPI'],
64
+ isGA: false,
65
+ requiresInstance: false, // SCAPI uses OAuth directly
66
+ inputSchema: {
67
+ category: z
68
+ .enum(['overall', 'sales', 'ecdn', 'third-party', 'scapi', 'scapi-hooks', 'mrt', 'controller', 'ocapi'])
69
+ .describe('Metrics category: overall (aggregate), sales, ecdn (CDN), third-party (external), scapi (SCAPI APIs), scapi-hooks, mrt (PWA Kit), controller, ocapi'),
70
+ from: z
71
+ .string()
72
+ .optional()
73
+ .describe('Start bound: relative ("1h", "7d" ago) or ISO 8601. Alone → a 24h window forward (capped at now).'),
74
+ to: z
75
+ .string()
76
+ .optional()
77
+ .describe('End bound: relative ("6h" ago) or ISO 8601. Alone → a 24h window back from it.'),
78
+ window: z
79
+ .string()
80
+ .optional()
81
+ .describe('Window duration ("1h", "30m", "2d"). With from → to=from+window; with to → from=to-window; alone → the last <window>. Defaults to 24h.'),
82
+ thirdPartyServiceId: z
83
+ .string()
84
+ .optional()
85
+ .describe('Filter by third-party service ID (third-party category only)'),
86
+ apiFamily: z.string().optional().describe('Filter by SCAPI API family (scapi category only)'),
87
+ apiName: z.string().optional().describe('Filter by SCAPI API name (scapi category only)'),
88
+ ocapiCategory: z.string().optional().describe('Filter by OCAPI category (ocapi category only)'),
89
+ ocapiApi: z.string().optional().describe('Filter by OCAPI API (ocapi category only)'),
90
+ },
91
+ async execute(args, { services: svc }) {
92
+ // Get client and tenant ID
93
+ const client = svc.getMetricsClient();
94
+ const tenantId = svc.getTenantId();
95
+ if (!tenantId) {
96
+ throw new Error('Tenant ID required. Provide --tenant-id, set SFCC_TENANT_ID, or configure tenant-id in dw.json.');
97
+ }
98
+ // Resolve the requested bounds into an explicit from+to range, filling any
99
+ // open bound from the 24-hour default window (the API caps a window at 24h
100
+ // and pairs a missing `to` with its own `now`). Throws a clear error on
101
+ // unparseable/over-specified input before the request.
102
+ const window = resolveMetricsWindow({ from: args.from, to: args.to, window: args.window });
103
+ const raw = await getMetricsByCategory(client, tenantId, args.category, {
104
+ from: window.from,
105
+ to: window.to,
106
+ thirdPartyServiceId: args.thirdPartyServiceId,
107
+ apiFamily: args.apiFamily,
108
+ apiName: args.apiName,
109
+ ocapiCategory: args.ocapiCategory,
110
+ ocapiApi: args.ocapiApi,
111
+ });
112
+ // Enrich each series with a structured `tags` object (realm/environment
113
+ // from the request, applied filters, and per-series dimensions parsed
114
+ // from the packed id). Additive and always-on for this machine consumer.
115
+ const response = enrichMetricsTags(raw, args.category, {
116
+ tenantId,
117
+ apiFamily: args.apiFamily,
118
+ apiName: args.apiName,
119
+ ocapiCategory: args.ocapiCategory,
120
+ ocapiApi: args.ocapiApi,
121
+ thirdPartyServiceId: args.thirdPartyServiceId,
122
+ });
123
+ return {
124
+ ...response,
125
+ query: {
126
+ category: args.category,
127
+ from: window.fromIso,
128
+ to: window.toIso,
129
+ fromEpochSeconds: window.fromEpochSeconds,
130
+ toEpochSeconds: window.toEpochSeconds,
131
+ clampedFrom: window.clampedFrom || undefined,
132
+ defaultedWindow: window.defaultedWindow || undefined,
133
+ thirdPartyServiceId: args.thirdPartyServiceId,
134
+ apiFamily: args.apiFamily,
135
+ apiName: args.apiName,
136
+ ocapiCategory: args.ocapiCategory,
137
+ ocapiApi: args.ocapiApi,
138
+ },
139
+ };
140
+ },
141
+ formatOutput: (output) => jsonResult(output),
142
+ }, loadServices);
143
+ }
144
+ //# sourceMappingURL=metrics-get.js.map
@@ -428,5 +428,5 @@
428
428
  "enableJsonFlag": false
429
429
  }
430
430
  },
431
- "version": "1.5.0"
431
+ "version": "1.6.0"
432
432
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@salesforce/b2c-dx-mcp",
3
3
  "description": "MCP server for B2C Commerce developer experience tools",
4
- "version": "1.5.0",
4
+ "version": "1.6.0",
5
5
  "author": "Salesforce",
6
6
  "license": "Apache-2.0",
7
7
  "repository": "SalesforceCommerceCloud/b2c-developer-tooling",
@@ -80,7 +80,7 @@
80
80
  "yaml": "2.9.0",
81
81
  "postcss": "8.5.15",
82
82
  "zod": "3.25.76",
83
- "@salesforce/b2c-tooling-sdk": "1.18.0"
83
+ "@salesforce/b2c-tooling-sdk": "1.19.0"
84
84
  },
85
85
  "devDependencies": {
86
86
  "@eslint/compat": "^1",