tools

package
v0.0.0-...-fd62f16 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatMCPServersForDisplay

func FormatMCPServersForDisplay(servers []string) []string

FormatMCPServersForDisplay formats a list of MCP server specs for display

func GetMCPDisplayName

func GetMCPDisplayName(serverSpec string) string

GetMCPDisplayName returns a display-friendly name for an MCP server spec Formats: "file.json → command args" or "file.json#server → command args"

func LoadMCPConfigFile

func LoadMCPConfigFile(jsonFile string) (map[string]MCPConfig, error)

LoadMCPConfigFile parses a config file and returns server configs Requires mcpServers format: {"mcpServers": {"name": {...}}}

func ParseServerSpec

func ParseServerSpec(spec string) (jsonFile string, serverName string)

ParseServerSpec splits a server spec into file path and server name Format: "path/to/config.json" or "path/to/config.json#servername"

Types

type ContextualTool

type ContextualTool interface {
	Tool
	SetContext(ctx any)
}

ContextualTool is a tool that needs external context to execute

type LoadResult

type LoadResult struct {
	Type    string         // "native", "shell", "mcp"
	Servers []ServerResult // For MCP, one per server loaded; for shell/native, single entry
}

LoadResult contains information about tools that were loaded

type Logger

type Logger interface {
	Log(message string)
}

Logger interface for dependency injection

type LoggerTool

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

LoggerTool is an example tool that needs context to be injected

func (*LoggerTool) Execute

func (t *LoggerTool) Execute(ctx context.Context, args map[string]any) (string, error)

func (*LoggerTool) GetName

func (t *LoggerTool) GetName() string

GetName returns the tool name

func (*LoggerTool) GetSchema

func (t *LoggerTool) GetSchema() *jsonschema.Schema

func (*LoggerTool) GetSource

func (t *LoggerTool) GetSource() string

GetSource returns "builtin" for native tools

func (*LoggerTool) GetType

func (t *LoggerTool) GetType() string

GetType returns "native" for built-in tools

func (*LoggerTool) SetContext

func (t *LoggerTool) SetContext(ctx any)

type MCPClient

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

MCPClient manages connection to an MCP server

func NewMCPClient

func NewMCPClient(serverSpec string) (*MCPClient, error)

NewMCPClient creates a new MCP client from a server spec Format: "path/to/config.json" or "path/to/config.json#servername"

func NewMCPClientFromConfig

func NewMCPClientFromConfig(config *MCPConfig) (*MCPClient, error)

NewMCPClientFromConfig creates a new MCP client from a JSON configuration

func (*MCPClient) Close

func (c *MCPClient) Close() error

Close closes the MCP client connection

func (*MCPClient) ListTools

func (c *MCPClient) ListTools() ([]Tool, error)

ListTools returns all tools available from the MCP server

type MCPConfig

type MCPConfig struct {
	// Local/stdio transport fields
	Command string            `json:"command,omitempty"`
	Args    []string          `json:"args,omitempty"`
	Env     map[string]string `json:"env,omitempty"`

	// Remote transport fields
	URL       string            `json:"url,omitempty"`       // Remote server URL
	Transport string            `json:"transport,omitempty"` // "stdio" | "sse" | "streamable"
	Headers   map[string]string `json:"headers,omitempty"`   // Auth headers, API keys
	Timeout   string            `json:"timeout,omitempty"`   // Connection timeout (e.g., "30s")
}

MCPConfig represents the JSON configuration for an MCP server

type MCPServersConfig

type MCPServersConfig struct {
	MCPServers map[string]MCPConfig `json:"mcpServers"`
}

MCPServersConfig represents the Claude Desktop format with multiple servers

type MCPTool

type MCPTool struct {
	Source string // Server spec that provided this tool
	// contains filtered or unexported fields
}

MCPTool wraps an MCP tool to implement the Tool interface

func NewMCPTool

func NewMCPTool(session *mcp.ClientSession, tool *mcp.Tool) *MCPTool

