用于命令行工具的MCP Wrapper - 通过简单的YAML配置,将命令行工具暴露为MCP(模型上下文协议)服务器。
MCP Wrapper允许您轻松创建封装命令行工具的MCP服务器。在简单的YAML配置文件中定义您的工具,并将其作为MCP工具暴露出来,以便AI助手和其他MCP客户端使用。
@modelcontextprotocol/sdknpm install -g mcp-wrapper
npm install mcp-wrapper
git clone <repository-url>
cd mcp-wrapper
npm install
npm run build
mcp-wrapper.yaml):tools:
calc:
description: "基于bc命令行的数学计算器"
input:
type: object
properties:
equation:
description: "与bc兼容的方程"
type: string
required: [equation]
cmd: "echo {{equation}} | bc -l"
mcp-wrapper --config mcp-wrapper.yaml
# 使用调试日志查看安全操作
mcp-wrapper --config mcp-wrapper.yaml --log-level debug
mcp-wrapper [选项]
选项:
-c, --config <文件> 配置文件路径(默认:"mcp-wrapper.yaml")
--timeout <秒数> 命令执行超时时间(默认:"30")
--name <名称> 服务器名称
--version-server <版本> 服务器版本
--log-level <级别> 日志级别:error|warn|info|debug(默认:"info")
-V, --version 显示版本号
-h, --help 显示帮助信息
注意:目前仅支持stdio传输。
# 使用默认配置文件启动
mcp-wrapper
# 使用自定义配置启动
mcp-wrapper --config tools.yaml
# 使用调试日志启动
mcp-wrapper --config tools.yaml --log-level debug
# 使用自定义服务器名称和超时时间启动
mcp-wrapper --config tools.yaml --name "我的工具服务器" --timeout 60
tools:
<工具名>:
name: <可选显示名称>
description: <工具描述>
input:
type: object
properties:
<属性名>:
type: <属性类型>
description: <属性描述>
default: <可选默认值>
required: [<必需属性名>]
cmd: <包含mustache模板的shell命令>
为了跨平台兼容性,您可以为不同的操作系统指定不同的命令:
tools:
file_list:
description: "列出目录中的文件"
input:
type: object
properties:
path:
type: string
description: "目录路径"
default: "."
cmd:
win: "dir {{path}}"
unix: "ls -la {{path}}"
macos: "ls -la {{path}}"
default: "ls {{path}}"
平台键:
win: Windows (platform() === 'win32')macos: macOS (platform() === 'darwin')unix: Unix/Linux (platform() === 'linux' or 'freebsd')default: 任何平台的回退支持的属性类型:
string: 文本输入number: 浮点数integer: 整数boolean: 真/假值array: 值列表属性选项:
description: 属性的帮助文本default: 如果未提供则使用的默认值enum: 允许的值列表minimum/maximum: 数字约束minLength/maxLength: 字符串长度约束pattern: 正则表达式验证命令支持使用输入参数的Mustache模板:
cmd: "echo {{expression}} | bc -l"
模板特性:
{{变量}}{{对象.属性}}参见**示例/**目录,获取包括以下内容的现成配置示例:
您也可以将MCP Wrapper作为库来使用:
import { MCPWrapperServer, loadConfig } from 'mcp-wrapper';
// 加载配置
const config = loadConfig('./my-tools.yaml');
// 创建服务器选项(默认使用stdio传输)
const options = {
configFile: './my-tools.yaml',
name: '我的自定义服务器',
version: '1.0.0'
};
// 创建并启动服务器
const server = new MCPWrapperServer(config, options);
await server.start();
此工具执行shell命令,存在固有的安全风险。用户负责测试配置并实施适当的安全措施。
📖 查看文档/安全.md以获取完整的安全配置指南,包括:
始终使用潜在恶意输入测试您的工具:
# 使用调试日志查看安全净化
mcp-wrapper --config tools.yaml --log-level debug
# 首先使用严格的安全部署级别测试
security:
level: strict
auditLogging: true
安全测试示例输入:
; rm -rf /../../../etc/passwd$(危险命令)|&;$()<>启用调试日志以查看详细的执行信息:
mcp-wrapper --config tools.yaml --log-level debug
这将显示:
git clone <repository-url>
cd mcp-wrapper
npm install
npm run dev # 开发模式
npm run build # 生产构建
npm test # 运行测试
此工具是在混合模式下开发的,其中核心架构和逻辑是手工制作的,而实现的一些部分是由各种LLM模型和工具辅助创建的。
MPL-2.0许可证 - 详情见LICENSE文件。