feat(mcps): add pages and posts functions to klz-payload-mcp

This commit is contained in:
2026-03-05 12:47:24 +01:00
parent d0a17a8a31
commit 751ffd59a0

View File

@@ -49,6 +49,52 @@ const LIST_LEADS_TOOL: Tool = {
},
};
const LIST_PAGES_TOOL: Tool = {
name: "payload_list_pages",
description: "List pages from KLZ Payload CMS",
inputSchema: {
type: "object",
properties: {
limit: { type: "number", description: "Maximum number of pages" },
},
},
};
const GET_PAGE_TOOL: Tool = {
name: "payload_get_page",
description: "Get a specific page by its slug or ID",
inputSchema: {
type: "object",
properties: {
slug: { type: "string", description: "Page slug" },
id: { type: "string", description: "Page ID (if slug is not used)" }
},
},
};
const LIST_POSTS_TOOL: Tool = {
name: "payload_list_posts",
description: "List posts/articles from KLZ Payload CMS",
inputSchema: {
type: "object",
properties: {
limit: { type: "number", description: "Maximum number of posts" },
},
},
};
const GET_POST_TOOL: Tool = {
name: "payload_get_post",
description: "Get a specific post by its slug or ID",
inputSchema: {
type: "object",
properties: {
slug: { type: "string", description: "Post slug" },
id: { type: "string", description: "Post ID (if slug is not used)" }
},
},
};
const server = new Server(
{ name: "klz-payload-mcp", version: "1.0.0" },
{ capabilities: { tools: {} } }
@@ -58,6 +104,10 @@ server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
SEARCH_PRODUCTS_TOOL,
LIST_LEADS_TOOL,
LIST_PAGES_TOOL,
GET_PAGE_TOOL,
LIST_POSTS_TOOL,
GET_POST_TOOL
],
}));
@@ -95,6 +145,58 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
}
}
if (request.params.name === "payload_list_pages") {
const { limit = 10 } = request.params.arguments as any;
try {
const res = await payloadClient.get('/pages', { params: { limit } });
return { content: [{ type: "text", text: JSON.stringify(res.data.docs, null, 2) }] };
} catch (e: any) {
return { isError: true, content: [{ type: "text", text: `Error: ${e.message}` }] };
}
}
if (request.params.name === "payload_get_page") {
const { slug, id } = request.params.arguments as any;
try {
if (id) {
const res = await payloadClient.get(`/pages/${id}`);
return { content: [{ type: "text", text: JSON.stringify(res, null, 2) }] };
} else if (slug) {
const res = await payloadClient.get('/pages', { params: { where: { slug: { equals: slug } }, limit: 1 } });
return { content: [{ type: "text", text: JSON.stringify(res.data.docs[0] || {}, null, 2) }] };
}
return { isError: true, content: [{ type: "text", text: "Error: must provide slug or id" }] };
} catch (e: any) {
return { isError: true, content: [{ type: "text", text: `Error: ${e.message}` }] };
}
}
if (request.params.name === "payload_list_posts") {
const { limit = 10 } = request.params.arguments as any;
try {
const res = await payloadClient.get('/posts', { params: { limit, sort: '-createdAt' } });
return { content: [{ type: "text", text: JSON.stringify(res.data.docs, null, 2) }] };
} catch (e: any) {
return { isError: true, content: [{ type: "text", text: `Error: ${e.message}` }] };
}
}
if (request.params.name === "payload_get_post") {
const { slug, id } = request.params.arguments as any;
try {
if (id) {
const res = await payloadClient.get(`/posts/${id}`);
return { content: [{ type: "text", text: JSON.stringify(res.data, null, 2) }] };
} else if (slug) {
const res = await payloadClient.get('/posts', { params: { where: { slug: { equals: slug } }, limit: 1 } });
return { content: [{ type: "text", text: JSON.stringify(res.data.docs[0] || {}, null, 2) }] };
}
return { isError: true, content: [{ type: "text", text: "Error: must provide slug or id" }] };
} catch (e: any) {
return { isError: true, content: [{ type: "text", text: `Error: ${e.message}` }] };
}
}
throw new Error(`Unknown tool: ${request.params.name}`);
});