Skip to main content
Glama
hafizrahman

Weather & WordPress MCP Server

by hafizrahman

get-latest-posts

Retrieve recent posts from a WordPress blog to access current content updates and information.

Instructions

Get the 10 most recent posts from hafiz.blog (WordPress.com)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • src/index.ts:179-207 (registration)
    Registration of the 'get-latest-posts' tool, including description, empty input schema, and the complete inline handler function that fetches and formats the latest 10 blog posts.
    server.tool(
        "get-latest-posts",
        "Get the 10 most recent posts from hafiz.blog (WordPress.com)",
        {},
        async () => {
            const postsUrl = `${WP_COM_API_BASE}/posts?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 or failed to fetch." }],
                };
            }
    
            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: `Latest posts from hafiz.blog:\n\n${formattedPosts.join("\n")}` }],
            };
        }
    );
  • The core handler function for executing the 'get-latest-posts' tool. It retrieves the 10 most recent posts via the WordPress.com API, handles errors, formats each post with title, date, link, and truncated excerpt, and returns formatted text content.
    async () => {
        const postsUrl = `${WP_COM_API_BASE}/posts?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 or failed to fetch." }],
            };
        }
    
        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: `Latest posts from hafiz.blog:\n\n${formattedPosts.join("\n")}` }],
        };
    }
  • TypeScript interface (schema) defining the expected structure of WordPress post objects used in the get-latest-posts handler.
    interface WPPost {
        id: number;
        title: { rendered: string };
        link: string;
        date: string;
        excerpt: { rendered: string };
    }
  • Helper utility function to remove HTML tags and normalize spaces from post excerpt text, used in post formatting.
    function stripHTML(html: string): string {
        return html.replace(/<\/?[^>]+(>|$)/g, "").replace(/ /g, " ");
    }
  • Shared helper function for fetching JSON from APIs with User-Agent header and error handling, used by the tool to retrieve 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