NewMCPTool creates a new MCP tool wrapper

func (*MCPTool) Execute

func (m *MCPTool) Execute(ctx context.Context, args map[string]any) (string, error)

Execute runs the MCP tool with the given arguments

func (*MCPTool) GetName

func (m *MCPTool) GetName() string

GetName returns the name of the tool

func (*MCPTool) GetSchema

func (m *MCPTool) GetSchema() *jsonschema.Schema

GetSchema returns the tool's schema (cached after first call)

func (*MCPTool) GetSource

func (m *MCPTool) GetSource() string

GetSource returns the server spec that provided this tool

func (*MCPTool) GetType

func (m *MCPTool) GetType() string

GetType returns "mcp" for MCP tools

type NamespacedTool

type NamespacedTool struct {
	Tool
	// contains filtered or unexported fields
}

NamespacedTool wraps a tool to provide a namespaced schema

func (*NamespacedTool) GetName

func (n *NamespacedTool) GetName() string

GetName returns the namespaced name

func (*NamespacedTool) GetSchema

func (n *NamespacedTool) GetSchema() *jsonschema.Schema

GetSchema returns a schema with the namespaced title

type ServerResult

type ServerResult struct {
	Name      string   // Server/tool name (e.g., "git", "filesystem", "datetime")
	ToolNames []string // Fully namespaced tool names loaded
}

ServerResult contains information about tools loaded from a single source

type ShellTool

type ShellTool struct {
	Command string
	// contains filtered or unexported fields
}

ShellTool wraps external commands/scripts as tools

func NewShellTool

func NewShellTool(command string) (*ShellTool, error)

NewShellTool creates a new shell tool from a command

func (*ShellTool) Execute

func (s *ShellTool) Execute(ctx context.Context, args map[string]any) (string, error)

Execute runs the tool with the given arguments

func (*ShellTool) GetName

func (s *ShellTool) GetName() string

GetName returns the name of the tool

func (*ShellTool) GetSchema

func (s *ShellTool) GetSchema() *jsonschema.Schema

GetSchema returns the tool's schema with namespaced title

func (*ShellTool) GetSource

func (s *ShellTool) GetSource() string

GetSource returns the command/script path

func (*ShellTool) GetType

func (s *ShellTool) GetType() string

GetType returns "shell" for shell tools

type Tool

type Tool interface {
	// Execution methods
	GetSchema() *jsonschema.Schema
	Execute(ctx context.Context, args map[string]any) (string, error)

	// Metadata methods
	GetName() string   // Returns the namespaced name (e.g., "script__toolname")
	GetType() string   // Returns the tool type: "shell", "mcp", or "native"
	GetSource() string // Returns the source path/spec (e.g., "/path/to/script.sh")
}

Tool is the generic interface for all tools

func LoadShellTools

func LoadShellTools(paths []string) ([]Tool, error)

LoadShellTools loads shell tools from the given file paths

type ToolCall

type ToolCall struct {
	ID   string         // Provider-specific ID (if any)
	Name string         // Tool name
	Args map[string]any // Parsed arguments
}

ToolCall represents a request to execute a tool

type ToolLoaderInfo

type ToolLoaderInfo struct {
	Name   string `json:"name"`   // Full namespaced tool name
	Type   string `json:"type"`   // "shell", "mcp", or "native"
	Source string `json:"source"` // Path for shell, server spec for MCP, "builtin" for native
}

ToolLoaderInfo stores information needed to reload a specific tool

type ToolRegistry

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

ToolRegistry manages available tools

func NewToolRegistry

func NewToolRegistry(tools []Tool) *ToolRegistry

NewToolRegistry creates a new tool registry from a list of tools

func (*ToolRegistry) All

func (r *ToolRegistry) All() []Tool

All returns all tools in the registry

func (*ToolRegistry) Close

func (r *ToolRegistry) Close() error

Close cleans up all resources

func (*ToolRegistry) Get

