开发写博客的MCP Server
开发一个写当前博客的MCP Server,为agent提供写博客工具,提高博客撰写效率
当前架构:
- 文章存储在 PostgreSQL 数据库中(
global-postgresDocker 容器) - 后端 API 使用 FastAPI + async SQLAlchemy
- 支持文章的 CRUD 操作和发布功能
MCP Server 结构
/data/personal-blog/backend/mcp_server.py
提供的 MCP 工具
| 工具名称 | 功能 | 参数 |
|---|---|---|
list_articles |
列出文章列表 | status: draft/published(可选) |
get_article |
获取文章详情 | id: 文章ID |
create_article |
创建新文章 | slug, title, description, content, tags, categories |
update_article |
更新文章 | id, 可选字段 |
publish_article |
发布文章 | id |
delete_article |
删除文章 | id |
技术实现
- 使用 FastMCP:
from mcp.server.fastmcp import FastMCP - 数据库连接:复用现有的
AsyncSessionLocal和Article模型 - 运行方式:
python mcp_server.py通过 stdio 传输
启动命令
cd /data/personal-blog/backend
python mcp_server.py
修改文件
/data/personal-blog/backend/mcp_server.py (新建)
代码结构
from mcp.server.fastmcp import FastMCP
from database import AsyncSessionLocal
from models import Article
from sqlalchemy import select
import asyncio
mcp = FastMCP("blog-editor")
@mcp.tool()
async def list_articles(status: str = None) -> str:
"""列出所有文章,可按状态筛选"""
...
@mcp.tool()
async def get_article(id: int) -> str:
"""获取文章详情"""
...
@mcp.tool()
async def create_article(slug: str, title: str, ...) -> str:
"""创建新文章"""
...
@mcp.tool()
async def update_article(id: int, ...) -> str:
"""更新文章"""
...
@mcp.tool()
async def publish_article(id: int) -> str:
"""发布文章"""
...
@mcp.tool()
async def delete_article(id: int) -> str:
"""删除文章"""
...
if __name__ == "__main__":
mcp.run(transport="stdio")