Skip to main content
Glama
leejersey

Hexo Blog MCP Server

by leejersey

list_posts

Retrieve and filter blog posts by title or tag keywords to manage content in Hexo blogging workflows.

Instructions

列出所有博客文章,支持按关键词过滤标题和标签

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
keywordNo可选的过滤关键词

Implementation Reference

  • The implementation of the `listPosts` function, which reads Markdown files from the posts directory, parses their front-matter, applies keyword filtering, and returns the sorted post metadata.
    export async function listPosts(keyword?: string): Promise<PostMeta[]> {
        const files = await fs.readdir(POSTS_DIR);
        const mdFiles = files.filter((f) => f.endsWith(".md"));
    
        const posts: PostMeta[] = [];
    
        for (const file of mdFiles) {
            const fullPath = path.join(POSTS_DIR, file);
            const raw = await fs.readFile(fullPath, "utf-8");
            const { data, content } = matter(raw);
    
            const meta: PostMeta = {
                title: data.title || file.replace(/\.md$/, ""),
                date: data.date ? String(data.date) : "未知",
                tags: Array.isArray(data.tags) ? data.tags : data.tags ? [data.tags] : [],
                filename: file,
                wordCount: content.length,
            };
    
            if (keyword) {
                const kw = keyword.toLowerCase();
                if (
                    meta.title.toLowerCase().includes(kw) ||
                    meta.tags.some((t) => t.toLowerCase().includes(kw))
                ) {
                    posts.push(meta);
                }
            } else {
                posts.push(meta);
            }
        }
    
        return posts.sort(
            (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
        );
    }
  • The registration of the "list_posts" tool, which invokes the `listPosts` helper and formats the results for the MCP response.
    server.tool(
        "list_posts",
        "列出所有博客文章,支持按关键词过滤标题和标签",
        { keyword: z.string().optional().describe("可选的过滤关键词") },
        async ({ keyword }) => {
            try {
                const posts = await listPosts(keyword);
                const summary = posts
                    .map(
                        (p, i) =>
                            `${i + 1}. 【${p.title}】\n   日期: ${p.date} | 标签: ${p.tags.join(", ") || "无"} | 字数: ${p.wordCount}\n   文件: ${p.filename}`
                    )
                    .join("\n\n");
                return {
                    content: [
                        {
                            type: "text" as const,
                            text: `共 ${posts.length} 篇文章:\n\n${summary}`,
                        },
                    ],
                };
            } catch (e: any) {
                return { content: [{ type: "text" as const, text: `错误: ${e.message}` }], isError: true };
            }
        }
    );

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/leejersey/hexo-mcp-server'

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