|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package lookergetfieldvaluesuggestions |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "net/http" |
| 20 | + "strings" |
| 21 | + |
| 22 | + yaml "github.com/goccy/go-yaml" |
| 23 | + "github.com/googleapis/mcp-toolbox/internal/tools" |
| 24 | + "github.com/googleapis/mcp-toolbox/internal/tools/looker/lookercommon" |
| 25 | + "github.com/googleapis/mcp-toolbox/internal/util" |
| 26 | + "github.com/googleapis/mcp-toolbox/internal/util/parameters" |
| 27 | + |
| 28 | + "github.com/looker-open-source/sdk-codegen/go/rtl" |
| 29 | + v4 "github.com/looker-open-source/sdk-codegen/go/sdk/v4" |
| 30 | +) |
| 31 | + |
| 32 | +const resourceType string = "looker-get-field-value-suggestions" |
| 33 | + |
| 34 | +func init() { |
| 35 | + if !tools.Register(resourceType, newConfig) { |
| 36 | + panic(fmt.Sprintf("tool type %q already registered", resourceType)) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func newConfig(ctx context.Context, name string, decoder *yaml.Decoder) (tools.ToolConfig, error) { |
| 41 | + actual := Config{ConfigBase: tools.ConfigBase{Name: name}} |
| 42 | + if err := decoder.DecodeContext(ctx, &actual); err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + return actual, nil |
| 46 | +} |
| 47 | + |
| 48 | +type compatibleSource interface { |
| 49 | + UseClientAuthorization() bool |
| 50 | + GetAuthTokenHeaderName() string |
| 51 | + LookerApiSettings() *rtl.ApiSettings |
| 52 | + GetLookerSDK(context.Context, string) (*v4.LookerSDK, error) |
| 53 | +} |
| 54 | + |
| 55 | +type Config struct { |
| 56 | + tools.ConfigBase `yaml:",inline"` |
| 57 | + Type string `yaml:"type" validate:"required"` |
| 58 | + Source string `yaml:"source" validate:"required"` |
| 59 | + Annotations *tools.ToolAnnotations `yaml:"annotations,omitempty"` |
| 60 | +} |
| 61 | + |
| 62 | +// validate interface |
| 63 | +var _ tools.ToolConfig = Config{} |
| 64 | + |
| 65 | +func (cfg Config) ToolConfigType() string { |
| 66 | + return resourceType |
| 67 | +} |
| 68 | + |
| 69 | +func (cfg Config) Initialize(context.Context) (tools.Tool, error) { |
| 70 | + if cfg.Description == "" { |
| 71 | + return nil, fmt.Errorf("description is required for tool %q", cfg.Name) |
| 72 | + } |
| 73 | + |
| 74 | + params := lookercommon.GetFieldParameters() |
| 75 | + |
| 76 | + // Add field value suggestion specific parameters |
| 77 | + fieldParam := parameters.NewStringParameter("field", "The name of the field to get suggestions for.") |
| 78 | + termParam := parameters.NewStringParameter("term", "Optional search term pattern.", parameters.WithStringRequired(false)) |
| 79 | + filtersParam := parameters.NewMapParameter("filters", "Optional filters to enable conditional suggestions (restricting suggestions based on other field values).", "", parameters.WithMapDefault(map[string]any{})) |
| 80 | + |
| 81 | + params = append(params, fieldParam, termParam, filtersParam) |
| 82 | + |
| 83 | + return Tool{ |
| 84 | + BaseTool: tools.NewBaseTool( |
| 85 | + cfg, |
| 86 | + tools.GetAnnotationsOrDefault(cfg.Annotations, tools.NewReadOnlyAnnotations), |
| 87 | + tools.Manifest{Description: cfg.Description, Parameters: params.Manifest(), AuthRequired: cfg.AuthRequired}, |
| 88 | + params, |
| 89 | + ), |
| 90 | + }, nil |
| 91 | +} |
| 92 | + |
| 93 | +// validate interface |
| 94 | +var _ tools.Tool = Tool{} |
| 95 | + |
| 96 | +type Tool struct { |
| 97 | + tools.BaseTool[Config] |
| 98 | +} |
| 99 | + |
| 100 | +func (t Tool) ToConfig() tools.ToolConfig { |
| 101 | + return t.Cfg |
| 102 | +} |
| 103 | + |
| 104 | +func (t Tool) Invoke(ctx context.Context, primitiveMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) { |
| 105 | + source, err := tools.GetCompatibleSource[compatibleSource](primitiveMgr, t.Cfg.Source, t.Cfg.Name, t.Cfg.Type) |
| 106 | + if err != nil { |
| 107 | + return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err) |
| 108 | + } |
| 109 | + |
| 110 | + logger, err := util.LoggerFromContext(ctx) |
| 111 | + if err != nil { |
| 112 | + return nil, util.NewClientServerError("unable to get logger from ctx", http.StatusInternalServerError, err) |
| 113 | + } |
| 114 | + |
| 115 | + mapParams := params.AsMap() |
| 116 | + |
| 117 | + model, ok := mapParams["model"].(string) |
| 118 | + if !ok { |
| 119 | + return nil, util.NewAgentError("model is required and must be a string", nil) |
| 120 | + } |
| 121 | + |
| 122 | + explore, ok := mapParams["explore"].(string) |
| 123 | + if !ok { |
| 124 | + return nil, util.NewAgentError("explore is required and must be a string", nil) |
| 125 | + } |
| 126 | + |
| 127 | + field, ok := mapParams["field"].(string) |
| 128 | + if !ok { |
| 129 | + return nil, util.NewAgentError("field is required and must be a string", nil) |
| 130 | + } |
| 131 | + |
| 132 | + var termPtr *string |
| 133 | + if val, ok := mapParams["term"].(string); ok && val != "" { |
| 134 | + termPtr = &val |
| 135 | + } |
| 136 | + |
| 137 | + filters, _ := mapParams["filters"].(map[string]any) |
| 138 | + |
| 139 | + sdk, err := source.GetLookerSDK(ctx, string(accessToken)) |
| 140 | + if err != nil { |
| 141 | + return nil, util.NewClientServerError("error getting Looker SDK", http.StatusInternalServerError, err) |
| 142 | + } |
| 143 | + |
| 144 | + req := v4.RequestModelFieldnameSuggestions{ |
| 145 | + ModelName: model, |
| 146 | + ViewName: explore, // Map 'explore' back to 'ViewName' |
| 147 | + FieldName: field, |
| 148 | + Term: termPtr, |
| 149 | + } |
| 150 | + if len(filters) > 0 { |
| 151 | + var f interface{} = filters |
| 152 | + req.Filters = &f |
| 153 | + } |
| 154 | + |
| 155 | + resp, err := sdk.ModelFieldnameSuggestions(req, source.LookerApiSettings()) |
| 156 | + if err != nil { |
| 157 | + if strings.Contains(err.Error(), "status=401") { |
| 158 | + return nil, util.NewClientServerError("unauthorized error", http.StatusUnauthorized, err) |
| 159 | + } |
| 160 | + return nil, util.ProcessGeneralError(err) |
| 161 | + } |
| 162 | + |
| 163 | + if resp.Error != nil && *resp.Error != "" { |
| 164 | + return nil, util.NewAgentError(fmt.Sprintf("Looker API error: %s", *resp.Error), nil) |
| 165 | + } |
| 166 | + |
| 167 | + logger.DebugContext(ctx, "suggestions = ", resp.Suggestions) |
| 168 | + |
| 169 | + if resp.Suggestions == nil { |
| 170 | + return map[string]any{"suggestions": []string{}}, nil |
| 171 | + } |
| 172 | + |
| 173 | + return map[string]any{"suggestions": *resp.Suggestions}, nil |
| 174 | +} |
| 175 | + |
| 176 | +func (t Tool) RequiresClientAuthorization(primitiveMgr tools.SourceProvider) (bool, error) { |
| 177 | + source, err := tools.GetCompatibleSource[compatibleSource](primitiveMgr, t.Cfg.Source, t.Cfg.Name, t.Cfg.Type) |
| 178 | + if err != nil { |
| 179 | + return false, err |
| 180 | + } |
| 181 | + return source.UseClientAuthorization(), nil |
| 182 | +} |
| 183 | + |
| 184 | +func (t Tool) GetAuthTokenHeaderName(primitiveMgr tools.SourceProvider) (string, error) { |
| 185 | + source, err := tools.GetCompatibleSource[compatibleSource](primitiveMgr, t.Cfg.Source, t.Cfg.Name, t.Cfg.Type) |
| 186 | + if err != nil { |
| 187 | + return "", err |
| 188 | + } |
| 189 | + return source.GetAuthTokenHeaderName(), nil |
| 190 | +} |
0 commit comments