|
| 1 | +import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 2 | +import type { z } from "zod"; |
| 3 | +import type { LabelsCreateResponse, LabelsListResponse } from "@/types/index.js"; |
| 4 | +import { LabelsCreateParamsSchema, LabelsSearchParamsSchema } from "../schemas/label.js"; |
| 5 | +import { handleApiError, makeApiRequest } from "../services/api-client.js"; |
| 6 | + |
| 7 | +async function cafe24_list_labels(params: z.infer<typeof LabelsSearchParamsSchema>) { |
| 8 | + try { |
| 9 | + const data = await makeApiRequest<LabelsListResponse>("/admin/labels", "GET", undefined, { |
| 10 | + shop_no: params.shop_no, |
| 11 | + limit: params.limit, |
| 12 | + offset: params.offset, |
| 13 | + }); |
| 14 | + |
| 15 | + return { |
| 16 | + content: [ |
| 17 | + { |
| 18 | + type: "text" as const, |
| 19 | + text: |
| 20 | + `# Order Labels (Shop: ${data.labels.shop_no})\n\n` + |
| 21 | + data.labels.names.map((name) => `- ${name}`).join("\n"), |
| 22 | + }, |
| 23 | + ], |
| 24 | + structuredContent: { |
| 25 | + labels: data.labels, |
| 26 | + links: data.links, |
| 27 | + }, |
| 28 | + }; |
| 29 | + } catch (error) { |
| 30 | + return { content: [{ type: "text" as const, text: handleApiError(error) }] }; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function cafe24_create_labels(params: z.infer<typeof LabelsCreateParamsSchema>) { |
| 35 | + try { |
| 36 | + const data = await makeApiRequest<LabelsCreateResponse>("/admin/labels", "POST", { |
| 37 | + shop_no: params.shop_no, |
| 38 | + requests: params.requests, |
| 39 | + }); |
| 40 | + |
| 41 | + return { |
| 42 | + content: [ |
| 43 | + { |
| 44 | + type: "text" as const, |
| 45 | + text: `Successfully created ${data.labels.length} label(s).`, |
| 46 | + }, |
| 47 | + ], |
| 48 | + structuredContent: { |
| 49 | + results: data.labels, |
| 50 | + }, |
| 51 | + }; |
| 52 | + } catch (error) { |
| 53 | + return { content: [{ type: "text" as const, text: handleApiError(error) }] }; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +export function registerTools(server: McpServer): void { |
| 58 | + server.registerTool( |
| 59 | + "cafe24_list_labels", |
| 60 | + { |
| 61 | + title: "List Cafe24 Order Labels", |
| 62 | + description: "Retrieve a list of order labels for a specific shop.", |
| 63 | + inputSchema: LabelsSearchParamsSchema, |
| 64 | + annotations: { |
| 65 | + readOnlyHint: true, |
| 66 | + destructiveHint: false, |
| 67 | + idempotentHint: true, |
| 68 | + openWorldHint: false, |
| 69 | + }, |
| 70 | + }, |
| 71 | + cafe24_list_labels, |
| 72 | + ); |
| 73 | + |
| 74 | + server.registerTool( |
| 75 | + "cafe24_create_labels", |
| 76 | + { |
| 77 | + title: "Create Cafe24 Order Labels", |
| 78 | + description: "Creates multiple labels for order items in a specific shop.", |
| 79 | + inputSchema: LabelsCreateParamsSchema, |
| 80 | + annotations: { |
| 81 | + readOnlyHint: false, |
| 82 | + destructiveHint: false, |
| 83 | + idempotentHint: false, |
| 84 | + openWorldHint: false, |
| 85 | + }, |
| 86 | + }, |
| 87 | + cafe24_create_labels, |
| 88 | + ); |
| 89 | +} |
0 commit comments