ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

如何用 MCP Toolbox 在本地跑通 PostgreSQL 自定义工具并用 MCP Inspector 验证工具列表

2026/9/14 22:19:51 拓冰建站 浏览量
如何用 MCP Toolbox 在本地跑通 PostgreSQL 自定义工具并用 MCP Inspector 验证工具列表 如何用 MCP Toolbox 在本地跑通 PostgreSQL 自定义工具并用 MCP Inspector 验证工具列表【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox这条路径对应一个具体的首次使用任务在本地准备一个 PostgreSQL 数据库下载 MCP Toolbox for Databases 二进制在tools.yaml中定义指向该数据库的自定义postgres-sql工具启动 Toolbox 服务器最后用 MCP Inspector 通过tools/list确认自定义工具都出现在工具列表中。适用前提是本地已安装 PostgreSQL 16 和psql客户端且能通过npx运行 MCP InspectorNode.js 环境。文档同时要求通过 MCP 使用 Toolbox 时版本需在 0.3.0 以上本文使用 1.11.0 二进制。准备条件开始前确认三件事PostgreSQL 16 与psql已安装且数据库服务正在运行。若连接被拒绝Connection refused在 Linux 上可用sudo systemctl status postgresql查看状态、sudo systemctl start postgresql启动服务机器上有curl与chmod下载并启用二进制已安装 Node.js可以执行npxMCP Inspector 的运行方式。如果psql -U postgres提示密码或报FATAL: role postgres does not exist说明你的安装使用了其他认证方式。文档给出的通用做法是切换到postgres操作系统用户peer 认证在本地连接时通常无需密码sudo -i -u postgres psql -h 127.0.0.1进入psql后执行下面的建库步骤结束后先输入\q退出psql再输入exit回到普通用户 shell。创建数据库、用户和示例数据按 MCP 快速入门 的步骤用psql连接到本地 PostgreSQLpostgres为默认的超级用户角色psql -h 127.0.0.1 -U postgres创建 Toolbox 要使用的专用用户和数据库并把库的所有权交给该用户。文档同时提示真实应用中应遵循最小权限原则只授予应用需要的权限。CREATE USER toolbox_user WITH PASSWORD my-password; CREATE DATABASE toolbox_db; GRANT ALL PRIVILEGES ON DATABASE toolbox_db TO toolbox_user; ALTER DATABASE toolbox_db OWNER TO toolbox_user;输入\q结束会话然后用新用户连接新库psql -h 127.0.0.1 -U toolbox_user -d toolbox_db在toolbox_db中创建文档使用的hotels表并插入 10 条示例数据示例数据来自文档仅用于演示工具调用CREATE TABLE hotels( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR NOT NULL, location VARCHAR NOT NULL, price_tier VARCHAR NOT NULL, checkin_date DATE NOT NULL, checkout_date DATE NOT NULL, booked BIT NOT NULL ); INSERT INTO hotels(id, name, location, price_tier, checkin_date, checkout_date, booked) VALUES (1, Hilton Basel, Basel, Luxury, 2024-04-22, 2024-04-20, B0), (2, Marriott Zurich, Zurich, Upscale, 2024-04-14, 2024-04-21, B0), (3, Hyatt Regency Basel, Basel, Upper Upscale, 2024-04-02, 2024-04-20, B0), (4, Radisson Blu Lucerne, Lucerne, Midscale, 2024-04-24, 2024-04-05, B0), (5, Best Western Bern, Bern, Upper Midscale, 2024-04-23, 2024-04-01, B0), (6, InterContinental Geneva, Geneva, Luxury, 2024-04-23, 2024-04-28, B0), (7, Sheraton Zurich, Zurich, Upper Upscale, 2024-04-27, 2024-04-02, B0), (8, Holiday Inn Basel, Basel, Upper Midscale, 2024-04-24, 2024-04-09, B0), (9, Courtyard Zurich, Zurich, Upscale, 2024-04-03, 2024-04-13, B0), (10, Comfort Inn Bern, Bern, Midscale, 2024-04-04, 2024-04-16, B0);再次输入\q结束会话。至此数据库侧就绪toolbox_db库、toolbox_user用户密码my-password、hotels表。下载 Toolbox 二进制并编写 tools.yaml下载对应操作系统和 CPU 架构的二进制。下面以linux/amd64为例文档给出的版本为 1.11.0OS可选值包括linux/amd64、darwin/arm64、darwin/amd64、windows/amd64、windows/arm64其他架构可查阅文档中的 发布页说明export OSlinux/amd64 # one of linux/amd64, darwin/arm64, darwin/amd64, windows/amd64, or windows/arm64 curl -O https://storage.googleapis.com/mcp-toolbox-for-databases/v1.11.0/$OS/toolbox文档还建议可选在下载后验证二进制完整性例如 Linux 上下载同目录的toolbox.asc签名文件、导入 Google 公钥后执行gpg --verify toolbox.asc toolboxmacOS 上执行codesign -v --verbose4 toolbox。这一步会下载额外的签名文件或公钥属于自愿的安全校验。让二进制可执行Linux 和 macOSchmod x toolbox把以下内容写入tools.yaml。文档明确提醒如果你在上一步自定义了user、password或database这里要同步修改实践中建议用${ENV_NAME}形式的环境变量替换来代替硬编码密钥。配置里包含一个kind: source的 PostgreSQL 数据源、五个kind: tool的自定义postgres-sql工具以及一个kind: toolset工具集kind: source name: my-pg-source type: postgres host: 127.0.0.1 port: 5432 database: toolbox_db user: toolbox_user password: my-password --- kind: tool name: search-hotels-by-name type: postgres-sql source: my-pg-source description: Search for hotels based on name. parameters: - name: name type: string description: The name of the hotel. statement: SELECT * FROM hotels WHERE name ILIKE % || $1 || %; --- kind: tool name: search-hotels-by-location type: postgres-sql source: my-pg-source description: Search for hotels based on location. parameters: - name: location type: string description: The location of the hotel. statement: SELECT * FROM hotels WHERE location ILIKE % || $1 || %; --- kind: tool name: book-hotel type: postgres-sql source: my-pg-source description: - Book a hotel by its ID. If the hotel is successfully booked, returns a NULL, raises an error if not. parameters: - name: hotel_id type: string description: The ID of the hotel to book. statement: UPDATE hotels SET booked B1 WHERE id $1; --- kind: tool name: update-hotel type: postgres-sql source: my-pg-source description: - Update a hotels check-in and check-out dates by its ID. Returns a message indicating whether the hotel was successfully updated or not. parameters: - name: hotel_id type: string description: The ID of the hotel to update. - name: checkin_date type: string description: The new check-in date of the hotel. - name: checkout_date type: string description: The new check-out date of the hotel. statement: - UPDATE hotels SET checkin_date CAST($2 as date), checkout_date CAST($3 as date) WHERE id $1; --- kind: tool name: cancel-hotel type: postgres-sql source: my-pg-source description: Cancel a hotel by its ID. parameters: - name: hotel_id type: string description: The ID of the hotel to cancel. statement: UPDATE hotels SET booked B0 WHERE id $1; --- kind: toolset name: my-toolset tools: - search-hotels-by-name - search-hotels-by-location - book-hotel - update-hotel - cancel-hotel几个配置要点依据 Tools 配置文档statement中的$1是 PostgreSQL 预编译语句占位符参数按parameters列表顺序绑定这也是文档建议的防 SQL 注入方式参数默认是必填的省略required等同required: true若调用时缺少必填参数会得到parameter airline is required一类的报错这是文档示例中的提示格式kind: toolset只是把工具逻辑分组方便按 Toolset 文档 所说的方式供不同 agent 加载。文档同时提示 toolset 是仅含工具的 group新配置建议迁移到kind: group可用toolbox migrate自动转换但kind: toolset仍然可用。启动 Toolbox 服务器在tools.yaml所在目录启动服务器./toolbox --config tools.yamlToolbox 默认启用配置动态重载如需关闭可加--disable-reload。按 MCP Client 文档HTTP 客户端连接的端点是http://127.0.0.1:5000/mcp如果只想暴露某个工具集可用http://127.0.0.1:5000/mcp/{toolset_name}。用 MCP Inspector 验证工具列表在另一个终端运行 Inspectornpx modelcontextprotocol/inspector提示安装 inspector 包时输入y。启动成功后终端会显示类似下面的内容文档示例YOUR_SESSION_TOKEN是本次会话实际生成的 token需记录下来Starting MCP inspector... ⚙️ Proxy server listening on localhost:6277 Session token: YOUR_SESSION_TOKEN Use this token to authenticate requests or set DANGEROUSLY_OMIT_AUTHtrue to disable auth MCP Inspector is up and running at: http://localhost:6274/?MCP_PROXY_AUTH_TOKENYOUR_SESSION_TOKEN在浏览器中打开该链接然后在 Inspector 页面做如下设置Transport Type选择Streamable HTTPURL填写http://127.0.0.1:5000/mcpConfiguration→Proxy Session Token确认填入的是刚才记录的那个YOUR_SESSION_TOKEN点击Connect选择List Tools。成功条件List Tools返回的工具列表中包含tools.yaml里配置的 5 个工具——search-hotels-by-name、search-hotels-by-location、book-hotel、update-hotel、cancel-hotel。看到它们即说明自定义工具已被 Toolbox 加载并可被 MCP 客户端发现。文档在 MCP 快速入门 的对应位置给出了 Inspector 连接界面与工具列表的截图inspector.png、inspector_tools.png位于docs/en/documentation/getting-started/mcp_quickstart/目录可对照确认界面状态。此时也可以直接在 Inspector 里对工具发起调用做初步测试。限制与替代路径stdio 方式Toolbox 也支持 stdio 传输。如果改用 stdio需以./toolbox --stdio启动日志默认warn级别不支持debug/infoInspector 则用npx modelcontextprotocol/inspector ./toolbox --stdio将其作为子进程运行Transport Type选STDIOCommand填./toolbox或二进制实际路径Arguments填--stdio。本文主路径使用 HTTP 方式两种传输不要混用。协议版本HTTP with SSE 端点/mcp/sse仅用于2024-11-05协议版本且已废弃本文不使用。MCP 不支持的特性Toolbox 的 Authenticated Parameters 和 Authorized Invocations 不在 MCP 的 auth 规范支持范围内带安全参数secure: true的工具只对协商了com.google.cloud/toolbox.v1扩展的客户端出现在tools/list中未协商的客户端会看不到这些工具。本文示例工具均未使用安全参数因此不影响列表验证。完成List Tools验证后这条首次使用路径就结束了后续若要让 agent 框架调用这些工具可参阅 Introduction 文档 中的 Client SDK 集成部分Python、JavaScript/TypeScript、Go。【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考