Learn how to integrate MCIP (Machine Customer Interaction Protocol) into your applications and connect your store to AI agents.
The simplest way to use MCIP is to call the search endpoint directly.
async function searchProducts(query: string) {
const storeUrl = "https://store.example.com";
const url = new URL("/search", storeUrl);
url.searchParams.set("q", query);
url.searchParams.set("take", "5");
const response = await fetch(url);
const data = await response.json();
return data.items;
}
// Usage
const products = await searchProducts("wireless headphones");MCIP stores return a consistent JSON structure:
{
"meta": {},
"items": [
{
"externalId": "prod-123",
"url": "https://store.example.com/products/headphones",
"title": "Wireless Headphones Pro",
"description": "Premium wireless headphones with noise cancellation",
"price": {
"amount": 199.99,
"currency": "USD"
},
"mainImage": "https://store.example.com/images/headphones.jpg",
"brand": "AudioTech",
"category": "Electronics",
"keywords": ["wireless", "bluetooth", "noise-cancelling"]
}
]
}Search across multiple MCIP stores and combine results:
async function searchMultipleStores(query: string) {
const stores = [
"https://store-a.example.com",
"https://store-b.example.com",
"https://store-c.example.com"
];
const searchPromises = stores.map(async (storeUrl) => {
try {
const url = new URL("/search", storeUrl);
url.searchParams.set("q", query);
url.searchParams.set("take", "3");
const response = await fetch(url);
const data = await response.json();
return data.items || [];
} catch (error) {
console.error(`Store ${storeUrl} failed:`, error);
return [];
}
});
const results = await Promise.all(searchPromises);
return results.flat();
}Use TypeScript for better type safety:
type UnifiedProduct = {
externalId: string;
url: string;
title: string;
description: string;
price: {
amount: number;
currency: "USD" | "EUR" | "UAH";
};
mainImage: string;
brand?: string;
category?: string;
keywords: string[];
};
function parseProduct(item: any): UnifiedProduct | null {
if (!item || typeof item !== "object") return null;
if (!item.url || !item.title || !item.description) return null;
if (!item.price?.amount || !item.price?.currency) return null;
return {
externalId: String(item.externalId || item.id),
url: item.url,
title: item.title,
description: item.description,
price: {
amount: item.price.amount,
currency: item.price.currency
},
mainImage: String(item.mainImage || ""),
brand: item.brand,
category: item.category,
keywords: Array.isArray(item.keywords) ? item.keywords : []
};
}Build a basic agent that helps users find products:
import { Annotation, StateGraph, START, END } from "@langchain/langgraph";
// Define agent state
const AgentState = Annotation.Root({
query: Annotation<string>,
products: Annotation<any[]>({ default: () => [] }),
answer: Annotation<string | null>({ default: () => null }),
});
type State = typeof AgentState.State;
// Search node: calls MCIP
function createSearchNode(search: (q: string) => Promise<any[]>) {
return async (state: State): Promise<Partial<State>> => {
const products = await search(state.query);
return { products };
};
}
// Format node: presents results to user
async function formatNode(state: State): Promise<Partial<State>> {
if (state.products.length === 0) {
return { answer: "No products found." };
}
const top3 = state.products.slice(0, 3);
const answer = `Found ${state.products.length} products:\n\n` +
top3.map((p, i) =>
`${i + 1}. ${p.title} - ${p.price.amount}\n ${p.url}`
).join('\n\n');
return { answer };
}
// Build the graph
function buildAgent(params: { search: (q: string) => Promise<any[]> }) {
const searchNode = createSearchNode(params.search);
const workflow = new StateGraph(AgentState)
.addNode("search", searchNode)
.addNode("format", formatNode)
.addEdge(START, "search")
.addEdge("search", "format")
.addEdge("format", END);
return workflow.compile();
}
// Use the agent
const graph = buildAgent({
search: async (q) => await searchProducts(q)
});
const result = await graph.invoke({
query: "wireless headphones",
products: [],
answer: null
});
console.log(result.answer);