Skip to content

Commit 12bd455

Browse files
authored
@W-21105798: Scaffold Custom API MCP Tool (#204)
@W-21105798: Scaffold Custom API MCP Tool Implementation
1 parent f0e3f1a commit 12bd455

11 files changed

Lines changed: 721 additions & 10 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
description: Generate a new custom SCAPI endpoint (OAS 3.0 schema, api.json, script.js) in an existing cartridge.
3+
---
4+
5+
# scapi_custom_api_scaffold
6+
7+
Generate a new custom SCAPI endpoint in an existing cartridge. Creates `schema.yaml` (OAS 3.0 contract), `api.json` (endpoint mapping), and `script.js` (implementation) under the cartridge's `rest-apis/<apiName>/` directory.
8+
9+
## Overview
10+
11+
The `scapi_custom_api_scaffold` tool scaffolds a new custom API using the B2C tooling SDK's `custom-api` scaffold. It:
12+
13+
- Creates an OpenAPI 3.0 schema, API manifest, and script stub in your project.
14+
- Uses the first cartridge found in the project if you don't specify one.
15+
- Supports **shopper** (siteId, customer-facing) or **admin** (no siteId) API types.
16+
17+
**No instance or OAuth required** — this tool works locally and only writes files into your project. To check registration status after deployment, use [`scapi_custom_apis_status`](./scapi-custom-apis-status).
18+
19+
## Parameters
20+
21+
| Parameter | Type | Required | Description |
22+
|-----------|------|----------|-------------|
23+
| `apiName` | string | Yes | API name in kebab-case (e.g. `my-products`). Must start with a lowercase letter; only letters, numbers, and hyphens. |
24+
| `cartridgeName` | string | No | Cartridge that will contain the API. Omit to use the first cartridge found under the project (working directory or `projectRoot`). |
25+
| `apiType` | `"admin"` \| `"shopper"` | No | `shopper` (siteId, customer-facing) or `admin` (no siteId). Default: `shopper`. |
26+
| `apiDescription` | string | No | Short description of the API. |
27+
| `projectRoot` | string | No | Project root for cartridge discovery. Default: MCP working directory (`--project-directory` / `SFCC_PROJECT_DIRECTORY`). |
28+
| `outputDir` | string | No | Output directory override. Default: project root (scaffold writes under cartridge path). |
29+
30+
## Usage Examples
31+
32+
### Create a custom API (default cartridge and type)
33+
34+
```
35+
Use the MCP tool to create a custom API named my-loyalty-api.
36+
```
37+
38+
### Create a shopper API with a description
39+
40+
```
41+
Use the MCP tool to scaffold a custom API named product-recommendations, type shopper, with description "Product recommendations by segment".
42+
```
43+
44+
### Create an admin API in a specific cartridge
45+
46+
```
47+
Use the MCP tool to create a custom admin API named inventory-sync in cartridge app_custom.
48+
```
49+
50+
## Output
51+
52+
### Success
53+
54+
Returns the scaffold ID, output directory, and list of created files:
55+
56+
```json
57+
{
58+
"scaffold": "custom-api",
59+
"outputDir": "/path/to/project",
60+
"dryRun": false,
61+
"files": [
62+
{ "path": ".../rest-apis/my-custom-api/schema.yaml", "action": "created" },
63+
{ "path": ".../rest-apis/my-custom-api/api.json", "action": "created" },
64+
{ "path": ".../rest-apis/my-custom-api/script.js", "action": "created" }
65+
],
66+
"postInstructions": "Custom API 'my-custom-api' has been created in cartridge 'app_storefrontnext_base'. ..."
67+
}
68+
```
69+
70+
### Errors
71+
72+
- **No cartridges found** — Project has no cartridge (e.g. no `.project` in a cartridge directory). Create a cartridge first (e.g. `b2c scaffold cartridge`).
73+
- **Scaffold not found** — SDK `custom-api` scaffold is missing; ensure `@salesforce/b2c-tooling-sdk` is installed.
74+
- **Parameter validation failed** — Invalid `apiName` (e.g. not kebab-case) or other parameter issue.
75+
76+
## Next steps after scaffolding
77+
78+
1. **Edit** `schema.yaml` to define paths, request/response schemas, and operation IDs.
79+
2. **Edit** `script.js` to implement the endpoint logic.
80+
3. **Deploy** the cartridge to your instance and **activate** the code version to register the API.
81+
4. **Verify** with [`scapi_custom_apis_status`](./scapi-custom-apis-status) that endpoints show as `active`.
82+
83+
Shopper APIs are available at:
84+
`https://{shortCode}.api.commercecloud.salesforce.com/custom/{apiName}/v1/organizations/{organizationId}/...` and require the `siteId` query parameter and ShopperToken authentication.
85+
86+
## Related Tools
87+
88+
- Part of the [SCAPI](../toolsets#scapi), [PWAV3](../toolsets#pwav3), and [STOREFRONTNEXT](../toolsets#storefrontnext) toolsets
89+
- [`scapi_custom_apis_status`](./scapi-custom-apis-status) — Check custom API endpoint registration status after deployment
90+
- [`scapi_schemas_list`](./scapi-schemas-list) — List or fetch custom API schemas (use `apiFamily: "custom"`)
91+
92+
## See Also
93+
94+
- [SCAPI Toolset](../toolsets#scapi) — Overview of SCAPI tools
95+
- [Scaffolding Guide](../../guide/scaffolding#custom-api) — CLI and SDK scaffolding (including `b2c scaffold custom-api`)
96+
- [CLI Reference](../../cli/scaffold)`b2c scaffold` commands

docs/mcp/tools/scapi-custom-apis-status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The `scapi_custom_apis_status` tool checks the registration status of custom API
1515
- Provides per-site details (one row per endpoint per site).
1616
- Supports filtering, grouping, and column selection.
1717

18-
**Important:** This tool is **remote only** - it queries your live instance. For schema definitions, use [`scapi_schemas_list`](./scapi-schemas-list) with `apiFamily: "custom"`.
18+
**Important:** This tool is **remote only** - it queries your live instance. For schema definitions, use [`scapi_schemas_list`](./scapi-schemas-list) with `apiFamily: "custom"`. To create a new custom API in your project, use [`scapi_custom_api_scaffold`](./scapi-custom-api-scaffold).
1919

2020
## Authentication
2121

docs/mcp/tools/scapi-schemas-list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Use the MCP tool to show me the full OpenAPI spec for shopper-products v1.
164164

165165
- [SCAPI Toolset](../toolsets#scapi) - Overview of SCAPI discovery tools
166166
- [scapi_custom_apis_status](./scapi-custom-apis-status) - Check custom API endpoint registration status
167+
- [scapi_custom_api_scaffold](./scapi-custom-api-scaffold) - Generate a new custom API in a cartridge
167168
- [Authentication Setup](../../guide/authentication#scapi-authentication) - Set up SCAPI authentication with required roles and scopes
168169
- [Configuration](../configuration) - Configure OAuth credentials
169170
- [CLI Reference](../../cli/scapi-schemas) - Equivalent CLI commands: `b2c scapi schemas list` and `b2c scapi schemas get`

docs/mcp/toolsets.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ PWA Kit v3 development tools for building headless storefronts.
6262
| Tool | Description | Documentation |
6363
|------|-------------|---------------|
6464
| [`scapi_schemas_list`](./tools/scapi-schemas-list) | List or fetch SCAPI schemas (standard and custom). Use apiFamily: "custom" for custom APIs. | [View details](./tools/scapi-schemas-list) |
65+
| [`scapi_custom_api_scaffold`](./tools/scapi-custom-api-scaffold) | Generate a new custom SCAPI endpoint (schema, api.json, script.js) in an existing cartridge. | [View details](./tools/scapi-custom-api-scaffold) |
6566
| [`scapi_custom_apis_status`](./tools/scapi-custom-apis-status) | Get registration status of custom API endpoints (active/not_registered). Remote only, requires OAuth. | [View details](./tools/scapi-custom-apis-status) |
6667
| [`mrt_bundle_push`](./tools/mrt-bundle-push) | Build, push bundle (optionally deploy) | [View details](./tools/mrt-bundle-push) |
6768

@@ -78,6 +79,7 @@ Salesforce Commerce API discovery and exploration.
7879
| Tool | Description | Documentation |
7980
|------|-------------|---------------|
8081
| [`scapi_schemas_list`](./tools/scapi-schemas-list) | List or fetch SCAPI schemas (standard and custom). Use apiFamily: "custom" for custom APIs. | [View details](./tools/scapi-schemas-list) |
82+
| [`scapi_custom_api_scaffold`](./tools/scapi-custom-api-scaffold) | Generate a new custom SCAPI endpoint (schema, api.json, script.js) in an existing cartridge. | [View details](./tools/scapi-custom-api-scaffold) |
8183
| [`scapi_custom_apis_status`](./tools/scapi-custom-apis-status) | Get registration status of custom API endpoints (active/not_registered). Remote only, requires OAuth. | [View details](./tools/scapi-custom-apis-status) |
8284

8385
## STOREFRONTNEXT
@@ -95,12 +97,13 @@ Storefront Next development tools for building modern storefronts.
9597
| `storefront_next_development_guidelines` | Get Storefront Next development guidelines and best practices ||
9698
| [`storefront_next_page_designer_decorator`](./tools/storefront-next-page-designer-decorator) | Add Page Designer decorators to Storefront Next components | [View details](./tools/storefront-next-page-designer-decorator) |
9799
| [`scapi_schemas_list`](./tools/scapi-schemas-list) | List or fetch SCAPI schemas (standard and custom). Use apiFamily: "custom" for custom APIs. | [View details](./tools/scapi-schemas-list) |
100+
| [`scapi_custom_api_scaffold`](./tools/scapi-custom-api-scaffold) | Generate a new custom SCAPI endpoint (schema, api.json, script.js) in an existing cartridge. | [View details](./tools/scapi-custom-api-scaffold) |
98101
| [`scapi_custom_apis_status`](./tools/scapi-custom-apis-status) | Get registration status of custom API endpoints (active/not_registered). Remote only, requires OAuth. | [View details](./tools/scapi-custom-apis-status) |
99102
| [`mrt_bundle_push`](./tools/mrt-bundle-push) | Build, push bundle (optionally deploy) | [View details](./tools/mrt-bundle-push) |
100103

101104
## Tool Deduplication
102105

103-
Some tools appear in multiple toolsets (for example, `mrt_bundle_push`, `scapi_schemas_list`, `scapi_custom_apis_status`). When using multiple toolsets, tools are automatically deduplicated, so you'll only see each tool once.
106+
Some tools appear in multiple toolsets (for example, `mrt_bundle_push`, `scapi_schemas_list`, `scapi_custom_api_scaffold`, `scapi_custom_apis_status`). When using multiple toolsets, tools are automatically deduplicated, so you'll only see each tool once.
104107

105108
## Next Steps
106109

packages/b2c-dx-mcp/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ The `storefront_next_development_guidelines` tool provides critical architecture
121121

122122
##### SCAPI Discovery
123123

124-
Use **scapi_schemas_list** for both standard SCAPI (Shop, Admin, Shopper APIs) and custom APIs. Use **scapi_custom_apis_status** for endpoint-level registration status (active/not_registered).
124+
Use **scapi_schemas_list** for both standard SCAPI (Shop, Admin, Shopper APIs) and custom APIs. Use **scapi_custom_apis_status** for endpoint-level registration status (active/not_registered). Use **scapi_customapi_scaffold** to generate a new custom API in an existing cartridge.
125125

126126
**SCAPI Schemas (tool: `scapi_schemas_list`):**
127127

@@ -138,6 +138,14 @@ Discover schema metadata and fetch OpenAPI specs for both standard and custom SC
138138
- ✅ "Use the MCP tool to list custom API definitions." → list with apiFamily: custom.
139139
- ✅ "Use the MCP tool to show me the loyalty-points custom API schema." → apiFamily: custom, apiName: loyalty-points, apiVersion: v1, includeSchemas: true.
140140

141+
**Custom API Scaffold (tool: `scapi_customapi_scaffold`):**
142+
143+
Generate a new custom SCAPI endpoint in an existing cartridge (OAS 3.0 schema.yaml, api.json, script.js with example GET endpoints). Requires **apiName** (kebab-case). Optional: **cartridgeName** (omit to use the first cartridge found under the working directory), **apiType** (shopper | admin; default shopper), **apiDescription**, **projectRoot**, **outputDir**. Set `--working-directory` (or SFCC_WORKING_DIRECTORY) so the server discovers cartridges in your project. Files are always generated (no dry run) and existing files are never overwritten.
144+
145+
- ✅ "Use the MCP tool to scaffold a new custom API named my-products."
146+
- ✅ "Use the MCP tool to create a custom admin API called customer-trips."
147+
- ✅ "Use the MCP tool to scaffold a new shopper custom API gift-registry-list in cartridge app_custom."
148+
141149
**Custom API Endpoint Status (tool: `scapi_custom_apis_status`):**
142150

143151
Get registration status of custom API endpoints deployed on the instance (remote only). Returns individual HTTP endpoints (e.g., GET /hello, POST /items/{id}) with registration status (active/not_registered), one row per endpoint per site. Requires OAuth with `sfcc.custom-apis` scope.
@@ -282,6 +290,7 @@ PWA Kit v3 development tools for building headless storefronts.
282290
|------|-------------|
283291
| `scapi_schemas_list` | List or fetch SCAPI schemas (standard and custom). Use apiFamily: "custom" for custom APIs. |
284292
| `scapi_custom_apis_status` | Get registration status of custom API endpoints (active/not_registered). Remote only, requires OAuth. |
293+
| `scapi_customapi_scaffold` | Generate a new custom SCAPI endpoint (OAS 3.0 schema, api.json, script.js) in an existing cartridge. Required: apiName. Optional: cartridgeName (defaults to first cartridge), apiType, apiDescription, projectRoot, outputDir. |
285294
| `mrt_bundle_push` | Build, push bundle (optionally deploy) |
286295

287296
#### SCAPI
@@ -292,6 +301,7 @@ Salesforce Commerce API discovery and exploration.
292301
|------|-------------|
293302
| `scapi_schemas_list` | List or fetch SCAPI schemas (standard and custom). Use apiFamily: "custom" for custom APIs. |
294303
| `scapi_custom_apis_status` | Get registration status of custom API endpoints (active/not_registered). Remote only, requires OAuth. |
304+
| `scapi_customapi_scaffold` | Generate a new custom SCAPI endpoint (OAS 3.0 schema, api.json, script.js) in an existing cartridge. Required: apiName. Optional: cartridgeName (defaults to first cartridge), apiType, apiDescription, projectRoot, outputDir. |
295305

296306
#### STOREFRONTNEXT
297307
Storefront Next development tools for building modern storefronts.
@@ -303,6 +313,7 @@ Storefront Next development tools for building modern storefronts.
303313
| `storefront_next_page_designer_decorator` | Add Page Designer decorators to Storefront Next components |
304314
| `scapi_schemas_list` | List or fetch SCAPI schemas (standard and custom). Use apiFamily: "custom" for custom APIs. |
305315
| `scapi_custom_apis_status` | Get registration status of custom API endpoints (active/not_registered). Remote only, requires OAuth. |
316+
| `scapi_customapi_scaffold` | Generate a new custom SCAPI endpoint (OAS 3.0 schema, api.json, script.js) in an existing cartridge. Required: apiName. Optional: cartridgeName (defaults to first cartridge), apiType, apiDescription, projectRoot, outputDir. |
306317
| `mrt_bundle_push` | Build, push bundle (optionally deploy) |
307318

308319
> **Note:** Some tools appear in multiple toolsets (e.g., `mrt_bundle_push`, `scapi_schemas_list`, `scapi_custom_apis_status`). When using multiple toolsets, tools are automatically deduplicated.

packages/b2c-dx-mcp/src/tools/scapi/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* SCAPI toolset for B2C Commerce.
99
*
1010
* This toolset provides MCP tools for Salesforce Commerce API (SCAPI) discovery and exploration.
11-
* Includes both standard SCAPI schemas and custom API status tools.
11+
* Includes standard SCAPI schemas, custom API status, and custom API scaffold tools.
1212
*
1313
* @module tools/scapi
1414
*/
@@ -17,6 +17,7 @@ import type {McpTool} from '../../utils/index.js';
1717
import type {Services} from '../../services.js';
1818
import {createScapiSchemasListTool} from './scapi-schemas-list.js';
1919
import {createScapiCustomApisStatusTool} from './scapi-custom-apis-status.js';
20+
import {createScaffoldCustomApiTool} from './scapi-custom-api-scaffold.js';
2021

2122
/**
2223
* Creates all tools for the SCAPI toolset.
@@ -25,5 +26,9 @@ import {createScapiCustomApisStatusTool} from './scapi-custom-apis-status.js';
2526
* @returns Array of MCP tools
2627
*/
2728
export function createScapiTools(loadServices: () => Services): McpTool[] {
28-
return [createScapiSchemasListTool(loadServices), createScapiCustomApisStatusTool(loadServices)];
29+
return [
30+
createScapiSchemasListTool(loadServices),
31+
createScapiCustomApisStatusTool(loadServices),
32+
createScaffoldCustomApiTool(loadServices),
33+
];
2934
}

0 commit comments

Comments
 (0)