GitHub googleapis/genai - toolbox数据库MCP工具包介绍

GitHub googleapis/genai - toolbox数据库MCP工具包介绍

技术背景

MCP Toolbox for Databases当前处于beta阶段,首次稳定版本(v1.0)发布前可能会有重大变更。它是一个用于数据库的开源MCP服务器,能通过处理连接池、身份验证等复杂操作,让开发者更轻松、快速、安全地开发工具。该解决方案最初名为“Gen AI Toolbox for Databases”,为与新增的MCP兼容性保持一致而更名。

实现步骤

安装服务器

  • 二进制安装:查看发布页面获取最新版本,示例如下:
1
2
3
export VERSION=0.9.0
curl -O https://storage.googleapis.com/genai-toolbox/v$VERSION/linux/amd64/toolbox
chmod +x toolbox
  • 容器镜像安装:同样查看发布页面获取版本,示例如下:
1
2
export VERSION=0.9.0
docker pull us-central1-docker.pkg.dev/database-toolbox/toolbox/toolbox:$VERSION
  • 从源代码编译安装:确保安装了最新版本的Go,然后运行:
1
go install github.com/googleapis/[email protected]

运行服务器

配置tools.yaml文件定义工具,然后启动服务器:

1
./toolbox --tools-file "tools.yaml"

若要禁用动态重新加载,使用--disable - reload标志。使用toolbox help可查看完整标志列表。要停止服务器,发送终止信号(大多数平台使用ctrl + c)。

集成应用

服务器启动后,可将工具加载到应用中,以下是不同框架的客户端SDK使用示例:

Python

1
2
3
4
5
6
7
8
9
10
# 安装Toolbox Core SDK
pip install toolbox-core

# 加载工具
from toolbox_core import ToolboxClient

# 更新URL指向你的服务器
async with ToolboxClient("http://127.0.0.1:5000") as client:
# 这些工具可传递给你的应用
tools = await client.load_toolset("toolset_name")

JavaScript/TypeScript

1
2
3
4
5
6
7
8
9
10
11
12
// 安装Toolbox Core SDK
npm install @toolbox-sdk/core

// 加载工具
import { ToolboxClient } from '@toolbox-sdk/core';

// 更新URL指向你的服务器
const URL = 'http://127.0.0.1:5000';
let client = new ToolboxClient(URL);

// 这些工具可传递给你的应用
const tools = await client.loadToolset('toolsetName');

Go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main

import (
"github.com/googleapis/mcp-toolbox-sdk-go/core"
"context"
)

func main() {
// 确保添加错误检查
// 更新URL指向你的服务器
URL := "http://127.0.0.1:5000";
ctx := context.Background()

client, err := core.NewToolboxClient(URL)

// 框架无关工具
tools, err := client.LoadToolset("toolsetName", ctx)
}

核心代码

tools.yaml配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sources:
my-pg-source:
kind: postgres
host: 127.0.0.1
port: 5432
database: toolbox_db
user: toolbox_user
password: my-password

tools:
search-hotels-by-name:
kind: 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 || '%';

toolsets:
my_first_toolset:
- my_first_tool
- my_second_tool
my_second_toolset:
- my_second_tool
- my_third_tool

最佳实践

  • 简化开发:使用Toolbox可在不到10行代码的情况下将工具集成到代理中,在多个代理或框架之间重用工具,并更轻松地部署新版本的工具。
  • 提高性能:采用连接池、身份验证等最佳实践。
  • 增强安全性:集成身份验证,更安全地访问数据。
  • 端到端可观测性:使用内置的OpenTelemetry支持进行指标和跟踪。

常见问题

  • MCP Toolbox for Databases处于beta阶段,是否稳定?:目前处于beta阶段,在首次稳定版本(v1.0)发布前可能会有重大变更。
  • 如何配置不同类型的数据源和工具?:可参考tools.yaml文件的sourcestoolstoolsets部分,详细信息可查看文档中的Sources、Tools和Toolsets部分。
  • 如何停止服务器?:在大多数平台上,发送终止信号(ctrl + c)即可停止服务器。

GitHub googleapis/genai - toolbox数据库MCP工具包介绍
https://119291.xyz/posts/github-googleapis-genai-toolbox-introduction/
作者
ww
发布于
2025年7月27日
许可协议