-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathscapi-schemas-list.ts
More file actions
364 lines (332 loc) · 14.1 KB
/
scapi-schemas-list.ts
File metadata and controls
364 lines (332 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
/**
* SCAPI Schemas List tool.
*
* Lists available SCAPI schemas with optional filtering by apiFamily, apiName, apiVersion, and status.
* Optionally fetches full OpenAPI schemas when includeSchemas=true is provided along with all three identifiers.
* Matches the CLI command: b2c scapi schemas list
*
* @module tools/scapi/scapi-schemas-list
*/
import {z} from 'zod';
import {createToolAdapter, jsonResult} from '../adapter.js';
import type {Services} from '../../services.js';
import type {McpTool} from '../../utils/index.js';
import type {SchemaListItem} from '@salesforce/b2c-tooling-sdk/clients';
import {getApiErrorMessage} from '@salesforce/b2c-tooling-sdk/clients';
import {collapseOpenApiSchema, type OpenApiSchemaInput} from '@salesforce/b2c-tooling-sdk/schemas';
/**
* Builds the base URL for a SCAPI API endpoint.
*
* Constructs the base URL for making SCAPI API calls based on the instance short code,
* API family, API name, and version.
*
* @param shortCode - SCAPI instance short code (e.g., "kv7kzm78")
* @param apiFamily - API family (e.g., "shopper", "checkout", "product")
* @param apiName - API name (e.g., "products", "baskets", "orders")
* @param apiVersion - API version (e.g., "v1", "v2")
* @returns Full base URL for the SCAPI API
*
* @example
* ```typescript
* const url = buildScapiApiUrl("kv7kzm78", "shopper", "products", "v1");
* // Returns: "https://kv7kzm78.api.commercecloud.salesforce.com/shopper/products/v1"
* ```
*/
function buildScapiApiUrl(shortCode: string, apiFamily: string, apiName: string, apiVersion: string): string {
return `https://${shortCode}.api.commercecloud.salesforce.com/${apiFamily}/${apiName}/${apiVersion}`;
}
/**
* Input parameters for scapi_schemas_list tool.
*/
interface SchemasListInput {
/** Filter by API family (e.g., "shopper", "product", "checkout") */
apiFamily?: string;
/** Filter by API name (e.g., "shopper-products", "shopper-baskets") */
apiName?: string;
/** Filter by API version (e.g., "v1", "v2") */
apiVersion?: string;
/** Filter by schema status ("current" or "deprecated"). Use "current" to see only active schemas, or "deprecated" to find schemas being phased out. Only works in list mode (discovery). Omit to return all schemas. */
status?: 'current' | 'deprecated';
/** Include full OpenAPI schemas (slower, requires all three: apiFamily, apiName, apiVersion) */
includeSchemas?: boolean;
/** If true, return full schema without collapsing (only works when includeSchemas=true) */
expandAll?: boolean;
}
/**
* Schema metadata without the authenticated 'link' field (with optional baseUrl).
*/
type SchemaMetadata = Omit<SchemaListItem, 'link'> & {
/** Base URL for calling the actual SCAPI API (not the schema endpoint) */
baseUrl?: string;
};
/**
* Output for discovery mode (listing schemas with metadata).
*/
interface SchemasListOutput {
/** Array of schema metadata objects (without 'link' field, with optional baseUrl) */
schemas: SchemaMetadata[];
/** Total number of schemas found */
total: number;
/** Timestamp of the query */
timestamp: string;
/** Unique API families found (for discovery) */
availableApiFamilies?: string[];
/** Unique API names found (for discovery) */
availableApiNames?: string[];
/** Unique API versions found (for discovery) */
availableVersions?: string[];
/** Helpful message when no schemas found or to explain results */
message?: string;
}
/**
* Output for fetch mode (getting a specific schema).
*/
interface SchemaGetOutput {
/** API family */
apiFamily: string;
/** API name */
apiName: string;
/** API version */
apiVersion: string;
/** Full OpenAPI schema (collapsed by default for context efficiency) */
schema: Record<string, unknown>;
/** Timestamp of the query */
timestamp: string;
/** Whether this is a collapsed schema */
collapsed: boolean;
/** Warning message if invalid parameter combinations were provided */
warning?: string;
/** Base URL for calling the actual SCAPI API */
baseUrl?: string;
}
/**
* Fetches a specific schema from the SCAPI Schemas API.
*
* @param params - Fetch parameters
* @param params.client - SCAPI Schemas client
* @param params.organizationId - Organization ID
* @param params.args - Input arguments with API identifiers
* @param params.shortCode - Optional short code for building base URL
* @returns Schema fetch output
*/
async function fetchSpecificSchema(params: {
client: ReturnType<Services['getScapiSchemasClient']>;
organizationId: string;
args: SchemasListInput;
shortCode?: string;
}): Promise<SchemaGetOutput> {
const {client, organizationId, args, shortCode} = params;
const {apiFamily, apiName, apiVersion, expandAll, status} = args;
// Warn if status filter was provided (it's ignored in fetch mode)
const warning = status
? `Note: 'status' filter is ignored when fetching a specific schema. The API endpoint for retrieving a specific schema (${apiFamily}/${apiName}/${apiVersion}) does not support status filtering - you're already specifying the exact version. Use discovery mode (omit one or more of apiFamily/apiName/apiVersion) to filter by status.`
: undefined;
const {data, error, response} = await client.GET(
'/organizations/{organizationId}/schemas/{apiFamily}/{apiName}/{apiVersion}',
{
params: {
path: {organizationId, apiFamily: apiFamily!, apiName: apiName!, apiVersion: apiVersion!},
},
},
);
if (error) {
throw new Error(
`Failed to fetch schema for ${apiFamily}/${apiName}/${apiVersion}: ${getApiErrorMessage(error, response)}`,
);
}
// Apply collapsing unless expandAll is requested
const collapsed = !expandAll;
const processedSchema: Record<string, unknown> = collapsed
? (collapseOpenApiSchema(data as OpenApiSchemaInput) as Record<string, unknown>)
: (data as Record<string, unknown>);
// Build base URL for the SCAPI API (where to call the API)
const baseUrl =
shortCode && apiFamily && apiName && apiVersion
? buildScapiApiUrl(shortCode, apiFamily, apiName, apiVersion)
: undefined;
return {
apiFamily: apiFamily!,
apiName: apiName!,
apiVersion: apiVersion!,
schema: processedSchema,
timestamp: new Date().toISOString(),
collapsed,
warning,
baseUrl,
};
}
/**
* Fetches and filters schemas list from the SCAPI Schemas API.
*
* @param params - Fetch parameters
* @param params.client - SCAPI Schemas client
* @param params.organizationId - Organization ID
* @param params.args - Input parameters
* @param params.shortCode - Optional short code for building base URLs
* @returns Discovery mode output
*/
async function fetchSchemasList(params: {
client: ReturnType<Services['getScapiSchemasClient']>;
organizationId: string;
args: SchemasListInput;
shortCode?: string;
}): Promise<SchemasListOutput> {
const {client, organizationId, args, shortCode} = params;
const {data, error, response} = await client.GET('/organizations/{organizationId}/schemas', {
params: {
path: {organizationId},
query: {
apiFamily: args.apiFamily,
apiName: args.apiName,
apiVersion: args.apiVersion,
status: args.status,
},
},
});
if (error) {
throw new Error(`Failed to fetch SCAPI schemas: ${getApiErrorMessage(error, response)}`);
}
const schemas = data?.data ?? [];
const filteredSchemas = prepareSchemaListForConsumer(schemas, shortCode);
const discoveryMetadata = getAvailableFilters(schemas);
// Generate helpful message for empty results
const message = schemas.length === 0 ? generateEmptyResultMessage(args) : undefined;
return {
schemas: filteredSchemas,
total: data?.total ?? schemas.length,
timestamp: new Date().toISOString(),
...discoveryMetadata,
message,
};
}
/**
* Generates helpful message when no schemas are found.
*/
function generateEmptyResultMessage(args: SchemasListInput): string {
const hasFilters = args.apiFamily || args.apiName || args.apiVersion || args.status;
if (hasFilters) {
const activeFilters: string[] = [];
if (args.apiFamily) activeFilters.push(`apiFamily="${args.apiFamily}"`);
if (args.apiName) activeFilters.push(`apiName="${args.apiName}"`);
if (args.apiVersion) activeFilters.push(`apiVersion="${args.apiVersion}"`);
if (args.status) activeFilters.push(`status="${args.status}"`);
return `No SCAPI schemas match the filters: ${activeFilters.join(', ')}. Try removing some filters or check the filter values. Use discovery mode without filters to see all available schemas.`;
}
return 'No SCAPI schemas available. This could indicate: (1) Invalid tenant ID or organization ID, (2) Missing OAuth scopes (requires sfcc.scapi-schemas), or (3) No schemas published for this organization. Verify your credentials and tenant configuration.';
}
/**
* Prepares schema list for consumer: strips link, adds baseUrl when shortCode provided.
*/
function prepareSchemaListForConsumer(schemas: SchemaListItem[], shortCode?: string): SchemaMetadata[] {
return schemas.map((item) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const {link, ...rest} = item;
const baseUrl =
shortCode && item.apiFamily && item.apiName && item.apiVersion
? buildScapiApiUrl(shortCode, item.apiFamily, item.apiName, item.apiVersion)
: undefined;
return {...rest, baseUrl};
});
}
/**
* Extracts unique filter values (apiFamily, apiName, apiVersion) from a schema list.
*/
function getAvailableFilters(schemas: SchemaListItem[]): {
availableApiFamilies?: string[];
availableApiNames?: string[];
availableVersions?: string[];
} {
if (schemas.length === 0) {
return {};
}
const availableApiFamilies = [
...new Set(schemas.map((s) => s.apiFamily).filter((v) => v !== undefined) as string[]),
].sort();
const availableApiNames = [
...new Set(schemas.map((s) => s.apiName).filter((v) => v !== undefined) as string[]),
].sort();
const availableVersions = [
...new Set(schemas.map((s) => s.apiVersion).filter((v) => v !== undefined) as string[]),
].sort();
return {
availableApiFamilies: availableApiFamilies.length > 0 ? availableApiFamilies : undefined,
availableApiNames: availableApiNames.length > 0 ? availableApiNames : undefined,
availableVersions: availableVersions.length > 0 ? availableVersions : undefined,
};
}
/**
* Creates the scapi_schemas_list tool.
*
* Mirrors CLI: b2c scapi schemas list (discovery) and b2c scapi schemas get (fetch).
* Lists or fetches SCAPI schema specifications; includes standard SCAPI and custom API as schema types.
*
* @param loadServices - Function that loads configuration and returns Services instance
* @returns MCP tool for listing/fetching SCAPI schemas
*/
export function createScapiSchemasListTool(loadServices: () => Promise<Services> | Services): McpTool {
return createToolAdapter<SchemasListInput, SchemaGetOutput | SchemasListOutput>(
{
name: 'scapi_schemas_list',
description: `List or fetch SCAPI schema metadata and OpenAPI specs for standard SCAPI (Shop/Admin/Shopper) and custom APIs (apiFamily: "custom"). For endpoint registration status, use scapi_custom_apis_get_status.
**Modes:**
- **List (discovery):** Omit includeSchemas or any identifier. Returns metadata: schemas[], total, availableApiFamilies/Names/Versions.
- **Fetch:** Set includeSchemas=true + all three: apiFamily, apiName, apiVersion. Returns full OpenAPI schema (collapsed by default; set expandAll=true for full).
**Rules:** includeSchemas requires all three identifiers. status only works in list mode (use "current" for active schemas, "deprecated" for phased-out schemas). Custom APIs use apiFamily: "custom".
**Requirements:** OAuth with sfcc.scapi-schemas scope.`,
toolsets: ['PWAV3', 'SCAPI', 'STOREFRONTNEXT'],
isGA: true,
requiresInstance: false, // SCAPI uses OAuth directly, doesn't need B2CInstance (hostname)
inputSchema: {
apiFamily: z.string().optional().describe('API family (e.g., "checkout", "product", "custom").'),
apiName: z.string().optional().describe('API name (e.g., "shopper-baskets", "shopper-products").'),
apiVersion: z.string().optional().describe('API version (e.g., "v1", "v2").'),
status: z
.enum(['current', 'deprecated'])
.optional()
.describe('Filter by status (list mode only). Omit to return all schemas.'),
includeSchemas: z
.boolean()
.default(false)
.describe('Fetch full OpenAPI schema. Requires apiFamily+apiName+apiVersion. Default: false.'),
expandAll: z
.boolean()
.default(false)
.describe('Return full uncompressed schema. Only when includeSchemas=true. Default: false.'),
},
async execute(args, {services: svc}) {
// Get client and organization ID
const client = svc.getScapiSchemasClient();
const organizationId = svc.getOrganizationId();
// Get shortCode for building base URLs (optional)
let shortCode: string | undefined;
try {
shortCode = svc.getShortCode();
} catch {
// Continue without shortCode if not available
}
// Determine operation mode
const hasAllIdentifiers = Boolean(args.apiFamily && args.apiName && args.apiVersion);
const isFetchMode = hasAllIdentifiers && args.includeSchemas;
// Validate includeSchemas flag
if (args.includeSchemas && !hasAllIdentifiers) {
throw new Error(
'includeSchemas=true requires all three identifiers: apiFamily, apiName, and apiVersion. ' +
'Please provide all three to fetch a specific schema, or omit includeSchemas to discover available schemas.',
);
}
// Execute appropriate mode
if (isFetchMode) {
return fetchSpecificSchema({client, organizationId, args, shortCode});
}
return fetchSchemasList({client, organizationId, args, shortCode});
},
formatOutput: (output) => jsonResult(output),
},
loadServices,
);
}