CLI工具封装器

MCP CLI Wrapper是一个灵活的服务,可将任何CLI工具封装为具有类型化参数的MCP工具。

  • by pyrex41
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 pyrex41/mcp-cli

使用说明

MCP CLI包装器

一个灵活的MCP(模型上下文协议)服务器,它封装任何CLI工具,并将其子命令作为具有类型化参数的单独MCP工具公开。

特性

  • 配置驱动工具:通过JSON配置将CLI子命令定义为MCP工具
  • 后退模式:单人 run_cli 未提供配置时的工具
  • 类型安全参数:执行前对所有输入进行Zod验证
  • 灵活的参数类型:支持位置参数、标志和布尔开关
  • 保留参数顺序:标志和位置按照定义保持交错
  • 嵌套子命令:支持 git remote add 样式命令
  • 安全执行:用途 spawnSync 防止命令注入
  • 完整输出捕获:返回stdout、stderr和退出代码

安装

npm install
npm run build

用法

基本(回退模式)

在没有配置文件的情况下运行以获取单个 run_cli 接受原始参数的工具:

node dist/index.js git

这暴露了一个工具:

  • run_cli -接受 args: string[] 并执行 git <args...>

通过配置

使用配置文件运行以公开多个类型化工具:

node dist/index.js git ./config.example.json

这暴露了以下工具:

  • git_status -显示工作树状态
  • git_commit -用打字提交 message 可选 all 旗帜
  • git_add -阶段文件与 pathspec 参数
  • git_log -显示日志 oneline 和 max_count 选项
  • 还有更多。..

配置格式

创建一个包含工具定义的JSON文件:

{
  "tools": [
    {
      "name": "git_commit",
      "description": "Record changes to the repository with a commit message.",
      "subcommand": "commit",
      "parameters": [
        {
          "name": "message",
          "type": "string",
          "description": "The commit message.",
          "flag": "-m",
          "required": true
        },
        {
          "name": "all",
          "type": "boolean",
          "description": "Automatically stage all modified files.",
          "flag": "--all",
          "required": false
        }
      ]
    }
  ]
}

参数配置

每个参数支持:

字段 类型 描述
name string 参数名称(在架构中使用)
type "string" "number"
description string 向AI代理显示的描述
flag string(可选) CLI标志(例如。, -m, --force).如果省略,则参数为位置
required boolean 参数是否为必填项

参数顺序

参数在中处理 申报顺序。这允许您定义标记必须位于位置参数之前的工具:

{
  "name": "docker_run",
  "subcommand": "run",
  "parameters": [
    { "name": "interactive", "flag": "-it", "type": "boolean", "required": false },
    { "name": "image", "type": "string", "required": true },
    { "name": "command", "type": "string", "required": false }
  ]
}

这产生了: docker run -it ubuntu bash (位置标记前的标记)

嵌套子命令

使用空格 subcommand 对于嵌套命令:

{
  "name": "git_remote_add",
  "subcommand": "remote add",
  "parameters": [
    { "name": "name", "type": "string", "required": true },
    { "name": "url", "type": "string", "required": true }
  ]
}

MCP客户端配置

克劳德桌面版

添加到您的Claude桌面配置(~/Library/Application Support/Claude/claude_desktop_config.json):

{
  "mcpServers": {
    "git": {
      "command": "node",
      "args": ["/path/to/mcp-cli-wrapper/dist/index.js", "git", "/path/to/config.json"]
    }
  }
}

克劳德代码

添加到MCP设置中:

{
  "mcpServers": {
    "git": {
      "command": "node",
      "args": ["./dist/index.js", "git", "./config.example.json"]
    }
  }
}

配置示例

Git(包括在内)

看 config.example.json 使用8个工具进行完整的Git配置。

Docker(示例)

{
  "tools": [
    {
      "name": "docker_ps",
      "description": "List running containers",
      "subcommand": "ps",
      "parameters": [
        { "name": "all", "type": "boolean", "flag": "-a", "description": "Show all containers", "required": false }
      ]
    },
    {
      "name": "docker_run",
      "description": "Run a container",
      "subcommand": "run",
      "parameters": [
        { "name": "detach", "type": "boolean", "flag": "-d", "description": "Run in background", "required": false },
        { "name": "name", "type": "string", "flag": "--name", "description": "Container name", "required": false },
        { "name": "image", "type": "string", "description": "Image to run", "required": true }
      ]
    }
  ]
}

安全

  • 无外壳插值:参数直接通过传递 spawnSync,防止命令注入
  • 输入验证:执行前使用Zod模式验证所有参数
  • 仅限本地执行:命令在具有用户权限的本地计算机上运行

发展

# Install dependencies
npm install

# Build
npm run build

# Run in development
npm run dev git ./config.example.json

许可证

麻省理工学院