MCP命令行工具

一个用于与模型上下文协议(MCP)服务器交互的命令行工具,支持工具发现、描述和调用。

  • by anothrNick
  • Go
  • 本地部署
  • 更新于
在 GitHub 查看 anothrNick/mcp-cli

使用说明

MCP-CLI

用于与模型上下文协议(MCP)服务器交互的命令行界面。

为什么?

我构建这个是为了理解 modelcontextprotocol/go-sdk 库以及模型上下文协议(通常)如何作为客户端工作。

特性

  • 列出工具:发现MCP服务器上的所有可用工具
  • 描述工具:查看任何工具的详细输入模式
  • 呼叫工具:使用JSON参数执行工具
  • 动态的:适用于任何MCP服务器-工具在运行时发现
  • 组态管理:在配置文件中存储多个MCP服务器

安装

go build -o mcp

快速开始

1.初始化配置

创建一个配置文件来存储您的MCP服务器:

./mcp init-config

这创造了 config.json 从 config.example.json.编辑它以添加您的服务器和API密钥:

{
  "servers": [
    {
      "name": "tavily",
      "endpoint": "https://mcp.tavily.com/mcp/",
      "api_key": "your_api_key_here",
      "timeout": 30
    }
  ]
}

2.列出您的服务器

./mcp list-servers

3.使用您配置的服务器

所有命令都需要 --server 标志指定要使用的服务器:

# List tools
./mcp list-tools --server tavily

# Describe a tool
./mcp describe-tool tavily_search --server tavily

# Call a tool
./mcp call-tool tavily_search --server tavily --args '{"query": "AI news"}'

用法

全球旗帜

所有MCP命令都需要这些标志:

  • --server, -s:配置文件中的服务器名称(必需)

命令

配置命令

初始化配置

从示例中初始化配置文件:

./mcp init-config
列出服务器

列出所有已配置的MCP服务器:

./mcp list-servers

输出示例:

SERVER NAME   ENDPOINT                        HAS API KEY   TIMEOUT
-----------   --------                        -----------   -------
tavily        https://mcp.tavily.com/mcp/     yes           30s
example       https://mcp.example.com/api/    no            default

Total: 2 server(s)

Usage: mcp list-tools --server <name>

MCP命令

列出工具

列出MCP服务器上的所有可用工具:

./mcp list-tools --server tavily

输出示例:

Connecting to 'tavily' (https://mcp.tavily.com/mcp/)...

TOOL NAME           DESCRIPTION
---------           -----------
search              Search the web for information
calculator          Perform mathematical calculations
translator          Translate text between languages

Total: 3 tool(s)
描述工具

查看特定工具的输入模式:

./mcp describe-tool search --server tavily

这显示了定义工具接受哪些输入的JSON模式。

选项:

  • --pretty, -p:打印JSON模式(默认值:true)
呼叫工具

执行带有参数的工具:

# Using inline JSON
./mcp call-tool search \
  --server tavily \
  --args '{"query": "latest news", "limit": 5}'

# Using a JSON file
./mcp call-tool search \
  --server tavily \
  --args-file examples/search-args.json

输出示例:

Calling tool 'search'...

Result:
-------
✅ Tool execution successful

Content:
{
  "type": "text",
  "text": "Found 5 results..."
}

配置文件

这 config.json 文件是所有MCP服务器配置的唯一真实来源。它是git-ignored,以确保您的API密钥的安全。

结构:

{
  "servers": [
    {
      "name": "unique-server-name",
      "endpoint": "https://mcp-server-url.com/path/",
      "api_key": "your-api-key",
      "timeout": 30
    }
  ]
}

领域:

  • name:服务器的唯一标识符(与 --server 旗帜)- 必需的
  • endpoint:MCP服务器的完整URL- 必需的
  • api_key:用于身份验证的API密钥(如果不需要,请保留空字符串)
  • timeout:连接超时(秒)(默认值:30)

重要:所有服务器配置(包括端点和API密钥)都必须存储在 config.jsonCLI不接受这些值作为命令行标志。

项目结构

mcp-cli/
├── main.go                  # Entry point
├── config.json              # Your MCP servers (git-ignored)
├── config.example.json      # Example config template
├── cmd/                     # Cobra commands
│   ├── root.go             # Root command and global flags
│   ├── servers.go          # Server management commands
│   ├── list_tools.go       # List tools command
│   ├── describe_tool.go    # Describe tool command
│   └── call_tool.go        # Call tool command
└── pkg/
    ├── client/             # MCP client wrapper
    │   └── client.go       # Connection and API logic
    └── config/             # Configuration management
        └── config.go       # Config loading and validation

发展

CLI由以下组件构建:

要添加新命令,请在中创建新文件 cmd/ 并用root命令注册它。

许可证

麻省理工学院