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 registerComponents(server: McpServer, ctx: ToolContext): void {
const { api, requireConfig, buildQuery } = ctx;
server.registerTool(
"fetch_components",
{
description: "List components. Optional per_page, page.",
inputSchema: { per_page: z.number().optional(), page: z.number().optional() },
},
async (a) => {
requireConfig();
const q = buildQuery({ per_page: a?.per_page, page: a?.page });
const d = await api(`/components${q}`);
return { content: [json(d)] };
}
);
server.registerTool(
"get_component",
{ description: "Get a component by ID.", inputSchema: { component_id: z.string() } },
async ({ component_id }) => {
requireConfig();
const d = await api(`/components/${component_id}`);
return { content: [json(d)] };
}
);
server.registerTool(
"create_component",
{
description: "Create a component. Pass component object with name, display_name, schema, etc.",
inputSchema: { component: z.record(z.unknown()) },
},
async ({ component }) => {
requireConfig();
const d = await api("/components", { method: "POST", body: JSON.stringify({ component }) });
return { content: [json(d)] };
}
);
server.registerTool(
"update_component",
{
description: "Update a component by ID.",
inputSchema: { component_id: z.string(), component: z.record(z.unknown()) },
},
async ({ component_id, component }) => {
requireConfig();
const d = await api(`/components/${component_id}`, { method: "PUT", body: JSON.stringify({ component }) });
return { content: [json(d)] };
}
);
server.registerTool(
"delete_component",
{ description: "Delete a component by ID.", inputSchema: { component_id: z.string() } },
async ({ component_id }) => {
requireConfig();
await api(`/components/${component_id}`, { method: "DELETE" });
return { content: [json({ success: true })] };
}
);
}