tools

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2025 License: MIT Imports: 15 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 core.ToolRegistry, mcpClient MCPClientInterface) error

RegisterMCPTools dynamically discovers tools from an MCP client and registers them into a core.ToolRegistry, wrapping them to conform to the core.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 InMemoryToolRegistry added in v0.27.0

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

InMemoryToolRegistry provides a basic in-memory implementation of the ToolRegistry interface.

func NewInMemoryToolRegistry added in v0.27.0

func NewInMemoryToolRegistry() *InMemoryToolRegistry

NewInMemoryToolRegistry creates a new, empty InMemoryToolRegistry.

func (*InMemoryToolRegistry) Get added in v0.27.0

func (r *InMemoryToolRegistry) Get(name string) (core.Tool, error)

Get retrieves a tool by its name. It returns an error if the tool is not found.

func (*InMemoryToolRegistry) List added in v0.27.0

func (r *InMemoryToolRegistry) List() []core.Tool

List returns a slice of all registered tools. The order is not guaranteed.

func (*InMemoryToolRegistry) Match added in v0.27.0

func (r *InMemoryToolRegistry) Match(intent string) []core.Tool

Match finds tools that might match a given intent string. This basic implementation checks if the intent contains the tool name (case-insensitive). More sophisticated matching (e.g., using descriptions or CanHandle) could be added.

func (*InMemoryToolRegistry) Register added in v0.27.0

func (r *InMemoryToolRegistry) Register(tool core.Tool) error

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

type MCPClientInterface added in v0.27.0

type MCPClientInterface interface {
	ListTools(ctx context.Context, cursor *models.Cursor) (*models.ListToolsResult, error)
	// Add CallTool if the wrapper's Execute needs it indirectly via the interface
	CallTool(ctx context.Context, name string, args map[string]interface{}) (*models.CallToolResult, error)
}

This improves testability by allowing mocks.

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 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"
)

type XMLAction added in v0.26.0

type XMLAction struct {
	XMLName   xml.Name      `xml:"action"`
	ToolName  string        `xml:"tool_name,omitempty"`
	Arguments []XMLArgument `xml:"arguments>arg,omitempty"`

	Content string `xml:",chardata"`
}

func (*XMLAction) GetArgumentsMap added in v0.26.0

func (xa *XMLAction) GetArgumentsMap() map[string]interface{}

Helper to convert XML arguments to map[string]interface{} Note: This currently stores all values as strings. More sophisticated type inference or checking could be added later if needed based on tool schemas.

type XMLArgument added in v0.26.0

type XMLArgument struct {
	XMLName xml.Name `xml:"arg"`
	Key     string   `xml:"key,attr"`
	Value   string   `xml:",chardata"` // Store raw value as string for now
}

Jump to

Keyboard shortcuts

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