tools

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

pkg/tools/func.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMCPClientFromStdio

func NewMCPClientFromStdio(reader io.Reader, writer io.Writer, options MCPClientOptions) (*client.Client, error)

NewMCPClientFromStdio creates a new MCP client using standard I/O for communication. This is useful for connecting to an MCP server launched as a subprocess.

func RegisterMCPTools

func RegisterMCPTools(registry *Registry, mcpClient *client.Client) error

RegisterMCPTools discovers and registers all tools from an MCP server in the provided registry. This automatically bridges MCP tools to the local tool interface.

Types

type FuncTool

type FuncTool struct {
	// contains filtered or unexported fields
}

FuncTool wraps a Go function as a Tool implementation.

func NewFuncTool

func NewFuncTool(name, description string, schema models.InputSchema, fn ToolFunc) *FuncTool

NewFuncTool creates a new function-based tool.

func (*FuncTool) Call

func (t *FuncTool) Call(ctx context.Context, args map[string]interface{}) (*models.CallToolResult, error)

Call executes the wrapped function with the provided arguments.

func (*FuncTool) CanHandle

func (t *FuncTool) CanHandle(ctx context.Context, intent string) bool

CanHandle checks if the tool can handle a specific action/intent.

func (*FuncTool) Description

func (t *FuncTool) Description() string

Description returns human-readable explanation of the tool.

func (*FuncTool) Execute

func (t *FuncTool) Execute(ctx context.Context, params map[string]interface{}) (core.ToolResult, error)

Execute runs the tool with provided parameters and adapts the result to the core interface.

func (*FuncTool) InputSchema

func (t *FuncTool) InputSchema() models.InputSchema

InputSchema returns the expected parameter structure.

func (*FuncTool) Metadata

func (t *FuncTool) Metadata() *core.ToolMetadata

Metadata returns the tool's metadata for intent matching.

func (*FuncTool) Name

func (t *FuncTool) Name() string

Name returns the tool's identifier.

func (*FuncTool) Type

func (t *FuncTool) Type() ToolType

Type returns the tool type.

func (*FuncTool) Validate

func (t *FuncTool) Validate(params map[string]interface{}) error

Validate checks if the parameters match the expected schema.

type MCPClientOptions

type MCPClientOptions struct {
	ClientName    string
	ClientVersion string
	Logger        logging.Logger
}

MCPClientOptions contains configuration options for creating an MCP client.

type MCPTool

type MCPTool struct {
	// contains filtered or unexported fields
}

MCPTool represents a tool that delegates to an MCP server.

func NewMCPTool

func NewMCPTool(name, description string, schema models.InputSchema,
	client *client.Client, toolName string) *MCPTool

NewMCPTool creates a new MCP-based tool.

func (*MCPTool) Call

func (t *MCPTool) Call(ctx context.Context, args map[string]interface{}) (*models.CallToolResult, error)

Call forwards the call to the MCP server and returns the result.

func (*MCPTool) CanHandle

func (t *MCPTool) CanHandle(ctx context.Context, intent string) bool

CanHandle checks if the tool can handle a specific action/intent.

func (*MCPTool) Description

func (t *MCPTool) Description() string

Description returns human-readable explanation of the tool.

func (*MCPTool) Execute

func (t *MCPTool) Execute(ctx context.Context, params map[string]interface{}) (core.ToolResult, error)

Execute runs the tool with provided parameters and adapts the result to the core interface.

func (*MCPTool) InputSchema

func (t *MCPTool) InputSchema() models.InputSchema

InputSchema returns the expected parameter structure.

func (*MCPTool) Metadata

func (t *MCPTool) Metadata() *core.ToolMetadata

Metadata returns the tool's metadata for intent matching.

func (*MCPTool) Name

func (t *MCPTool) Name() string

Name returns the tool's identifier.

func (*MCPTool) Type

func (t *MCPTool) Type() ToolType

Type returns the tool type.

func (*MCPTool) Validate

func (t *MCPTool) Validate(params map[string]interface{}) error

Validate checks if the parameters match the expected schema.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages a collection of tools and provides methods for registration, retrieval, and invocation. It safely handles concurrent access to tools.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new empty tool registry.

func (*Registry) Call

func (r *Registry) Call(ctx context.Context, name string, args map[string]interface{}) (*models.CallToolResult, error)

Call invokes a tool by name with the given arguments. This provides a convenient way to call tools without retrieving them first.

func (*Registry) Get

func (r *Registry) Get(name string) (Tool, error)

Get retrieves a tool by name. Returns an error if the tool doesn't exist.

func (*Registry) List

func (r *Registry) List() []Tool

List returns all registered tools.

func (*Registry) Register

func (r *Registry) Register(tool Tool) error

Register adds a tool to the registry. Returns an error if a tool with the same name already exists.

func (*Registry) Unregister

func (r *Registry) Unregister(name string) error

Unregister removes a tool from the registry. Returns an error if the tool doesn't exist.

type Tool

type Tool interface {
	// Name returns the tool's identifier
	Name() string

	// Description returns human-readable explanation of the tool's purpose
	Description() string

	// InputSchema returns the expected parameter structure
	InputSchema() models.InputSchema

	// Call executes the tool with the provided arguments
	Call(ctx context.Context, args map[string]interface{}) (*models.CallToolResult, error)
}

Tool defines a callable tool interface that abstracts both local functions and remote MCP tools. This provides a unified way to interact with tools regardless of their implementation details.

type ToolFunc

type ToolFunc func(ctx context.Context, args map[string]interface{}) (*models.CallToolResult, error)

ToolFunc represents a function that can be called as a tool.

type ToolType

type ToolType string

ToolType represents the source/type of a tool.

const (
	// ToolTypeFunc represents a tool backed by a local Go function.
	ToolTypeFunc ToolType = "function"

	// ToolTypeMCP represents a tool backed by an MCP server.
	ToolTypeMCP ToolType = "mcp"
)

Jump to

Keyboard shortcuts

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