mcpField
M
MCP CLI client

MCP CLI client

github.com/williamvd4框架与 SDK186 周前更新

关于 MCP CLI client

MCP CLI client 是一个命令行工具,支持任何 MCP 兼容服务器,可与 OpenAI、Groq 或本地模型等多种 LLM 配合使用。

主要功能

  1. 01命令行接口
  2. 02多 LLM 提供商支持
  3. 03提示词模板
  4. 04图像输入分析
  5. 05工具执行确认
  6. 06剪贴板集成

适用场景

  1. 01从终端快速查询大模型
  2. 02使用 MCP 服务扩展 LLM 能力
  3. 03通过模板自动化常见任务
  4. 04分析图像和截图

MCP 相关

该客户端提供了一个 CLI 接口来使用任何 MCP 兼容服务器,使用户可在终端中灵活配置和执行工具。

详细介绍

MCP CLI 客户端

一个简单的 CLI 程序,用于运行大模型提示词并实现 Model Context Protocol (MCP) 客户端。

您可以在终端的便利性中使用任何 MCP 兼容的服务。

这可作为 Claude Desktop 的替代客户端。此外,您可以通过 llama 使用任何大模型提供商,如 OpenAI、Groq 或本地大模型。

C4 图表

设置

  1. 通过 pip 安装:

    pip install mcp-client-cli
    
  2. 创建 ~/.llm/config.json 文件来配置您的大模型和 MCP 服务:

    {
      "systemPrompt": "You are an AI assistant helping a software engineer...",
      "llm": {
        "provider": "openai",
        "model": "gpt-4",
        "api_key": "your-openai-api-key",
        "temperature": 0.7,
        "base_url": "https://api.openai.com/v1"  // Optional, for OpenRouter or other providers
      },
      "mcpServers": {
        "fetch": {
          "command": "uvx",
          "args": ["mcp-server-fetch"],
          "requires_confirmation": ["fetch"],
          "enabled": true,  // Optional, defaults to true
          "exclude_tools": []  // Optional, list of tool names to exclude
        },
        "brave-search": {
          "command": "npx",
          "args": ["-y", "@modelcontextprotocol/server-brave-search"],
          "env": {
            "BRAVE_API_KEY": "your-brave-api-key"
          },
          "requires_confirmation": ["brave_web_search"]
        },
        "youtube": {
          "command": "uvx",
          "args": ["--from", "git+https://github.com/adhikasp/mcp-youtube", "mcp-youtube"]
        }
      }
    }
    

    注意:

    • 使用 requires_confirmation 指定哪些工具在执行前需要用户确认
    • 大模型 API 密钥也可以通过环境变量 LLM_API_KEY 或 OPENAI_API_KEY 设置
    • 配置文件可以放在 ~/.llm/config.json 或 $PWD/.llm/config.json
    • 您可以用 // 注释 JSON 配置文件以切换配置
  3. 运行 CLI:

    llm "What is the capital city of North Sumatra?"
    

使用

基本用法
$ llm What is the capital city of North Sumatra?
The capital city of North Sumatra is Medan.

您可以省略引号,但要小心 bash 特殊字符,如 &、|、; 等可能被您的 shell 解释的字符。

您也可以从其他命令或文件输入管道数据:

$ echo "What is the capital city of North Sumatra?" | llm
The capital city of North Sumatra is Medan.

$ echo "Given a location, tell me its capital city." > instructions.txt
$ cat instruction.txt | llm "West Java"
The capital city of West Java is Bandung.
图像输入

您可以通过管道输入图像文件,使用多模态大模型来分析它们:

$ cat image.jpg | llm "What do you see in this image?"
[LLM will analyze and describe the image]

$ cat screenshot.png | llm "Is there any error in this screenshot?"
[LLM will analyze the screenshot and point out any errors]
使用提示词模板

您可以使用 p 前缀后跟模板名称和其参数来使用预定义的提示词模板:

