mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-01-14 01:48:29 -05:00
Compare commits
2 Commits
docs/toolb
...
tool-name-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60d35e5f40 | ||
|
|
d69792d843 |
@@ -16,6 +16,7 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
yaml "github.com/goccy/go-yaml"
|
||||
@@ -139,6 +140,10 @@ func (c *SourceConfigs) UnmarshalYAML(ctx context.Context, unmarshal func(interf
|
||||
}
|
||||
|
||||
for name, u := range raw {
|
||||
err := NameValidation(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Unmarshal to a general type that ensure it capture all fields
|
||||
var v map[string]any
|
||||
if err := u.Unmarshal(&v); err != nil {
|
||||
@@ -183,6 +188,10 @@ func (c *AuthServiceConfigs) UnmarshalYAML(ctx context.Context, unmarshal func(i
|
||||
}
|
||||
|
||||
for name, u := range raw {
|
||||
err := NameValidation(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var v map[string]any
|
||||
if err := u.Unmarshal(&v); err != nil {
|
||||
return fmt.Errorf("unable to unmarshal %q: %w", name, err)
|
||||
@@ -226,6 +235,10 @@ func (c *EmbeddingModelConfigs) UnmarshalYAML(ctx context.Context, unmarshal fun
|
||||
}
|
||||
|
||||
for name, u := range raw {
|
||||
err := NameValidation(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Unmarshal to a general type that ensure it capture all fields
|
||||
var v map[string]any
|
||||
if err := u.Unmarshal(&v); err != nil {
|
||||
@@ -270,6 +283,10 @@ func (c *ToolConfigs) UnmarshalYAML(ctx context.Context, unmarshal func(interfac
|
||||
}
|
||||
|
||||
for name, u := range raw {
|
||||
err := NameValidation(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var v map[string]any
|
||||
if err := u.Unmarshal(&v); err != nil {
|
||||
return fmt.Errorf("unable to unmarshal %q: %w", name, err)
|
||||
@@ -323,6 +340,10 @@ func (c *ToolsetConfigs) UnmarshalYAML(ctx context.Context, unmarshal func(inter
|
||||
}
|
||||
|
||||
for name, toolList := range raw {
|
||||
err := NameValidation(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
(*c)[name] = tools.ToolsetConfig{Name: name, ToolNames: toolList}
|
||||
}
|
||||
return nil
|
||||
@@ -342,6 +363,10 @@ func (c *PromptConfigs) UnmarshalYAML(ctx context.Context, unmarshal func(interf
|
||||
}
|
||||
|
||||
for name, u := range raw {
|
||||
err := NameValidation(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var v map[string]any
|
||||
if err := u.Unmarshal(&v); err != nil {
|
||||
return fmt.Errorf("unable to unmarshal prompt %q: %w", name, err)
|
||||
@@ -389,7 +414,31 @@ func (c *PromptsetConfigs) UnmarshalYAML(ctx context.Context, unmarshal func(int
|
||||
}
|
||||
|
||||
for name, promptList := range raw {
|
||||
err := NameValidation(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
(*c)[name] = prompts.PromptsetConfig{Name: name, PromptNames: promptList}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Tools naming validation is added in the MCP v2025-11-25, but we'll be
|
||||
// implementing it across Toolbox
|
||||
// Tool names SHOULD be between 1 and 128 characters in length (inclusive).
|
||||
// Tool names SHOULD be considered case-sensitive.
|
||||
// The following SHOULD be the only allowed characters: uppercase and lowercase ASCII letters (A-Z, a-z), digits (0-9), underscore (_), hyphen (-), and dot (.)
|
||||
// Tool names SHOULD NOT contain spaces, commas, or other special characters.
|
||||
// Tool names SHOULD be unique within a server.
|
||||
func NameValidation(name string) error {
|
||||
strLen := len(name)
|
||||
if strLen < 1 || strLen > 128 {
|
||||
return fmt.Errorf("resource name SHOULD be between 1 and 128 characters in length (inclusive)")
|
||||
}
|
||||
validChars := regexp.MustCompile("^[a-zA-Z0-9_.-]+$")
|
||||
isValid := validChars.MatchString(name)
|
||||
if !isValid {
|
||||
return fmt.Errorf("invalid character for resource name; only uppercase and lowercase ASCII letters (A-Z, a-z), digits (0-9), underscore (_), hyphen (-), and dot (.) is allowed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -200,3 +200,62 @@ func TestUpdateServer(t *testing.T) {
|
||||
t.Errorf("error updating server, promptset (-want +got):\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNameValidation(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
resourceName string
|
||||
errStr string
|
||||
}{
|
||||
{
|
||||
desc: "names with 0 length",
|
||||
resourceName: "",
|
||||
errStr: "resource name SHOULD be between 1 and 128 characters in length (inclusive)",
|
||||
},
|
||||
{
|
||||
desc: "names with allowed length",
|
||||
resourceName: "foo",
|
||||
},
|
||||
{
|
||||
desc: "names with 128 length",
|
||||
resourceName: strings.Repeat("a", 128),
|
||||
},
|
||||
{
|
||||
desc: "names with more than 128 length",
|
||||
resourceName: strings.Repeat("a", 129),
|
||||
errStr: "resource name SHOULD be between 1 and 128 characters in length (inclusive)",
|
||||
},
|
||||
{
|
||||
desc: "names with space",
|
||||
resourceName: "foo bar",
|
||||
errStr: "invalid character for resource name; only uppercase and lowercase ASCII letters (A-Z, a-z), digits (0-9), underscore (_), hyphen (-), and dot (.) is allowed",
|
||||
},
|
||||
{
|
||||
desc: "names with commas",
|
||||
resourceName: "foo,bar",
|
||||
errStr: "invalid character for resource name; only uppercase and lowercase ASCII letters (A-Z, a-z), digits (0-9), underscore (_), hyphen (-), and dot (.) is allowed",
|
||||
},
|
||||
{
|
||||
desc: "names with other special character",
|
||||
resourceName: "foo!",
|
||||
errStr: "invalid character for resource name; only uppercase and lowercase ASCII letters (A-Z, a-z), digits (0-9), underscore (_), hyphen (-), and dot (.) is allowed",
|
||||
},
|
||||
{
|
||||
desc: "names with allowed special character",
|
||||
resourceName: "foo_.-bar6",
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
err := server.NameValidation(tc.resourceName)
|
||||
if err != nil {
|
||||
if tc.errStr != err.Error() {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
}
|
||||
if err == nil && tc.errStr != "" {
|
||||
t.Fatalf("expect error: %s", tc.errStr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,13 +85,66 @@ func initDataplexConnection(ctx context.Context) (*dataplex.CatalogClient, error
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// cleanupOldAspectTypes Deletes AspectTypes older than the specified duration.
|
||||
func cleanupOldAspectTypes(t *testing.T, ctx context.Context, client *dataplex.CatalogClient, oldThreshold time.Duration) {
|
||||
parent := fmt.Sprintf("projects/%s/locations/us", DataplexProject)
|
||||
olderThanTime := time.Now().Add(-oldThreshold)
|
||||
|
||||
listReq := &dataplexpb.ListAspectTypesRequest{
|
||||
Parent: parent,
|
||||
PageSize: 100, // Fetch up to 100 items
|
||||
OrderBy: "create_time asc", // Order by creation time
|
||||
}
|
||||
|
||||
const maxDeletes = 8 // Explicitly limit the number of deletions
|
||||
it := client.ListAspectTypes(ctx, listReq)
|
||||
var aspectTypesToDelete []string
|
||||
for len(aspectTypesToDelete) < maxDeletes {
|
||||
aspectType, err := it.Next()
|
||||
if err == iterator.Done {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Logf("Warning: Failed to list aspect types during cleanup: %v", err)
|
||||
return
|
||||
}
|
||||
// Perform time-based filtering in memory
|
||||
if aspectType.CreateTime != nil {
|
||||
createTime := aspectType.CreateTime.AsTime()
|
||||
if createTime.Before(olderThanTime) {
|
||||
aspectTypesToDelete = append(aspectTypesToDelete, aspectType.GetName())
|
||||
}
|
||||
} else {
|
||||
t.Logf("Warning: AspectType %s has no CreateTime", aspectType.GetName())
|
||||
}
|
||||
}
|
||||
if len(aspectTypesToDelete) == 0 {
|
||||
t.Logf("cleanupOldAspectTypes: No aspect types found older than %s to delete.", oldThreshold.String())
|
||||
return
|
||||
}
|
||||
|
||||
for _, aspectTypeName := range aspectTypesToDelete {
|
||||
deleteReq := &dataplexpb.DeleteAspectTypeRequest{Name: aspectTypeName}
|
||||
op, err := client.DeleteAspectType(ctx, deleteReq)
|
||||
if err != nil {
|
||||
t.Logf("Warning: Failed to delete aspect type %s: %v", aspectTypeName, err)
|
||||
continue // Skip to the next item if initiation fails
|
||||
}
|
||||
|
||||
if err := op.Wait(ctx); err != nil {
|
||||
t.Logf("Warning: Failed to delete aspect type %s, operation error: %v", aspectTypeName, err)
|
||||
} else {
|
||||
t.Logf("cleanupOldAspectTypes: Successfully deleted %s", aspectTypeName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDataplexToolEndpoints(t *testing.T) {
|
||||
sourceConfig := getDataplexVars(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
var args []string
|
||||
|
||||
bigqueryClient, err := initBigQueryConnection(ctx, DataplexProject)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create Cloud SQL connection pool: %s", err)
|
||||
@@ -102,6 +155,9 @@ func TestDataplexToolEndpoints(t *testing.T) {
|
||||
t.Fatalf("unable to create Dataplex connection: %s", err)
|
||||
}
|
||||
|
||||
// Cleanup older aspecttypes
|
||||
cleanupOldAspectTypes(t, ctx, dataplexClient, 1*time.Hour)
|
||||
|
||||
// create resources with UUID
|
||||
datasetName := fmt.Sprintf("temp_toolbox_test_%s", strings.ReplaceAll(uuid.New().String(), "-", ""))
|
||||
tableName := fmt.Sprintf("param_table_%s", strings.ReplaceAll(uuid.New().String(), "-", ""))
|
||||
|
||||
Reference in New Issue
Block a user