@opra-app/mcp
Version:
MCP server for OPRA's business and discount data
41 lines (40 loc) • 1.5 kB
JavaScript
import { z } from "zod";
import { listDiscounts, listDiscountsInCategory, getDiscountsByCity, } from "../api.js";
export const registerListDiscountsTool = (server) => {
server.tool("list-discounts", "Lists available discount offers. Optionally filter by category or city. Use list-categories or list-cities first to find valid IDs. Without filters, returns all discounts (can be a large list).", {
categoryId: z
.string()
.optional()
.describe("Filter by category. Use list-categories to find valid IDs."),
cityId: z
.string()
.optional()
.describe("Filter by city. Use list-cities to find valid IDs."),
}, async ({ categoryId, cityId }) => {
try {
let discounts;
if (categoryId) {
discounts = await listDiscountsInCategory(categoryId);
}
else if (cityId) {
discounts = await getDiscountsByCity(cityId);
}
else {
discounts = await listDiscounts();
}
return {
content: [
{ type: "text", text: JSON.stringify(discounts, null, 2) },
],
};
}
catch (error) {
return {
isError: true,
content: [
{ type: "text", text: `Failed to retrieve discounts: ${error}` },
],
};
}
});
};