post_blog
Publish blog articles to a CMS with structured validation for title, description, content, and slug fields to ensure proper formatting and data integrity.
Instructions
Post a blog article to the CMS API using Zod validation
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| author_avatar_url | No | Author avatar URL | |
| author_name | No | Author name | |
| content | Yes | Content of the blog post | |
| cover_url | No | Cover image URL | |
| description | Yes | Description of the blog post | |
| locale | No | Language locale | |
| slug | Yes | URL slug for the blog post | |
| status | No | Status of the blog post (draft), only draft is supported | |
| title | Yes | Title of the blog post |
Implementation Reference
- src/index.ts:256-272 (handler)MCP server tool handler for 'post_blog' that invokes cmsClient.postBlog with user arguments and returns the API response.case 'post_blog': { try { // 使用 CMS 客户端发布博客,内置 Zod 验证 const result = await cmsClient.postBlog(request.params.arguments || {}) return { content: [ { type: 'text', text: `Successfully posted blog!\nResponse: ${JSON.stringify(result, null, 2)}`, }, ], } } catch (error) { throw new Error(`Failed to post blog: ${error instanceof Error ? error.message : String(error)}`) } }
- src/index.ts:139-185 (schema)Input schema definition for the 'post_blog' tool returned by ListToolsRequestHandler.{ name: 'post_blog', description: 'Post a blog article to the CMS API using Zod validation', inputSchema: { type: 'object', properties: { title: { type: 'string', description: 'Title of the blog post', }, description: { type: 'string', description: 'Description of the blog post', }, content: { type: 'string', description: 'Content of the blog post', }, slug: { type: 'string', description: 'URL slug for the blog post', }, status: { type: 'string', description: 'Status of the blog post (draft), only draft is supported', enum: ['draft'], }, cover_url: { type: 'string', description: 'Cover image URL', }, author_name: { type: 'string', description: 'Author name', }, author_avatar_url: { type: 'string', description: 'Author avatar URL', }, locale: { type: 'string', description: 'Language locale', }, }, required: ['title', 'description', 'content', 'slug'], }, },
- src/cms-blog.ts:57-93 (helper)Core implementation of postBlog method in CMSBlogClient that validates input with Zod, makes POST request to CMS API, and handles responses/errors.async postBlog(blogData: Partial<BlogPost>): Promise<BlogApiResponse> { try { // 使用 Zod 验证和标准化数据 const validatedData = BlogPostSchema.parse(blogData) console.log('Posting blog with data:', validatedData) const response = await fetch(`${this.apiUrl}/blog`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(validatedData), }) if (!response.ok) { throw new Error(`HTTP error! status: ${response.status} ${response.statusText}`) } const result = (await response.json()) as BlogApiResponse if (!result.success) { throw new Error(`API error: ${result.errMsg || 'Unknown error'}`) } return result } catch (error) { if (error instanceof z.ZodError) { // 处理验证错误 const errorMessages = error.issues.map((err: any) => `${err.path.join('.')}: ${err.message}`).join(', ') throw new Error(`数据验证失败: ${errorMessages}`) } console.error('Failed to post blog:', error) throw error } }
- src/cms-blog.ts:11-21 (schema)Zod schema used for validating blog post data in the postBlog implementation.export const BlogPostSchema = z.object({ title: z.string().min(1, '标题不能为空'), description: z.string().min(1, '描述不能为空'), content: z.string().min(1, '内容不能为空'), slug: z.string().min(1, 'URL 地址不能为空'), status: z.enum(['draft', 'published']).default('draft'), cover_url: z.url('封面图片 URL 格式不正确').default('https://example.com/cover.jpg'), author_name: z.string().default('Lafe'), author_avatar_url: z.url('作者头像 URL 格式不正确').default('https://example.com/avatar.jpg'), locale: z.string().default('en'), })