tools

package
v1.30.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DescriptionParam is the parameter name for the description
	DescriptionParam = "description"
)

Variables

This section is empty.

Functions

func As

func As[T any](ts ToolSet) (T, bool)

As performs a type assertion on a ToolSet, walking the wrapper chain if needed. It checks the outermost toolset first, then recursively unwraps through any Unwrapper implementations (including StartableToolSet and decorator wrappers) until it finds a match or reaches the end of the chain.

Example:

if pp, ok := tools.As[tools.PromptProvider](toolset); ok {
    prompts, _ := pp.ListPrompts(ctx)
}

func ConfigureHandlers

func ConfigureHandlers(ts ToolSet, elicitHandler ElicitationHandler, oauthHandler func(), managedOAuth bool)

ConfigureHandlers sets all applicable handlers on a toolset. It checks for Elicitable and OAuthCapable interfaces and configures them. This is a convenience function that handles the capability checking internally.

func ConvertSchema

func ConvertSchema(params, v any) error

func DescribeToolSet

func DescribeToolSet(ts ToolSet) string

DescribeToolSet returns a short description for ts suitable for user-visible messages. It unwraps a StartableToolSet, then delegates to Describer if implemented. Falls back to the Go type name when not.

func ExtractDescription

func ExtractDescription(arguments string) string

ExtractDescription extracts the description from tool call arguments.

func GetInstructions

func GetInstructions(ts ToolSet) string

GetInstructions returns instructions if the toolset implements Instructable. Returns empty string if the toolset doesn't provide instructions.

func MustSchemaFor

func MustSchemaFor[T any]() any

func SchemaFor

func SchemaFor[T any]() (any, error)

func SchemaToMap

func SchemaToMap(params any) (map[string]any, error)

func ToolsetIdentifier

func ToolsetIdentifier(ts ToolSet) string

ToolsetIdentifier returns a human-readable identifier for a toolset. It falls back to the toolset type when no metadata is available.

Types

type AudioContent

type AudioContent = MediaContent

AudioContent is an alias kept for readability at call sites.

type ChangeNotifier

type ChangeNotifier interface {
	SetToolsChangedHandler(handler func())
}

ChangeNotifier is implemented by toolsets that can notify when their tool list changes (e.g. after an MCP ToolListChanged notification).

type Describer

type Describer interface {
	Describe() string
}

Describer can be implemented by a ToolSet to provide a short, user-visible description that uniquely identifies the toolset instance (e.g. for use in error messages and warnings). The string must never contain secrets.

type Elicitable

type Elicitable interface {
	SetElicitationHandler(handler ElicitationHandler)
}

Elicitable is implemented by toolsets that support MCP elicitation.

type ElicitationAction

type ElicitationAction string
const (
	ElicitationActionAccept  ElicitationAction = "accept"
	ElicitationActionDecline ElicitationAction = "decline"
	ElicitationActionCancel  ElicitationAction = "cancel"
)

type ElicitationHandler

type ElicitationHandler func(ctx context.Context, req *mcp.ElicitParams) (ElicitationResult, error)

ElicitationHandler is a function type that handles elicitation requests from the MCP server This allows the runtime to handle elicitation requests and propagate them to its own client

type ElicitationResult

type ElicitationResult struct {
	Action  ElicitationAction `json:"action"`
	Content map[string]any    `json:"content,omitempty"`
}

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
}

type ImageContent

type ImageContent = MediaContent

ImageContent is an alias kept for readability at call sites.

type Instructable

type Instructable interface {
	Instructions() string
}

Instructable is implemented by toolsets that provide custom instructions.

type MediaContent

type MediaContent struct {
	// Data is the base64-encoded payload.
	Data string `json:"data"`
	// MimeType identifies the content type (e.g. "image/png", "audio/wav").
	MimeType string `json:"mimeType"`
}

MediaContent represents base64-encoded binary data (image, audio, etc.) returned by a tool.

type OAuthCapable

type OAuthCapable interface {
	SetOAuthSuccessHandler(handler func())
	SetManagedOAuth(managed bool)
}

OAuthCapable is implemented by toolsets that support OAuth flows.

type Startable

type Startable interface {
	Start(ctx context.Context) error
	Stop(ctx context.Context) error
}

