AI助手CLI应用

一个使用Anthropic API与MCP客户端和服务器交互的AI助手命令行聊天应用,支持工具调用和时间查询功能。

  • by t-shiratori
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 t-shiratori/mcp-client-example

使用说明

Mcp客户端示例

这是一个用于AI助手的示例CLI聊天应用程序,使用Anthropic API与MCP客户端和MCP服务器。

参考: 面向客户端开发人员-模型上下文协议

无烟煤API: 初始设置-Anthropic 信息-人物

系统图

序列图

sequenceDiagram
    participant User
    participant MCPClient
    participant AnthropicAPI
    participant MCPServer

    User->>MCPClient: Run
    MCPClient->>MCPServer: Connect and get tool info
    MCPServer-->>MCPClient: Tool info

    loop Chat loop (until 'quit' is entered)
        User->>MCPClient: Enter query
        MCPClient->>AnthropicAPI: Send query
        AnthropicAPI-->>MCPClient: AI response

        alt When tool usage is required
            MCPClient->>MCPServer: Call tool
            MCPServer-->>MCPClient: Tool execution result
            MCPClient->>AnthropicAPI: Send query again including tool result
            AnthropicAPI-->>MCPClient: Final response
        end

        MCPClient-->>User: Display response
    end

    User->>MCPClient: Enter 'quit'
    MCPClient->>MCPServer: Disconnect
    MCPClient-->>User: Exit

设置

pnpm install

.env

ANTHROPIC_API_KEY=your-api-key
MCP_SERVER_SCRIPT_PATH=/path/to/mcp-server.js

构建

pnpm build

跑

pnpm start

输出示例

使用此mcp服务器 https://github.com/t-shiratori/time-tools-mcp-server


mcp-client-example $ pnpm start

> mcp-client-example@1.0.0 start /path/to/mcp-client-example
> node build/index.js

Example MCP Server running on stdio
Connected to server with tools: [ 'get_current_date_time', 'get_elapsed_time' ]

MCP Client Started!
Type your queries or 'quit' to exit.

🖌 Query: Hello

Hello! I'm here to assist you. I can help you with tasks related to date and time, such as getting the current date and time or calculating the elapsed time between two dates.

Is there something specific you'd like to know about dates or times today?

🖌 Query: What time is it?

I can check the current date and time for you.
[Calling tool get_current_date_time with args {}]
Based on the given timestamp, the time is 10:37:19 AM on May 6th, 2025.