MCP CLI: On-Demand MCP Server Usage
MCP CLI:按需使用MCP服务器
Use the
CLI tool to dynamically discover and invoke MCP server capabilities without pre-configuring them as permanent integrations.
使用
CLI工具动态发现并调用MCP服务器的功能,无需将它们预配置为永久集成。
When to Use This Skill
何时使用该技能
Use this skill when you need to:
- Explore an MCP server's capabilities before deciding to use it
- Make one-off calls to an MCP server without permanent integration
- Access MCP functionality without polluting the context window
- Test or debug MCP servers
- Use MCP servers that aren't pre-configured
在以下场景中使用本技能:
- 在决定使用某MCP服务器前,探索其功能
- 对MCP服务器进行一次性调用,无需永久集成
- 访问MCP功能,且不占用上下文窗口
- 测试或调试MCP服务器
- 使用未预配置的MCP服务器
The
CLI must be installed at
. If not present:
Clone and build
Clone and build
cd /tmp && git clone --depth 1
https://github.com/f/mcptools.git
cd mcptools && CGO_ENABLED=0 go build -o ~/.local/bin/mcp ./cmd/mcptools
Always ensure PATH includes the binary:
```bash
export PATH="$HOME/.local/bin:$PATH"
cd /tmp && git clone --depth 1
https://github.com/f/mcptools.git
cd mcptools && CGO_ENABLED=0 go build -o ~/.local/bin/mcp ./cmd/mcptools
请确保PATH环境变量包含该二进制文件:
```bash
export PATH="$HOME/.local/bin:$PATH"
Step 1: Discover Available Tools
步骤1:发现可用工具
bash
mcp tools <server-command>
Examples:
bash
mcp tools <server-command>
示例:
Filesystem server
Filesystem server
mcp tools npx -y @modelcontextprotocol/server-filesystem /path/to/allow
mcp tools npx -y @modelcontextprotocol/server-filesystem /path/to/allow
Memory/knowledge graph server
Memory/knowledge graph server
mcp tools npx -y @modelcontextprotocol/server-memory
mcp tools npx -y @modelcontextprotocol/server-memory
GitHub server (requires token)
GitHub server (requires token)
mcp tools docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN ghcr.io/github/github-mcp-server
mcp tools docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN ghcr.io/github/github-mcp-server
HTTP-based server
HTTP-based server
Step 2: Discover Resources (if supported)
步骤2:发现资源(如支持)
bash
mcp resources <server-command>
Resources are data sources the server exposes (files, database entries, etc.).
bash
mcp resources <server-command>
资源指服务器暴露的数据源(如文件、数据库条目等)。
Step 3: Discover Prompts (if supported)
步骤3:发现提示模板(如支持)
bash
mcp prompts <server-command>
Prompts are pre-defined prompt templates the server provides.
bash
mcp prompts <server-command>
提示模板指服务器提供的预定义提示模板。
Step 4: Get Detailed Info (JSON format)
步骤4:获取详细信息(JSON格式)
For full schema details including parameter types
获取包含参数类型的完整架构详情
mcp tools --format json <server-command>
mcp tools --format pretty <server-command>
mcp tools --format json <server-command>
mcp tools --format pretty <server-command>
bash
mcp call <tool_name> --params '<json>' <server-command>
bash
mcp call <tool_name> --params '<json>' <server-command>
Read a file:
bash
mcp call read_file --params '{"path": "/tmp/example.txt"}' \
npx -y @modelcontextprotocol/server-filesystem /tmp
Write a file:
bash
mcp call write_file --params '{"path": "/tmp/test.txt", "content": "Hello world"}' \
npx -y @modelcontextprotocol/server-filesystem /tmp
List directory:
bash
mcp call list_directory --params '{"path": "/tmp"}' \
npx -y @modelcontextprotocol/server-filesystem /tmp
Create entities (memory server):
bash
mcp call create_entities --params '{"entities": [{"name": "Project", "entityType": "Software", "observations": ["Uses TypeScript"]}]}' \
npx -y @modelcontextprotocol/server-memory
Search (memory server):
bash
mcp call search_nodes --params '{"query": "TypeScript"}' \
npx -y @modelcontextprotocol/server-memory
读取文件:
bash
mcp call read_file --params '{"path": "/tmp/example.txt"}' \
npx -y @modelcontextprotocol/server-filesystem /tmp
写入文件:
bash
mcp call write_file --params '{"path": "/tmp/test.txt", "content": "Hello world"}' \
npx -y @modelcontextprotocol/server-filesystem /tmp
列出目录:
bash
mcp call list_directory --params '{"path": "/tmp"}' \
npx -y @modelcontextprotocol/server-filesystem /tmp
创建实体(内存服务器):
bash
mcp call create_entities --params '{"entities": [{"name": "Project", "entityType": "Software", "observations": ["Uses TypeScript"]}]}' \
npx -y @modelcontextprotocol/server-memory
搜索(内存服务器):
bash
mcp call search_nodes --params '{"query": "TypeScript"}' \
npx -y @modelcontextprotocol/server-memory
For nested objects and arrays, ensure valid JSON:
bash
mcp call edit_file --params '{
"path": "/tmp/file.txt",
"edits": [
{"oldText": "foo", "newText": "bar"},
{"oldText": "baz", "newText": "qux"}
]
}' npx -y @modelcontextprotocol/server-filesystem /tmp
对于嵌套对象和数组,请确保使用有效的JSON格式:
bash
mcp call edit_file --params '{
"path": "/tmp/file.txt",
"edits": [
{"oldText": "foo", "newText": "bar"},
{"oldText": "baz", "newText": "qux"}
]
}' npx -y @modelcontextprotocol/server-filesystem /tmp
Table (default, human-readable)
表格格式(默认,易读)
mcp call <tool> --params '{}' <server>
mcp call <tool> --params '{}' <server>
JSON (for parsing)
JSON格式(用于解析)
mcp call <tool> --params '{}' -f json <server>
mcp call <tool> --params '{}' -f json <server>
Pretty JSON (readable JSON)
美化JSON格式(易读的JSON)
mcp call <tool> --params '{}' -f pretty <server>
mcp call <tool> --params '{}' -f pretty <server>
List available resources
列出可用资源
mcp resources <server-command>
mcp resources <server-command>
Read a specific resource
读取特定资源
mcp read-resource <resource-uri> <server-command>
mcp read-resource <resource-uri> <server-command>
mcp call resource:<resource-uri> <server-command>
mcp call resource:<resource-uri> <server-command>
List available prompts
列出可用提示模板
mcp prompts <server-command>
mcp prompts <server-command>
Get a prompt (may require arguments)
获取提示模板(可能需要参数)
mcp get-prompt <prompt-name> <server-command>
mcp get-prompt <prompt-name> <server-command>
mcp get-prompt <prompt-name> --params '{"arg": "value"}' <server-command>
mcp get-prompt <prompt-name> --params '{"arg": "value"}' <server-command>
Server Aliases (for repeated use)
服务器别名(重复使用时)
If using a server frequently during a session:
mcp alias add fs npx -y @modelcontextprotocol/server-filesystem /home/user
mcp alias add fs npx -y @modelcontextprotocol/server-filesystem /home/user
mcp tools fs
mcp call read_file --params '{"path": "README.md"}' fs
mcp tools fs
mcp call read_file --params '{"path": "README.md"}' fs
mcp alias remove fs
Aliases are stored in `~/.mcpt/aliases.json`.
mcp alias remove fs
别名存储在`~/.mcpt/aliases.json`文件中。
bash
mcp tools --auth-user "username:password" https://api.example.com/mcp
bash
mcp tools --auth-user "username:password" https://api.example.com/mcp
bash
mcp tools --auth-header "Bearer your-token-here" https://api.example.com/mcp
bash
mcp tools --auth-header "Bearer your-token-here" https://api.example.com/mcp
Environment Variables (for Docker-based servers)
环境变量(适用于基于Docker的服务器)
bash
mcp tools docker run -i --rm \
-e GITHUB_PERSONAL_ACCESS_TOKEN="$GITHUB_TOKEN" \
ghcr.io/github/github-mcp-server
bash
mcp tools docker run -i --rm \
-e GITHUB_PERSONAL_ACCESS_TOKEN="$GITHUB_TOKEN" \
ghcr.io/github/github-mcp-server
Stdio (default for npx/node commands)
Stdio(npx/node命令默认使用)
bash
mcp tools npx -y @modelcontextprotocol/server-filesystem /tmp
bash
mcp tools npx -y @modelcontextprotocol/server-filesystem /tmp
HTTP (auto-detected for http/https URLs)
HTTP(自动检测http/https URL)
bash
mcp tools https://example.com/mcp
bash
mcp tools https://example.com/mcp
SSE (Server-Sent Events)
SSE(服务器发送事件)
bash
mcp tools http://localhost:3001/sse
bash
mcp tools http://localhost:3001/sse
Common MCP Servers
常见MCP服务器
Allow access to specific directory
允许访问特定目录
mcp tools npx -y @modelcontextprotocol/server-filesystem /path/to/allow
mcp tools npx -y @modelcontextprotocol/server-filesystem /path/to/allow
Memory (Knowledge Graph)
内存(知识图谱)
bash
mcp tools npx -y @modelcontextprotocol/server-memory
bash
mcp tools npx -y @modelcontextprotocol/server-memory
bash
export GITHUB_PERSONAL_ACCESS_TOKEN="your-token"
mcp tools docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN ghcr.io/github/github-mcp-server
bash
export GITHUB_PERSONAL_ACCESS_TOKEN="your-token"
mcp tools docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN ghcr.io/github/github-mcp-server
bash
export BRAVE_API_KEY="your-key"
mcp tools npx -y @anthropic/mcp-server-brave-search
bash
export BRAVE_API_KEY="your-key"
mcp tools npx -y @anthropic/mcp-server-brave-search
Puppeteer (Browser Automation)
Puppeteer(浏览器自动化)
bash
mcp tools npx -y @anthropic/mcp-server-puppeteer
bash
mcp tools npx -y @anthropic/mcp-server-puppeteer
1. Always Discover First
1. 始终先发现
Before calling tools, run
to understand what's available and the exact parameter schema.
在调用工具前,运行
命令了解可用功能和确切的参数架构。
2. Use JSON Format for Parsing
2. 使用JSON格式进行解析
When you need to process results programmatically:
bash
mcp call <tool> --params '{}' -f json <server> | jq '.field'
当需要以编程方式处理结果时:
bash
mcp call <tool> --params '{}' -f json <server> | jq '.field'
3. Validate Parameters
3. 验证参数
The table output shows parameter signatures. Match them exactly:
- = string
- = number
- = boolean
- = array of strings
- = optional parameter
表格输出会显示参数签名,请严格匹配:
- = 字符串
- = 数字
- = 布尔值
- = 字符串数组
- = 可选参数
4. Handle Errors Gracefully
4. 优雅处理错误
Tool calls may fail. Check exit codes and stderr:
bash
if ! result=$(mcp call tool --params '{}' server 2>&1); then
echo "Error: $result"
fi
工具调用可能失败,请检查退出码和标准错误输出:
bash
if ! result=$(mcp call tool --params '{}' server 2>&1); then
echo "Error: $result"
fi
5. Use Aliases for Multi-Step Operations
5. 对多步骤操作使用别名
If making several calls to the same server:
bash
mcp alias add tmp-server npx -y @modelcontextprotocol/server-filesystem /tmp
mcp call list_directory --params '{"path": "/tmp"}' tmp-server
mcp call read_file --params '{"path": "/tmp/file.txt"}' tmp-server
mcp alias remove tmp-server
如果需要对同一服务器进行多次调用:
bash
mcp alias add tmp-server npx -y @modelcontextprotocol/server-filesystem /tmp
mcp call list_directory --params '{"path": "/tmp"}' tmp-server
mcp call read_file --params '{"path": "/tmp/file.txt"}' tmp-server
mcp alias remove tmp-server
6. Restrict Capabilities with Guard
6. 使用Guard限制功能
For safety, limit what tools are accessible:
Only allow read operations
仅允许读取操作
mcp guard --allow 'tools:read_,list_' --deny 'tools:write_,delete_'
npx -y @modelcontextprotocol/server-filesystem /home
mcp guard --allow 'tools:read_,list_' --deny 'tools:write_,delete_'
npx -y @modelcontextprotocol/server-filesystem /home
bash
mcp tools --server-logs <server-command>
bash
mcp tools --server-logs <server-command>
Check Alias Configuration
检查别名配置
bash
cat ~/.mcpt/aliases.json
bash
cat ~/.mcpt/aliases.json
Use
for detailed JSON output to debug parameter issues.
| Action | Command |
|---|
| List tools | |
| List resources | |
| List prompts | |
| Call tool | mcp call <tool> --params '<json>' <server>
|
| Read resource | mcp read-resource <uri> <server>
|
| Get prompt | mcp get-prompt <name> <server>
|
| Add alias | mcp alias add <name> <server-command>
|
| Remove alias | |
| JSON output | Add or |
| 操作 | 命令 |
|---|
| 列出工具 | |
| 列出资源 | |
| 列出提示模板 | |
| 调用工具 | mcp call <tool> --params '<json>' <server>
|
| 读取资源 | mcp read-resource <uri> <server>
|
| 获取提示模板 | mcp get-prompt <name> <server>
|
| 添加别名 | mcp alias add <name> <server-command>
|
| 删除别名 | |
| JSON输出 | 添加或 |
Example: Complete Workflow
示例:完整工作流
1. Discover what's available
1. 发现可用功能
mcp tools npx -y @modelcontextprotocol/server-filesystem /home/user/project
mcp tools npx -y @modelcontextprotocol/server-filesystem /home/user/project
2. Check for resources
2. 检查资源
mcp resources npx -y @modelcontextprotocol/server-filesystem /home/user/project
mcp resources npx -y @modelcontextprotocol/server-filesystem /home/user/project
3. Create alias for convenience
3. 创建别名以方便使用
mcp alias add proj npx -y @modelcontextprotocol/server-filesystem /home/user/project
mcp alias add proj npx -y @modelcontextprotocol/server-filesystem /home/user/project
4. Explore directory structure
4. 探索目录结构
mcp call directory_tree --params '{"path": "/home/user/project"}' proj
mcp call directory_tree --params '{"path": "/home/user/project"}' proj
5. Read specific files
5. 读取特定文件
mcp call read_file --params '{"path": "/home/user/project/README.md"}' proj
mcp call read_file --params '{"path": "/home/user/project/README.md"}' proj
6. Search for patterns
6. 搜索模式
mcp call search_files --params '{"path": "/home/user/project", "pattern": "**/*.ts"}' proj
mcp call search_files --params '{"path": "/home/user/project", "pattern": "**/*.ts"}' proj
"command not found: mcp"
"command not found: mcp"
Ensure PATH is set:
export PATH="$HOME/.local/bin:$PATH"
确保已设置PATH环境变量:
export PATH="$HOME/.local/bin:$PATH"
JSON parse errors
JSON解析错误
- Escape special characters properly
- Avoid shell expansion issues by using single quotes around JSON
- For complex JSON, write to a temp file and use
--params "$(cat params.json)"
- 正确转义特殊字符
- 使用单引号包裹JSON,避免shell扩展问题
- 对于复杂JSON,可将其写入临时文件,然后使用
--params "$(cat params.json)"
Some servers take time to start. The mcp CLI waits for initialization automatically.
部分服务器启动需要时间,mcp CLI会自动等待初始化完成。
For filesystem server, ensure the allowed directory path is correct and accessible.
对于文件系统服务器,请确保允许访问的目录路径正确且可访问。