使用说明
MCP服务器
LLM工具集成的模型上下文协议服务器。此服务器为LLM驱动的应用程序提供了一种处理工具连接的标准化方法。
设置
先决条件
- Python 3.12或更高版本
- Docker(可选)
地方发展设置
- 创建虚拟环境:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- 安装依赖项:
uv pip install -e .
- 创建一个
.env文件:
SECRET_KEY=your-secure-secret-key
SEARCH_API_KEY=your-search-api-key # Optional
SEARCH_ENGINE_ID=your-search-engine-id # Optional
- 运行服务器:
python -m mcp_server.server
Docker设置
- 使用Docker Compose构建和运行:
docker-compose up --build
服务器将在以下时间可用 http://localhost:8000
API文档
认证
除令牌端点外的所有端点都需要使用JWT令牌进行身份验证。
获取令牌
curl -X POST http://localhost:8000/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=testuser&password=testpass"
答复:
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
"token_type": "bearer"
}
工具API
列出可用工具
curl http://localhost:8000/list_tools \
-H "Authorization: Bearer YOUR_TOKEN_HERE"
答复:
{
"tools": [
{
"name": "web_search",
"description": "Search the web for information",
"type": "web_search",
"parameters": [
{
"name": "query",
"type": "string",
"description": "The search query to find information on the web",
"required": true
},
{
"name": "num_results",
"type": "integer",
"description": "Number of results to return",
"required": false,
"default": 5
}
]
}
]
}
执行工具
curl -X POST http://localhost:8000/execute_tool \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"tool_name": "web_search",
"parameters": {
"query": "example search query",
"num_results": 3
}
}'
答复:
{
"status": "success",
"result": {
"query": "example search query",
"results": [
{
"title": "Example Result",
"snippet": "This is a placeholder for actual search results",
"url": "https://example.com"
}
]
},
"error": null
}
注册新工具
curl -X POST http://localhost:8000/register_tool \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"name": "custom_tool",
"description": "A custom tool example",
"type": "function",
"parameters": [
{
"name": "param1",
"type": "string",
"description": "First parameter",
"required": true
},
{
"name": "param2",
"type": "integer",
"description": "Second parameter",
"required": false,
"default": 0
}
]
}'
答复:
{
"status": "success",
"message": "Tool custom_tool registered successfully"
}
发展
项目结构
mcp-server/
├── src/
│ └── mcp_server/
│ ├── __init__.py
│ └── server.py
├── main.py
├── pyproject.toml
├── Dockerfile
└── docker-compose.yml
添加新工具
要添加新工具,请执行以下操作:
- 在服务器代码中定义工具
- 使用注册
register_tool函数 - 在中实现工具的逻辑
execute_tool端点
环境变量
SECRET_KEY:JWT令牌生成的密钥SEARCH_API_KEY:用于web搜索的API密钥(可选)SEARCH_ENGINE_ID:用于网络搜索的搜索引擎ID(可选)HOST:服务器主机(默认值:0.0.0.0)PORT:服务器端口(默认值:8000)
安全说明
- 应在生产环境中更改默认用户凭据(testuser/testpass)
- 在生产中使用强大的SECRET_KEY
- 实施适当的用户管理和数据库存储
- 增加生产使用的速率限制
- 在生产环境中使用HTTPS
许可证
麻省理工学院
