import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import type { ToolContext } from "../utils/api.js";
const json = (d: unknown) => ({ type: "text" as const, text: JSON.stringify(d, null, 2) });
export function registerWebhooks(server: McpServer, ctx: ToolContext): void {
const { api, requireConfig } = ctx;
server.registerTool(
"fetch_webhooks",
{ description: "List webhooks in the space." },
async () => {
requireConfig();
const d = await api("/webhooks");
return { content: [json(d)] };
}
);
server.registerTool(
"get_webhook",
{ description: "Get a webhook by ID.", inputSchema: { webhook_id: z.string() } },
async ({ webhook_id }) => {
requireConfig();
const d = await api(`/webhooks/${webhook_id}`);
return { content: [json(d)] };
}
);
server.registerTool(
"create_webhook",
{
description: "Create a webhook. Pass name, url, and optional events, description, secret.",
inputSchema: {
name: z.string(),
url: z.string().url(),
events: z.array(z.string()).optional(),
description: z.string().optional(),
secret: z.string().optional(),
},
},
async (body) => {
requireConfig();
const d = await api("/webhooks", { method: "POST", body: JSON.stringify({ webhook: body }) });
return { content: [json(d)] };
}
);
server.registerTool(
"update_webhook",
{
description: "Update a webhook.",
inputSchema: {
webhook_id: z.string(),
name: z.string().optional(),
url: z.string().url().optional(),
events: z.array(z.string()).optional(),
description: z.string().optional(),
secret: z.string().optional(),
},
},
async ({ webhook_id, ...rest }) => {
requireConfig();
const d = await api(`/webhooks/${webhook_id}`, { method: "PUT", body: JSON.stringify({ webhook: rest }) });
return { content: [json(d)] };
}
);
server.registerTool(
"delete_webhook",
{ description: "Delete a webhook by ID.", inputSchema: { webhook_id: z.string() } },
async ({ webhook_id }) => {
requireConfig();
await api(`/webhooks/${webhook_id}`, { method: "DELETE" });
return { content: [json({ success: true })] };
}
);
}