使用说明
FastMCP服务器
使用OAuth 2.0实现生产就绪模型上下文协议
一个生产就绪的模型上下文协议(MCP)服务器,使AI代理能够安全地执行工具。使用FastMCP构建,具有OAuth 2.0身份验证、SQLite数据库和全面的安全控制。
概述
此MCP服务器为AI代理提供了4个安全工具:
- 文件操作 (公共)-使用路径验证进行安全的文件读/写
- 天气 (公共)-OpenWeatherMap API集成
- 备注 (OAuth保护)-带标签的个人笔记管理
- 个人资料 (OAuth保护)-用户配置文件管理
主要特点
安全第一
- OAuth 2.0身份验证与Auth0 JWT验证
- 路径遍历保护和文件验证
- 用户数据隔离
- 基于环境的秘密
生产就绪
- Docker和Docker Compose支持
- 多阶段构建(~150MB图像)
- 健康监测
- 全面的错误处理
- 结构化日志记录
数据库集成
- SQLite与SQLAlchemy ORM
- 3个模型:用户、个人资料、注释
- 上下文管理会话
快速开始
选项1:Docker(推荐)
先决条件: Docker 20.10+和Docker Compose 2.0+
# Clone repository
git clone https://github.com/ShashankShekhar00/fastmcp-server.git
cd fastmcp-server
# Configure environment
cp .env.example .env
# Edit .env with your Auth0 and OpenWeatherMap credentials
# Start server
docker compose up --build -d
# View logs
docker compose logs -f
# Check health
curl http://localhost:8000/mcp
看 医生.md 查看完整的Docker文档。
方案2:地方发展
先决条件: Python 3.12+,Auth0帐户,OpenWeatherMap API密钥
# Clone and setup
git clone https://github.com/ShashankShekhar00/fastmcp-server.git
cd fastmcp-server
# Create virtual environment
python -m venv .venv
.venv\Scripts\activate # Windows
# source .venv/bin/activate # Linux/Mac
# Install dependencies
pip install -r requirements.txt
# Configure environment
cp .env.example .env
# Edit .env with your credentials
# Run server
python -m src.server_oauth
服务器启动于 http://localhost:8000
配置
创建 .env 使用您的凭据文件:
# OAuth 2.0 (Auth0)
OAUTH_DOMAIN=dev-example.auth0.com
OAUTH_CLIENT_ID=your_client_id
OAUTH_CLIENT_SECRET=your_client_secret
OAUTH_AUDIENCE=https://api.mcp-server.com
OAUTH_TOKEN_URL=https://dev-example.auth0.com/oauth/token
OAUTH_JWKS_URL=https://dev-example.auth0.com/.well-known/jwks.json
OAUTH_ISSUER=https://dev-example.auth0.com/
# OpenWeatherMap API
OPENWEATHER_API_KEY=your_api_key
# File Operations Security
ALLOWED_FILE_PATHS=C:\Users\YourName\Desktop,C:\Users\YourName\Downloads
MAX_FILE_SIZE_MB=10
ALLOWED_FILE_EXTENSIONS=.txt,.json,.csv,.md
# Server Configuration
HOST=0.0.0.0
PORT=8000
ENVIRONMENT=development
LOG_LEVEL=INFO
# Database
DATABASE_URL=sqlite:///./mcp_server.db
# Security
SECRET_KEY=your_secret_key_here
MCP工具
公共工具(无需身份验证)
1.文件操作
使用路径验证进行安全的文件读/写。
{
"name": "file_operations",
"arguments": {
"operation": "read",
"filepath": "C:\\Users\\YourName\\Desktop\\example.txt"
}
}
{
"name": "file_operations",
"arguments": {
"operation": "write",
"filepath": "C:\\Users\\YourName\\Desktop\\output.txt",
"content": "Hello, World!"
}
}
特征:
- 路径满列表验证
- 文件扩展名白名单
- 10MB大小限制
- 元数据抽取
2.天气
OpenWeatherMap API集成。
{
"name": "weather",
"arguments": {
"city": "London"
}
}
退货: 温度、湿度、风速、天气状况
OAuth保护工具(需要承载令牌)
3.注意事项
使用CRUD操作进行个人笔记管理。
{
"name": "notes",
"arguments": {
"action": "create",
"content": "My note",
"title": "Important",
"tags": ["work", "urgent"]
}
}
操作: 创建、获取、列出、更新、删除、存档、取消存档、固定、取消固定
4.简介
用户配置文件管理。
{
"name": "profile",
"arguments": {
"action": "create",
"name": "John Doe",
"bio": "Software Developer"
}
}
操作: 获取、创建、更新、删除
项目结构
fastmcp-server/
├── src/
│ ├── server_oauth.py # Main FastMCP server (ACTIVE)
│ ├── config.py # Configuration management
│ ├── auth/ # OAuth 2.0 & JWT validation
│ ├── database/ # SQLAlchemy session management
│ ├── models/ # User, Profile, Note models
│ ├── services/ # Business logic layer
│ ├── tools/ # 4 MCP tools
│ └── utils/ # Errors, logging, validators
├── scripts/ # Testing & utility scripts
├── Dockerfile # Multi-stage container build
├── docker-compose.yml # Orchestration
├── healthcheck.py # Docker health monitoring
└── requirements.txt # Python dependencies
关键组件
- FastMCP -MCP协议实现(HTTP传输)
- SQL炼金术 -带有SQLite数据库的ORM
- 身份验证0 -OAuth 2.0身份验证提供程序
- JWT验证 -使用JWKS进行令牌验证
- 码头工人 -多阶段构建的容器化
测试
# Test OAuth flow
python scripts/test_oauth.py
# Test OAuth-protected tools
python scripts/test_oauth_tools.py
# Test weather API
python scripts/test_weather_api.py
# Test file operations
python scripts/test_tools.py
# Use tools locally
python scripts/use_tools_locally.py
Docker部署
使用Docker Compose
docker compose up --build -d
手动Docker构建
# Build image
docker build -t fastmcp-server .
# Run container
docker run -p 8000:8000 --env-file .env fastmcp-server
健康检查
curl http://localhost:8000/mcp
看 医生.md 用于高级Docker配置、部署选项和故障排除。
安全特性
- 使用Auth0的OAuth 2.0 JWT身份验证
- 路径遍历保护
- 文件扩展名验证(白名单)
- 文件大小限制(默认10MB)
- 可配置的路径分配
- 用户数据隔离
- 日志中的令牌编辑
- 基于环境的秘密
- 非root Docker用户
技术栈
| 组件 | 技术 |
|---|---|
| 协议 | FastMCP 2.13 |
| 语言 | Python 3.12+ |
| 数据库 | SQLite+SQLAlchemy |
| 身份验证 | OAuth 2.0(Auth0) |
| 容器 | Docker+Docker组合 |
| API | OpenWeatherMap |
文档
- README.md -此文件(快速入门)
- 项目_创新.md -完整的项目概述
- 医生.md -Docker部署指南
- DOCKER_SETUP.md -快速Docker设置
- DOCKER_QUICKREF.md -Docker命令参考
- 测试指南.md -测试文件
贡献
欢迎投稿!请按照以下步骤操作:
- 复刻仓库
- 创建特征分支(
git checkout -b feature/AmazingFeature) - 提交更改(
git commit -m 'Add AmazingFeature') - 推送到分支(
git push origin feature/AmazingFeature) - 打开拉取请求
许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
致谢
- FastMCP -MCP协议实现
- 身份验证0 -身份验证平台
- OpenWeatherMap -天气数据API
- SQL炼金术 -Python SQL工具包
联系
沙尚克·谢卡尔
- github: @沙尚克·谢卡尔00
- 存储库: fastmcp服务器
具有OAuth 2.0、Docker支持和全面安全控制的生产就绪MCP服务器。