|
| 1 | +import { Tool } from '@modelcontextprotocol/sdk/types.js'; |
| 2 | +import { create } from '../client.js'; |
| 3 | +import type { BulkToolParameters, BulkCreateResult } from '../types.js'; |
| 4 | +import { FIELD_FORMATS_DESCRIPTION } from '../constants.js'; |
| 5 | + |
| 6 | +const BULK_DESCRIPTION = ` |
| 7 | + Create multiple contents in microCMS at once. |
| 8 | + This tool processes contents sequentially and continues even if some fail. |
| 9 | + Results include success/failure status for each content. |
| 10 | +
|
| 11 | + ${FIELD_FORMATS_DESCRIPTION} |
| 12 | +`; |
| 13 | + |
| 14 | +export const createContentsBulkPublishedTool: Tool = { |
| 15 | + name: 'microcms_create_contents_bulk_published', |
| 16 | + description: BULK_DESCRIPTION, |
| 17 | + inputSchema: { |
| 18 | + type: 'object', |
| 19 | + properties: { |
| 20 | + endpoint: { |
| 21 | + type: 'string', |
| 22 | + description: 'Content type name (e.g., "blogs", "news")', |
| 23 | + }, |
| 24 | + contents: { |
| 25 | + type: 'array', |
| 26 | + description: 'Array of contents to create', |
| 27 | + items: { |
| 28 | + type: 'object', |
| 29 | + properties: { |
| 30 | + content: { |
| 31 | + type: 'object', |
| 32 | + description: 'Content data to create (JSON object)', |
| 33 | + }, |
| 34 | + contentId: { |
| 35 | + type: 'string', |
| 36 | + description: 'Specific content ID to assign (optional)', |
| 37 | + }, |
| 38 | + }, |
| 39 | + required: ['content'], |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + required: ['endpoint', 'contents'], |
| 44 | + }, |
| 45 | +}; |
| 46 | + |
| 47 | +export const createContentsBulkDraftTool: Tool = { |
| 48 | + name: 'microcms_create_contents_bulk_draft', |
| 49 | + description: BULK_DESCRIPTION, |
| 50 | + inputSchema: { |
| 51 | + type: 'object', |
| 52 | + properties: { |
| 53 | + endpoint: { |
| 54 | + type: 'string', |
| 55 | + description: 'Content type name (e.g., "blogs", "news")', |
| 56 | + }, |
| 57 | + contents: { |
| 58 | + type: 'array', |
| 59 | + description: 'Array of contents to create as draft', |
| 60 | + items: { |
| 61 | + type: 'object', |
| 62 | + properties: { |
| 63 | + content: { |
| 64 | + type: 'object', |
| 65 | + description: 'Content data to create (JSON object)', |
| 66 | + }, |
| 67 | + contentId: { |
| 68 | + type: 'string', |
| 69 | + description: 'Specific content ID to assign (optional)', |
| 70 | + }, |
| 71 | + }, |
| 72 | + required: ['content'], |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + required: ['endpoint', 'contents'], |
| 77 | + }, |
| 78 | +}; |
| 79 | + |
| 80 | +async function handleBulkCreate( |
| 81 | + params: BulkToolParameters, |
| 82 | + isDraft: boolean |
| 83 | +): Promise<BulkCreateResult> { |
| 84 | + const { endpoint, contents } = params; |
| 85 | + |
| 86 | + if (!contents || !Array.isArray(contents) || contents.length === 0) { |
| 87 | + throw new Error('contents array is required and must not be empty'); |
| 88 | + } |
| 89 | + |
| 90 | + const results: BulkCreateResult['results'] = []; |
| 91 | + let successCount = 0; |
| 92 | + let failureCount = 0; |
| 93 | + |
| 94 | + for (let i = 0; i < contents.length; i++) { |
| 95 | + const item = contents[i]; |
| 96 | + |
| 97 | + try { |
| 98 | + const createOptions: { isDraft: boolean; contentId?: string } = { |
| 99 | + isDraft, |
| 100 | + }; |
| 101 | + |
| 102 | + if (item.contentId) { |
| 103 | + createOptions.contentId = item.contentId; |
| 104 | + } |
| 105 | + |
| 106 | + const result = await create(endpoint, item.content, createOptions); |
| 107 | + |
| 108 | + results.push({ |
| 109 | + index: i, |
| 110 | + success: true, |
| 111 | + id: result.id, |
| 112 | + }); |
| 113 | + successCount++; |
| 114 | + } catch (error) { |
| 115 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 116 | + results.push({ |
| 117 | + index: i, |
| 118 | + success: false, |
| 119 | + error: errorMessage, |
| 120 | + }); |
| 121 | + failureCount++; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return { |
| 126 | + totalCount: contents.length, |
| 127 | + successCount, |
| 128 | + failureCount, |
| 129 | + results, |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +export async function handleCreateContentsBulkPublished( |
| 134 | + params: BulkToolParameters |
| 135 | +): Promise<BulkCreateResult> { |
| 136 | + return handleBulkCreate(params, false); |
| 137 | +} |
| 138 | + |
| 139 | +export async function handleCreateContentsBulkDraft( |
| 140 | + params: BulkToolParameters |
| 141 | +): Promise<BulkCreateResult> { |
| 142 | + return handleBulkCreate(params, true); |
| 143 | +} |
| 144 | + |
0 commit comments