Startable is implemented by toolsets that require initialization before use. Toolsets that don't implement this interface are assumed to be ready immediately.

type StartableToolSet

type StartableToolSet struct {
	ToolSet
	// contains filtered or unexported fields
}

StartableToolSet wraps a ToolSet with lazy, single-flight start semantics. This is the canonical way to manage toolset lifecycle.

func NewStartable

func NewStartable(ts ToolSet) *StartableToolSet

NewStartable wraps a ToolSet for lazy initialization.

func (*StartableToolSet) IsStarted

func (s *StartableToolSet) IsStarted() bool

IsStarted returns whether the toolset has been successfully started. For toolsets that don't implement Startable, this always returns true.

func (*StartableToolSet) Start

func (s *StartableToolSet) Start(ctx context.Context) error

Start starts the toolset with single-flight semantics. Concurrent callers block until the start attempt completes. If start fails, a future call will retry. If the underlying toolset doesn't implement Startable, this is a no-op.

func (*StartableToolSet) Stop

func (s *StartableToolSet) Stop(ctx context.Context) error

Stop stops the toolset if it implements Startable.

func (*StartableToolSet) Unwrap

func (s *StartableToolSet) Unwrap() ToolSet

Unwrap returns the underlying ToolSet.

type Tool

type Tool struct {
	Name                    string          `json:"name"`
	Category                string          `json:"category"`
	Description             string          `json:"description,omitempty"`
	Parameters              any             `json:"parameters"`
	Annotations             ToolAnnotations `json:"annotations"`
	OutputSchema            any             `json:"outputSchema"`
	Handler                 ToolHandler     `json:"-"`
	AddDescriptionParameter bool            `json:"-"`
}

func AddDescriptionParameter

func AddDescriptionParameter(toolList []Tool) []Tool

AddDescriptionParameter adds a "description" parameter to tools that have AddDescriptionParameter set to true. This allows the LLM to provide context about what it's doing with each tool call.

func (*Tool) DisplayName

func (t *Tool) DisplayName() string

type ToolAnnotations

type ToolAnnotations mcp.ToolAnnotations

type ToolCall

type ToolCall struct {
	ID       string       `json:"id,omitempty"`
	Type     ToolType     `json:"type"`
	Function FunctionCall `json:"function"`
}

type ToolCallResult

type ToolCallResult struct {
	Output  string `json:"output"`
	IsError bool   `json:"isError,omitempty"`
	Meta    any    `json:"meta,omitempty"`
	// Images contains optional image attachments returned by the tool.
	Images []MediaContent `json:"images,omitempty"`
	// Audios contains optional audio attachments returned by the tool.
	Audios []MediaContent `json:"audios,omitempty"`
	// StructuredContent holds optional structured output returned by an MCP
	// tool whose definition includes an OutputSchema. When non-nil it is the
	// JSON-decoded structured result from the server.
	StructuredContent any `json:"structuredContent,omitempty"`
}

func ResultError

func ResultError(output string) *ToolCallResult

func ResultSuccess

func ResultSuccess(output string) *ToolCallResult

type ToolHandler

type ToolHandler func(ctx context.Context, toolCall ToolCall) (*ToolCallResult, error)

func NewHandler

func NewHandler[T any](fn func(context.Context, T) (*ToolCallResult, error)) ToolHandler

NewHandler creates a type-safe tool handler from a function that accepts typed parameters. It handles JSON unmarshaling of the tool call arguments into the specified type T.

type ToolSet

type ToolSet interface {
	Tools(ctx context.Context) ([]Tool, error)
}

ToolSet defines the interface for a set of tools.

type ToolType

type ToolType string

type ToolsetMetadata

type ToolsetMetadata interface {
	ToolsetID() string
}

ToolsetMetadata exposes optional details for toolset identification. Implemented by toolsets that can provide additional context for warnings.

type Unwrapper

type Unwrapper interface {
	Unwrap() ToolSet
}

Unwrapper is implemented by toolset wrappers that decorate another ToolSet. This allows As to walk the wrapper chain and find inner capabilities.

Directories

Path Synopsis
Package a2a provides a toolset implementation for connecting to remote A2A agents.
Package a2a provides a toolset implementation for connecting to remote A2A agents.

Jump to

Keyboard shortcuts

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