MCP 工具提供了一个多功能的命令行界面,用于处理模型上下文协议(MCP)服务器。它使您能够:
brew tap f/mcptools
brew install mcp
❕ 二进制文件安装为
mcp,但也可以通过mcpt访问,以避免与其他可能使用mcp命令名称的工具发生冲突。
go install github.com/f/mcptools/cmd/mcptools@latest
❕ 二进制文件将被安装为
<img width="500" alt="2025-05-05 22 21 29" src="https://gips1.baidu.com/it/u=1670181972,1914941324&fm=3081&app=3081&f=PNG?w=1670&h=762" />mcptools,但可以将其别名为mcpt以便于使用。<sub>Windows 11 运行示例</sub>
使用 MCP 工具最简单的方法是连接到 MCP 服务器并列出可用工具:
# 列出文件系统服务器上的所有可用工具
mcp tools npx -y @modelcontextprotocol/server-filesystem ~
# 调用特定工具
mcp call read_file --params '{"path":"README.md"}' npx -y @modelcontextprotocol/server-filesystem ~
# 打开交互式 shell
mcp shell npx -y @modelcontextprotocol/server-filesystem ~
MCP 工具支持广泛的与 MCP 服务器交互的功能:
用法:
mcp [命令]
可用命令:
version 打印版本信息
tools 列出 MCP 服务器上的可用工具
resources 列出 MCP 服务器上的可用资源
prompts 列出 MCP 服务器上的可用提示
call 调用 MCP 服务器上的工具、资源或提示
get-prompt 获取 MCP 服务器上的提示
read-resource 读取 MCP 服务器上的资源
shell 启动 MCP 命令的交互式 shell
web 启动 MCP 命令的 Web 接口
mock 创建带有工具、提示和资源的模拟 MCP 服务器
proxy 将 MCP 工具请求代理到 shell 脚本
alias 管理 MCP 服务器别名
configs 管理 MCP 服务器配置
new 创建新的 MCP 项目组件
help 关于任何命令的帮助
completion 为指定的 shell 生成自动补全脚本
标志:
-f, --format string 输出格式(table, json, pretty)(默认 "table")
-h, --help mcp 的帮助
-p, --params string 要传递给工具的参数的 JSON 字符串(仅限 call 命令)(默认 "{}")
使用 "mcp [命令] --help" 获取有关命令的更多信息。
MCP 工具支持多种与 MCP 服务器通信的传输方法:
使用 stdin/stdout 通过 JSON-RPC 2.0 与 MCP 服务器通信。这对于实现 MCP 协议的命令行工具非常有用。
mcp tools npx -y @modelcontextprotocol/server-filesystem ~
使用 HTTP 和服务器发送事件(SSE)通过 JSON-RPC 2.0 与 MCP 服务器通信。这对于连接到实现旧版 MCP 协议的远程服务器非常有用。当 URL 以 /sse 结尾时,传输会自动检测。
mcp tools http://localhost:3001/sse
# 示例:使用 everything 样本服务器
# docker run -p 3001:3001 --rm -it tzolov/mcp-everything-server:v1
注意: HTTP SSE 目前仅支持 MCP 协议版本 2024-11-05。
使用流式 HTTP 通过 JSON-RPC 2.0 与 MCP 服务器通信。这是现代且推荐的方法,用于连接到实现 MCP 协议的远程服务器。它支持流式响应和简单的请求/响应模式。这是 HTTP/HTTPS URL 的默认传输方式。
# HTTP URL 的默认传输方式
mcp tools http://localhost:3000
# 流式 HTTP 传输(从 URL 自动检测)
mcp tools http://localhost:3000
# 远程服务器示例
mcp tools https://api.example.com/mcp
mcp tools https://ne.tools
流式 HTTP 的优点:
MCP 工具支持三种输出格式以满足不同的需求:
mcp tools npx -y @modelcontextprotocol/server-filesystem ~
默认格式现在以彩色手册页样式显示工具:
read_file(path:str)
从文件系统中读取整个文件的内容。
read_multiple_files(paths:str[])
同时读取多个文件的内容。
list_dir(path:str)
列出目录的内容。
write_file(path:str, content:str)
将内容写入文件。
grep_search(pattern:str, [excludePatterns:str[]])
使用模式搜索文件。
edit_file(edits:{newText:str,oldText:str}[], path:str)
使用多个文本替换编辑文件。
格式的关键特性:
path:str)[limit:int])[] 后缀表示(例如,str[]){prop1:type1,prop2:type2}){notifications:{enabled:bool,sound:bool}})str 代替 string,int 代替 integer)mcp tools --format json npx -y @modelcontextprotocol/server-filesystem ~
mcp tools --format pretty npx -y @modelcontextprotocol/server-filesystem ~
MCP 工具包括几个核心命令,用于与 MCP 服务器交互:
mcp tools npx -y @modelcontextprotocol/server-filesystem ~
mcp resources npx -y @modelcontextprotocol/server-filesystem ~
mcp prompts npx -y @modelcontextprotocol/server-filesystem ~
mcp call read_file --params '{"path":"/path/to/file"}' npx -y @modelcontextprotocol/server-filesystem ~
mcp call resource:test://static/resource/1 npx -y @modelcontextprotocol/server-everything -f json | jq ".contents[0].text"
或
mcp read-resource test://static/resource/1 npx -y @modelcontextprotocol/server-everything -f json | jq ".contents[0].text"
mcp get-prompt simple_prompt npx -y @modelcontextprotocol/server-everything -f json | jq ".messages[0].content.text"
当使用客户端命令调用服务器时,您可以添加 --server-logs 标志来查看与您的请求相关的服务器日志:
# 查看列出工具时的服务器日志
mcp tools --server-logs npx -y @modelcontextprotocol/server-filesystem ~
输出:
[>] 安全的 MCP 文件系统服务器正在 stdio 上运行
[>] 允许的目录:[ '/Users/fka/' ]
read_file(path:str)
从文件系统中读取整个文件的内容。
read_multiple_files(paths:str[])
同时读取多个文件的内容。
... 以及此服务器上可用的其他工具
这有助于调试或理解执行这些命令时服务器端发生了什么。
交互式 shell 模式允许您在一个会话中运行多个 MCP 命令:
mcp shell npx -y @modelcontextprotocol/server-filesystem ~
这将打开一个具有以下功能的交互式 shell:
mcp tools shell
已连接到:npx -y @modelcontextprotocol/server-filesystem /Users/fka
mcp > 输入 '/h' 获取帮助或 '/q' 退出
mcp > tools
read_file(path:str, [limit:int], [offset:int])
从文件系统中读取文件
list_dir(path:str)
列出目录内容
grep_search(pattern:str, [excludePatterns:str[]])
使用模式搜索文件
edit_file(edits:{newText:str,oldText:str}[], path:str)
使用多个文本替换编辑文件
# 直接调用工具
mcp > read_file {"path":"README.md"}
... README.md 的内容 ...
# 调用具有复杂对象参数的工具
mcp > edit_file {"path":"main.go","edits":[{"oldText":"foo","newText":"bar"}]}
... 编辑操作的结果 ...
# 获取帮助
mcp > /h
MCP Shell 命令:
tools 列出可用工具
resources 列出可用资源
prompts 列出可用提示
call <实体> [--params '{...}'] 调用工具、资源或提示
format [json|pretty|table] 获取或设置输出格式
特殊命令:
/h, /help 显示此帮助
/q, /quit, exit 退出 shell
MCP 工具提供了一个基于浏览器的 UI 的 Web 接口,用于与 MCP 服务器交互:
# 在默认端口(41999)启动文件系统服务器的 Web 接口
mcp web npx -y @modelcontextprotocol/server-filesystem ~
# 使用自定义端口
mcp web --port 8080 docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN ghcr.io/github/github-mcp-server
# 使用 SSE
mcp web https://ne.tools
Web 接口包括:
启动后,您可以通过在浏览器中打开 http://localhost:41999(或您自定义的端口)来访问该界面。
MCP 工具提供了一个脚手架功能,可以快速创建具有 TypeScript 的新 MCP 服务器:
mkdir my-mcp-server
cd my-mcp-server
# 创建具有特定组件的项目
mcp new tool:calculate resource:file prompt:greet
# 创建具有特定 SDK(目前仅支持 TypeScript/ts)的项目
mcp new tool:calculate --sdk=ts
# 创建具有特定传输类型的项目
mcp new tool:calculate --transport=stdio
mcp new tool:calculate --transport=sse
脚手架创建了完整的项目结构,包括:
脚手架后,您可以构建并运行您的 MCP 服务器:
# 安装依赖项
npm install
# 构建 TypeScript 代码
npm run build
# 使用 MCP 工具测试服务器
mcp tools node build/index.js
项目模板存储在:
./templates/ 目录~/.mcpt/templates//opt/homebrew/Cellar/mcp/v#.#.#/templates)通过 Homebrew 安装时,模板会自动安装到您的主目录。但如果使用源码安装,则需要运行 make install-templates。
MCP 工具允许您保存并重复使用带有友好别名的服务器命令:
# 添加一个新的服务器别名
mcp alias add myfs npx -y @modelcontextprotocol/server-filesystem ~/
# 列出所有注册的服务器别名
mcp alias list
# 删除一个服务器别名
mcp alias remove myfs
# 使用别名与任何 MCP 命令
mcp tools myfs
mcp call read_file --params '{"path":"README.md"}' myfs
服务器别名存储在 $HOME/.mcpt/aliases.json 中,并提供了一种方便的方式,无需反复输入长命令即可与常用的 MCP 服务器一起工作。
MCP 工具提供了一个强大的配置管理系统,帮助您跨多个应用程序管理 MCP 服务器配置:
🚧 目前仅在 macOS 上工作。
# 在所有支持的应用程序中扫描 MCP 服务器配置
mcp configs scan
# 列出所有配置(`configs view --all` 的别名)
mcp configs ls
# 通过别名查看特定配置
mcp configs view vscode
# 在配置中添加或更新服务器
mcp configs set vscode my-server npm run mcp-server
mcp configs set cursor my-api https://api.example.com/mcp --headers "Authorization=Bearer token"
# 一次添加到多个配置
mcp configs set vscode,cursor,claude-desktop my-server npm run mcp-server
# 从配置中删除服务器
mcp configs remove vscode my-server
# 为自定义配置文件创建别名
mcp configs alias myapp ~/myapp/config.json
# 同步并合并来自多个来源的配置
mcp configs sync vscode cursor --output vscode --default interactive
# 将命令行转换为 MCP 服务器 JSON 配置格式