通过


为存储过程配置自定义 MCP 工具

重要

SQL 模型上下文协议 (MCP) 服务器在数据 API 生成器版本 1.7 及更高版本中可用。

注释

本节中所述的 SQL MCP Server 2.0 功能目前处于预览状态,在正式发布之前可能会更改。 有关详细信息,请参阅 版本 2.0 中的新增功能。

SQL MCP Server 通过通用数据操作语言 (DML) 工具公开表和视图。 对于存储过程,可以进一步:在实体上设置 custom-tool: true ,以便该过程以命名的专用工具的形式显示在 MCP 工具列表中。 AI 代理按名称发现它,查看其说明,并直接调用它 -- 无需 SQL。

重要

自定义工具名称派生自实体名称,但转换为 snake_case。 例如,名为 GetProductById 的实体在 MCP 工具列表中变为 get_product_by_id。 调用该工具时,请使用snake_case名称。 PascalCase 实体名称不能用作工具名称。

本文的其余部分介绍如何添加、配置和测试存储过程支持的自定义 MCP 工具。

先决条件

  • 数据 API 生成器版本 2.0 或更高版本
  • 具有至少一个存储过程的 SQL Server 数据库
  • 启用了 MCP 的现有dab-config.json
  • 已安装 DAB CLI

在配置中启用 MCP

如果尚未启用 MCP,可在运行时部分中启用 MCP。

dab configure --runtime.mcp.enabled true

这会将以下内容添加到你的 dab-config.json

{
  "runtime": {
    "mcp": {
      "enabled": true
    }
  }
}

将存储过程添加为自定义工具

使用dab add--source.type stored-procedure--mcp.custom-tool true

dab add GetProductById \
  --source dbo.get_product_by_id \
  --source.type "stored-procedure" \
  --permissions "anonymous:execute" \
  --mcp.custom-tool true

这会在实体 dab-config.json 中生成以下实体:

{
  "entities": {
    "GetProductById": {
      "source": {
        "object": "dbo.get_product_by_id",
        "type": "stored-procedure"
      },
      "graphql": {
        "enabled": true,
        "operation": "mutation",
        "type": {
          "singular": "GetProductById",
          "plural": "GetProductByIds"
        }
      },
      "rest": {
        "enabled": true,
        "methods": [
          "post"
        ]
      },
      "permissions": [
        {
          "role": "anonymous",
          "actions": [
            {
              "action": "execute"
            }
          ]
        }
      ],
      "mcp": {
        "custom-tool": true
      }
    }
  }
}

重要

custom-tool 属性仅在存储过程实体上有效。 在表或视图实体上设置它会导致启动时出现配置错误。

添加描述以提高代理准确性

如果没有说明,代理将仅看到技术名称 GetProductById。 使用说明,他们了解它的作用以及何时使用它。

dab update GetProductById \
  --description "Returns full product details including pricing and inventory for a given product ID"
{
  "entities": {
    "GetProductById": {
      "description": "Returns full product details including pricing and inventory for a given product ID",
      "source": {
        "object": "dbo.get_product_by_id",
        "type": "stored-procedure"
      },
      "fields": [],
      "graphql": {
        "enabled": true,
        "operation": "mutation",
        "type": {
          "singular": "GetProductById",
          "plural": "GetProductByIds"
        }
      },
      "rest": {
        "enabled": true,
        "methods": [
          "post"
        ]
      },
      "permissions": [
        {
          "role": "anonymous",
          "actions": [
            {
              "action": "execute"
            }
          ]
        }
      ],
      "mcp": {
        "custom-tool": true
      }
    }
  }
}

验证工具是否显示在工具列表中

启动 DAB 并调用 tools/list MCP 终结点以确认工具已注册。

dab start

当 MCP 客户端调用 tools/list时,响应将自定义工具与 DML 工具一起包括:

{
  "tools": [
    {
      "name": "get_product_by_id",
      "description": "Returns full product details including pricing and inventory for a given product ID",
      "inputSchema": {
        "type": "object",
        "properties": {}
      }
    }
  ]
}

注释

工具名称使用snake_case(例如,get_product_by_id用于GetProductById实体)。 inputSchema当前返回空 properties。 代理依赖于工具说明和 describe_entities 来确定正确的参数。

配置多个自定义工具

可以在同一配置中将多个存储过程注册为自定义工具。

dab add SearchProducts \
  --source dbo.search_products \
  --source.type "stored-procedure" \
  --permissions "anonymous:execute" \
  --mcp.custom-tool true \
  --description "Full-text search across product names and descriptions"

dab add GetOrderSummary \
  --source dbo.get_order_summary \
  --source.type "stored-procedure" \
  --permissions "authenticated:execute" \
  --mcp.custom-tool true \
  --description "Returns order totals and line item counts for a given customer"

控制哪些角色可以调用该工具

自定义工具遵循与所有其他 DAB 实体相同的基于角色的访问控制(RBAC)。 设置实体上的 permissions,以限制哪些角色可以执行该过程。

{
  "entities": {
    "GetOrderSummary": {
      "source": {
        "object": "dbo.get_order_summary",
        "type": "stored-procedure"
      },
      "graphql": {
        "enabled": true,
        "operation": "mutation",
        "type": {
          "singular": "GetOrderSummary",
          "plural": "GetOrderSummarys"
        }
      },
      "rest": {
        "enabled": true,
        "methods": [
          "post"
        ]
      },
      "permissions": [
        {
          "role": "authenticated",
          "actions": [
            {
              "action": "execute"
            }
          ]
        }
      ],
      "mcp": {
        "custom-tool": true
      }
    }
  }
}

当代理以 anonymous 角色调用时,get_order_summary 不会出现在 tools/list 中,并且任何直接 tools/call 操作都会返回权限错误。

禁用自定义工具而不将其删除

custom-tool 被设置为 false 以从代理中隐藏工具,而无需删除实体。

dab update GetProductById \
  --mcp.custom-tool false

实体保留在配置中,以后可以通过设置 --mcp.custom-tool true重新启用它。