网关cli
用于与进行交互的命令行工具 主控程序 服务器。它发现注册的MCP服务器暴露的工具,缓存它们的模式,并允许您直接从终端调用它们——完全支持管道、脚本和与其他CLI工具的组合。
安装
Homebrew(macOS和Linux)```bash
brew install VincentK1991/tap/gateway-cli
### curl(macOS和Linux)```bash
curl -fsSL https://raw.githubusercontent.com/VincentK1991/mcp-gateway-cli/main/install.sh | bash
这会检测您的操作系统和架构,从最新的GitHub版本下载正确的二进制文件,验证校验和,并安装到 /usr/local/bin.
要安装特定版本,请执行以下操作:```bash
VERSION=v1.2.0 curl -fsSL https://raw.githubusercontent.com/VincentK1991/mcp-gateway-cli/main/install.sh | bash
### 去安装(需要Go 1.23+)```bash
go install github.com/VincentK1991/mcp-gateway-cli@latest
验证安装```bash
gateway-cli --version
______________________________________________________________________
## 注册MCP服务器
MCP服务器已在中注册 `~/.gateway-cli/config.yaml`。如果文件不存在,请创建该文件:```yaml
mcps:
calculator:
url: http://localhost:8000/mcp
matrix:
url: http://localhost:8001/mcp
每个条目都需要一个名称(用作CLI子命令)和MCP服务器的URL /mcp 终点。
对于需要身份验证或自定义标头的MCP服务器,添加 headers 块:```yaml
mcps:
calculator:
url: http://localhost:8000/mcp
my-api:
url: https://api.example.com/mcp
headers:
Authorization: "Bearer mytoken"
another-api:
url: https://other.example.com/mcp
headers:
Authorization: "Bearer ${MY_API_TOKEN}" # reads from environment variable
X-Tenant-Id: "acme"
标头值支持环境变量扩展--使用 `${VAR}` 避免将机密直接存储在配置文件中。在运行任何命令之前,在shell中设置变量:```bash
export MY_API_TOKEN="your-secret-token"
gateway-cli my-api some-tool --flag value
编辑配置后,刷新模式缓存:```bash
gateway-cli schema refresh
您可以检查发现的内容:```bash
gateway-cli schema info
``````
Last fetched : 2026-03-16 22:29:12
MCP servers : 2
Total tools : 2
calculator (1 tools)
calculate Perform a basic integer calculation.
matrix (1 tools)
process_matrix Perform matrix operations on 1D/2D arrays or scalars.
运行工具
工具调用方式如下: gateway-cli [flags]
每个工具的标志都是根据其输入模式自动生成的。使用 --help 查看可用标志:```bash
gateway-cli calculator calculate --help
gateway-cli matrix process_matrix --help
**基本示例:**```bash
# Integer arithmetic
gateway-cli calculator calculate --a 10 --b 5 --operation add
gateway-cli calculator calculate --a 10 --b 3 --operation mul
gateway-cli calculator calculate --a 10 --b 0 --operation div # returns error gracefully
# Vector operations
gateway-cli matrix process_matrix --a '[1,2,3]' --b '[4,5,6]' --operation add
# Matrix dot product
gateway-cli matrix process_matrix --a '[[1,2],[3,4]]' --b '[[5,6],[7,8]]' --operation dot
合成工具输出
使用 --json / -j 标记以剥离MCP信封并仅返回结构化结果。这使得将工具链接在一起变得简单明了:```bash
Step 1: compute a scale factor
Step 2: use it as input to the matrix tool
SCALE=$(gateway-cli --json calculator calculate --a 3 --b 4 --operation mul | jq '.result | floor')
gateway-cli --json matrix process_matrix --a '[[1,2],[3,4]]' --b "$SCALE" --operation mul
``````json
{
"error": null,
"result": [[12, 24], [36, 48]]
}
多级管道:```bash
# Chain three calculator calls
A=$(gateway-cli --json calculator calculate --a 2 --b 3 --operation add | jq '.result | floor')
B=$(gateway-cli --json calculator calculate --a 4 --b 2 --operation mul | jq '.result | floor')
gateway-cli --json calculator calculate --a "$A" --b "$B" --operation add | jq '.result'
# → 13
与其他CLI工具结合使用
输出是标准的JSON,因此它与Unix工具链自然集成。
jq——提取和转换:```bash
Extract just the result value
gateway-cli --json calculator calculate --a 10 --b 3 --operation mul | jq '.result'
Extract matrix rows
gateway-cli --json matrix process_matrix --a '[[1,2],[3,4]]' --b '[[5,6],[7,8]]' --operation dot \
| jq '.result[]'
**将输出保存到文件:**```bash
gateway-cli --json matrix process_matrix --a '[[1,2],[3,4]]' --b '[[5,6],[7,8]]' --operation dot \
> result.json
在shell脚本中使用:```bash
!/bin/bash
for op in add sub mul; do
echo -n "$op: "
gateway-cli --json calculator calculate --a 10 --b 3 --operation "$op" | jq '.result'
done
**插入其他工具(`fx`, `python`等等):**```bash
# Interactive JSON explorer
gateway-cli --json matrix process_matrix --a '[[1,2],[3,4]]' --b '[[5,6],[7,8]]' --operation dot | fx
# Process in Python
gateway-cli --json calculator calculate --a 10 --b 3 --operation add \
| python3 -c "import sys, json; d = json.load(sys.stdin); print(f'Result is {d[\"result\"]}')"
架构缓存管理```bash
gateway-cli schema refresh # force re-fetch from all MCP servers
gateway-cli schema info # show cached MCPs and tools
gateway-cli schema invalidate # delete the cache
每个命令上都有可用的标志:
|标志|描述|
|---|---|
| `--json`, `-j` |仅输出结构化内容(无MCP信封)|
| `--text`, `-t` |将第一个文本内容项以纯字符串形式输出|
| `--refresh-schema` |运行命令前重新获取架构|
| `--offline` |仅使用缓存的架构,切勿联系MCP服务器|
| `--config` |自定义配置文件的路径|
### 检查并更新您的版本```bash
# Show installed version
gateway-cli --version
# Homebrew
brew upgrade gateway-cli
# curl (re-runs the installer, overwrites the binary)
curl -fsSL https://raw.githubusercontent.com/VincentK1991/mcp-gateway-cli/main/install.sh | bash
# Go install
go install github.com/VincentK1991/mcp-gateway-cli@latest
gateway-cli还会在后台自动检查新版本(最多每天一次),并在有新版本可用时向stderr打印提醒。
