MCP服务器作为HTTP核心
一种高性能的基于Rust的HTTP服务器,为模型上下文协议(MCP)服务器提供REST API接口。这是可以通过特定语言的实现进行扩展的核心库。
特性
- 高性能:采用Rust和Axum构建,实现最佳性能
- 运行时抽象:支持Node.js、Python和Go MCP服务器
- 认证:具有可配置安全性的承载令牌身份验证
- 流程管理:强大的MCP服务器进程通信
- 配置:灵活的基于JSON的配置系统
- 库管理:自动克隆和构建Git存储库
- 可观测性:具有跟踪功能的全面日志记录
建筑
该核心库提供:
- HTTP服务器:基于Axum的REST API服务器
- 认证:承载令牌验证中间件
- 运行时界面:不同语言运行时的抽象
- 过程通信:与MCP服务器异步stdin/stdout通信
- 配置管理:服务器配置的JSON模式
快速开始
基本用法
选项1:使用.env文件(推荐)
# Copy the example environment file
cp .env.example .env
# Edit .env file with your configuration
# Uncomment and set the values you need:
# HTTP_API_KEY=your-secret-api-key-here
# MCP_CONFIG_FILE=mcp_servers.config.json
# MCP_SERVER_NAME=your-server
# PORT=3000
# Run the server
cargo run选项2:直接使用环境变量
# Set environment variables
export MCP_CONFIG_FILE=mcp_servers.config.json
export MCP_SERVER_NAME=your-server
export PORT=3000
# Run the server
cargo run配置
创建 mcp_servers.config.json:
{
"version": "1.0",
"servers": {
"example-server": {
"repository": "https://github.com/user/mcp-server",
"build_command": "npm install && npm run build",
"command": "node",
"args": ["dist/index.js"],
"env": {
"CUSTOM_VAR": "value"
}
}
}
}环境变量
可以使用环境变量配置服务器。为方便起见,您可以使用 .env 文件:
# Copy the example file and customize it
cp .env.example .env可用环境变量:
HTTP_API_KEY:用于身份验证的承载令牌(可选)DISABLE_AUTH:设置为“true”以禁用身份验证(默认值:“false”)MCP_CONFIG_FILE:配置文件的路径(默认:“mcp_servers.config.json”)MCP_SERVER_NAME:要使用的配置中的服务器名称(默认值:“redmine”)PORT:HTTP服务器端口(默认值:3000)RUST_LOG:日志级配置(默认值:“mcp_server_as_http_core=debug”)
备注:直接在shell中设置的环境变量将覆盖 .env 文件。
API使用
认证
在授权标头中包含承载令牌:
curl -X POST http://localhost:3000/api/v1 \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{"command": "{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"tools/list\", \"params\": {}}"}'示例请求
curl -X POST http://localhost:3000/api/v1 \
-H "Content-Type: application/json" \
-d '{"command": "{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"tools/list\", \"params\": {}}"}'发展
建筑
# Build the project
cargo build
# Run tests
cargo test
# Run with debug logging
RUST_LOG=debug cargo run测试
# Run all tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Run specific test
cargo test test_name运行时支持
Node.js运行时
- npm/yarn依赖自动安装
- TypeScript编译支持
- 环境变量传递
Python运行时(计划中)
- 虚拟环境管理
- pip/poetry依赖安装
- Python 3.8+支持
Go运行时(计划)
- Go模块支持
- 自动依赖解析
- 交叉编译支持
特定语言存储库
此核心库旨在供特定语言的存储库使用:
mcp-server-as-http-node:Node.js/TypeScript优化实现mcp-server-as-http-python:Python优化实现mcp-server-as-http-go:优化实施
每种语言特定的存储库都提供:
- 优化的Docker镜像
- 特定语言的依赖关系管理
- 特定于运行时的优化
- 专用配置选项
错误处理
该库提供全面的错误处理,并包含详细的错误消息:
AuthenticationError:身份验证失败ConfigurationError:配置解析问题ProcessError:MCP服务器通信问题RuntimeError:运行时设置失败HttpServerError:HTTP服务器问题
日志记录
带跟踪的结构化日志记录:
# Set log level
export RUST_LOG=debug
# Filter by module
export RUST_LOG=mcp_server_as_http_core=debug
# Multiple filters
export RUST_LOG=mcp_server_as_http_core=debug,axum=info贡献
- 分叉存储库
- 创建要素分支
- 为新功能编写测试
- 确保所有测试通过
- 提交拉取请求
许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
