Skip to main content
Glama
hafizrahman

Weather & WordPress MCP Server

by hafizrahman

get-posts-by-category

Retrieve WordPress posts from hafiz.blog by category slug to filter content for specific topics like technology or life.

Instructions

Get posts from a specific category on hafiz.blog (WordPress.com)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
categorySlugYesSlug of the category (e.g., 'technology', 'life')

Implementation Reference

  • The handler function fetches the category ID by slug from the WordPress API, retrieves the 10 most recent posts in that category, formats each with title, date, link, and excerpt (HTML stripped), and returns them as a markdown-like text block.
    async ({ categorySlug }) => { // Get category ID using the category slug const categoriesUrl = `${WP_COM_API_BASE}/categories?slug=${categorySlug}`; const categories = await fetchJson<WPCategory[]>(categoriesUrl); if (!categories || categories.length === 0) { return { content: [{ type: "text", text: `No category found with slug '${categorySlug}'` }], }; } // Fetch posts based on the category ID const categoryId = categories[0].id; const postsUrl = `${WP_COM_API_BASE}/posts?categories=${categoryId}&per_page=10&_fields=id,title,link,date,excerpt`; const posts = await fetchJson<WPPost[]>(postsUrl); if (!posts || posts.length === 0) { return { content: [{ type: "text", text: `No posts found in category '${categorySlug}'` }], }; } const formattedPosts = posts.map((post) => [ `Title: ${post.title.rendered}`, `Date: ${new Date(post.date).toLocaleString()}`, `Link: ${post.link}`, `Excerpt: ${stripHTML(post.excerpt.rendered).slice(0, 200)}...`, "---", ].join("\n") ); return { content: [{ type: "text", text: `Posts in category '${categorySlug}':\n\n${formattedPosts.join("\n")}` }], }; } );
  • Input schema defining the required 'categorySlug' parameter as a string, with description providing examples.
    { categorySlug: z.string().describe("Slug of the category (e.g., 'technology', 'life')"), },
  • src/index.ts:244-286 (registration)
    Full tool registration call using server.tool(), specifying name, description, input schema, and inline handler function.
    server.tool( "get-posts-by-category", "Get posts from a specific category on hafiz.blog (WordPress.com)", { categorySlug: z.string().describe("Slug of the category (e.g., 'technology', 'life')"), }, async ({ categorySlug }) => { // Get category ID using the category slug const categoriesUrl = `${WP_COM_API_BASE}/categories?slug=${categorySlug}`; const categories = await fetchJson<WPCategory[]>(categoriesUrl); if (!categories || categories.length === 0) { return { content: [{ type: "text", text: `No category found with slug '${categorySlug}'` }], }; } // Fetch posts based on the category ID const categoryId = categories[0].id; const postsUrl = `${WP_COM_API_BASE}/posts?categories=${categoryId}&per_page=10&_fields=id,title,link,date,excerpt`; const posts = await fetchJson<WPPost[]>(postsUrl); if (!posts || posts.length === 0) { return { content: [{ type: "text", text: `No posts found in category '${categorySlug}'` }], }; } const formattedPosts = posts.map((post) => [ `Title: ${post.title.rendered}`, `Date: ${new Date(post.date).toLocaleString()}`, `Link: ${post.link}`, `Excerpt: ${stripHTML(post.excerpt.rendered).slice(0, 200)}...`, "---", ].join("\n") ); return { content: [{ type: "text", text: `Posts in category '${categorySlug}':\n\n${formattedPosts.join("\n")}` }], }; } );
  • Helper function used to clean HTML from post excerpts before including in the output.
    function stripHTML(html: string): string { return html.replace(/<\/?[^>]+(>|$)/g, "").replace(/&nbsp;/g, " "); }
  • Shared utility function for making JSON API requests with error handling, used for fetching categories and posts.
    async function fetchJson<T>(url: string, headers: Record<string, string> = {}): Promise<T | null> { try { const response = await fetch(url, { headers: { "User-Agent": USER_AGENT, ...headers } }); if (!response.ok) throw new Error(`HTTP error ${response.status}`); return (await response.json()) as T; } catch (err) { console.error("Fetch error:", err); return null; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/hafizrahman/wp-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server