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)
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get-latest-posts"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
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")}` }], }; } ); - src/index.ts:183-206 (handler)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")}` }], }; } - src/index.ts:167-173 (schema)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 }; } - src/index.ts:175-177 (helper)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, " "); } - src/index.ts:20-29 (helper)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; } }