# List available prompt templates
$ llm --list-prompts

# Use a template
$ llm p review  # Review git changes
$ llm p commit  # Generate commit message
$ llm p yt url=https://youtube.com/...  # Summarize YouTube video
触发工具
$ llm What is the top article on hackernews today?

================================== Ai Message ==================================
Tool Calls:
  brave_web_search (call_eXmFQizLUp8TKBgPtgFo71et)
 Call ID: call_eXmFQizLUp8TKBgPtgFo71et
  Args:
    query: site:news.ycombinator.com
    count: 1
Brave Search MCP Server running on stdio

# If the tool requires confirmation, you'll be prompted:
Confirm tool call? [y/n]: y

================================== Ai Message ==================================
Tool Calls:
  fetch (call_xH32S0QKqMfudgN1ZGV6vH1P)
 Call ID: call_xH32S0QKqMfudgN1ZGV6vH1P
  Args:
    url: https://news.ycombinator.com/
================================= Tool Message =================================
Name: fetch

[TextContent(type='text', text='Contents [REDACTED]]
================================== Ai Message ==================================

The top article on Hacker News today is:

### [Why pipes sometimes get "stuck": buffering](https://jvns.ca)
- **Points:** 31
- **Posted by:** tanelpoder
- **Posted:** 1 hour ago

You can view the full list of articles on [Hacker News](https://news.ycombinator.com/)

若要绕过工具确认要求,请使用 --no-confirmations 标志:

$ llm --no-confirmations "What is the top article on hackernews today?"

要在 bash 脚本中使用,请添加 --no-intermediates,这样它只会打印最后的结论消息,不会打印中间消息。

$ llm --no-intermediates "What is the time in Tokyo right now?"
继续对话

在您的消息前加上 c 前缀以继续最后一次对话。

$ llm asldkfjasdfkl
It seems like your message might have been a typo or an error. Could you please clarify or provide more details about what you need help with?
$ llm c what did i say previously?
You previously typed "asldkfjasdfkl," which appears to be a random string of characters. If you meant to ask something specific or if you have a question, please let me know!
剪贴板支持

您可以使用 cb 命令从剪贴板使用内容:

# After copying text to clipboard
$ llm cb
[LLM will process the clipboard text]

$ llm cb "What language is this code written in?"
[LLM will analyze the clipboard text with your question]

# After copying an image to clipboard
$ llm cb "What do you see in this image?"
[LLM will analyze the clipboard image]

# You can combine it with continuation
$ llm cb c "Tell me more about what you see"
[LLM will continue the conversation about the clipboard content]

剪贴板功能在以下环境中有效:

  • 本机 Windows / macOS / Linux 环境
    • Windows:使用 PowerShell
    • macOS:使用 pbpaste 处理文本,pngpaste 处理图像(可选)
    • Linux:使用 xclip(剪贴板支持必需)
  • Windows 子系统 Linux (WSL)
    • 通过 PowerShell 访问 Windows 剪贴板
    • 支持文本和图像
    • 确保您可以从 WSL 访问 powershell.exe

剪贴板支持所需工具:

  • Windows:PowerShell(内置)
  • macOS:
    • pbpaste(内置)用于文本
    • pngpaste(可选)用于图像:brew install pngpaste
  • Linux:
    • xclip:sudo apt install xclip 或等效命令

CLI 会自动检测剪贴板内容是文本还是图像,并进行相应处理。

其他选项
$ llm --list-tools                # List all available tools
$ llm --list-prompts              # List available prompt templates
$ llm --no-tools                  # Run without any tools
$ llm --force-refresh             # Force refresh tool capabilities cache
$ llm --text-only                 # Output raw text without markdown formatting
$ llm --show-memories             # Show user memories
$ llm --model gpt-4               # Override the model specified in config

贡献

欢迎提交问题和拉取请求以改进或修复错误。

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。