FastAPI MCP Notes服务器
基于FastAPI的模型上下文协议(MCP)服务器,允许客户端添加和管理具有文件名和内容的注释。
概述
此MCP服务器为创建和存储笔记提供了一个简单的界面。客户端可以发送带有文件名和内容的笔记,然后将其存储在本地可配置的目录中。
特性
- 添加注释:使用自定义文件名和内容创建注释
- FastAPI后端:建立在FastAPI之上,实现高性能和自动化的API文档
- MCP集成:遵循模型上下文协议,与MCP客户端无缝集成
- 异步文件操作:具有文件锁定的非阻塞文件I/O,以实现并发安全
- 基于环境的配置:可通过环境变量配置笔记目录
技术栈
- 快速API:用于构建API的现代快速web框架
- Python 3.12+:核心编程语言
- FastMCP:用于标准化通信的MCP协议实现
- aiofiles:异步文件操作
- python dotenv:环境变量管理
安装
- 克隆存储库 (或导航到项目目录)
- 安装依赖项 使用紫外线:
uv sync- 配置环境变量:
# Copy the example environment file
cp .env.example .env
# Edit .env to customize the notes directory (optional)
# Default: ~/.mcp-notes配置
服务器使用环境变量进行配置。创建一个 .env 项目根目录中的文件:
# Directory where notes will be stored
NOTES_DIR=~/.mcp-notes您可以自定义 NOTES_DIR 走你喜欢的任何一条路。如果目录不存在,将自动创建。
用法
运行服务器
# Start the FastAPI server
uvicorn main:app --reload服务器将在以下时间启动并可用:
- HTTP API: http://localhost:8000
- MCP端点: http://localhost:8000/mcp
- 健康检查: http://localhost:8000/health
- API文件: http://localhost:8000/docs
使用Claude代码
此服务器在Claude Code中配置为MCP服务器 ~/.claude/mcp_servers.json:
{
"notes-server": {
"type": "sse",
"url": "http://localhost:8000/mcp"
}
}要使用:
- 启动服务器:
uvicorn main:app --reload - 重新启动Claude Code或重新加载MCP服务器
- 这
write_to_file该工具将在Claude Code中可用
可用的MCP工具:
write_to_file(file_name, file_content)-将笔记写入配置的笔记目录中的文件
API终点
健康检查
GET /health返回服务器状态。
写笔记(HTTP)
POST /write-note
Content-Type: application/json
{
"file_name": "my-note",
"file_content": "This is my note content"
}MCP工具:write_to_file
服务器公开了一个MCP工具 write_to_file MCP客户端可以调用:
- 文件名:笔记文件的名称(无扩展名)
- 文件内容:写笔记的内容
发展
本项目使用:
- 快速API 对于web框架
- FastMCP 用于MCP协议实现
- aiofiles 用于异步文件操作
- 文件锁定 (asyncio.Lock)以防止并发写入期间出现竞争情况
项目结构
.
├── main.py # Main FastAPI application
├── schemas.py # Pydantic models for request/response
├── .env # Environment configuration (not in git)
├── .env.example # Example environment configuration
└── pyproject.toml # Project dependencies许可证
\[在此处添加您的许可证\]
