MCP服务器

多集成MCP(模型上下文协议)服务器,提供对各种业务工具的安全API访问。
特性
- 多重集成:Salesforce、HubSpot等
- 安全认证:通过NextJS后端进行基于API密钥的身份验证
- RESTful API:标准HTTP/JSON接口
- Docker支持:使用Docker Compose完全容器化
- CI/CD管道:通过GitHub Actions自动部署到AWS EC2
- 可扩展:添加新集成很简单
- 生产就绪:健康检查、日志记录和错误处理
快速链接
建筑
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ GitHub │────────▶│GitHub Actions│────────▶│ AWS ECR │
│ Repository │ Push │ (CI/CD) │ Build │ (Images) │
└─────────────┘ └──────────────┘ └─────────────┘
│
│ Pull
▼
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ Client │────────▶│ MCP Server │────────▶│ Salesforce │
│ Application │ API │ (Docker) │ Auth │ HubSpot │
└─────────────┘ └──────────────┘ └─────────────┘
│
│ Verify
▼
┌──────────────┐
│ NextJS │
│ Backend │
└──────────────┘快速开始
先决条件
- Python 3.11+
- 码头工人
- AWS帐户(用于生产)
- GitHub帐户(用于CI/CD)
地方发展
- 克隆和设置:
cd mcp-server
cp .env.example .env
# Edit .env with your configuration- 安装依赖项:
pip install -r requirements.txt- 运行服务器:
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000- 访问API:
- 服务器:http://localhost:8000
- 健康检查:http://localhost:8000/health
- API文件:http://localhost:8000/docs
Docker部署
- 塑造形象:
docker build -t mcp-server .- 运行容器:
docker run -d \
-p 8000:8000 \
-e NEXTJS_API_URL=https://yourdomain.com/api/verify-key \
-e ALLOWED_ORIGINS=https://yourdomain.com \
--name mcp-server \
mcp-server- 检查日志:
docker logs -f mcp-serverAPI使用
认证
所有请求都需要 X-API-Key 头球
curl -X POST http://localhost:8000/mcp/salesforce \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{
"method": "soql",
"params": {
"query": "SELECT Id, Name FROM Account LIMIT 10"
},
"credentials": {
"username": "user@example.com",
"password": "password",
"security_token": "token"
}
}'可用集成
赛富时
方法:
soql-执行SOQL查询get_record-获取单个记录describe_object-获取对象元数据list_objects-列出所有对象search-执行SOSL搜索
示例:
{
"method": "soql",
"params": {
"query": "SELECT Id, Name FROM Account"
},
"credentials": {
"username": "user@salesforce.com",
"password": "password123",
"security_token": "securitytoken"
}
}集客营销
方法:
get_contacts-列出联系人get_contact-获取单一联系人search_contacts-搜索联系人get_companies-列出公司get_company-获得单一公司
示例:
{
"method": "get_contacts",
"params": {
"limit": 50
},
"credentials": {
"api_key": "your-hubspot-api-key"
}
}添加新集成
- 创建新文件夹:
app/integrations/yourintegration/
- 创建
__init__.py和server.py
- 实现你的MCP类:
from app.integrations.base import BaseMCP
class YourIntegrationMCP(BaseMCP):
def __init__(self):
super().__init__("yourintegration")
def validate_credentials(self, credentials: Dict) -> bool:
# Validate credentials
return True
async def execute(self, method: str, params: Dict, credentials: Dict) -> Any:
# Implement your methods
pass- 注册
app/main.py:
from app.integrations.yourintegration.server import YourIntegrationMCP
yourintegration_mcp = YourIntegrationMCP()
integrations = {
"salesforce": salesforce_mcp,
"hubspot": hubspot_mcp,
"yourintegration": yourintegration_mcp,
}环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
NEXTJS_API_URL | NextJS身份验证端点 | 必填 |
ALLOWED_ORIGINS | CORS允许的来源 | http://localhost:3000 |
AUTH_TIMEOUT | 身份验证超时(秒) | 5.0 |
PORT | 服务器端口 | 8000 |
HOST | 服务器主机 | 0.0.0.0 |
LOG_LEVEL | 日志记录级别 | info |
测试
# Run tests
pytest
# With coverage
pytest --cov=app tests/生产部署
带CI/CD的AWS EC2
看 部署.md 获取完整的AWS EC2部署指南,其中包含自动化的GitHub Actions CI/CD。
快速启动:
# 1. Setup EC2 instance
./scripts/setup-ec2.sh
# 2. Create ECR repository
./scripts/create-ecr.sh
# 3. Configure GitHub Secrets (see DEPLOYMENT.md)
# 4. Push to GitHub - auto-deploys!
git push origin main局部测试
./scripts/local-deploy.sh铁路/渲染
- 连接你的Git仓库
- 设置环境变量
- 自动部署
安全
- 所有凭据均按请求传递(不存储)
- API密钥通过NextJS后端验证
- CORS强制执行
- HTTPS建议用于生产
- 建议速率限制(使用nginx/ALB)
故障排除
连接被拒绝:
- 检查服务器是否正在运行
- 验证端口是否正确
- 检查防火墙规则
认证失败:
- 验证NEXTJS_API_URL是否正确
- 检查API密钥是否有效
- 确保NextJS后端正在运行
集成错误:
- 验证凭据是否正确
- 检查集成特定要求
- 查看响应中的错误消息
许可证
麻省理工学院
支持
有关问题和疑问,请打开GitHub问题。
