使用说明
MCP-CLI
Rust中的MCP CLI客户端——无运行时依赖的跨平台MCP服务器交互
一个CLI,用于使用惰性后台守护进程从模型上下文协议(MCP)服务器中发现、检查和执行工具。适用于Linux、macOS和 视窗 (通过命名管道)。
这只是一个纯粹的实验。我不打算严重依赖它。使用原件https://github.com/philschmid/mcp-cli.
快速开始
# Install from source
git clone https://github.com/gtrak/mcp-cli-rs.git
cd mcp-cli-rs
cargo build --release
# Create config at ~/.config/mcp/mcp_servers.toml
cat > ~/.config/mcp/mcp_servers.toml << 'EOF'
[[servers]]
name = "filesystem"
transport = { type = "stdio", command = "npx", args = ["-y", "@modelcontextprotocol/server-filesystem", "/home/user"] }
EOF
# List available tools
./target/release/mcp list
为什么要重写?
这是对原始版本的Rust重写 基于Bun的MCP CLI.主要改进:
- Windows支持 --使用命名管道与Windows完全兼容(原始版本存在Windows进程生成问题)
- 无运行时依赖关系 --单编译二进制文件,不需要Bun/Node.js
- 交叉平台的 --Linux/macOS上的原生Unix套接字,Windows上的命名管道
- 更好的性能 --Rust的异步运行时,通过守护进程进行连接缓存
安装
先决条件
- 防锈工具链 (1.70+)
来源
# Clone and build
git clone https://github.com/gtrak/mcp-cli-rs.git
cd mcp-cli-rs
cargo build --release
# Binary will be at ./target/release/mcp
# Optional: Copy to your PATH
cp ./target/release/mcp ~/.local/bin/
平台说明
- Linux/macOS:为守护进程IPC使用Unix域套接字
- 视窗:为守护进程IPC使用命名管道(服务器连接需要守护进程模式)
用法
命令格式
工具调用支持两种样式:
# Space-separated (clearer for complex arguments)
mcp call filesystem read_file '{"path": "/etc/hosts"}'
# Slash-separated (compact)
mcp call filesystem/read_file '{"path": "/etc/hosts"}'
基本示例
# List all servers and their tools
mcp list
mcp list -d # With descriptions
mcp list --json # JSON output for scripting
# Show server information
mcp info filesystem # Server details
mcp info filesystem read_file # Tool schema
# Search for tools
mcp search "*file*" # Pattern matching across all servers
# Call a tool
mcp call filesystem read_file '{"path": "/etc/hosts"}'
mcp call filesystem/read_file < /tmp/args.json # From stdin
# Daemon control
mcp daemon # Start daemon
mcp shutdown # Stop daemon
全球旗帜
-c, --config <PATH> Custom config file path
--json Output as JSON
--no-daemon Run in direct mode (no connection caching)
--auto-daemon Auto-spawn daemon if not running (default)
--require-daemon Fail if daemon is not running
-h, --help Show help
-V, --version Show version
配置
配置文件位置(按优先级顺序):
- 指定路径
--config ~/.config/mcp/mcp_servers.toml~/.config/mcp/mcp.toml
TOML格式
[[servers]]
name = "filesystem"
transport = { type = "stdio", command = "npx", args = ["-y", "@modelcontextprotocol/server-filesystem", "/home/user"] }
[[servers]]
name = "fetch"
transport = { type = "stdio", command = "npx", args = ["-y", "@modelcontextprotocol/server-fetch"] }
[[servers]]
name = "http-server"
transport = { type = "http", url = "http://localhost:3000/mcp" }
# Optional: Global settings
concurrency_limit = 5 # Max concurrent operations
retry_max = 3 # Max retry attempts
retry_delay_ms = 1000 # Initial retry delay
timeout_secs = 1800 # Operation timeout
daemon_ttl = 60 # Daemon idle timeout in seconds
工具筛选
[[servers]]
name = "filesystem"
transport = { type = "stdio", command = "npx", args = ["-y", "@modelcontextprotocol/server-filesystem", "/"] }
# Allow only specific tools
allowed_tools = ["read_file", "list_directory"]
# Or block specific tools
disabled_tools = ["write_file", "delete_file"]
环境变量
| 变量 | 描述 |
|---|---|
MCP_NO_DAEMON=1 |
禁用守护进程(直接模式) |
MCP_DAEMON_TTL=N |
设置守护进程空闲超时(秒)(默认值:60) |
命令
list --列出服务器和工具
mcp list # List all servers with tool counts
mcp list -d # Include tool descriptions
mcp list -v # Verbose with full schemas
mcp list --json # Machine-readable JSON output
info --显示服务器或工具详细信息
mcp info <server> # Server details (transport, tools count)
mcp info <server> <tool> # Tool schema (parameters, description)
# Examples
mcp info filesystem
mcp info filesystem read_file
mcp info fetch fetch_url
call --执行工具
# With JSON argument
mcp call <server> <tool> '<json>'
mcp call <server/tool> '<json>'
# Examples
mcp call filesystem read_file '{"path": "/etc/hosts"}'
mcp call filesystem/read_file '{"path": "/etc/hosts"}'
# With arguments from stdin
echo '{"url": "https://example.com"}' | mcp call fetch fetch_url
# Flag-style arguments (auto-converted to JSON)
mcp call fetch fetch_url --url https://example.com
mcp call filesystem read_file --path /etc/hosts --limit 100
search --按图案查找工具
mcp search "*file*" # Find tools with "file" in the name
mcp search "read*" # Glob pattern matching
daemon --连接缓存
mcp daemon # Start daemon (runs in foreground)
mcp daemon --ttl 300 # Start with custom 5-minute TTL
mcp shutdown # Stop running daemon
守护进程模式:
--no-daemon:直接模式,无缓存(速度较慢但更简单)--auto-daemon:如果需要,生成守护进程(默认,推荐)--require-daemon:如果守护进程未运行,则失败
发展
设置
git clone https://github.com/gary/mcp-cli-rs.git
cd mcp-cli-rs
构建
cargo build # Debug build
cargo build --release # Optimized release build
测试
# Run library tests
cargo test --lib
# Run integration tests
cargo test --test '*'
# Run all tests
cargo test
项目结构
src/
├── cli/ # CLI parsing and command dispatch
├── client/ # MCP client implementations (stdio, HTTP)
├── config/ # Configuration loading and types
├── daemon/ # Daemon lifecycle and management
├── error/ # Error types and handling
├── format/ # Output formatting (text, JSON)
├── ipc/ # Inter-process communication (sockets/pipes)
├── server/ # MCP server protocol handling
├── shutdown/ # Graceful shutdown handling
└── transport/ # Transport abstractions
故障排除
“守护进程未运行”
启动守护进程:
mcp daemon
# Or use direct mode:
mcp --no-daemon list
“找不到配置文件”
创建配置文件:
mkdir -p ~/.config/mcp
cat > ~/.config/mcp/mcp_servers.toml << 'EOF'
[[servers]]
name = "filesystem"
transport = { type = "stdio", command = "npx", args = ["-y", "@modelcontextprotocol/server-filesystem", "/"] }
EOF
“找不到工具”
- 检查服务器名称:
mcp list - 检查工具名称:
mcp info <server> - 验证服务器是否正在运行:
mcp info <server>
“连接被拒绝”或“没有这样的文件或目录”
- 验证MCP服务器包是否已安装(例如。,
npx @modelcontextprotocol/server-filesystem) - 检查配置中的命令路径
- 对于HTTP服务器,请验证URL是否可访问:
curl http://localhost:3000/mcp
Windows特定
在Windows上,命名管道要求守护进程正在运行:
# Start daemon first
mcp daemon
# Then use in another terminal
mcp list
获取调试输出
# Enable debug logging
RUST_LOG=debug mcp list
# Check daemon logs
cat ~/.cache/mcp-cli/daemon.log
许可证
MIT许可证-请参阅 许可证 了解详情。
