import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new McpServer({
name: "my-store-server",
version: "1.0.0"
});
// ---- 商品搜索 ----
server.tool(
"search_products",
"搜索商品。支持关键词、分类筛选。返回商品名、价格、库存状态。",
{
query: { type: "string", description: "搜索关键词" },
category: { type: "string", description: "分类名(可选)" },
minPrice: { type: "number", description: "最低价格(可选)" },
maxPrice: { type: "number", description: "最高价格(可选)" },
limit: { type: "number", description: "返回数量,默认10,最大50" }
},
async ({ query, category, minPrice, maxPrice, limit = 10 }) => {
const filters: any = {};
if (category) filters.category = category;
if (minPrice) filters.price = { $gte: minPrice };
if (maxPrice) filters.price = { ...filters.price, $lte: maxPrice };
const results = await db.products.search(query, filters, Math.min(limit, 50));
return {
content: [{
type: "text",
text: JSON.stringify({
results: results.map(p => ({
name: p.name,
sku: p.sku,
price: p.price,
currency: "CNY",
inStock: p.inventory > 0,
category: p.category,
url: `https://mystore.com/product/${p.slug}`
})),
total: results.totalCount,
query
}, null, 2)
}]
};
}
);
// ---- 商品详情 ----
server.tool(
"get_product",
"获取单个商品的完整详情,包括描述、规格、评分、库存。",
{
sku: { type: "string", description: "商品SKU编号" }
},
async ({ sku }) => {
const product = await db.products.findBySku(sku);
if (!product) {
return {
content: [{ type: "text", text: `未找到SKU: ${sku}` }],
isError: true
};
}
return {
content: [{
type: "text",
text: JSON.stringify({
name: product.name,
sku: product.sku,
description: product.description,
price: product.price,
currency: "CNY",
brand: product.brand,
category: product.category,
inStock: product.inventory > 0,
inventory: product.inventory,
rating: product.averageRating,
reviewCount: product.reviewCount,
specs: product.specifications,
images: product.images,
url: `https://mystore.com/product/${product.slug}`
}, null, 2)
}]
};
}
);
// ---- 订单查询 ----
server.tool(
"get_order",
"查询订单状态和明细。需要订单号。",
{
orderId: { type: "string", description: "订单号" }
},
async ({ orderId }) => {
const order = await db.orders.findById(orderId);
if (!order) {
return {
content: [{ type: "text", text: `未找到订单: ${orderId}` }],
isError: true
};
}
return {
content: [{
type: "text",
text: JSON.stringify({
id: order.id,
status: order.status,
createdAt: order.createdAt,
items: order.items.map(i => ({
name: i.name, quantity: i.quantity, price: i.price
})),
total: order.total,
shipping: {
method: order.shippingMethod,
trackingNumber: order.trackingNumber,
estimatedDelivery: order.estimatedDelivery
}
}, null, 2)
}]
};
}
);
// ---- OTR信任查询(ORBEXA集成)----
server.tool(
"otr_verify",
"查询商家网站的OTR信任评分。返回六维信任评估结果。",
{
domain: { type: "string", description: "商家域名,如 example.com" }
},
async ({ domain }) => {
const response = await fetch(
`https://${domain}/.well-known/otr/verify`
);
if (!response.ok) {
return {
content: [{ type: "text", text: `${domain} 未部署OTR协议` }],
isError: true
};
}
const otr = await response.json();
return {
content: [{
type: "text",
text: JSON.stringify({
domain: otr.domain,
trustScore: otr.trustScore,
dimensions: otr.dimensions,
badges: otr.badges,
lastScan: otr.lastScan
}, null, 2)
}]
};
}
);
// 启动
const transport = new StdioServerTransport();
await server.connect(transport);