项目简介

本项目 'mcp-experiments' 包含了一个简单的 MCP (Model Context Protocol) 服务器的实验性实现,以及一个与之交互的控制台客户端示例。该服务器演示了如何使用 MCP 协议来注册和执行工具,并通过 Stdio 和 SSE (Server-Sent Events) 两种传输协议与客户端进行通信。

主要功能点:

  • 工具注册与执行: 服务器可以注册工具(例如 'fetch-weather'),并允许客户端通过 MCP 协议调用这些工具。
  • Stdio 传输协议: 演示了如何通过标准的输入输出流 (stdio) 建立 MCP 服务器和客户端之间的连接。
  • SSE 传输协议: 演示了如何通过 Server-Sent Events (SSE) 建立 MCP 服务器和客户端之间的连接,适用于 Web 环境。
  • 客户端示例: 提供了一个简单的控制台客户端,可以连接到 MCP 服务器,发现可用的工具,并调用工具执行。

安装步骤:

  1. 克隆仓库:

    git clone https://github.com/dipeshdulal/mcp-experiments.git
    cd mcp-experiments/clients/example-console
  2. 安装依赖: 确保你已经安装了 Bun,然后运行:

    bun install

服务器配置:

1. Stdio 服务器配置

  • 服务器启动命令: 'bun'

  • 命令参数: 'run ./test_mcp_server.ts'

    配置信息 (JSON 格式):

    {
      "serverName": "Test MCP Server",
      "command": "bun",
      "args": ["run", "./test_mcp_server.ts"]
    }

    参数注释:

    • 'serverName': 服务器的名称,用于客户端识别。
    • 'command': 启动服务器的可执行命令,这里使用 'bun' 运行时。
    • 'args': 传递给 'bun' 命令的参数,指定要运行的服务器脚本 './test_mcp_server.ts'。

2. SSE 服务器配置

  • 服务器启动命令: 'bun'

  • 命令参数: 'run ./test_mcp_sse_server.ts'

    配置信息 (JSON 格式):

    {
      "serverName": "Test SSE MCP Server",
      "command": "bun",
      "args": ["run", "./test_mcp_sse_server.ts"]
    }

    参数注释:

    • 'serverName': 服务器的名称,用于客户端识别。
    • 'command': 启动服务器的可执行命令,这里使用 'bun' 运行时。
    • 'args': 传递给 'bun' 命令的参数,指定要运行的服务器脚本 './test_mcp_sse_server.ts'。

    注意: SSE 服务器需要在端口 '3000' 运行,客户端需要连接到 'http://localhost:3000/sse'。

基本使用方法:

  1. 启动 MCP 服务器:

    • Stdio 服务器: 在 'clients/example-console' 目录下,运行 'bun run ./test_mcp_server.ts'。
    • SSE 服务器: 在 'clients/example-console' 目录下,运行 'bun run ./test_mcp_sse_server.ts'。
  2. 运行 MCP 客户端: 在同一个 'clients/example-console' 目录下,运行 'bun run ./index.ts'。

  3. 与客户端交互: 客户端启动后,你可以在控制台中输入消息与 LLM 进行对话。客户端会自动连接到 MCP 服务器,发现并使用服务器提供的工具。当 LLM 需要调用工具时,客户端会提示你是否批准执行。

示例对话 (使用 'fetch-weather' 工具):

You: What's the weather in London?
Assistant wants to use tools:
Tool: fetch-weather
Arguments: {"city":"london"}
Approve tool execution? (y/n): y
Executing tool fetch-weather...
tool execution result:  { content: { type: 'content', content: [ { type: 'text', text: 'The weather in london is sunny.' } ] } }

Assistant: The weather in london is sunny.

信息

分类

开发者工具