mcp-cli-tool
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesemcp-cli Tool
mcp-cli工具
Overview
概述
mcp-cli- On-demand schema loading - Only fetch tool schemas when needed, saving AI context tokens
- Shell composability - JSON output for piping with , chaining, and scripting
jq - Connection pooling - Lazy-spawn daemon keeps connections warm (60s idle timeout)
- Universal support - Works with both stdio and HTTP MCP servers
- Tool filtering - Allow/disable specific tools per server via config
Use cases:
- AI agents accessing MCP tools without loading full schemas into context
- Shell scripts automating MCP server interactions
- Discovering and exploring available MCP capabilities
mcp-cli- 按需加载schema - 仅在需要时获取工具schema,节省AI上下文令牌
- Shell可组合性 - 输出JSON格式,可与配合使用管道、链式调用和脚本编写
jq - 连接池 - 延迟启动的守护进程保持连接活跃(空闲超时时间60秒)
- 通用支持 - 兼容stdio和HTTP类型的MCP服务器
- 工具过滤 - 通过配置为每个服务器启用/禁用特定工具
使用场景:
- AI Agent无需加载完整schema即可访问MCP工具
- Shell脚本自动化MCP服务器交互
- 发现和探索可用的MCP功能
Installation
安装
Quick Install (Recommended)
快速安装(推荐)
bash
curl -fsSL https://raw.githubusercontent.com/philschmid/mcp-cli/main/install.sh | bashbash
curl -fsSL https://raw.githubusercontent.com/philschmid/mcp-cli/main/install.sh | bashManual Install (Requires Bun)
手动安装(需要Bun)
bash
bun install -g https://github.com/philschmid/mcp-clibash
bun install -g https://github.com/philschmid/mcp-cliVerify Installation
验证安装
bash
mcp-cli --versionbash
mcp-cli --versionConfiguration
配置
Config File Location
配置文件位置
Create in one of these locations (searched in order):
mcp_servers.json- Path from environment variable
MCP_CONFIG_PATH - Path from CLI argument
-c/--config - (current directory)
./mcp_servers.json ~/.mcp_servers.json~/.config/mcp/mcp_servers.json
在以下任一位置创建文件(按顺序查找):
mcp_servers.json- 环境变量指定的路径
MCP_CONFIG_PATH - CLI参数指定的路径
-c/--config - (当前目录)
./mcp_servers.json ~/.mcp_servers.json~/.config/mcp/mcp_servers.json
Basic Config Format
基础配置格式
Compatible with Claude Desktop, Gemini, and VS Code:
json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"."
]
},
"github": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-github"
],
"env": {
"GITHUB_TOKEN": "${GITHUB_TOKEN}"
}
},
"deepwiki": {
"url": "https://mcp.deepwiki.com/mcp"
}
}
}兼容Claude Desktop、Gemini和VS Code:
json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"."
]
},
"github": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-github"
],
"env": {
"GITHUB_TOKEN": "${GITHUB_TOKEN}"
}
},
"deepwiki": {
"url": "https://mcp.deepwiki.com/mcp"
}
}
}Advanced Config Options
高级配置选项
json
{
"mcpServers": {
"local-server": {
"command": "node",
"args": ["./server.js"],
"env": {
"API_KEY": "${API_KEY}"
},
"cwd": "/path/to/directory",
"allowedTools": ["read_*", "list_*"],
"disabledTools": ["delete_*"]
},
"remote-server": {
"url": "https://mcp.example.com",
"headers": {
"Authorization": "Bearer ${TOKEN}"
}
}
}
}json
{
"mcpServers": {
"local-server": {
"command": "node",
"args": ["./server.js"],
"env": {
"API_KEY": "${API_KEY}"
},
"cwd": "/path/to/directory",
"allowedTools": ["read_*", "list_*"],
"disabledTools": ["delete_*"]
},
"remote-server": {
"url": "https://mcp.example.com",
"headers": {
"Authorization": "Bearer ${TOKEN}"
}
}
}
}Environment Variable Substitution
环境变量替换
Use syntax anywhere in config:
${VAR_NAME}json
{
"mcpServers": {
"api-server": {
"command": "node",
"args": ["server.js"],
"env": {
"DATABASE_URL": "${DATABASE_URL}",
"API_KEY": "${API_KEY}"
}
}
}
}Control behavior:
- (default): Error on missing variables
MCP_STRICT_ENV=true - : Use empty values with warning
MCP_STRICT_ENV=false
在配置的任意位置使用语法:
${VAR_NAME}json
{
"mcpServers": {
"api-server": {
"command": "node",
"args": ["server.js"],
"env": {
"DATABASE_URL": "${DATABASE_URL}",
"API_KEY": "${API_KEY}"
}
}
}
}控制行为:
- (默认值):变量缺失时抛出错误
MCP_STRICT_ENV=true - :使用空值并发出警告
MCP_STRICT_ENV=false
Tool Filtering
工具过滤
Restrict available tools using glob patterns:
json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "."],
"allowedTools": ["read_file", "list_directory"],
"disabledTools": ["delete_file"]
}
}
}Rules:
- : Whitelist (supports
allowedTools,*glob patterns)? - : Blacklist (takes precedence over allowedTools)
disabledTools - Applies to all operations (info, grep, call)
Examples:
json
// Only read operations
"allowedTools": ["read_*", "list_*", "search_*"]
// Disable destructive operations
"disabledTools": ["delete_*", "write_*", "create_*"]
// Combine filters
"allowedTools": ["*file*"],
"disabledTools": ["delete_file"]使用通配符模式限制可用工具:
json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "."],
"allowedTools": ["read_file", "list_directory"],
"disabledTools": ["delete_file"]
}
}
}规则:
- :白名单(支持
allowedTools、*通配符模式)? - :黑名单(优先级高于白名单)
disabledTools - 适用于所有操作(info、grep、call)
示例:
json
// 仅允许读取操作
"allowedTools": ["read_*", "list_*", "search_*"]
// 禁用破坏性操作
"disabledTools": ["delete_*", "write_*", "create_*"]
// 组合过滤器
"allowedTools": ["*file*"],
"disabledTools": ["delete_file"]Core Commands
核心命令
List All Servers and Tools
列出所有服务器和工具
bash
undefinedbash
undefinedBasic listing
基础列表
mcp-cli
mcp-cli
With descriptions
包含描述
mcp-cli -d
**Output:**github
• search_repositories
• get_file_contents
• create_or_update_file
filesystem
• read_file
• write_file
• list_directory
undefinedmcp-cli -d
**输出:**github
• search_repositories
• get_file_contents
• create_or_update_file
filesystem
• read_file
• write_file
• list_directory
undefinedSearch Tools by Pattern
按模式搜索工具
bash
undefinedbash
undefinedFind file-related tools
查找与文件相关的工具
mcp-cli grep "file"
mcp-cli grep "file"
Search with descriptions
搜索并显示描述
mcp-cli grep "search" -d
**Output:**github/get_file_contents
github/create_or_update_file
filesystem/read_file
filesystem/write_file
undefinedmcp-cli grep "search" -d
**输出:**github/get_file_contents
github/create_or_update_file
filesystem/read_file
filesystem/write_file
undefinedView Server Details
查看服务器详情
bash
mcp-cli info <server>Example:
bash
mcp-cli info githubOutput:
Server: github
Transport: stdio
Command: npx -y @modelcontextprotocol/server-github
Tools (12):
search_repositories
Search for GitHub repositories
Parameters:
• query (string, required) - Search query
• page (number, optional) - Page number
...bash
mcp-cli info <server>示例:
bash
mcp-cli info github输出:
Server: github
Transport: stdio
Command: npx -y @modelcontextprotocol/server-github
Tools (12):
search_repositories
Search for GitHub repositories
Parameters:
• query (string, required) - Search query
• page (number, optional) - Page number
...View Tool Schema
查看工具Schema
Both formats work:
bash
mcp-cli info <server> <tool>
mcp-cli info <server>/<tool>Example:
bash
mcp-cli info github search_repositoriesOutput:
Tool: search_repositories
Server: github
Description:
Search for GitHub repositories
Input Schema:
{
"type": "object",
"properties": {
"query": { "type": "string", "description": "Search query" },
"page": { "type": "number" }
},
"required": ["query"]
}以下两种格式均可使用:
bash
mcp-cli info <server> <tool>
mcp-cli info <server>/<tool>示例:
bash
mcp-cli info github search_repositories输出:
Tool: search_repositories
Server: github
Description:
Search for GitHub repositories
Input Schema:
{
"type": "object",
"properties": {
"query": { "type": "string", "description": "Search query" },
"page": { "type": "number" }
},
"required": ["query"]
}Call a Tool
调用工具
bash
undefinedbash
undefinedInline JSON
内联JSON
mcp-cli call <server> <tool> '{"key": "value"}'
mcp-cli call <server> <tool> '{"key": "value"}'
From stdin (auto-detected, no '-' needed)
从标准输入读取(自动检测,无需'-')
echo '{"path": "./file"}' | mcp-cli call <server> <tool>
echo '{"path": "./file"}' | mcp-cli call <server> <tool>
Heredoc for complex JSON
使用here文档传递复杂JSON
mcp-cli call <server> <tool> <<EOF
{"content": "Text with 'quotes' and "escapes""}
EOF
**Example:**
```bash
mcp-cli call github search_repositories '{"query": "mcp server", "per_page": 5}'Output (JSON):
json
{
"content": [
{
"type": "text",
"text": "{\"items\": [{\"name\": \"mcp-cli\", \"url\": \"...\"}]}"
}
]
}mcp-cli call <server> <tool> <<EOF
{"content": "Text with 'quotes' and "escapes""}
EOF
**示例:**
```bash
mcp-cli call github search_repositories '{"query": "mcp server", "per_page": 5}'输出(JSON格式):
json
{
"content": [
{
"type": "text",
"text": "{\"items\": [{\"name\": \"mcp-cli\", \"url\": \"...\"}]}"
}
]
}Practical Usage Patterns
实用使用模式
1. Discover → Inspect → Execute Workflow
1. 发现 → 查看 → 执行工作流
bash
undefinedbash
undefinedStep 1: List available servers
步骤1:列出可用服务器
mcp-cli
mcp-cli
Step 2: View server tools
步骤2:查看服务器工具
mcp-cli info filesystem
mcp-cli info filesystem
Step 3: Get tool schema
步骤3:获取工具schema
mcp-cli info filesystem read_file
mcp-cli info filesystem read_file
Step 4: Call the tool
步骤4:调用工具
mcp-cli call filesystem read_file '{"path": "./README.md"}'
undefinedmcp-cli call filesystem read_file '{"path": "./README.md"}'
undefined2. Pipe JSON with jq
2. 使用jq处理JSON管道
bash
undefinedbash
undefinedExtract specific field
提取特定字段
mcp-cli call github search_repositories '{"query": "mcp"}' | jq '.content[0].text'
mcp-cli call github search_repositories '{"query": "mcp"}' | jq '.content[0].text'
Parse nested JSON
解析嵌套JSON
mcp-cli call github search_repositories '{"query": "mcp"}'
| jq -r '.content[0].text | fromjson | .items[].html_url'
| jq -r '.content[0].text | fromjson | .items[].html_url'
mcp-cli call github search_repositories '{"query": "mcp"}'
| jq -r '.content[0].text | fromjson | .items[].html_url'
| jq -r '.content[0].text | fromjson | .items[].html_url'
Filter results
过滤结果
mcp-cli call filesystem list_directory '{"path": "."}'
| jq -r '.content[0].text | split("\n")[] | select(endswith(".md"))'
| jq -r '.content[0].text | split("\n")[] | select(endswith(".md"))'
undefinedmcp-cli call filesystem list_directory '{"path": "."}'
| jq -r '.content[0].text | split("\n")[] | select(endswith(".md"))'
| jq -r '.content[0].text | split("\n")[] | select(endswith(".md"))'
undefined3. Chain Multiple MCP Calls
3. 链式调用多个MCP命令
bash
undefinedbash
undefinedSearch and read first result
搜索并读取第一个结果
mcp-cli call filesystem search_files '{"path": "src/", "pattern": "*.ts"}'
| jq -r '.content[0].text | split("\n")[0]'
| xargs -I {} mcp-cli call filesystem read_file '{"path": "{}"}'
| jq -r '.content[0].text | split("\n")[0]'
| xargs -I {} mcp-cli call filesystem read_file '{"path": "{}"}'
mcp-cli call filesystem search_files '{"path": "src/", "pattern": "*.ts"}'
| jq -r '.content[0].text | split("\n")[0]'
| xargs -I {} mcp-cli call filesystem read_file '{"path": "{}"}'
| jq -r '.content[0].text | split("\n")[0]'
| xargs -I {} mcp-cli call filesystem read_file '{"path": "{}"}'
Process all matching files
处理所有匹配文件
mcp-cli call filesystem search_files '{"path": ".", "pattern": "*.md"}'
| jq -r '.content[0].text | split("\n")[]'
| while read file; do echo "=== $file ===" mcp-cli call filesystem read_file "{"path": "$file"}" | jq -r '.content[0].text' done
| jq -r '.content[0].text | split("\n")[]'
| while read file; do echo "=== $file ===" mcp-cli call filesystem read_file "{"path": "$file"}" | jq -r '.content[0].text' done
undefinedmcp-cli call filesystem search_files '{"path": ".", "pattern": "*.md"}'
| jq -r '.content[0].text | split("\n")[]'
| while read file; do echo "=== $file ===" mcp-cli call filesystem read_file "{"path": "$file"}" | jq -r '.content[0].text' done
| jq -r '.content[0].text | split("\n")[]'
| while read file; do echo "=== $file ===" mcp-cli call filesystem read_file "{"path": "$file"}" | jq -r '.content[0].text' done
undefined4. Error Handling in Scripts
4. 脚本中的错误处理
bash
undefinedbash
undefinedConditional execution
条件执行
mcp-cli call filesystem list_directory '{"path": "."}'
| jq -e '.content[0].text | contains("README.md")'
&& mcp-cli call filesystem read_file '{"path": "./README.md"}'
| jq -e '.content[0].text | contains("README.md")'
&& mcp-cli call filesystem read_file '{"path": "./README.md"}'
mcp-cli call filesystem list_directory '{"path": "."}'
| jq -e '.content[0].text | contains("README.md")'
&& mcp-cli call filesystem read_file '{"path": "./README.md"}'
| jq -e '.content[0].text | contains("README.md")'
&& mcp-cli call filesystem read_file '{"path": "./README.md"}'
Fallback on error
错误时使用回退方案
if result=$(mcp-cli call filesystem read_file '{"path": "./config.json"}' 2>/dev/null); then
echo "$result" | jq '.content[0].text | fromjson'
else
echo "File not found, using defaults"
fi
undefinedif result=$(mcp-cli call filesystem read_file '{"path": "./config.json"}' 2>/dev/null); then
echo "$result" | jq '.content[0].text | fromjson'
else
echo "文件未找到,使用默认配置"
fi
undefined5. Save Output to File
5. 将输出保存到文件
bash
mcp-cli call github get_file_contents '{"owner": "user", "repo": "project", "path": "src/main.ts"}' \
| jq -r '.content[0].text' > main.tsbash
mcp-cli call github get_file_contents '{"owner": "user", "repo": "project", "path": "src/main.ts"}' \
| jq -r '.content[0].text' > main.ts6. Aggregate Results from Multiple Servers
6. 聚合多个服务器的结果
bash
{
mcp-cli call github search_repositories '{"query": "mcp", "per_page": 3}'
mcp-cli call filesystem list_directory '{"path": "./src"}'
} | jq -s '.'bash
{
mcp-cli call github search_repositories '{"query": "mcp", "per_page": 3}'
mcp-cli call filesystem list_directory '{"path": "./src"}'
} | jq -s '.'7. Complex JSON Arguments
7. 复杂JSON参数
For JSON with special characters, use stdin to avoid shell escaping:
bash
undefined对于包含特殊字符的JSON,使用标准输入避免Shell转义问题:
bash
undefinedHeredoc (recommended)
Here文档(推荐)
mcp-cli call server tool <<EOF
{
"content": "Text with 'single quotes' and "double quotes"",
"metadata": {
"nested": "value"
}
}
EOF
mcp-cli call server tool <<EOF
{
"content": "Text with 'single quotes' and "double quotes"",
"metadata": {
"nested": "value"
}
}
EOF
From file
从文件读取
cat args.json | mcp-cli call server tool
cat args.json | mcp-cli call server tool
Using jq to build complex JSON
使用jq构建复杂JSON
jq -n '{query: "mcp", filters: ["active", "starred"]}' | mcp-cli call github search
undefinedjq -n '{query: "mcp", filters: ["active", "starred"]}' | mcp-cli call github search
undefinedEnvironment Variables
环境变量
| Variable | Description | Default |
|---|---|---|
| Path to config file | (none) |
| Enable debug output | |
| Request timeout (seconds) | |
| Servers processed in parallel | |
| Retry attempts for transient errors | |
| Base retry delay (milliseconds) | |
| Error on missing | |
| Disable connection caching | |
| Idle timeout for cached connections (seconds) | |
Example:
bash
undefined| 变量名 | 描述 | 默认值 |
|---|---|---|
| 配置文件路径 | 无 |
| 启用调试输出 | |
| 请求超时时间(秒) | |
| 并行处理的服务器数量 | |
| 临时错误的重试次数 | |
| 基础重试延迟(毫秒) | |
| 配置中缺失 | |
| 禁用连接缓存 | |
| 缓存连接的空闲超时时间(秒) | |
示例:
bash
undefinedEnable debug mode
启用调试模式
export MCP_DEBUG=true
mcp-cli info github
export MCP_DEBUG=true
mcp-cli info github
Use custom config
使用自定义配置
export MCP_CONFIG_PATH=/path/to/config.json
mcp-cli
export MCP_CONFIG_PATH=/path/to/config.json
mcp-cli
Disable connection pooling
禁用连接池
export MCP_NO_DAEMON=true
mcp-cli call server tool '{}'
undefinedexport MCP_NO_DAEMON=true
mcp-cli call server tool '{}'
undefinedCLI Options
CLI选项
| Option | Description |
|---|---|
| Show help message |
| Show version number |
| Include tool descriptions |
| Path to config file |
Example:
bash
undefined| 选项 | 描述 |
|---|---|
| 显示帮助信息 |
| 显示版本号 |
| 包含工具描述 |
| 指定配置文件路径 |
示例:
bash
undefinedList with descriptions
列出工具并显示描述
mcp-cli -d
mcp-cli -d
Use custom config
使用自定义配置
mcp-cli -c ./my-config.json info github
mcp-cli -c ./my-config.json info github
Get help
获取帮助
mcp-cli --help
undefinedmcp-cli --help
undefinedCommon MCP Server Setups
常见MCP服务器配置
Filesystem Server
文件系统服务器
json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/path/to/directory"
],
"allowedTools": ["read_file", "list_directory", "search_files"],
"disabledTools": ["delete_file"]
}
}
}Usage:
bash
mcp-cli call filesystem read_file '{"path": "./README.md"}'
mcp-cli call filesystem list_directory '{"path": "."}'json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/path/to/directory"
],
"allowedTools": ["read_file", "list_directory", "search_files"],
"disabledTools": ["delete_file"]
}
}
}用法:
bash
mcp-cli call filesystem read_file '{"path": "./README.md"}'
mcp-cli call filesystem list_directory '{"path": "."}'GitHub Server
GitHub服务器
json
{
"mcpServers": {
"github": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-github"
],
"env": {
"GITHUB_TOKEN": "${GITHUB_TOKEN}"
}
}
}
}Usage:
bash
mcp-cli call github search_repositories '{"query": "mcp server"}'
mcp-cli call github get_file_contents '{"owner": "user", "repo": "repo", "path": "README.md"}'json
{
"mcpServers": {
"github": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-github"
],
"env": {
"GITHUB_TOKEN": "${GITHUB_TOKEN}"
}
}
}
}用法:
bash
mcp-cli call github search_repositories '{"query": "mcp server"}'
mcp-cli call github get_file_contents '{"owner": "user", "repo": "repo", "path": "README.md"}'HTTP Server
HTTP服务器
json
{
"mcpServers": {
"api": {
"url": "https://mcp.example.com",
"headers": {
"Authorization": "Bearer ${API_TOKEN}"
}
}
}
}Usage:
bash
mcp-cli info api
mcp-cli call api tool_name '{"param": "value"}'json
{
"mcpServers": {
"api": {
"url": "https://mcp.example.com",
"headers": {
"Authorization": "Bearer ${API_TOKEN}"
}
}
}
}用法:
bash
mcp-cli info api
mcp-cli call api tool_name '{"param": "value"}'Custom Local Server
自定义本地服务器
json
{
"mcpServers": {
"custom": {
"command": "node",
"args": ["./my-server.js"],
"cwd": "/path/to/project",
"env": {
"PORT": "3000",
"API_KEY": "${API_KEY}"
}
}
}
}json
{
"mcpServers": {
"custom": {
"command": "node",
"args": ["./my-server.js"],
"cwd": "/path/to/project",
"env": {
"PORT": "3000",
"API_KEY": "${API_KEY}"
}
}
}
}Troubleshooting
故障排除
Config File Not Found
未找到配置文件
Error:
Error: No MCP configuration foundSolution:
- Create in current directory or
mcp_servers.json~/.config/mcp/ - Or set environment variable
MCP_CONFIG_PATH - Or use flag:
-cmcp-cli -c /path/to/config.json
错误:
Error: No MCP configuration found解决方案:
- 在当前目录或下创建
~/.config/mcp/文件mcp_servers.json - 或者设置环境变量
MCP_CONFIG_PATH - 或者使用参数:
-cmcp-cli -c /path/to/config.json
Server Not Starting
服务器无法启动
Error:
Error: Failed to connect to server 'github'Solution:
- Check command is installed:
npx @modelcontextprotocol/server-github --version - Verify environment variables are set
- Enable debug mode:
MCP_DEBUG=true mcp-cli info github - Check server logs in stderr
错误:
Error: Failed to connect to server 'github'解决方案:
- 检查命令是否已安装:
npx @modelcontextprotocol/server-github --version - 验证环境变量是否已设置
- 启用调试模式:
MCP_DEBUG=true mcp-cli info github - 在stderr中查看服务器日志
Missing Environment Variable
缺失环境变量
Error:
Error: Environment variable GITHUB_TOKEN not foundSolution:
- Set the variable:
export GITHUB_TOKEN=your_token - Or use to allow empty values (not recommended)
MCP_STRICT_ENV=false
错误:
Error: Environment variable GITHUB_TOKEN not found解决方案:
- 设置变量:
export GITHUB_TOKEN=your_token - 或者设置允许空值(不推荐)
MCP_STRICT_ENV=false
Tool Not Found
未找到工具
Error:
Error: Tool 'invalid_tool' not found on server 'github'Solution:
- List available tools:
mcp-cli info github - Check for typos in tool name
- Verify tool isn't disabled in config ()
disabledTools
错误:
Error: Tool 'invalid_tool' not found on server 'github'解决方案:
- 列出可用工具:
mcp-cli info github - 检查工具名称是否有拼写错误
- 验证工具是否未在配置中被禁用()
disabledTools
Tool Filtered Out
工具被过滤
Error:
Error: Tool 'write_file' is disabled by configurationSolution:
- Check config and
allowedToolsdisabledTools - Update config to allow the tool
- Remove or modify filtering rules
错误:
Error: Tool 'write_file' is disabled by configuration解决方案:
- 检查配置中的和
allowedToolsdisabledTools - 更新配置以启用该工具
- 删除或修改过滤规则
JSON Parsing Error
JSON解析错误
Error:
Error: Invalid JSON argumentsSolution:
- Use stdin for complex JSON:
echo '{}' | mcp-cli call server tool - Escape quotes properly:
'{"key": "value"}' - Use heredoc for multi-line JSON
错误:
Error: Invalid JSON arguments解决方案:
- 对复杂JSON使用标准输入:
echo '{}' | mcp-cli call server tool - 正确转义引号:
'{"key": "value"}' - 对多行JSON使用here文档
Connection Timeout
连接超时
Error:
Error: Request timeout after 1800 secondsSolution:
- Increase timeout:
export MCP_TIMEOUT=3600 - Check server responsiveness
- Disable daemon for fresh connection:
export MCP_NO_DAEMON=true
错误:
Error: Request timeout after 1800 seconds解决方案:
- 增加超时时间:
export MCP_TIMEOUT=3600 - 检查服务器响应情况
- 禁用守护进程以建立新连接:
export MCP_NO_DAEMON=true
Permission Denied
权限拒绝
Error:
Error: EACCES: permission deniedSolution:
- Check file permissions in directory
cwd - Verify user has permission to execute command
- For filesystem server, ensure paths are accessible
错误:
Error: EACCES: permission denied解决方案:
- 检查目录下的文件权限
cwd - 验证用户是否有执行命令的权限
- 对于文件系统服务器,确保路径可访问
AI Agent Integration
AI Agent集成
System Prompt Template
系统提示词模板
Add this to your AI agent's system prompt:
markdown
undefined将以下内容添加到AI Agent的系统提示词中:
markdown
undefinedMCP Servers
MCP Servers
You have access to MCP servers via the CLI.
mcp-cliCommands:
- - List all servers
mcp-cli info - - Show server tools
mcp-cli info <server> - - Get tool schema
mcp-cli info <server> <tool> - - Search tools
mcp-cli grep "<pattern>" - - Call with JSON args
mcp-cli call <server> <tool> '{}' - - Call from stdin
echo '{}' | mcp-cli call <server> <tool>
Workflow:
- Discover: to see available servers
mcp-cli info - Inspect: to get schema
mcp-cli info <server> <tool> - Execute: with arguments
mcp-cli call <server> <tool> '{}'
Use stdin for complex JSON to avoid shell escaping issues.
undefinedYou have access to MCP servers via the CLI.
mcp-cliCommands:
- - List all servers
mcp-cli info - - Show server tools
mcp-cli info <server> - - Get tool schema
mcp-cli info <server> <tool> - - Search tools
mcp-cli grep "<pattern>" - - Call with JSON args
mcp-cli call <server> <tool> '{}' - - Call from stdin
echo '{}' | mcp-cli call <server> <tool>
Workflow:
- Discover: to see available servers
mcp-cli info - Inspect: to get schema
mcp-cli info <server> <tool> - Execute: with arguments
mcp-cli call <server> <tool> '{}'
Use stdin for complex JSON to avoid shell escaping issues.
undefinedToken-Efficient Workflow
令牌高效工作流
Instead of loading all tool schemas:
bash
undefined无需加载所有工具schema:
bash
undefined❌ Avoid: Loading everything into context
❌ 避免:将所有内容加载到上下文
mcp-cli -d # Thousands of tokens
mcp-cli -d # 数千个令牌
✅ Efficient: On-demand loading
✅ 高效:按需加载
mcp-cli # List servers (minimal tokens)
mcp-cli info github # Show tools when needed
mcp-cli info github search_repositories # Get schema only when calling
mcp-cli call github search_repositories '{"query": "mcp"}'
undefinedmcp-cli # 列出服务器(最少令牌)
mcp-cli info github # 需要时查看工具
mcp-cli info github search_repositories # 调用时才获取schema
mcp-cli call github search_repositories '{"query": "mcp"}'
undefinedScript Generation Example
脚本生成示例
AI can generate shell scripts combining multiple MCP calls:
bash
#!/bin/bashAI可以生成组合多个MCP调用的Shell脚本:
bash
#!/bin/bashGenerated by AI agent to analyze repository
Generated by AI agent to analyze repository
Search for repos
Search for repos
repos=$(mcp-cli call github search_repositories '{"query": "mcp server", "per_page": 5}')
repos=$(mcp-cli call github search_repositories '{"query": "mcp server", "per_page": 5}')
Extract URLs
Extract URLs
urls=$(echo "$repos" | jq -r '.content[0].text | fromjson | .items[].html_url')
urls=$(echo "$repos" | jq -r '.content[0].text | fromjson | .items[].html_url')
For each repo, get README
For each repo, get README
for url in $urls; do
owner=$(echo "$url" | cut -d'/' -f4)
repo=$(echo "$url" | cut -d'/' -f5)
echo "=== $owner/$repo ==="
mcp-cli call github get_file_contents "{"owner": "$owner", "repo": "$repo", "path": "README.md"}"
| jq -r '.content[0].text' done
| jq -r '.content[0].text' done
undefinedfor url in $urls; do
owner=$(echo "$url" | cut -d'/' -f4)
repo=$(echo "$url" | cut -d'/' -f5)
echo "=== $owner/$repo ==="
mcp-cli call github get_file_contents "{"owner": "$owner", "repo": "$repo", "path": "README.md"}"
| jq -r '.content[0].text' done
| jq -r '.content[0].text' done
undefinedBest Practices
最佳实践
- Use stdin for complex JSON - Avoid shell escaping issues
- Enable descriptions sparingly - Use only when needed to save tokens
-d - Filter tools in config - Reduce attack surface and simplify discovery
- Set environment variables - Use substitution for secrets
${VAR} - Pipe to jq - Process JSON output efficiently
- Check schemas first - Use before
infoto validate argumentscall - Handle errors - Check exit codes and stderr in scripts
- Use connection pooling - Default daemon keeps connections warm
- Search before listing - Use for specific tools instead of listing all
grep
- 对复杂JSON使用标准输入 - 避免Shell转义问题
- 谨慎启用描述 - 仅在需要时使用参数以节省令牌
-d - 在配置中过滤工具 - 减少攻击面并简化发现过程
- 设置环境变量 - 使用替换敏感信息
${VAR} - 使用jq处理管道输出 - 高效处理JSON输出
- 先检查schema - 在调用之前使用
call验证参数info - 处理错误 - 在脚本中检查退出码和stderr
- 使用连接池 - 默认守护进程保持连接活跃
- 先搜索再列出 - 使用查找特定工具,而非列出所有工具
grep