MCP Toolbox 的 looker-get-connections 工具:用 MCP 一键枚举 Looker 全部数据库连接
【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox
looker-get-connections是 MCP Toolbox 中 Looker 集成提供的一个只读工具,用于一次性返回 Looker 源中配置的全部数据库连接及其关键元数据(连接名、方言、默认 Schema、数据库、多数据库支持能力)。本文以 looker-get-connections 官方文档 为主体,结合仓库中该工具的 Go 实现与预置配置,讲解其功能、YAML 声明方式、输出结构以及底层实现原理,帮助你快速在自己的 MCP 配置中启用并正确使用它。
工具概览:无参数、全量返回
looker-get-connections的设计非常简单直接:它不接受任何参数,调用后返回 Looker 系统中配置的所有数据库连接。这在语义层驱动的 Agent 工作流中非常有用——LLM 往往需要先了解当前 Looker 实例上挂了哪些数据库、分别是什么方言,才能进一步发起查询、判断连接名合法性,或为下游工具(如 looker-get-connection-tables、looker-get-connection-schemas)提供正确的连接名参数。
从源码结构看,该工具的实现位于 lookergetconnections.go,通过常量resourceType = "looker-get-connections"在工具注册表中注册,并实现了tools.ToolConfig与tools.Tool两个接口(ToolConfigType、Initialize、Invoke、GetSourceName、ValidateSource等)。其中Invoke方法不接受任何输入参数,直接通过 Looker SDK 拉取连接列表。
兼容的源类型
该工具声明了一个compatibleSource接口约束,要求其关联的源必须实现以下方法:
UseClientAuthorization() bool GetAuthTokenHeaderName() string LookerApiSettings() *rtl.ApiSettings GetLookerSDK(context.Context, string) (*v4.LookerSDK, error)对应到配置层面,即type: looker的源。若声明的source不是兼容类型,工具在ValidateSource阶段就会返回“invalid source for …”错误,配置无法通过校验。
在配置文件中声明该工具
looker-get-connections本身是工具声明(kind: tool),必须关联到一个已经定义好的 Looker 源(kind: source)。官方文档给出的最小可运行示例:
kind: tool name: get_connections type: looker-get-connections source: looker-source description: | This tool retrieves a list of all database connections configured in the Looker system. Parameters: This tool takes no parameters. Output: A JSON array of objects, each representing a database connection and including details such as: - `name`: The connection's unique identifier. - `dialect`: The database dialect (e.g., "mysql", "postgresql", "bigquery"). - `default_schema`: The default schema for the connection. - `database`: The associated database name (if applicable). - `supports_multiple_databases`: A boolean indicating if the connection can access multiple databases.字段参考表
| field | type | required | description |
|---|---|---|---|
| type | string | true | Must be "looker-get-connections". |
| source | string | true | Name of the source Looker instance. |
| description | string | true | Description of the tool that is passed to the LLM. |
几个要点:
type必须严格为looker-get-connections。源码中该字段带validate:"required"标签,并且工具注册表按此字符串分发解析,写错类型将无法加载。source必须指向一个type: looker的源名,例如上文示例中的looker-source。description是必填项。在Initialize中,如果cfg.Description == "",会直接返回description is required for tool ...错误。这段描述会被注入到 LLM 的上下文(tools.Manifest{Description: cfg.Description, ...}),用于让模型理解工具用途,因此建议写清参数与输出语义,如上例所示。- 可选的
annotations字段可以覆盖工具的默认注解(该工具默认按只读注解NewReadOnlyAnnotations处理,因为它只做查询、不产生副作用)。
该 YAML 示例与仓库预置配置 looker-dev.yaml 中get_connections工具的声明完全一致,可直接参考使用。
定义 Looker 源:前置条件
由于工具本身无参数,它的可用性完全取决于关联源的配置。Looker 源(source.md)只使用 API 认证,你需要先在 Looker 中创建一个 API 用户,获取client_id与client_secret。一个典型配置如下:
kind: source name: looker-source type: looker base_url: ${LOOKER_BASE_URL} client_id: ${LOOKER_CLIENT_ID:} client_secret: ${LOOKER_CLIENT_SECRET:} verify_ssl: ${LOOKER_VERIFY_SSL:true} timeout: 600s use_client_oauth: ${LOOKER_USE_CLIENT_OAUTH:false} show_hidden_models: ${LOOKER_SHOW_HIDDEN_MODELS:true} show_hidden_explores: ${LOOKER_SHOW_HIDDEN_EXPLORES:true} show_hidden_fields: ${LOOKER_SHOW_HIDDEN_FIELDS:true}注意事项(来自官方文档):
base_url形如https://looker.example.com,不要带尾部斜杠;Looker 部署在本机时通常需要加上 API 端口,如https://looker.example.com:19999。verify_ssl几乎总是true(全小写),仅当 Looker 使用自签名证书时才改为false;任何非"true"的值都会被解释为false。client_id/client_secret是 Looker 服务器分配的一串随机字符;若使用 Looker OAuth 则无需填写。- 强烈建议用
${ENV_NAME}环境变量替换方式注入敏感信息,不要把密钥硬编码进配置文件。 - 如果使用 Conversational Analytics 相关工具,还需在源上配置
project与location,并启用geminidataanalytics.googleapis.com、cloudaicompanion.googleapis.com两个 GCP API 以及对应的 IAM 角色;但对于looker-get-connections这类常规工具,仅需 API 认证即可。
调用过程与输出结构
底层调用链
Invoke方法的核心逻辑如下:
- 通过
source.GetLookerSDK(ctx, accessToken)获得 Looker SDK 客户端; - 调用
sdk.AllConnections("name, dialect(name), database, schema", ...),按字段列表拉取全部连接; - 对每个连接再调用
sdk.ConnectionFeatures(connName, "multiple_databases", ...),探测该连接是否支持多数据库; - 将每条连接整理成 map 并聚合为一个 JSON 数组返回。
这里有一处值得注意的实现细节:返回字段名是dialect_name与schema,而不是文档描述中直译的dialect与default_schema。源码中映射关系为:
vMap["name"] = *v.Name vMap["dialect_name"] = *v.Dialect.Name vMap["database"] = *v.Database // 仅当非空 vMap["schema"] = *v.Schema // 仅当非空 vMap["supports_multiple_databases"] = *conn.MultipleDatabases因此该工具实际返回的 JSON 对象形如:
[ { "name": "my_mysql_conn", "dialect_name": "mysql", "database": "analytics", "schema": "public", "supports_multiple_databases": false }, { "name": "my_pg_conn", "dialect_name": "postgresql", "supports_multiple_databases": true } ]name:连接的唯一标识,后续连接相关工具(tables/schemas/columns/databases)都需要它作为输入参数。dialect_name:数据库方言名称(如mysql、postgresql、bigquery等)。database/schema:可能为空,源码中只有指针非空时才写入 map,因此字段可能缺席。supports_multiple_databases:布尔值,来自 Looker 的连接特性探测结果。
错误处理与安全性
源码对调用做了防御性处理:SDK 返回 401 时映射为http.StatusUnauthorized的“unauthorized error”,其他错误走统一的ProcessGeneralError处理;工具在RequiresClientAuthorization/GetAuthTokenHeaderName中透传源的客户端授权配置,因此当源开启use_client_oauth时,会转发客户端的 OAuth 访问令牌,避免把长期凭证暴露给每个调用方。
测试验证
该工具的配置解析行为由 lookergetconnections_test.go 覆盖:
TestParseFromYamlLookerGetConnections:验证一段最小的 YAML(kind: tool/name/type: looker-get-connections/source/description)能被正确解析为Config,并断言字段值一致;TestFailParseFromYamlLookerGetConnections:验证声明了未知字段(如method: GOT)时解析失败并给出明确的错误定位信息。
这说明“仅type、source、description三个必填字段、无其他可选参数”的行为是经过测试保证的,配置时不必也不能添加额外字段。
典型使用场景
在 MCP 驱动的 Looker 语义层问答工作流中,looker-get-connections通常承担“侦察”角色,常见组合:
- 连接发现:LLM 先调用
get_connections,了解当前实例上有哪些数据库连接及各自方言; - 元数据下钻:选定一个
name后,配合 looker-get-connection-tables、looker-get-connection-schemas、looker-get-connection-table-columns 获取表、Schema、字段级别的元数据; - 健康检查辅助:仓库中 lookerhealthpulse.go 的
checkDBConnections也调用了AllConnections并按连接逐一执行TestConnection,可见连接枚举是 Looker 可观测性与运维类工具共用的基础能力。
小结
looker-get-connections是一个无参数、只读的工具,一次调用返回 Looker 实例的全部数据库连接与关键属性,是 Agent 理解 Looker 数据环境的入口。- 配置只需三要素:
type: looker-get-connections、指向type: looker源的source,以及会被注入 LLM 上下文的description。 - 实际返回字段以源码为准:
name、dialect_name、database、schema、supports_multiple_databases;其中database、schema在为空时不会出现在结果中。 - 完整可运行的示例可参考仓库预置配置 looker-dev.yaml 与 Looker 工具文档目录 tools/。
【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考