内容
# MCP Applications Wrapper
一个用于各种应用程序的MCP(模型上下文协议)服务器包装器,使用FastMCP构建。该工具允许您通过MCP接口包装Python、Node.js和自定义应用程序,使其可供Claude等AI助手访问。
## 特性
- 通过MCP服务器运行Python和Node.js应用程序
- 使用集中配置管理多个应用程序
- 支持不同的部署模式:
- 本地(子进程)[实验性 - 进行中]
- Docker(容器化)[实验性 - 进行中]
- 远程(SSE部署)
- 自动为Python应用程序设置虚拟环境
- 从requirements.txt安装依赖项
- 命令行接口用于创建、验证和运行配置
- 丰富的日志记录,提供详细输出
## 安装
### 先决条件
- Python 3.10+
- Docker(用于Docker部署模式)
- SSH访问(用于远程部署模式)
### 设置
1. 克隆仓库:
```bash
git clone https://github.com/gaord/MCP-Applications-Wrapper.git
cd MCP-Applications-Wrapper
```
2. 创建虚拟环境并安装依赖项:
```bash
python -m venv venv
source venv/bin/activate # 在Windows上: venv\Scripts\activate
pip install -r requirements.txt
```
## 配置
创建一个配置文件(YAML或JSON)以定义您的应用程序:
```bash
python main.py create-config config.yaml
```
这将创建一个示例配置文件,您可以对其进行修改。
每个应用程序的配置包含以下属性:
- `name`: 应用程序的显示名称
- `description`: 应用程序功能的描述
- `working_directory`: 应用程序所在的目录
- `interpreter_type`: 解释器类型(`python`、`node`或`custom`)
- `interpreter_path`: (可选)解释器可执行文件的路径
- `command`: 要执行的命令
- `args`: (可选)应用程序的默认参数
- `env_vars`: (可选)要设置的环境变量
- `resources_limit`: (可选)CPU和内存限制
- `timeout`: (可选)应用程序执行的超时时间(秒)
示例配置:
```yaml
applications:
echo_app:
name: Echo Service
description: 简单的服务,回显输入
working_directory: /path/to/echo_app
interpreter_type: python
command: echo.py
timeout: 120 # 2分钟超时
hello_app:
name: Hello World
description: 一个简单的Hello World应用程序
working_directory: examples/hello_app
interpreter_type: node
command: hello.js
timeout: 60 # 1分钟超时
deployment_mode: local
# Docker配置(用于deployment_mode: docker)
docker_config:
base_image: python:3.11-slim
network: bridge
volumes:
/host/path: /container/path
# 远程配置(用于deployment_mode: remote)
# remote_config:
# host: example.com
# port: 22
# username: user
# password: pass
# # 或使用密钥认证
# # key_path: /path/to/private/key
# deploy_path: /remote/path
```
### 配置参数
#### 应用程序配置
- `name`: 应用程序的显示名称
- `description`: 可选描述
- `working_directory`: 应用程序所在的目录
- `interpreter_type`: 解释器类型(`python`、`node`或`custom`)
- `interpreter_path`: 解释器的路径(`custom`类型必需,其他类型可选)
- `command`: 要运行的主脚本或命令
- `args`: 可选的参数列表
- `env_vars`: 可选的环境变量
- `resources_limit`: 可选的资源限制(用于Docker部署)
- `timeout`: 可选的应用程序执行超时时间(秒)
#### 部署模式
- `local`: 在本地机器上作为子进程运行应用程序(注意:此模式为实验性,尚未经过全面测试)
- `docker`: 在Docker容器中运行应用程序(注意:此模式为实验性,尚未经过全面测试)
- `remote`: 通过SSE在远程服务器上部署和运行应用程序
## 使用
### 验证配置
在运行服务器之前,您可以验证您的配置:
```bash
python main.py validate config.yaml
```
### 运行服务器
启动MCP服务器:
```bash
python main.py run config.yaml
```
根据需要修改现有的config.yaml,添加选项自定义:
```bash
python main.py run examples/config.yaml --transport sse --log-level DEBUG --port 8001
```
然后您可以通过以下URL访问服务器:
```bash
http://localhost:8001/sse
```
或者使用以下命令测试服务器:
```bash
python tests/test_sse_client.py --url http://localhost:8001/sse --language cn --timeout 1800
```
> 注意:FastMCP默认在8000端口自动运行。
### MCP工具
通过服务器可以使用以下MCP工具:
- `list_applications`: 列出所有配置的应用程序及其状态
- `get_deployment_mode`: 获取当前的部署模式
- `get_application_help`: 获取应用程序的帮助信息,使用--help参数
- `execute_application`: 执行应用程序并可选传递参数
## Docker部署(进行中)
要使用Docker部署模式(注意:当前为实验性,进行中):
1. 在配置文件中设置`deployment_mode: docker`
2. 使用适当的设置配置`docker_config`部分
3. 确保您的系统上已安装并运行Docker
系统将自动:
- 为您的应用程序构建容器
- 按照配置中指定的方式挂载卷
- 根据您的设置配置网络
## 远程部署
要使用远程部署模式:
1. 在配置文件中设置`deployment_mode: remote`
2. 配置`remote_config`部分,提供SSE连接详细信息
3. 确保您可以访问远程服务器的网络
系统将:
- 通过SSE连接到远程服务器
- 在远程机器上执行应用程序
- 将结果流回客户端
## 开发
### 添加新的解释器类型
要添加对新解释器类型的支持:
1. 更新`config/settings.py`中的`InterpreterType`枚举
2. 修改`ApplicationRunner`类以适当地处理新类型
### 贡献
欢迎贡献!请随时提交Pull Request。
## 许可证
本项目根据MIT许可证授权 - 详细信息请参见LICENSE文件。
## 致谢
- [FastMCP](https://github.com/jlowin/fastmcp) - 构建MCP服务器和客户端的快速、Pythonic方式