MCP Toolbox 中 bigtable-list-instances 工具:配置、参数与源码级调用链全解析
【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox
MCP Toolbox for Databases(Google 开源的数据库 MCP Server)为 Bigtable 提供了一组管理(admin)生命周期工具,bigtable-list-instances是其中的入口级只读工具:通过 MCP 协议列出当前 GCP 项目下的全部 Bigtable 实例。本文基于官方文档页与该工具的源码实现,完整讲解其配置方式、字段参考、兼容性约束,并深入 工具实现文件 与 Bigtable Source 封装 解析其底层调用链、部分可用(partially unavailable)结果的容错逻辑,以及单元测试与集成测试中的验证方式,帮助你在自己的 MCP 配置中正确接入并使用该工具。
工具定位:它做什么、返回什么
bigtable-list-instances的官方定义只有一句话:List all Bigtable instances in the project(列出项目中所有 Bigtable 实例)。它的几个关键特性:
- 项目级而非实例级操作:与其他需要传
instance_id、table_id等参数的 Bigtable 工具不同,该工具不接收任何参数——因为 Bigtable 实例天然归属于某个 GCP 项目,列出"项目内所有实例"不需要额外入参(见后文"无参数设计"一节源码佐证)。 - 只读(read-only)工具:初始化时若未显式指定
annotations,工具会自动打上只读注解,便于 MCP 客户端识别该工具不会产生副作用。 - 默认描述:若配置中省略
description,服务端会自动填充默认描述 "List all Bigtable instances in the project."。 - 属于 20 个 Bigtable admin 生命周期工具之一:在集成测试中,它与
bigtable-create-instance、bigtable-update-instance、bigtable-delete-instance、bigtable-get-instance等一起被验证覆盖(见 集成测试 中的工具清单)。
前置条件:先配置 Bigtable Source
该工具必须绑定到一个type: bigtable的 source 上执行。按照 Bigtable Source 文档,一个最小可用的 source 配置如下:
kind: source name: my-bigtable-source type: "bigtable" project: "my-project-id" instance: "test-instance"| field | type | required | description |
|---|---|---|---|
| type | string | true | 必须为"bigtable"。 |
| project | string | true | 集群所在 GCP 项目 ID(如 "my-project-id")。 |
| instance | string | true | Bigtable 实例名称。 |
需要特别注意两点,这也是 Bigtable 集成文档中明确列出的Requirements:
- IAM 权限:Bigtable 使用 IAM 控制对项目、实例、表和备份级别的访问。MCP Toolbox 会使用你的 Application Default Credentials (ADC) 完成认证与授权,运行身份需要具备实例管理(instance management)相关的读取权限,
list-instances属于只读操作,通常要求实例查看类权限即可。 - ADC 设置:在部署 Toolbox 服务器前,须按 Google Cloud 的 ADC 规范为服务器配置凭据,否则对 Bigtable API 的调用会认证失败。
配置示例与字段参考
按照官方工具文档,最小可用配置如下(与文档中的 Example 保持一致):
kind: tool name: bigtable_list_instances type: bigtable-list-instances source: my-bigtable-source description: List all Bigtable instances in the project.官方字段参考表:
| field | type | required | description |
|---|---|---|---|
| type | string | true | 必须为bigtable-list-instances。 |
| source | string | true | 要执行该工具的 source 名称。 |
| description | string | false | 传给 LLM 的工具描述。 |
结合源码中的Config结构体(见 Config 定义),还可以补充文档表格未完整覆盖的字段:
name(string,必填):工具在配置中的名称,即 LLM 看到的工具名;测试用例中以
example_tool为名称验证了解析行为。annotations(object,可选):工具注解,例如
readOnlyHint。若省略,默认使用只读注解(见 Initialize 实现 中的tools.GetAnnotationsOrDefault)。authRequired(string 列表,可选):声明该工具调用前需要哪些认证服务。单元测试 bigtablelistinstances_test.go 中的 "with auth required" 用例验证了如下配置可被正确解析:
kind: tool name: example_tool type: bigtable-list-instances source: my-bq-instance description: some description authRequired: - my-google-auth-service - other-auth-servicedescription 缺省值:源码中
Initialize方法明确写道,当cfg.Description == ""时自动补为 "List all Bigtable instances in the project."(默认描述)。
无参数设计:为什么它不需要任何 LLM 传参
该工具在初始化时构建了一个空的参数列表:
allParameters := parameters.Parameters{} // ... tools.Manifest{Description: cfg.Description, Parameters: allParameters.Manifest(), ...}(见 Initialize)这意味着 LLM 调用该工具时不需要也不应提供任何参数。这一设计源自 Bigtable 的资源层级模型:实例(instance)直接挂在项目下,"列出全部实例"是一个确定的项目级查询,没有需要用户输入的维度。集成测试也印证了这一点——调用时传入的就是空参数map[string]any{}(见 集成测试调用)。
源码解析:从工具注册到 ListInstances 调用链
1. 注册机制
每个工具通过包级init()向全局注册表登记自己的类型字符串:
const resourceType string = "bigtable-list-instances" func init() { if !tools.Register(resourceType, newConfig) { panic(fmt.Sprintf("tool type %q already registered", resourceType)) } }(见 init 注册)该包在 cmd/internal/imports.go 中以匿名导入的方式被引入,从而在服务器启动时完成注册。配置中type: bigtable-list-instances正是通过resourceType与newConfig反序列化函数匹配到的。
2. 兼容性校验:只接受实现了 ListInstances 的 source
工具通过一个接口断言来声明它对 source 的要求:
type compatibleSource interface { ListInstances(context.Context) (any, error) }(见 compatibleSource)ValidateSource在服务器装载配置阶段校验 source 是否满足该接口,不满足即报错 "source is not compatible with the tool";运行时Invoke中会再做一次同样的断言,失败则返回 500 客户端/服务端错误(见 ValidateSource 与 Invoke)。
在源码结构中,目前实现该接口的 Bigtable 管理方法集中在 admin_wrappers.go 中,因此与该工具兼容的就是bigtable类型的 source。
3. 实际调用:InstanceAdmin.Instances 与部分可用容错
真正干活的是 source 侧的ListInstances封装(admin_wrappers.go):
func (s *Source) ListInstances(ctx context.Context) (any, error) { instances, err := s.InstanceAdmin.Instances(ctx) if err != nil { var partialErr bigtable.ErrPartiallyUnavailable if errors.As(err, &partialErr) { return instances, nil } return nil, fmt.Errorf("failed to list instances: %w", err) } return instances, nil }这段实现有两个值得注意的工程细节:
- 底层走的是 Bigtable Go 客户端的InstanceAdmin管理通道(
s.InstanceAdmin.Instances),属于管理 API 而非数据面 API; - 当返回的列表部分可用(
bigtable.ErrPartiallyUnavailable,即某些实例因服务区域暂时不可用而缺失)时,该封装不会报错,而是直接把已获取到的部分实例列表返回给调用方。换句话说,LLM 拿到的实例清单在极端情况下可能不是全量,这是由底层客户端语义决定的行为,使用方应知悉。
工具侧Invoke拿到结果后直接透传;若发生其他错误,则经util.ProcessGcpError归类为 GCP 错误后返回(见 Invoke)。
测试中的验证方式
仓库中对该工具有两层测试覆盖,可作为行为依据:
单元测试(bigtablelistinstances_test.go):
TestParseFromYamlBigtable验证 YAML 配置解析:包含基础示例与带authRequired的两种场景,解析结果应精确等于Config结构体;TestInvoke用一个 mock source 实现ListInstances:成功路径断言返回值为[]string{"inst-1"},错误路径(source 返回 "gcp error")断言工具返回非 nil 错误。
集成测试(bigtable_integration_test.go):
runBigTableAdminToolsTest中通过 MCP 协议真实调用bigtable-list-instances,传入空参数,并断言响应文本包含预期创建的实例 ID:listInstResp := assertMCPSuccess(t, "bigtable-list-instances", map[string]any{}) if len(listInstResp.Result.Content) == 0 || !strings.Contains(listInstResp.Result.Content[0].Text, instanceId) { t.Fatalf("bigtable-list-instances output does not contain expected instance %q: %v", ...) }该测试同时表明该工具在整套 Bigtable 管理工具中承担"第一步勘察"的角色——先列出实例,再对其中的实例做 get / list-clusters 等后续操作(如紧随其后的
bigtable-get-instance调用,见 集成测试上下文)。
典型使用姿势与注意事项
将上述配置放入 Toolbox 的tools.yaml(与 source 定义放在同一套服务端配置中),启动服务器后,LLM 客户端即可看到名为bigtable_list_instances的 MCP 工具并直接调用。实践建议:
- 作为 Bigtable 管理会话的起点:先
list-instances确认目标实例 ID,再配合bigtable-get-instance、bigtable-list-clusters、bigtable-list-tables等兄弟工具(工具全集可参见 Bigtable 工具目录)逐级下钻。 - 结果可能部分缺失:如前所述,底层
ErrPartiallyUnavailable场景下会静默返回部分列表,不要在自动化流程中假设该列表一定全量。 - 权限不足时:错误会经由 GCP 错误处理链返回给客户端,此时应检查运行身份的 IAM 角色(参见 Bigtable Source 的 IAM 要求)。
- 版本与适用前提:本文所述行为以当前仓库代码为准,工具类型字符串
bigtable-list-instances、source 类型bigtable以及字段集合均与当前实现一致;若未来 Bigtable API 客户端升级,ListInstances的底层行为可能随之变化。
相关文档
- bigtable-list-instances 官方文档
- Bigtable Source 配置
- Bigtable 集成概述
- 工具实现 / 单元测试 / 集成测试
【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考