MCP 服务器指南
安装
要安装MCP Server,请按照以下步骤操作:
先决条件
- 操作系统:Linux、macOS 或 Windows(推荐使用 Linux)
- Python 版本:3.6 或更高版本
- Git:用于克隆代码库
克隆仓库
运行以下命令来克隆 MCP Server 仓库:
git clone https://github.com/yourusername/mcp-server.git
cd mcp-server
安装依赖项
使用 pip 安装所需的 Python 包:
pip install -r requirements.txt
启动服务器
安装完成后,运行以下命令启动 MCP Server:
python3 server.py --help
配置
MCP Server 的配置通过 config.json
文件进行。该文件位于项目根目录下。
示例配置
{
"server": {
"host": "0.0.0.0",
"port": 5000,
"debug": true
},
"api_key": "your_api_key_here",
"workspace_id": "your_workspace_id_here"
}
环境变量
MCP Server 支持通过环境变量进行配置。以下是常用的环境变量:
MCP_API_KEY
:你的 API 密钥
MCP_WORKSPACE_ID
:你的工作区 ID
MCP_DEBUG
:启用调试模式(true 或 false)
命令行选项
MCP Server 提供以下命令行选项:
启动服务器
python3 server.py --host HOST --port PORT [--debug]
参数
--host
: 服务器绑定的主机地址(默认:0.0.0.0
)
--port
: 服务器监听的端口(默认:5000
)
--debug
: 启用调试模式(默认:false)
日志记录
python3 server.py --log-file LOG_PATH [--log-level LEVEL]
参数
LOG_PATH
: 日志文件的路径
LEVEL
: 日志级别(DEBUG, INFO, WARNING, ERROR, CRITICAL,默认:INFO)
示例 curl 请求
创建会话
curl -X POST "http://localhost:5000/api/v1/conversations" \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"title": "系统思考对话",
"model": "claude-3-5-sonnet-20240620"
}'
发送消息
curl -X POST "http://localhost:5000/api/v1/conversations/{conversation_id}/messages" \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"content": "解释 MCP 协议的详细信息",
"mentions": [{
"configurationId": "your_agent_id_here",
"context": {
"时区": "Europe/Berlin",
"模型设置": {"提供者": "anthropic"}
}
}],
"context": {
"时区": "Europe/Berlin"
}
}'
检索消息
curl -X POST "http://localhost:5000/api/v1/conversations/{conversation_id}/messages" \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"content": "返回所有消息",
"mentions": [{
"configurationId": "your_agent_id_here",
"context": {
"时区": "Europe/Berlin",
"模型设置": {"提供者": "anthropic"}
}
}]
}'
错误处理
MCP Server 支持以下错误代码:
400 Bad Request
:无效请求
401 Unauthorized
:未授权访问
404 Not Found
:资源未找到
500 Internal Server Error
:服务器内部错误
贡献指南
提交问题
在 GitHub 仓库中提交 issue 来报告问题或提出建议。
提交功能请求
通过 Pull Request 提交新的功能或改进。
注意事项
- 确保 API 密钥和工作区 ID 的安全性,避免泄露。
- 在生产环境中禁用调试模式。
关于 MCP 服务器
MCP Server 是一个用于管理任务控制协议(Mission Control Protocol)的工具。它提供了创建会话、发送消息和检索消息的功能,并支持通过 curl 请求进行交互。
MCP 服务器 API 文档
简介
MCP 服务器提供 RESTful API,允许客户端与 MCP 服务进行交互。以下是可用的 API 端点及其详细说明。
创建会话
POST /api/v1/conversations
描述
创建一个新的对话会话。
请求头
Authorization
: Bearer Token(必填)
Content-Type
: application/json
请求体
{
"title": "string",
"model": "string"
}
响应
{
"id": "string",
"title": "string",
"created_at": "datetime"
}
发送消息
POST /api/v1/conversations/{conversation_id}/messages
描述
向指定对话会话发送一条新消息。
请求头
Authorization
: Bearer Token(必填)
Content-Type
: application/json
请求体
{
"content": "string",
"mentions": array
}
响应
{
"id": "string",
"content": "string",
"sent_at": "datetime"
}
检索消息
POST /api/v1/conversations/{conversation_id}/messages
描述
检索指定对话会话中的所有消息。
请求头
Authorization
: Bearer Token(必填)
Content-Type
: application/json
请求体
{
"content": "string",
"mentions": array
}
响应
[
{
"id": "string",
"content": "string",
"sent_at": "datetime"
},
...
]
示例
创建会话
curl -X POST "http://localhost:5000/api/v1/conversations" \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"title": "系统思考对话",
"model": "claude-3-5-sonnet-20240620"
}'
发送消息
curl -X POST "http://localhost:5000/api/v1/conversations/{conversation_id}/messages" \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"content": "解释 MCP 协议的详细信息",
"mentions": [{
"configurationId": "your_agent_id_here",
"context": {
"时区": "Europe/Berlin",
"模型设置": {"提供者": "anthropic"}
}
}]
}'
检索消息
curl -X POST "http://localhost:5000/api/v1/conversations/{conversation_id}/messages" \
-H "Authorization: Bearer your_api_key_here" \
-H "Content-Type: application/json" \
-d '{
"content": "返回所有消息",
"mentions": [{
"configurationId": "your_agent_id_here",
"context": {
"时区": "Europe/Berlin",