tools

package
v1.3.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: Apache-2.0, Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownToolType = fmt.Errorf("unknown tool type")

Functions

func IsAuthorized

func IsAuthorized(authRequiredSources []string, verifiedAuthServices []string) bool

Helper function that returns if a tool invocation request is authorized

func IsValidName

func IsValidName(s string) bool

func Register

func Register(resourceType string, factory ToolConfigFactory) bool

Register allows individual tool packages to register their configuration factory function. This is typically called from an init() function in the tool's package. It associates a 'type' string with a function that can produce the specific ToolConfig type. It returns true if the registration was successful, and false if a tool with the same type was already registered.

func ShouldSuppress

func ShouldSuppress(ctx context.Context, t Tool, src sources.Source) bool

ShouldSuppress returns true if the tool should be suppressed from registration when its data source is in read-only mode, and logs relevant warnings or info messages. By default, if the source is read-only and the tool's ReadOnlyHint is explicitly false, the tool is suppressed. Unannotated tools (ReadOnlyHint == nil) or read-only tools (ReadOnlyHint == true) are not suppressed.

Types

type AccessToken

type AccessToken string

func (AccessToken) ParseBearerToken

func (token AccessToken) ParseBearerToken() (string, error)

type BaseTool

type BaseTool[T ToolMeta] struct {
	Cfg T

	StaticParameters parameters.Parameters
	// contains filtered or unexported fields
}

BaseTool provides default implementations of various methods on the Tool interface. Tools embed BaseTool to drop their boilerplate and override only methods that need custom behavior.

func NewBaseTool

func NewBaseTool[T ToolMeta](cfg T, annotations *ToolAnnotations, metadata Manifest, staticParameters parameters.Parameters) BaseTool[T]

NewBaseTool constructs a BaseTool from a resolved Config (typically the per-tool Config after Initialize has filled in defaults), the resolved annotations, the precomputed Manifest, and the tool's static parameters.

func (BaseTool[T]) Authorized

func (b BaseTool[T]) Authorized(verifiedAuthServices []string) bool

func (BaseTool[T]) EmbedParams

func (b BaseTool[T]) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, pMgr PrimitiveManagerI) (parameters.ParamValues, error)

func (BaseTool[T]) GetAnnotations

func (b BaseTool[T]) GetAnnotations(_ sources.Source) *ToolAnnotations

func (BaseTool[T]) GetAuthRequired

func (b BaseTool[T]) GetAuthRequired() []string

func (BaseTool[T]) GetAuthTokenHeaderName

func (b BaseTool[T]) GetAuthTokenHeaderName(_ sources.Source) (string, error)

func (BaseTool[T]) GetDescription

func (b BaseTool[T]) GetDescription() string

func (BaseTool[T]) GetName

func (b BaseTool[T]) GetName() string

func (BaseTool[T]) GetParameters

func (b BaseTool[T]) GetParameters(_ sources.Source) (parameters.Parameters, error)

func (BaseTool[T]) GetScopesRequired

func (b BaseTool[T]) GetScopesRequired() []string

func (BaseTool[T]) GetToolUIMetadata

func (b BaseTool[T]) GetToolUIMetadata() *ToolUIMetadata

func (BaseTool[T]) HasSecureParams

func (b BaseTool[T]) HasSecureParams() bool

func (BaseTool[T]) Manifest

func (b BaseTool[T]) Manifest(_ sources.Source) (Manifest, error)

Manifest returns the precomputed metadata. It and GetParameters stay trivial and never call each other: embedded methods have no virtual dispatch, so a BaseTool method calling another would miss a concrete tool's override.

func (BaseTool[T]) RequiresClientAuthorization

func (b BaseTool[T]) RequiresClientAuthorization(_ sources.Source) (bool, error)

func (BaseTool[T]) StaticManifest

func (b BaseTool[T]) StaticManifest() Manifest

StaticManifest returns the manifest baked at Initialize, with no source resolution. Dynamic tools override Manifest/GetParameters to refine params against a live source, but not this method, so it always reaches the baked skeleton — used for offline generation (e.g. skills) where no source exists.

type ConfigBase

type ConfigBase struct {
	Name           string           `yaml:"name"           validate:"required"`
	Description    string           `yaml:"description"`
	AuthRequired   []string         `yaml:"authRequired"`
	ScopesRequired []string         `yaml:"scopesRequired"`
	Annotations    *ToolAnnotations `yaml:"annotations,omitempty"`
	UI             *ToolUIMetadata  `yaml:"ui,omitempty"`
}

ConfigBase owns the YAML fields that every tool's Config shares and that BaseTool reads through. Description is eagerly defaulted by the tool's Initialize (many prebuilt configs omit description: and rely on a canned per-tool string), so post-Initialize ConfigBase.Description holds the resolved value.

func (ConfigBase) GetAuthRequired

func (c ConfigBase) GetAuthRequired() []string

func (ConfigBase) GetDescription

func (c ConfigBase) GetDescription() string

func (ConfigBase) GetName

func (c ConfigBase) GetName() string

func (ConfigBase) GetScopesRequired

func (c ConfigBase) GetScopesRequired() []string

func (ConfigBase) GetToolUIMetadata

func (c ConfigBase) GetToolUIMetadata() *ToolUIMetadata

type HTTPMethod

type HTTPMethod string

HTTPMethod is a string of a valid HTTP method (e.g "GET")

func (*HTTPMethod) UnmarshalYAML

func (i *HTTPMethod) UnmarshalYAML(ctx context.Context, unmarshal func(interface{}) error) error

type Manifest

type Manifest struct {
	Description  string                         `json:"description"`
	Parameters   []parameters.ParameterManifest `json:"parameters"`
	AuthRequired []string                       `json:"authRequired"`
}

