AI聊天CLI工具

一个基于Claude的聊天CLI应用,通过Model Context Protocol (MCP)与外部工具、数据源和服务进行交互。

  • by nathankrishnan
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 nathankrishnan/mcp-reference-example

使用说明

MCP聊天参考示例

Claude支持的聊天CLI应用程序,与 模型上下文协议(MCP) 服务器。

描述

模型上下文协议(MCP) 是一个开放标准,允许AI模型以可组合的方式与外部工具、数据源和服务进行交互。您可以定义MCP服务器,而不是硬编码集成,这些服务器公开:

  • 工具:Claude可以调用的函数
  • 资源:克劳德可以读取的数据
  • 提示:可重用提示模板

然后,任何兼容MCP的客户端都可以使用它们。

该项目展示了完整的端到端集成:

  • 一 MCP服务器 建于 FastMCP 它管理一组模拟文档,将它们作为工具、资源和提示模板公开。
  • 一 MCP客户端 它连接到一个或多个服务器,发现它们的功能,并将它们作为工具公开给Claude。
  • A. Claude驱动的聊天CLI 使用代理工具使用循环、document@references和slash命令,所有这些都通过带有标签完成的终端UI驱动。

建筑

+---------+     +-----------+     +------------+
|   CLI   | --> | Chat Loop | <-> | Claude API |
+---------+     +-----------+     +------------+
                      |
                      v
               +------------+     +------------+
               | MCP Client | --> | MCP Server |
               +------------+     +------------+
                                  tools, resources, prompts

您在中键入消息 命令行界面,它解析@mentions和slash命令,并用文档上下文丰富提示。这 聊天循环 将提示发送到 克劳德,这可能会请求工具调用。这 MCP客户端 将这些呼叫转发给 MCP服务器 并返回结果。重复此循环,直到Claude产生最终响应。

特性

  • 代理工具使用循环:Claude自主调用MCP工具(读取/编辑文档),直到它有足够的信息做出响应
  • @提及:引用任何内联文档(例如。 compare @report.pdf with @financials.docx)其内容被自动提取并注入到提示中
  • 斜杠命令:直接从CLI调用服务器端提示模板(/format, /summarize)
  • Tab 补全:斜线命令、文档ID和@提及的自动补全
  • 多服务器支持:同时连接多个MCP服务器;克劳德看到他们所有的工具都统一了
  • MCP图元:服务器演示了所有三个MCP原语:工具、资源和提示

先决条件

设置

  1. 安装依赖项

    使用 uv (推荐):

    uv sync
    

    或与 pip:

    pip install -e .
    
  2. 配置环境变量

    创建一个 .env 项目根目录中的文件:

    ANTHROPIC_API_KEY=sk-ant-...       # Your Anthropic API key
    CLAUDE_MODEL=claude-sonnet-4-6     # Claude model to use
    USE_UV=1                           # 1 if using uv, 0 if using python
    
  3. 运行应用程序

    uv run main.py
    # or
    python main.py
    

用法

定期聊天

只需键入您的信息,然后按Enter键。Claude可以访问用于阅读和编辑与服务器捆绑在一起的示例模拟文档的工具。

> What documents are available?
> Read the deposition and give me a brief summary.
> Edit report.pdf to fix the typo in the second paragraph.

@提及

在任何文档ID前添加前缀 @ 自动获取其内容并将其包含在提示中,而无需调用工具。

> What are the key differences between @financials.docx and @outlook.pdf?
> Does @deposition.md contradict anything in @report.pdf?

键入后可以完成选项卡 @.

斜杠命令

Slash命令调用服务器端提示模板。选项卡补全可用于命令和文档ID。

> /summarize report.pdf
> /format spec.txt
命令 描述
/summarize <doc_id> 总结文档
/format <doc_id> 将文档重新格式化为Markdown

连接其他MCP服务器

将其他服务器脚本作为参数传递给 main.py 在内置文档服务器旁边连接它们:

uv run main.py path/to/other_server.py
# or
python main.py path/to/other_server.py

所有服务器的工具都会自动聚合并提供给Claude。

退出

> :q
> :quit

有用的命令

# Run the chat app
uv run main.py
# or
python main.py

# Inspect the MCP server interactively with the MCP developer tools
uv run mcp dev server/mcp_server.py

# Run all tests
pytest

# Run tests with verbose output
pytest -v

项目结构

+---------------------+     +-----------------------------+     +----------------+
| CLI                 | --> | Chat Loop                   | <-> | Claude API     |
| core/cli.py         |     | core/chat.py                |     | core/claude.py |
| core/cli_chat.py    |     | core/tools.py               |     +----------------+
+---------------------+     +-----------------------------+
                                        |
                                        v
                             +------------------+     +------------------------+
                             | MCP Client       | --> | MCP Server             |
                             | client/          |     | server/mcp_server.py   |
                             | mcp_client.py    |     +------------------------+
                             +------------------+

                             main.py wires all of the above together
类别 文件 描述
入口点 main.py 将所有服务连接在一起并启动CLI
MCP服务器 server/mcp_server.py 模拟文档工具、资源和提示模板
MCP客户端 client/mcp_client.py MCP SDK周围的异步MCP客户端包装器
聊天 core/chat.py 代理工具使用循环
聊天 core/tools.py 跨多个MCP客户端的工具聚合和执行
聊天 core/claude.py 带有消息历史记录的Anthropic SDK包装器
CLI core/cli.py 带有标签完成的交互式REPL
CLI core/cli_chat.py @notice和slash命令处理
测试 tests/ 所有核心模块和MCP客户端的单元测试