
MCP Toolbox 中 Bigtable 物化视图更新工具 bigtable-update-materialized-view 配置与源码解析【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox本文以bigtable-update-materialized-view工具文档为主体完整覆盖其参数说明、YAML 配置示例与 Reference 参考表并结合 mcp-toolbox 仓库中的工具实现工具实现、Bigtable 源封装admin_wrappers.go与单元测试bigtableupdatematerializedview_test.go解析该工具从配置解析、参数校验到实际调用 Bigtable Admin API 的完整链路帮助你在 MCP 服务端安全地让 LLM 更新 Bigtable 实例上已有物化视图的定义。工具概览bigtable-update-materialized-view是 MCP Toolbox 面向 Google Cloud Bigtable 提供的管理类工具用于更新某个 Bigtable 实例上一个已存在物化视图Materialized View的定义。物化视图本身由一段 GoogleSQL 查询定义Toolbox 把它作为可执行资源持久化在 Bigtable 实例中当你需要调整该视图的查询语句例如修改SELECT的列、过滤条件或关联表时就使用本工具提交新的query文本而不是删除后重建。从源码结构看该工具注册名为bigtable-update-materialized-view见 bigtableupdatematerializedview.go#L29 中的resourceType常量通过init()函数注册到 Toolbox 的工具注册表中bigtableupdatematerializedview.go#L31-L35。该工具与bigtable-create-materialized-view、bigtable-get-materialized-view、bigtable-delete-materialized-view、bigtable-list-materialized-views等工具共同构成 Bigtable 物化视图的完整生命周期管理可分别参考 bigtable-create-materialized-view 文档 等文档。兼容的 Source本工具只能运行在实现了UpdateMaterializedView方法的 Bigtable 源上。工具源码中通过一个接口约束bigtableupdatematerializedview.go#L45-L47来声明这一兼容性type compatibleSource interface { UpdateMaterializedView(context.Context, string, string, string) (any, error) }即type: bigtable的 source参见 Bigtable Source 文档。如果配置中指定的 source 类型不满足该接口工具在初始化阶段就会因ValidateSource校验失败而报错bigtableupdatematerializedview.go#L93-L99在调用阶段同样会返回source used is not compatible with the tool错误。Bigtable source 的最小配置如下源自 source.md 的示例kind: source name: my-bigtable-source type: bigtable project: my-project-id instance: test-instance需要特别说明工具调用参数中的instance_id是显式传入的并不直接复用 source 配置里的instance字段。也就是说同一个 Bigtable source 理论上可以指向多个实例执行管理操作只要你的 ADCApplication Default Credentials对相应实例具有 IAM 权限。Bigtable 通过 IAM 在 project、instance、table 等多个层级控制访问权限更新物化视图属于实例管理Instance Admin类操作因此运行该工具的身份需要具备相应的bigtable.instanceAdmin级别权限详见 Bigtable Source 文档 的 IAM Permissions 一节。工具参数LLM 调用该工具时必须提供以下 3 个参数与文档 Parameters 表格一致源码定义见 bigtableupdatematerializedview.go#L67-L71字段类型必填说明instance_idstringtrue目标 Bigtable 实例的 ID。materialized_view_idstringtrue要更新的物化视图 ID。querystringtrue更新后的物化视图 GoogleSQL 查询语句。三个参数在Initialize中均通过parameters.NewStringParameter(...)注册没有默认值、没有枚举约束全部为必填字符串。调用时工具会把参数值按下标取出并强转为 string 传给底层方法bigtableupdatematerializedview.go#L111-L113paramsMap : params.AsMap() res, err : source.UpdateMaterializedView(ctx, paramsMap[instance_id].(string), paramsMap[materialized_view_id].(string), paramsMap[query].(string))关于query参数需要注意它是整段替换式更新——你提交的query会成为物化视图新的完整定义而不是在旧查询上做增量修改。Bigtable 的 SQL 查询由集群节点处理编写查询时应遵循 GoogleSQL 的最佳实践避免全表扫描、复杂过滤等参见 source.md 对 GoogleSQL 的说明。YAML 配置示例将工具加入你的 Toolbox 配置文件tools.yamlkind: tool name: update_materialized_view type: bigtable-update-materialized-view source: my-bigtable-source description: Use this tool to update a materialized view.如果未显式给出description工具会在初始化时填入默认描述Update an existing Bigtable materialized view.bigtableupdatematerializedview.go#L62-L65但建议像示例一样显式书写因为这段描述会直接传递给 LLM 作为工具选择依据。Reference 参考表字段类型必填说明typestringtrue必须为bigtable-update-materialized-view。sourcestringtrue执行该工具所用 source 的名称。descriptionstringfalse传递给 LLM 的工具描述。对应的 Go 配置结构bigtableupdatematerializedview.go#L49-L54type Config struct { tools.ConfigBase yaml:,inline Type string yaml:type validate:required Source string yaml:source validate:required Annotations *tools.ToolAnnotations yaml:annotations,omitempty }ConfigBase还内联了name、description以及authRequired等公共字段。单元测试TestParseFromYamlBigtablebigtableupdatematerializedview_test.go#L32-L99验证了基础 YAML 与带authRequired列表两种写法均可被正确解析kind: tool name: example_tool type: bigtable-update-materialized-view source: my-bt-instance description: some description authRequired: - my-google-auth-service底层调用链从工具到 Bigtable Admin API工具Invoke的最终落点是 Bigtable source 的UpdateMaterializedView封装方法admin_wrappers.go#L257-L267func (s *Source) UpdateMaterializedView(ctx context.Context, instanceId, materializedViewId, query string) (any, error) { conf : bigtable.MaterializedViewInfo{ // MUST be value per bigtable SDK MaterializedViewID: materializedViewId, Query: query, } err : s.InstanceAdmin.UpdateMaterializedView(ctx, instanceId, conf) if err ! nil { return nil, fmt.Errorf(failed to update materialized view: %w, err) } return map[string]string{status: materialized view updated successfully}, nil }这里有三个值得注意的实现细节使用值类型而非指针源码注释明确标注MUST be value per bigtable SDK——bigtable.MaterializedViewInfo必须以值非指针传入 SDK 的InstanceAdmin.UpdateMaterializedView否则会触发 SDK 的参数校验错误。同文件中UpdateLogicalView有同样的约束admin_wrappers.go#L217-L227。走 Instance Admin 通道source 初始化时bigtable.go#L60-L86会同时创建三类客户端——数据客户端bigtable.ClientgRPC 连接池大小为 10、实例管理客户端InstanceAdminClient和管理客户端AdminClient。更新物化视图使用的是InstanceAdmin因此它属于 Bigtable 实例管理面 API与bigtable-sql走的数据面通道相互独立。返回结构统一更新成功后返回{status: materialized view updated successfully}失败则被包装为failed to update materialized view: ...错误工具层再经util.ProcessGcpError归一化为标准错误bigtableupdatematerializedview.go#L114-L116便于 MCP 客户端与 LLM 解析。测试用例如何验证行为单元测试TestInvokebigtableupdatematerializedview_test.go#L120-L173用 mock source 覆盖了三种场景成功路径传入instance_idinst-1、materialized_view_idmview-1、querySELECT _key FROM t断言三个参数原样透传到 source 方法且返回值为{status: materialized view updated successfully}GCP 错误路径mock source 返回错误时Invoke将其包装为util.ToolboxError返回不兼容 source 路径使用未实现UpdateMaterializedView的 source 时直接报错。这从测试层面印证了该工具是“纯参数透传 错误归一化”的薄封装核心语义完全由 Bigtable SDK 的UpdateMaterializedView决定。配套操作更新前后如何验证由于更新是整段替换视图定义推荐按以下流程操作均可由同系列的 Toolbox 工具完成更新前确认现状用bigtable-get-materialized-view查看当前视图定义或用bigtable-list-materialized-views列出实例上所有物化视图对应的源方法见 admin_wrappers.go#L197-L203提交更新调用本工具传入目标instance_id、materialized_view_id与新的query更新后复核再次get该视图确认查询语句已生效。相关文档bigtable-get-materialized-view、bigtable-list-materialized-views、bigtable-delete-materialized-view。安全提示破坏性操作标记物化视图的更新会改变已有查询资源的行为可能直接影响依赖该视图的下游查询结果。Toolbox 对这类工具默认施加了破坏性destructive注解——Initialize中通过tools.GetAnnotationsOrDefault(cfg.Annotations, tools.NewDestructiveAnnotations)bigtableupdatematerializedview.go#L74-L78确保即使未显式配置annotations该工具也会被标记为破坏性操作。MCP 客户端如支持工具注解的 Agent可据此要求人工确认或额外授权后再执行。如果你的部署环境对该工具有信任边界要求还可以配合authRequired指定必须提供的认证服务解析行为见前文测试用例。小结bigtable-update-materialized-view用三个必填字符串参数instance_id、materialized_view_id、query以整段替换方式更新 Bigtable 实例上已存在物化视图的 GoogleSQL 定义配置上只需typesource外加可选description/authRequired工具自动注册并默认带破坏性注解实现链路由工具层参数透传工具实现落到 source 层的InstanceAdmin.UpdateMaterializedView封装admin_wrappers.go运行身份需具备 Bigtable 实例管理面的 IAM 权限。【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考