common

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package common provides shared utilities and types used across the MCPShell.

Package common provides shared utilities and types used across the MCPShell.

Package common provides utility functions and shared components.

Package common provides shared utilities and types used across the MCPShell.

Index

Constants

View Source
const DownloadTimeout = 10 * time.Second

DownloadTimeout is the timeout for downloading content from a URL

Variables

View Source
var SupportedContentTypes = []string{
	"text/",
	"application/json",
	"application/xml",
	"application/yaml",
	"application/x-yaml",
	"application/javascript",
	"application/ecmascript",
	"application/markdown",
	"application/x-markdown",
}

List of supported text content types

Functions

func CheckExecutableExists

func CheckExecutableExists(executableName string) bool

CheckExecutableExists checks if a command is available in the system PATH.

Parameters:

  • executableName: The name of the executable to check

Returns:

  • true if the executable exists and is accessible, false otherwise

func CheckOSMatches

func CheckOSMatches(requiredOS string) bool

CheckOSMatches checks if the current operating system matches the required OS.

Parameters:

  • requiredOS: The required operating system (e.g., "darwin", "linux", "windows") Can be empty to skip OS check.

Returns:

  • true if the current OS matches the required OS or if requiredOS is empty, false otherwise

func ConvertStringToType added in v0.0.10

func ConvertStringToType(value string, paramType string) (interface{}, error)

ConvertStringToType converts a string value to the appropriate type based on the parameter type. This is used when parsing command line arguments for direct tool execution.

Parameters:

  • value: The string value to convert
  • paramType: The parameter type ("string", "number", "integer", "boolean")

Returns:

  • The converted value
  • An error if the conversion fails

func FetchURLText added in v0.1.5

func FetchURLText(url string) ([]byte, error)

fetchURLContent downloads content from a URL and verifies it's a text format Returns the content as a byte slice if successful, or an error if download fails or if the content is not a supported text format.

func ProcessTemplate

func ProcessTemplate(text string, args map[string]interface{}) (string, error)

ProcessTemplate processes a template with the given arguments. It uses Go's template engine to substitute variables in the template.

Parameters:

  • text: The template to process
  • args: Map of variable names to their values

Returns:

  • The processed template string with substituted variables
  • An error if template processing fails

func ProcessTemplateListFlexible

func ProcessTemplateListFlexible(list []string, args map[string]interface{}) []string

func RecoverPanic

func RecoverPanic() bool

RecoverPanic recovers from a panic and logs it to the provided logger. It returns true if a panic was recovered, false otherwise.

This function should be used in deferred calls to catch panics.

func SetLogger

func SetLogger(logger *Logger)

SetLogger sets the global application logger

Types

type CompiledConstraints

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

CompiledConstraints holds the compiled CEL programs for a tool's constraints

func NewCompiledConstraints

func NewCompiledConstraints(constraints []string, paramTypes map[string]ParamConfig, logger *log.Logger) (*CompiledConstraints, error)

NewCompiledConstraints compiles a list of CEL constraint expressions paramTypes is a map of parameter names to their types logger is required for logging constraint compilation and evaluation information

func (*CompiledConstraints) Evaluate

func (cc *CompiledConstraints) Evaluate(args map[string]interface{}, params map[string]ParamConfig) (bool, []string, error)

Evaluate evaluates all compiled constraints against the provided arguments and returns details about which constraints failed.

Parameters:

  • args: Map of argument names to their values
  • paramTypes: Map of parameter names to their type configurations

Returns:

  • true if all constraints pass, false otherwise
  • slice of strings containing the failed constraint expressions
  • error if evaluation fails or if a required parameter is missing

type LogLevel

type LogLevel int

LogLevel represents logging verbosity levels

const (
	// LogLevelNone disables logging
	LogLevelNone LogLevel = iota
	// LogLevelError logs only errors
	LogLevelError
	// LogLevelInfo logs information and errors
	LogLevelInfo
	// LogLevelDebug logs detailed debug information
	LogLevelDebug
)

func LogLevelFromString

func LogLevelFromString(level string) LogLevel

LogLevelFromString converts a string representation to a LogLevel

type Logger

type Logger struct {
	// The underlying Go logger
	*log.Logger
	// contains filtered or unexported fields
}

Logger provides a structured logging interface for the application

func GetLogger

func GetLogger() *Logger

GetLogger returns the global application logger. If the logger hasn't been initialized yet, it returns a default stderr logger.

func NewLogger

func NewLogger(prefix string, filePath string, level LogLevel, truncate bool) (*Logger, error)

NewLogger creates a new Logger instance

Parameters:

  • prefix: The prefix for all log messages
  • filePath: Path to the log file (empty string disables file logging)
  • level: The logging verbosity level
  • truncate: If true, truncate the log file; if false, append to it

Returns:

  • A new Logger instance
  • An error if the log file cannot be opened

func (*Logger) Close

func (l *Logger) Close() error

Close closes the log file if it's open

func (*Logger) Debug

func (l *Logger) Debug(format string, v ...interface{})

Debug logs a message at debug level

func (*Logger) Error

func (l *Logger) Error(format string, v ...interface{})

Error logs a message at error level

func (*Logger) FilePath

func (l *Logger) FilePath() string

FilePath returns the current log file path

func (*Logger) Info

func (l *Logger) Info(format string, v ...interface{})

Info logs a message at info level

func (*Logger) Level

func (l *Logger) Level() LogLevel

Level returns the current log level

func (*Logger) SetLevel

func (l *Logger) SetLevel(level LogLevel)

SetLevel changes the current log level

type LoggingConfig

type LoggingConfig struct {
	// File is the path to the log file
	File string

	// Level sets the logging verbosity (e.g., "info", "debug", "error")
	Level string `yaml:"level,omitempty"`
}

LoggingConfig defines configuration options for application logging.

type OutputConfig

type OutputConfig struct {
	// Prefix is a template string that gets prepended to the command output.
	// It can use the same template variables as the command itself.
	Prefix string `yaml:"prefix,omitempty"`
}

OutputConfig defines how tool output should be formatted before being returned.

type ParamConfig

type ParamConfig struct {
	// Type specifies the parameter data type. Valid values: "string" (default), "number"/"integer", "boolean"
	Type string `yaml:"type,omitempty"`

	// Description provides information about the parameter's purpose
	Description string `yaml:"description"`

	// Required indicates whether the parameter must be provided
	Required bool `yaml:"required,omitempty"`

	// Default specifies a default value to use when the parameter is not provided
	Default interface{} `yaml:"default,omitempty"`
}

ParamConfig defines the configuration for a single parameter in a tool.

type PromptsConfig added in v0.1.6

type PromptsConfig struct {
	System []string `yaml:"system,omitempty"` // System prompts
	User   []string `yaml:"user,omitempty"`   // User prompts
}

PromptsConfig holds prompt configuration with system and user prompts

func (PromptsConfig) GetSystemPrompts added in v0.1.6

func (p PromptsConfig) GetSystemPrompts() string

GetSystemPrompts returns all system prompts joined with newlines

func (PromptsConfig) GetUserPrompts added in v0.1.6

func (p PromptsConfig) GetUserPrompts() string

GetUserPrompts returns all user prompts joined with newlines

func (PromptsConfig) HasSystemPrompts added in v0.1.6

func (p PromptsConfig) HasSystemPrompts() bool

HasSystemPrompts returns true if there are any system prompts configured

func (PromptsConfig) HasUserPrompts added in v0.1.6

func (p PromptsConfig) HasUserPrompts() bool

HasUserPrompts returns true if there are any user prompts configured

Jump to

Keyboard shortcuts

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