// Copyright 2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package serverlesssparkcreatepysparkbatch import ( "context" "fmt" dataproc "cloud.google.com/go/dataproc/v2/apiv1/dataprocpb" "github.com/goccy/go-yaml" "github.com/googleapis/mcp-toolbox/internal/tools" "github.com/googleapis/mcp-toolbox/internal/tools/serverlessspark/createbatch" "github.com/googleapis/mcp-toolbox/internal/util/parameters" ) const resourceType = "serverless-spark-create-pyspark-batch" func init() { if !tools.Register(resourceType, newConfig) { panic(fmt.Sprintf("tool type %q already registered", resourceType)) } } func newConfig(ctx context.Context, name string, decoder *yaml.Decoder) (tools.ToolConfig, error) { baseCfg, err := createbatch.NewConfig(ctx, name, decoder) if err != nil { return nil, err } return Config{Config: baseCfg}, nil } type Config struct { createbatch.Config ScopesRequired []string `yaml:"scopesRequired"` } // validate interface var _ tools.ToolConfig = Config{} // ToolConfigType returns the unique name for this tool. func (cfg Config) ToolConfigType() string { return resourceType } // Initialize creates a new Tool instance. func (cfg Config) Initialize(context.Context) (tools.Tool, error) { return createbatch.NewTool(cfg.Config, cfg, &PySparkBatchBuilder{}) } type PySparkBatchBuilder struct{} func (b *PySparkBatchBuilder) Parameters() parameters.Parameters { return parameters.Parameters{ parameters.NewStringParameter("mainFile", "The path to the main Python file, as a gs://... URI.", parameters.WithStringRequired(true)), parameters.NewArrayParameter("args", "Optional. A list of arguments passed to the main file.", parameters.NewStringParameter("arg", "An argument."), parameters.WithArrayRequired(false)), parameters.NewStringParameter("version", "Optional. The Serverless runtime version to execute with.", parameters.WithStringRequired(false)), } } func (b *PySparkBatchBuilder) BuildBatch(params parameters.ParamValues) (*dataproc.Batch, error) { paramMap := params.AsMap() mainFile := paramMap["mainFile"].(string) batch := &dataproc.Batch{ BatchConfig: &dataproc.Batch_PysparkBatch{ PysparkBatch: &dataproc.PySparkBatch{ MainPythonFileUri: mainFile, }, }, } if args, ok := paramMap["args"].([]any); ok { for _, arg := range args { batch.GetPysparkBatch().Args = append(batch.GetPysparkBatch().Args, fmt.Sprintf("%v", arg)) } } return batch, nil }