Documentation
¶
Overview ¶
Package toolgen provides automatic tool generation from Cobra commands for AI interfaces. It extracts command metadata and generates SDK-agnostic tool definitions that can be adapted for different AI SDKs (Copilot, MCP, etc.).
Index ¶
- Constants
- Variables
- func BuildCommandArgs(tool ToolDefinition, params map[string]any) ([]string, error)
- func FormatToolName(commandPath string) string
- func ToCopilotTools(tools []ToolDefinition, opts ToolOptions) []copilot.Tool
- func ToMCPTools(server *mcp.Server, tools []ToolDefinition, opts ToolOptions)
- func ToolParametersFromJSON(jsonParams string) (map[string]any, error)
- type FlagDef
- type OutputChunk
- type Parameter
- type SubcommandDef
- type ToolDefinition
- type ToolOptions
Constants ¶
const ( AnnotationExclude = annotations.AnnotationExclude AnnotationDescription = annotations.AnnotationDescription AnnotationPermission = annotations.AnnotationPermission AnnotationConsolidate = annotations.AnnotationConsolidate )
Re-export annotation constants from pkg/cli/annotations for backward compatibility. These constants are now defined in the annotations package to allow CLI commands to use them without depending on the toolgen package.
IMPORTANT: The annotations package must never import toolgen to avoid circular dependencies.
Variables ¶
var ( // ErrMissingSubcommandParam indicates the subcommand parameter is missing or invalid. ErrMissingSubcommandParam = errors.New("missing or invalid subcommand parameter") // ErrInvalidSubcommand indicates an unknown subcommand was provided. ErrInvalidSubcommand = errors.New("invalid subcommand") // ErrArgsNotArray indicates the args parameter is not an array. ErrArgsNotArray = errors.New("args parameter must be an array") )
Sentinel errors for tool execution.
Functions ¶
func BuildCommandArgs ¶
func BuildCommandArgs(tool ToolDefinition, params map[string]any) ([]string, error)
BuildCommandArgs constructs command-line arguments from parameters.
func FormatToolName ¶
FormatToolName formats a tool name from a command path. Example: "ksail cluster create" -> "cluster_create".
func ToCopilotTools ¶
func ToCopilotTools(tools []ToolDefinition, opts ToolOptions) []copilot.Tool
ToCopilotTools converts tool definitions to Copilot SDK tools.
func ToMCPTools ¶
func ToMCPTools(server *mcp.Server, tools []ToolDefinition, opts ToolOptions)
ToMCPTools converts tool definitions to MCP server tools. Each tool definition is registered with the MCP server. Note: mcp.AddTool does not return errors; registration failures will panic.
Types ¶
type FlagDef ¶
type FlagDef struct {
// Name is the flag name.
Name string
// Type is the JSON schema type.
Type string
// ItemsType is the JSON schema type for array items (e.g., "string", "integer").
// Only populated when Type is "array".
ItemsType string
// Description describes the flag.
Description string
// Required indicates if this flag is required.
Required bool
// Default is the default value.
Default any
// AppliesToSubcommands lists which subcommands this flag applies to.
// Empty means it applies to all subcommands.
AppliesToSubcommands []string
}
FlagDef contains metadata about a flag in a consolidated tool.
type OutputChunk ¶
type OutputChunk struct {
ToolID string // Identifier for the tool that produced this output (for correlation)
Source string // "stdout" or "stderr"
Chunk string // The actual output content
}
OutputChunk represents a chunk of output from a running command.
type Parameter ¶
type Parameter struct {
Name string
Type string // JSON schema type: "string", "integer", "boolean", "array", "object"
Description string
Required bool
Default any
Enum []string // Valid values for enum types
Items *Parameter // For array types
}
Parameter represents a single tool parameter extracted from a Cobra flag.
type SubcommandDef ¶
type SubcommandDef struct {
// Name is the subcommand name (e.g., "deployment", "restart", "encrypt").
Name string
// Description describes what this subcommand does.
Description string
// CommandParts are the full command parts for execution.
CommandParts []string
// Flags contains metadata about flags specific to or modified by this subcommand.
Flags map[string]*FlagDef
}
SubcommandDef contains metadata about a subcommand in a consolidated tool.
type ToolDefinition ¶
type ToolDefinition struct {
// Name is the tool identifier (e.g., "ksail_cluster_create").
Name string
// Description provides context for the AI about what the tool does.
Description string
// Parameters is a map of parameter names to their JSON schema properties.
// Format: map[paramName]map[string]any where inner map has "type", "description", etc.
Parameters map[string]any
// CommandPath is the full command path (e.g., "ksail cluster create").
CommandPath string
// CommandParts contains the command split into parts (e.g., ["ksail", "cluster", "create"]).
CommandParts []string
// RequiresPermission indicates if this tool performs edit operations.
RequiresPermission bool
// IsConsolidated indicates if this tool represents multiple subcommands.
IsConsolidated bool
// SubcommandParam is the name of the parameter that selects the subcommand.
// Only set when IsConsolidated is true (e.g., "resource_type", "action", "operation").
SubcommandParam string
// Subcommands maps subcommand names to their metadata.
// Only populated when IsConsolidated is true.
Subcommands map[string]*SubcommandDef
}
ToolDefinition is an SDK-agnostic representation of a tool generated from a Cobra command. It contains all the metadata needed to create SDK-specific tools (Copilot, MCP, etc.).
func GenerateTools ¶
func GenerateTools(root *cobra.Command, opts ToolOptions) []ToolDefinition
GenerateTools traverses a Cobra command tree and generates tool definitions. It returns SDK-agnostic tool definitions for all runnable leaf commands.
type ToolOptions ¶
type ToolOptions struct {
// ExcludeCommands is a list of command paths to exclude (e.g., "ksail chat").
ExcludeCommands []string
// IncludeHidden includes hidden commands in tool generation.
IncludeHidden bool
// CommandTimeout is the timeout for command execution.
CommandTimeout time.Duration
// WorkingDirectory is the directory to run commands in.
WorkingDirectory string
// OutputChan receives real-time output chunks from running commands.
// If nil, output is only available after command completion.
OutputChan chan<- OutputChunk
// Logger is used for debug logging during command execution.
// If nil, no logging is performed.
Logger *slog.Logger
}
ToolOptions configures tool generation behavior.
func DefaultOptions ¶
func DefaultOptions() ToolOptions
DefaultOptions returns sensible default options for tool generation. With permission-based consolidation, all workload and cluster commands are grouped into 6 tools: cluster_read, cluster_write, workload_read, workload_write, cipher_read, and cipher_write.