// src/tools/offers.ts
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { getGhostApiConfig, generateGhostAdminToken } from "../ghostApi.js";
import axios from 'axios';
// Parameter schemas as ZodRawShape (object literals)
const browseParams = {
filter: z.string().optional(),
limit: z.number().optional(),
page: z.number().optional(),
order: z.string().optional(),
};
const readParams = {
id: z.string().optional(),
code: z.string().optional(),
};
const addParams = {
name: z.string(),
code: z.string(),
cadence: z.string(),
duration: z.string(),
amount: z.number(),
tier_id: z.string(),
type: z.string(),
display_title: z.string().optional(),
display_description: z.string().optional(),
duration_in_months: z.number().optional(),
currency: z.string().optional(),
// Add more fields as needed
};
const editParams = {
id: z.string(),
name: z.string().optional(),
code: z.string().optional(),
display_title: z.string().optional(),
display_description: z.string().optional(),
// Only a subset of fields are editable per Ghost API docs
};
const deleteParams = {
id: z.string(),
};
export function registerOfferTools(server: McpServer) {
// Browse offers
server.tool(
"offers_browse",
browseParams,
async (args, _extra) => {
const config = getGhostApiConfig();
if (!config) {
return { isError: true, content: [{ type: "text", text: "Ghost API not configured" }] };
}
try {
const token = generateGhostAdminToken(config.key);
const url = `${config.url}/ghost/api/admin/offers/`;
const headers = {
'Authorization': `Ghost ${token}`
};
const response = await axios.get(url, { params: args, headers });
return {
content: [
{
type: "text",
text: JSON.stringify(response.data.offers, null, 2),
},
],
};
} catch (error: any) {
const status = error?.response?.status ?? error?.status ?? "unknown";
const body = error?.response?.data ?? error?.data ?? error?.message ?? String(error);
const bodyText = typeof body === "string" ? body : JSON.stringify(body, null, 2);
return {
isError: true,
content: [
{
type: "text",
text: `offers_browse failed. status=${status}\n${bodyText}`,
},
],
};
}
}
);
// Read offer
server.tool(
"offers_read",
readParams,
async (args, _extra) => {
const config = getGhostApiConfig();
if (!config) {
return { isError: true, content: [{ type: "text", text: "Ghost API not configured" }] };
}
try {
const token = generateGhostAdminToken(config.key);
const url = `${config.url}/ghost/api/admin/offers/${args.id}/`;
const headers = {
'Authorization': `Ghost ${token}`
};
const response = await axios.get(url, { headers });
return {
content: [
{
type: "text",
text: JSON.stringify(response.data.offers[0], null, 2),
},
],
};
} catch (error: any) {
const status = error?.response?.status ?? error?.status ?? "unknown";
const body = error?.response?.data ?? error?.data ?? error?.message ?? String(error);
const bodyText = typeof body === "string" ? body : JSON.stringify(body, null, 2);
return {
isError: true,
content: [
{
type: "text",
text: `offers_read failed. status=${status}\n${bodyText}`,
},
],
};
}
}
);
// Add offer
server.tool(
"offers_add",
addParams,
async (args, _extra) => {
const config = getGhostApiConfig();
if (!config) {
return { isError: true, content: [{ type: "text", text: "Ghost API not configured" }] };
}
try {
const token = generateGhostAdminToken(config.key);
const url = `${config.url}/ghost/api/admin/offers/`;
const headers = {
'Authorization': `Ghost ${token}`
};
const response = await axios.post(url, { offers: [args] }, { headers });
return {
content: [
{
type: "text",
text: JSON.stringify(response.data.offers[0], null, 2),
},
],
};
} catch (error: any) {
const status = error?.response?.status ?? error?.status ?? "unknown";
const body = error?.response?.data ?? error?.data ?? error?.message ?? String(error);
const bodyText = typeof body === "string" ? body : JSON.stringify(body, null, 2);
return {
isError: true,
content: [
{
type: "text",
text: `offers_add failed. status=${status}\n${bodyText}`,
},
],
};
}
}
);
// Edit offer
server.tool(
"offers_edit",
editParams,
async (args, _extra) => {
const config = getGhostApiConfig();
if (!config) {
return { isError: true, content: [{ type: "text", text: "Ghost API not configured" }] };
}
try {
const token = generateGhostAdminToken(config.key);
const url = `${config.url}/ghost/api/admin/offers/${args.id}/`;
const headers = {
'Authorization': `Ghost ${token}`
};
const response = await axios.put(url, { offers: [args] }, { headers });
return {
content: [
{
type: "text",
text: JSON.stringify(response.data.offers[0], null, 2),
},
],
};
} catch (error: any) {
const status = error?.response?.status ?? error?.status ?? "unknown";
const body = error?.response?.data ?? error?.data ?? error?.message ?? String(error);
const bodyText = typeof body === "string" ? body : JSON.stringify(body, null, 2);
return {
isError: true,
content: [
{
type: "text",
text: `offers_edit failed. status=${status}\n${bodyText}`,
},
],
};
}
}
);
// Delete offer
server.tool(
"offers_delete",
deleteParams,
async (args, _extra) => {
const config = getGhostApiConfig();
if (!config) {
return { isError: true, content: [{ type: "text", text: "Ghost API not configured" }] };
}
try {
const token = generateGhostAdminToken(config.key);
const url = `${config.url}/ghost/api/admin/offers/${args.id}/`;
const headers = {
'Authorization': `Ghost ${token}`
};
await axios.delete(url, { headers });
return {
content: [
{
type: "text",
text: `Offer with id ${args.id} deleted.`,
},
],
};
} catch (error: any) {
const status = error?.response?.status ?? error?.status ?? "unknown";
const body = error?.response?.data ?? error?.data ?? error?.message ?? String(error);
const bodyText = typeof body === "string" ? body : JSON.stringify(body, null, 2);
return {
isError: true,
content: [
{
type: "text",
text: `offers_delete failed. status=${status}\n${bodyText}`,
},
],
};
}
}
);
}