Manifest is the representation of tools sent to Client SDKs.

type PrimitiveManagerI

type PrimitiveManagerI interface {
	GetSource(string) (sources.Source, bool)
	GetEmbeddingModel(string) (embeddingmodels.EmbeddingModel, bool)
}

PrimitiveManagerI defines the minimal view of the primitives.PrimitiveManager that the Tool package needs. This is implemented to prevent import cycles.

type Tool

type Tool interface {
	GetName() string
	GetSourceName() string
	GetDescription() string
	GetAuthRequired() []string
	GetAnnotations(sources.Source) *ToolAnnotations
	GetToolUIMetadata() *ToolUIMetadata
	Invoke(context.Context, sources.Source, parameters.ParamValues, AccessToken) (any, util.ToolboxError)
	EmbedParams(context.Context, parameters.ParamValues, PrimitiveManagerI) (parameters.ParamValues, error)
	Manifest(sources.Source) (Manifest, error)
	StaticManifest() Manifest
	Authorized([]string) bool
	RequiresClientAuthorization(sources.Source) (bool, error)
	ToConfig() ToolConfig
	GetAuthTokenHeaderName(sources.Source) (string, error)
	GetParameters(sources.Source) (parameters.Parameters, error)
	GetScopesRequired() []string
	ValidateSource(sources.Source) error
	HasSecureParams() bool
}

type ToolAnnotations

type ToolAnnotations struct {
	DestructiveHint *bool `json:"destructiveHint,omitempty" yaml:"destructiveHint,omitempty"`
	IdempotentHint  *bool `json:"idempotentHint,omitempty" yaml:"idempotentHint,omitempty"`
	OpenWorldHint   *bool `json:"openWorldHint,omitempty" yaml:"openWorldHint,omitempty"`
	ReadOnlyHint    *bool `json:"readOnlyHint,omitempty" yaml:"readOnlyHint,omitempty"`
}

https://modelcontextprotocol.io/specification/2025-06-18/schema#toolannotations

func GetAnnotationsOrDefault

func GetAnnotationsOrDefault(annotations *ToolAnnotations, defaultFn func() *ToolAnnotations) *ToolAnnotations

GetAnnotationsOrDefault returns the provided annotations if non-nil, otherwise returns the result of calling defaultFn.

func NewDestructiveAnnotations

func NewDestructiveAnnotations() *ToolAnnotations

NewDestructiveAnnotations creates default annotations for a destructive tool. Use this for tools that create, update, or delete data.

func NewReadOnlyAnnotations

func NewReadOnlyAnnotations() *ToolAnnotations

NewReadOnlyAnnotations creates default annotations for a read-only tool. Use this for tools that only query/fetch data without side effects.

func NewWriteAnnotations

func NewWriteAnnotations() *ToolAnnotations

NewWriteAnnotations creates default annotations for a non-destructive write tool: ReadOnlyHint is false, DestructiveHint is left unset.

type ToolConfig

type ToolConfig interface {
	ToolConfigType() string
	Initialize(context.Context) (Tool, error)
}

func DecodeConfig

func DecodeConfig(ctx context.Context, resourceType string, name string, decoder *yaml.Decoder) (ToolConfig, error)

DecodeConfig looks up the registered factory for the given type and uses it to decode the tool configuration.

type ToolConfigFactory

type ToolConfigFactory func(ctx context.Context, name string, decoder *yaml.Decoder) (ToolConfig, error)

ToolConfigFactory defines the signature for a function that creates and decodes a specific tool's configuration. It takes the context, the tool's name, and a YAML decoder to parse the config.

type ToolMeta

type ToolMeta interface {
	GetName() string
	GetDescription() string
	GetAuthRequired() []string
	GetScopesRequired() []string
	GetToolUIMetadata() *ToolUIMetadata
}

ToolMeta is the read-only view BaseTool needs of any tool's Config. Tools satisfy it for free by embedding ConfigBase.

type ToolUIMetadata

type ToolUIMetadata struct {
	Resource   string           `yaml:"resource" validate:"required"`
	Visibility []ToolVisibility `yaml:"visibility,omitempty" validate:"omitempty,dive,oneof=model app"`
}

ToolUIMetadata defines the UI-specific metadata for a tool with UI Resource.

type ToolVisibility

type ToolVisibility string

ToolVisibility defines the visibility of a tool in the UI.

const (
	VisibilityModel ToolVisibility = "model"
	VisibilityApp   ToolVisibility = "app"
)

type Toolset

type Toolset struct {
	ToolsetConfig
	Tools    []*Tool         `yaml:",inline"`
	Manifest ToolsetManifest `yaml:",inline"`
	// contains filtered or unexported fields
}

func (Toolset) BuildManifest

func (t Toolset) BuildManifest(pMgr PrimitiveManagerI) (ToolsetManifest, error)

BuildManifest resolves the manifest for every tool in the toolset against the provided sources, returning an error if any tool's manifest cannot be built.

func (Toolset) ContainsTool

func (t Toolset) ContainsTool(name string) bool

ContainsTool reports whether the toolset includes a tool with the given name. When built via Initialize, lookups are O(1) via toolNameSet; for Toolsets constructed directly (e.g., in tests), falls back to a linear scan of ToolNames.

func (Toolset) ToConfig

func (t Toolset) ToConfig() ToolsetConfig

type ToolsetConfig

type ToolsetConfig struct {
	Name      string   `yaml:"name"`
	ToolNames []string `yaml:",inline"`
}

func (ToolsetConfig) Initialize

func (t ToolsetConfig) Initialize(serverVersion string, toolsMap map[string]Tool) (Toolset, error)

type ToolsetManifest

type ToolsetManifest struct {
	ServerVersion string              `json:"serverVersion"`
	ToolsManifest map[string]Manifest `json:"tools"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL