|
| 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 | + |
| 15 | +package elasticsearchexecuteesql |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + |
| 22 | + yaml "github.com/goccy/go-yaml" |
| 23 | + "github.com/googleapis/mcp-toolbox/internal/embeddingmodels" |
| 24 | + "github.com/googleapis/mcp-toolbox/internal/sources" |
| 25 | + es "github.com/googleapis/mcp-toolbox/internal/sources/elasticsearch" |
| 26 | + "github.com/googleapis/mcp-toolbox/internal/tools" |
| 27 | + "github.com/googleapis/mcp-toolbox/internal/util" |
| 28 | + "github.com/googleapis/mcp-toolbox/internal/util/parameters" |
| 29 | +) |
| 30 | + |
| 31 | +const resourceType string = "elasticsearch-execute-esql" |
| 32 | + |
| 33 | +func init() { |
| 34 | + if !tools.Register(resourceType, newConfig) { |
| 35 | + panic(fmt.Sprintf("tool type %q already registered", resourceType)) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func newConfig(ctx context.Context, name string, decoder *yaml.Decoder) (tools.ToolConfig, error) { |
| 40 | + actual := Config{Name: name} |
| 41 | + if err := decoder.DecodeContext(ctx, &actual); err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + return actual, nil |
| 45 | +} |
| 46 | + |
| 47 | +type compatibleSource interface { |
| 48 | + ElasticsearchClient() es.EsClient |
| 49 | + RunSQL(ctx context.Context, format, query string, params []map[string]any) (any, error) |
| 50 | +} |
| 51 | + |
| 52 | +type Config struct { |
| 53 | + Name string `yaml:"name" validate:"required"` |
| 54 | + Type string `yaml:"type" validate:"required"` |
| 55 | + Source string `yaml:"source" validate:"required"` |
| 56 | + Description string `yaml:"description" validate:"required"` |
| 57 | + AuthRequired []string `yaml:"authRequired"` |
| 58 | + Format string `yaml:"format"` |
| 59 | + Annotations *tools.ToolAnnotations `yaml:"annotations,omitempty"` |
| 60 | +} |
| 61 | + |
| 62 | +var _ tools.ToolConfig = Config{} |
| 63 | + |
| 64 | +func (cfg Config) ToolConfigType() string { |
| 65 | + return resourceType |
| 66 | +} |
| 67 | + |
| 68 | +func (cfg Config) Initialize(srcs map[string]sources.Source) (tools.Tool, error) { |
| 69 | + queryParameter := parameters.NewStringParameter("query", "The ES|QL statement to execute.") |
| 70 | + params := parameters.Parameters{queryParameter} |
| 71 | + |
| 72 | + annotations := tools.GetAnnotationsOrDefault(cfg.Annotations, tools.NewDestructiveAnnotations) |
| 73 | + mcpManifest := tools.GetMcpManifest(cfg.Name, cfg.Description, cfg.AuthRequired, params, annotations) |
| 74 | + |
| 75 | + t := Tool{ |
| 76 | + Config: cfg, |
| 77 | + Parameters: params, |
| 78 | + manifest: tools.Manifest{Description: cfg.Description, Parameters: params.Manifest(), AuthRequired: cfg.AuthRequired}, |
| 79 | + mcpManifest: mcpManifest, |
| 80 | + } |
| 81 | + return t, nil |
| 82 | +} |
| 83 | + |
| 84 | +var _ tools.Tool = Tool{} |
| 85 | + |
| 86 | +type Tool struct { |
| 87 | + Config |
| 88 | + Parameters parameters.Parameters `yaml:"parameters"` |
| 89 | + manifest tools.Manifest |
| 90 | + mcpManifest tools.McpManifest |
| 91 | +} |
| 92 | + |
| 93 | +func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, params parameters.ParamValues, accessToken tools.AccessToken) (any, util.ToolboxError) { |
| 94 | + source, err := tools.GetCompatibleSource[compatibleSource](resourceMgr, t.Source, t.Name, t.Type) |
| 95 | + if err != nil { |
| 96 | + return nil, util.NewClientServerError("source used is not compatible with the tool", http.StatusInternalServerError, err) |
| 97 | + } |
| 98 | + |
| 99 | + paramsMap := params.AsMap() |
| 100 | + query, ok := paramsMap["query"].(string) |
| 101 | + if !ok { |
| 102 | + return nil, util.NewAgentError(fmt.Sprintf("unable to get cast %s", paramsMap["query"]), nil) |
| 103 | + } |
| 104 | + |
| 105 | + // Default to json format |
| 106 | + format := t.Format |
| 107 | + if format == "" { |
| 108 | + format = "json" |
| 109 | + } |
| 110 | + |
| 111 | + // Get logger |
| 112 | + logger, err := util.LoggerFromContext(ctx) |
| 113 | + if err != nil { |
| 114 | + return nil, util.NewClientServerError("error getting logger", http.StatusInternalServerError, err) |
| 115 | + } |
| 116 | + logger.DebugContext(ctx, fmt.Sprintf("executing `%s` tool query: %s with format: %s", resourceType, query, format)) |
| 117 | + |
| 118 | + resp, err := source.RunSQL(ctx, format, query, nil) |
| 119 | + if err != nil { |
| 120 | + return nil, util.ProcessGeneralError(err) |
| 121 | + } |
| 122 | + return resp, nil |
| 123 | +} |
| 124 | + |
| 125 | +func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) { |
| 126 | + return parameters.EmbedParams(ctx, t.Parameters, paramValues, embeddingModelsMap, nil) |
| 127 | +} |
| 128 | + |
| 129 | +func (t Tool) Manifest() tools.Manifest { |
| 130 | + return t.manifest |
| 131 | +} |
| 132 | + |
| 133 | +func (t Tool) McpManifest() tools.McpManifest { |
| 134 | + return t.mcpManifest |
| 135 | +} |
| 136 | + |
| 137 | +func (t Tool) Authorized(verifiedAuthServices []string) bool { |
| 138 | + return tools.IsAuthorized(t.AuthRequired, verifiedAuthServices) |
| 139 | +} |
| 140 | + |
| 141 | +func (t Tool) RequiresClientAuthorization(resourceMgr tools.SourceProvider) (bool, error) { |
| 142 | + return false, nil |
| 143 | +} |
| 144 | + |
| 145 | +func (t Tool) ToConfig() tools.ToolConfig { |
| 146 | + return t.Config |
| 147 | +} |
| 148 | + |
| 149 | +func (t Tool) GetAuthTokenHeaderName(resourceMgr tools.SourceProvider) (string, error) { |
| 150 | + return "Authorization", nil |
| 151 | +} |
| 152 | + |
| 153 | +func (t Tool) GetParameters() parameters.Parameters { |
| 154 | + return t.Parameters |
| 155 | +} |
0 commit comments