项目简介

'developers-guide-to-ai' 仓库包含了一本名为《The Developer's Guide to AI - From Prompts to Agents》书籍的代码示例。在 'part5/mcp' 目录下,提供了两个基于Model Context Protocol (MCP) SDK实现的服务器示例:一个通用计算器服务和一个个人费用追踪器服务。这些示例展示了如何定义和管理资源(Resources)以及注册和执行工具(Tools),以便大型语言模型(LLM)客户端能够通过标准化的JSON-RPC协议与这些后端功能进行交互。

主要功能点

  • 资源托管与管理: MCP服务器可以托管和管理各种资源,例如通过API获取的处理过的数据,或从数据库查询的用户列表。LLM客户端可以请求这些资源以获取上下文信息。
  • 工具注册与执行: 服务器注册各种工具(例如执行加法运算的计算器工具,或向数据库添加/查询费用的工具),LLM客户端可以通过调用这些工具来执行外部功能,实现智能自动化。
  • 标准化通信: 使用JSON-RPC协议进行通信,确保LLM客户端与后端服务之间的数据交换具有一致性和可预测性。
  • 会话管理与能力声明: 服务器支持会话管理,并能够向客户端声明其提供的资源和工具能力。

安装步骤

  1. 克隆仓库:
    git clone https://github.com/jorshali/developers-guide-to-ai.git
    cd developers-guide-to-ai
  2. 进入MCP示例目录:
    cd part5/mcp
  3. 安装Node.js依赖:
    npm install
  4. (可选)启动Express后端服务: 其中一个MCP服务器示例 'mcp-server.ts' 依赖一个Express后端。如果需要运行该示例,请先启动Express服务:
    npm run start-express-server
    此命令将启动一个监听在 'http://localhost:3000' 的Express服务器。

服务器配置

MCP客户端需要配置MCP服务器的启动命令及其参数才能建立连接。以下是两个MCP服务器示例的推荐配置信息:

  • 通用计算器MCP服务器 (my_custom_calculator): 此MCP服务器示例暴露了一个特殊的加法工具和一个获取数据的资源。

    {
      "serverName": "my_custom_calculator",
      "command": "npm",
      "args": ["run", "start-calculator-mcp-server"],
      "description": "提供了一个加法工具和获取数据的资源,依赖本地运行的Express后端服务。",
      "capabilities": {
        "resources": {
          "get_processed_data": {
            "description": "获取处理过的数据。"
          }
        },
        "tools": {
          "special_calculator_add": {
            "description": "以特殊方式将两个数字相加。",
            "inputSchema": {
              "number1": { "type": "number", "description": "第一个数字" },
              "number2": { "type": "number", "description": "第二个数字" }
            },
            "outputSchema": {
              "result": { "type": "number", "description": "加法结果" }
            }
          }
        }
      }
    }

    注意:运行此服务器前,请确保已启动Express后端服务('npm run start-express-server')。

  • 费用追踪器MCP服务器 (my_expense_tracker): 此MCP服务器示例提供了添加、获取费用以及列出用户的功能,需要MongoDB数据库支持。

    {
      "serverName": "my_expense_tracker",
      "command": "npm",
      "args": ["run", "start-expense-tracker-mcp-server"],
      "description": "提供费用管理工具(添加/获取费用)和用户列表资源,需要MongoDB数据库。",
      "capabilities": {
        "resources": {
          "list_users": {
            "description": "列出数据库中的所有用户。"
          }
        },
        "tools": {
          "add_expense": {
            "description": "向数据库添加一项费用。",
            "inputSchema": {
              "amount": { "type": "number", "description": "费用金额" },
              "description": { "type": "string", "description": "费用描述" },
              "date": { "type": "string", "description": "费用日期" }
            },
            "outputSchema": {
              "content": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "type": { "type": "string", "enum": ["text"] },
                    "text": { "type": "string" }
                  },
                  "required": ["type", "text"]
                }
              }
            }
          },
          "get_expenses": {
            "description": "从数据库获取所有费用。",
            "outputSchema": {
              "content": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "type": { "type": "string", "enum": ["text"] },
                    "text": { "type": "string" }
                  },
                  "required": ["type", "text"]
                }
              }
            }
          }
        }
      }
    }

    注意:运行此服务器前,请确保已启动本地MongoDB数据库服务。

基本使用方法

MCP服务器启动后,将通过Stdio(标准输入/输出)与MCP客户端进行通信。通常,您无需直接与服务器的Stdio交互。MCP客户端将根据上述配置信息,通过执行相应的 'command' 和 'args' 来启动并连接到MCP服务器。一旦连接建立,客户端即可通过JSON-RPC协议向服务器发送请求,例如调用已注册的工具或读取托管的资源。

例如,一个LLM客户端可以:

  • 请求 'my_custom_calculator' 服务器的 'get_processed_data' 资源。
  • 调用 'my_custom_calculator' 服务器的 'special_calculator_add' 工具,传入 'number1' 和 'number2' 参数。
  • 调用 'my_expense_tracker' 服务器的 'add_expense' 工具,传入费用金额、描述和日期。
  • 请求 'my_expense_tracker' 服务器的 'list_users' 资源。

信息

分类

AI与计算