mcpField
M
Mcp client example

Mcp client example

本地服务
github.com/t-shiratoriAI 与智能体077 周前更新

关于 Mcp client example

一个命令行聊天示例应用,将 Anthropic API 与 MCP 客户端和服务端结合。它演示连接 MCP 服务、获取其工具,并在助手聊天中使用这些工具。

主要功能

  1. 01命令行聊天应用
  2. 02Anthropic API 集成
  3. 03连接 MCP 客户端与服务端
  4. 04获取服务端工具信息

适用场景

  1. 01试用由 Anthropic 驱动的 MCP 聊天
  2. 02查看 MCP 服务公开的工具
  3. 03了解 MCP 客户端交互流程

MCP 相关

客户端连接配置的 MCP 服务,并获取其工具信息供聊天使用。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

MCP 客户端示例

这是一个示例 CLI 聊天应用,使用 Anthropic API,并与 MCP 客户端和 MCP 服务配合,为 AI 助手提供支持。

参考: 面向客户端开发者 - Model Context Protocol

Anthropic API: 初始设置 - Anthropic 消息 - Anthropic

系统图

README 配图

时序图

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

> [email protected] 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.

评论

支持 Markdown

第一次接触 MCP?

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

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