Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"commerce",
"checkout",
"product-feed",
"mcp",
"model-context-protocol",
"ai",
"chatgpt",
"openai",
Expand Down Expand Up @@ -49,6 +51,10 @@
"types": "./dist/feed/next/index.d.ts",
"import": "./dist/feed/next/index.js"
},
"./mcp": {
"types": "./dist/mcp/index.d.ts",
"import": "./dist/mcp/index.js"
},
"./test": {
"types": "./dist/test/index.d.ts",
"import": "./dist/test/index.js"
Expand Down
11 changes: 9 additions & 2 deletions packages/sdk/src/feed/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ export const ProductFeedItemSchema = z.object({
// Availability & Inventory
availability_date: z.string().datetime().optional(),
expiration_date: z.string().datetime().optional(),
pickup_method: z.enum(["buy_online_pickup_in_store", "curbside", "in_store"]).optional(),
pickup_method: z
.enum(["buy_online_pickup_in_store", "curbside", "in_store"])
.optional(),
pickup_sla: z.string().optional(), // e.g., "1 hour", "same day"

// Variants & Item Groups
Expand Down Expand Up @@ -232,7 +234,12 @@ export const ProductFeedItemSchema = z.object({
// Related Products
related_product_ids: z.array(z.string()).optional(),
relationship_type: z
.enum(["often_bought_with", "similar_to", "accessories_for", "alternative_to"])
.enum([
"often_bought_with",
"similar_to",
"accessories_for",
"alternative_to",
])
.optional(),

// Reviews & Q&A
Expand Down
152 changes: 152 additions & 0 deletions packages/sdk/src/mcp/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/**
* Configuration for creating MCP handlers
*/
export interface HandlerConfig {
/**
* Base URL of your store's API (e.g., 'https://mystore.com')
*/
baseUrl: string;

/**
* Optional headers to include in all requests (e.g., auth tokens)
*/
headers?: Record<string, string>;

/**
* Optional fetch implementation (useful for testing or custom logic)
*/
fetch?: typeof fetch;
}

/**
* Create handlers that call your acp-handler API endpoints
*
* @example
* ```typescript
* const handlers = createHandlers({
* baseUrl: 'https://mystore.com',
* headers: { 'Authorization': 'Bearer token' }
* });
*
* server.registerTool(
* 'search_products',
* tools.searchProducts,
* handlers.searchProducts
* );
* ```
*/
export function createHandlers(config: HandlerConfig) {
const { baseUrl, headers = {}, fetch: customFetch = fetch } = config;

const request = async (
path: string,
options: RequestInit = {},
): Promise<unknown> => {
const url = `${baseUrl}${path}`;
const response = await customFetch(url, {
...options,
headers: {
"Content-Type": "application/json",
...headers,
...options.headers,
},
});

if (!response.ok) {
const error = await response.json().catch(() => ({
message: response.statusText,
}));
throw new Error(
`API request failed: ${error.message || response.statusText}`,
);
}

return response.json();
};

return {
/**
* Search for products in the catalog
*/
searchProducts: async (input: {
query: string;
category?: string;
limit?: number;
}): Promise<unknown> => {
const params = new URLSearchParams({
q: input.query,
});

if (input.category) {
params.set("category", input.category);
}

if (input.limit) {
params.set("limit", String(input.limit));
}

return request(`/api/products/search?${params}`);
},

/**
* Get detailed information about a specific product
*/
getProduct: async (input: { product_id: string }): Promise<unknown> => {
return request(`/api/products/${input.product_id}`);
},

/**
* Create a new checkout session
*/
createCheckout: async (input: any): Promise<unknown> => {
return request("/api/checkout", {
method: "POST",
body: JSON.stringify(input),
});
},

/**
* Update an existing checkout session
*/
updateCheckout: async (input: any): Promise<unknown> => {
const { session_id, ...body } = input;
return request(`/api/checkout/${session_id}`, {
method: "PATCH",
Copy link

@vercel vercel bot Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updateCheckout handler uses HTTP method PATCH, but the ACP API expects POST for checkout updates. This will cause update operations to fail.

View Details
📝 Patch Details
diff --git a/packages/sdk/src/mcp/handlers.ts b/packages/sdk/src/mcp/handlers.ts
index f2b8b76..3e369e2 100644
--- a/packages/sdk/src/mcp/handlers.ts
+++ b/packages/sdk/src/mcp/handlers.ts
@@ -111,7 +111,7 @@ export function createHandlers(config: HandlerConfig) {
 		updateCheckout: async (input: any): Promise<unknown> => {
 			const { session_id, ...body } = input;
 			return request(`/api/checkout/${session_id}`, {
-				method: "PATCH",
+				method: "POST",
 				body: JSON.stringify(body),
 			});
 		},
diff --git a/packages/sdk/src/mcp/tools.ts b/packages/sdk/src/mcp/tools.ts
index 7b1086f..8abded1 100644
--- a/packages/sdk/src/mcp/tools.ts
+++ b/packages/sdk/src/mcp/tools.ts
@@ -97,7 +97,7 @@ export const createCheckout: MCPToolDefinition = {
 /**
  * Update an existing checkout session
  *
- * Maps to: PATCH /api/checkout/:id
+ * Maps to: POST /api/checkout/:id
  */
 export const updateCheckout: MCPToolDefinition = {
 	description:

Analysis

HTTP method mismatch in updateCheckout handler causes 405 Method Not Allowed errors

What fails: The updateCheckout handler in packages/sdk/src/mcp/handlers.ts line 114 uses method: "PATCH" to call /api/checkout/:id, but the API route only exports { GET, POST } methods.

How to reproduce:

  1. Call the MCP updateCheckout handler with a session ID
  2. Handler sends PATCH request to /api/checkout/{session_id}
  3. Next.js route handler (created by createNextCatchAll) only exports GET and POST

Result: Next.js returns 405 Method Not Allowed per Next.js route handler documentation, causing all checkout update operations to fail.

Expected: Should use POST method, matching the API implementation in packages/sdk/src/next/index.ts (lines 66-72) which routes POST /:id requests to the update handler, and consistent with the example implementation in examples/chat-sdk/lib/ai/tools/update-checkout.ts line 75 which explicitly uses POST.

Fixed:

  • Changed method: "PATCH" to method: "POST" in packages/sdk/src/mcp/handlers.ts line 114
  • Updated comment from PATCH /api/checkout/:id to POST /api/checkout/:id in packages/sdk/src/mcp/tools.ts line 100

body: JSON.stringify(body),
});
},

/**
* Complete a checkout session and process payment
*/
completeCheckout: async (input: any): Promise<unknown> => {
const { session_id, ...body } = input;
return request(`/api/checkout/${session_id}/complete`, {
method: "POST",
body: JSON.stringify(body),
});
},

/**
* Cancel a checkout session
*/
cancelCheckout: async (input: { session_id: string }): Promise<unknown> => {
const { session_id } = input;
return request(`/api/checkout/${session_id}/cancel`, {
method: "POST",
});
},

/**
* Get the current state of a checkout session
*/
getCheckout: async (input: { session_id: string }): Promise<unknown> => {
return request(`/api/checkout/${input.session_id}`);
},
};
}

/**
* Type for the handlers object returned by createHandlers
*/
export type Handlers = ReturnType<typeof createHandlers>;
67 changes: 67 additions & 0 deletions packages/sdk/src/mcp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* MCP (Model Context Protocol) tools for acp-handler
*
* This module provides tool definitions and handlers to expose your acp-handler
* checkout API as MCP tools for ChatGPT Apps. This allows merchants to sell
* products through ChatGPT without waiting for ACP approval.
*
* @example Basic usage
* ```typescript
* import { McpServer } from 'mcp-handler';
* import { tools, createHandlers } from 'acp-handler/mcp';
*
* const server = new McpServer({ name: 'my-store' });
* const handlers = createHandlers({ baseUrl: 'https://mystore.com' });
*
* // Register all tools
* server.registerTool('search_products', tools.searchProducts, handlers.searchProducts);
* server.registerTool('create_checkout', tools.createCheckout, handlers.createCheckout);
* server.registerTool('complete_checkout', tools.completeCheckout, handlers.completeCheckout);
*
* server.start();
* ```
*
* @example With customization
* ```typescript
* import { McpServer } from 'mcp-handler';
* import { tools, createHandlers } from 'acp-handler/mcp';
*
* const server = new McpServer({ name: 'my-store' });
* const handlers = createHandlers({
* baseUrl: 'https://mystore.com',
* headers: { 'Authorization': 'Bearer secret' }
* });
*
* // Customize tool definitions
* server.registerTool(
* 'search_products',
* {
* ...tools.searchProducts,
* description: 'Search our awesome product catalog!',
* _meta: {
* 'openai/outputTemplate': 'ui://widget/custom-products.html'
* }
* },
* handlers.searchProducts
* );
*
* // Or use custom handler logic
* server.registerTool(
* 'create_checkout',
* tools.createCheckout,
* async (input) => {
* // Custom logic before calling API
* console.log('Creating checkout:', input);
* const result = await handlers.createCheckout(input);
* // Custom logic after
* return result;
* }
* );
* ```
*
* @module mcp
*/

export { createHandlers, type HandlerConfig, type Handlers } from "./handlers";
export type { MCPToolDefinition } from "./tools";
export { tools } from "./tools";
Loading