MCP命令行工具

用于与模型上下文协议(MCP)服务器交互的命令行客户端,支持列出可用工具和调用特定工具。

  • by moritalous
  • Python
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 moritalous/mcp-tools-cli

使用说明

mcp工具cli

这是一个用于与模型上下文协议(MCP)服务器交互的命令行客户端。

安装

您可以安装 mcp-tools-cli 使用pip:

pip install mcp-tools-cli

配置

客户端使用名为的配置文件 mcp_config.json 以存储MCP服务器的连接详细信息。创建一个名为的文件 mcp_config.json 内容如下:

{
  "mcpServers": {
    "time": {
      "command": "python",
      "args": ["-m", "mcp_server_time", "--local-timezone=America/New_York"]
    }
  }
}

用您自己的MCP服务器配置替换这些值。

用法

mcp-tools-cli <action> --mcp-name <mcp_name> [options]

参数

  • action (必填):要执行的操作。必须是以下之一:
    • list-tools:列出MCP服务器上可用的工具。
    • call-tool:调用MCP服务器上的特定工具。
  • --mcp-name (必填):要连接的MCP服务器的名称,如中所定义 mcp_config.json.
  • --tool-name (需要 call-tool action):要调用的工具的名称。
  • --tool-args (可选):工具的参数。可以是JSON字符串或单个字符串值。如果单个字符串值不是有效的JSON,它将作为 query 工具的论点。
  • --config-path (可选):路径 mcp_config.json 文件。默认为 mcp_config.json 在当前目录中。

配置

客户端使用名为的配置文件 mcp_config.json 以存储MCP服务器的连接详细信息。文件应采用以下格式:

{
  "mcpServers": {
    "<mcp_name>": {
      "command": "<command to run the server>",
      "args": ["<argument1>", "<argument2>", ...],
      "env": {
        "<environment variable name>": "<environment variable value>",
        ...
      }
    },
    ...
  }
}

替换 <mcp_name> 使用您的MCP服务器的名称(例如。, time).这 command, args,以及 env 字段指定如何运行服务器。

例子

使用 时间MCP服务器 在mcp_config.sample.json中提供。如果你想使用这个,请执行 pip install mcp-server-time 事先。

  1. 列出可用工具:
mcp-tools-cli list-tools --mcp-name time --config-path mcp_config.sample.json
  1. 打电话给 get_current_time 带有查询的工具:
mcp-tools-cli call-tool --mcp-name time --tool-name get_current_time --config-path mcp_config.sample.json

错误处理

如果发生任何错误,客户端将向控制台打印错误消息,例如:

  • FileNotFoundError:如果找不到配置文件。
  • json。JSONDecodeError:如果配置文件不是有效的JSON文件。
  • ValueError:如果在配置文件中找不到MCP服务器,或者缺少命令。
  • argparse。ArgumentError:如果命令行参数无效。
  • 工具执行过程中的其他异常。