list_posts
Retrieve and filter blog posts by title or tag keywords to manage content in Hexo blogging workflows.
Instructions
列出所有博客文章,支持按关键词过滤标题和标签
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": "list_posts"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| keyword | No | 可选的过滤关键词 |
Implementation Reference
- src/utils/post-manager.ts:41-76 (handler)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() ); } - src/tools/post-tools.ts:17-42 (registration)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 }; } } );