FastMCP凭证存储演示
FastMCP服务器演示了使用Redis和Fernet加密的安全凭证存储。
目的
使用以下方法演示FastMCP中间件中的安全凭据管理:
- Redis用于分布式凭证存储
- Fernet加密用于数据保护
- Docker部署的HTTP传输
- FastMCP CLI配置
结构
fastmcp-test/
├── src/ # Source code
│ ├── server.py # FastMCP server with tools
│ ├── settings.py # Configuration management
│ ├── middlewares.py # Credential storage middleware
│ └── services/ # Mock API services
├── test_scripts/ # Test scripts
├── fastmcp.json # FastMCP CLI configuration
├── docker-compose.yml # Docker deployment
├── Dockerfile # Multi-stage build
└── .env.example # Environment template如何跑步
本地开发
- 设置环境
cp .env.example .env
# Edit .env with your encryption keys- 安装依赖项
uv sync- 启动服务器
fastmcp runDocker部署
- 启动服务
docker compose up -d- 与客户进行测试
python test_scripts/test_client_auth.py可用工具
api_get_user_data(user_id)-获取用户数据db_query_users(limit)-从数据库查询用户
配置
关键环境变量:
REDIS_HOST-Redis服务器主机名REDIS_PORT-Redis服务器端口ENCRYPTION_KEY-Fernet加密密钥
安全特性
- 存储凭据的Fernet加密
- 通过中间件自动注入凭证
- 命名空间Redis存储
- 带有健康端点的HTTP传输
测试
# Test client authentication
python test_scripts/test_client_auth.py
# Test health endpoint
curl http://localhost:8000/health```