func (r *ToolRegistry) Get(name string) (Tool, bool)

Get retrieves a tool by name

func (*ToolRegistry) GetActiveToolLoaders

func (r *ToolRegistry) GetActiveToolLoaders() []ToolLoaderInfo

GetActiveToolLoaders returns loader information for all tools This returns one entry per tool to allow selective loading

func (*ToolRegistry) GetLoadedMCPServers

func (r *ToolRegistry) GetLoadedMCPServers() []string

GetLoadedMCPServers returns list of loaded server specs

func (*ToolRegistry) GetSchemas

func (r *ToolRegistry) GetSchemas() []*jsonschema.Schema

GetSchemas returns all tool schemas

func (*ToolRegistry) LoadMCPServer

func (r *ToolRegistry) LoadMCPServer(serverSpec string) (LoadResult, error)

LoadMCPServer connects to an MCP server and registers its tools with namespace For multi-server configs (mcpServers format), loads ALL servers

func (*ToolRegistry) LoadMCPServerWithFilter

func (r *ToolRegistry) LoadMCPServerWithFilter(serverSpec string, allowedTools []string) error

LoadMCPServerWithFilter connects to an MCP server and only registers specified tools serverSpec format: "path/to/config.json#servername"

func (*ToolRegistry) LoadMCPServers

func (r *ToolRegistry) LoadMCPServers(serverSpecs []string) error

LoadMCPServers batch loads multiple servers

func (*ToolRegistry) LoadShellTool

func (r *ToolRegistry) LoadShellTool(path string) (LoadResult, error)

LoadShellTool loads a single shell tool from a file path with namespace

func (*ToolRegistry) LoadToolAuto

func (r *ToolRegistry) LoadToolAuto(pathOrServer string) (LoadResult, error)

LoadToolAuto attempts to load a tool, auto-detecting if it's native, shell tool, or MCP server

func (*ToolRegistry) Register

func (r *ToolRegistry) Register(tool Tool)

Register adds a tool to the registry

func (*ToolRegistry) RegisterNative

func (r *ToolRegistry) RegisterNative(name string, factory func() Tool)

RegisterNative registers a native tool factory

func (*ToolRegistry) Remove

func (r *ToolRegistry) Remove(namespacedName string)

Remove removes a tool by namespaced name from the registry

func (*ToolRegistry) UnloadMCPServer

func (r *ToolRegistry) UnloadMCPServer(serverSpec string) error

UnloadMCPServer removes all tools from a server and closes it

type UpperCaseTool

type UpperCaseTool struct{}

UpperCaseTool is a simple example of a native Go tool

func (*UpperCaseTool) Execute

func (t *UpperCaseTool) Execute(ctx context.Context, args map[string]any) (string, error)

func (*UpperCaseTool) GetName

func (t *UpperCaseTool) GetName() string

GetName returns the tool name

func (*UpperCaseTool) GetSchema

func (t *UpperCaseTool) GetSchema() *jsonschema.Schema

func (*UpperCaseTool) GetSource

func (t *UpperCaseTool) GetSource() string

GetSource returns "builtin" for native tools

func (*UpperCaseTool) GetType

func (t *UpperCaseTool) GetType() string

GetType returns "native" for built-in tools

type WordCountTool

type WordCountTool struct{}

WordCountTool counts words in text

func (*WordCountTool) Execute

func (t *WordCountTool) Execute(ctx context.Context, args map[string]any) (string, error)

func (*WordCountTool) GetName

func (t *WordCountTool) GetName() string

GetName returns the tool name

func (*WordCountTool) GetSchema

func (t *WordCountTool) GetSchema() *jsonschema.Schema

func (*WordCountTool) GetSource

func (t *WordCountTool) GetSource() string

GetSource returns "builtin" for native tools

func (*WordCountTool) GetType

func (t *WordCountTool) GetType() string

GetType returns "native" for built-in tools

Jump to

Keyboard shortcuts

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