command

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package command provides functions for creating and executing command handlers.

This package defines the core functionality for translating MCP tool calls into shell command executions, providing the bridge between the MCP protocol and the underlying operating system commands.

Package command provides functions for creating and executing command handlers.

Package command provides functions for creating and executing command handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandHandler

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

CommandHandler encapsulates the configuration and behavior needed to handle tool commands.

func NewCommandHandler

func NewCommandHandler(tool config.Tool, params map[string]common.ParamConfig, shell string, logger *common.Logger) (*CommandHandler, error)

NewCommandHandler creates a new CommandHandler instance.

Parameters:

  • tool: The tool definition containing command, constraints, and output configuration
  • params: Map of parameter names to their type configurations
  • shell: The shell to use for command execution
  • logger: Logger for detailed execution information (required)

Returns:

  • A new CommandHandler instance and nil if successful
  • nil and an error if constraint compilation fails or if a required parameter is missing

func (*CommandHandler) ExecuteCommand added in v0.0.10

func (h *CommandHandler) ExecuteCommand(params map[string]interface{}) (string, error)

ExecuteCommand handles the direct execution of a command without going through the MCP server. This is used by the "exe" command to execute a tool directly from the command line.

Parameters:

  • params: Map of parameter names to their values

Returns:

  • The command output as a string
  • An error if command execution fails

func (*CommandHandler) GetMCPHandler

func (h *CommandHandler) GetMCPHandler() func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error)

GetMCPHandler returns a function that handles MCP tool calls by executing shell commands.

This is the function that should be registered with the MCP server.

Returns:

  • A function that handles MCP tool calls

type DockerRunner added in v0.1.0

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

DockerRunner executes commands inside a Docker container.

func NewDockerRunner added in v0.1.0

func NewDockerRunner(options RunnerOptions, logger *common.Logger) (*DockerRunner, error)

NewDockerRunner creates a new Docker runner with the specified options.

func (*DockerRunner) CheckImplicitRequirements added in v0.1.4

func (r *DockerRunner) CheckImplicitRequirements() error

CheckImplicitRequirements checks if the runner meets its implicit requirements Docker runner requires the docker executable and a running daemon

func (*DockerRunner) Run added in v0.1.0

func (r *DockerRunner) Run(ctx context.Context, shell string, cmd string, env []string, params map[string]interface{}, tmpfile bool) (string, error)

Run executes the command using Docker.

type DockerRunnerOptions added in v0.1.0

type DockerRunnerOptions struct {
	// The Docker image to use (required)
	Image string `json:"image"`

	// Additional Docker run options
	DockerRunOpts string `json:"docker_run_opts"`

	// Mount points in the format "hostpath:containerpath"
	Mounts []string `json:"mounts"`

	// Whether to allow networking in the container
	AllowNetworking bool `json:"allow_networking"`

	// Specific network to connect container to (e.g. "host", "bridge", or custom network name)
	Network string `json:"network"`

	// User to run as inside the container (defaults to current user)
	User string `json:"user"`

	// Working directory inside the container
	WorkDir string `json:"workdir"`

	// PrepareCommand is a command to run before the main command
	PrepareCommand string `json:"prepare_command"`

	// Memory limit (e.g. "512m", "1g")
	Memory string `json:"memory"`

	// Memory soft limit (e.g. "256m", "512m")
	MemoryReservation string `json:"memory_reservation"`

	// Swap limit equal to memory plus swap: '-1' to enable unlimited swap
	MemorySwap string `json:"memory_swap"`

	// Tune container memory swappiness (0 to 100)
	MemorySwappiness int `json:"memory_swappiness"`

	// Linux capabilities to add to the container
	CapAdd []string `json:"cap_add"`

	// Linux capabilities to drop from the container
	CapDrop []string `json:"cap_drop"`

	// Custom DNS servers for the container
	DNS []string `json:"dns"`

	// Custom DNS search domains for the container
	DNSSearch []string `json:"dns_search"`

	// Set platform if server is multi-platform capable (e.g., "linux/amd64", "linux/arm64")
	Platform string `json:"platform"`
}

DockerRunnerOptions represents configuration options for the Docker runner.

func NewDockerRunnerOptions added in v0.1.5

func NewDockerRunnerOptions(genericOpts RunnerOptions) (DockerRunnerOptions, error)

NewDockerRunnerOptions extracts Docker-specific options from generic runner options.

func (*DockerRunnerOptions) GetBaseDockerCommand added in v0.1.5

func (o *DockerRunnerOptions) GetBaseDockerCommand(env []string) []string

GetBaseDockerCommand creates the common parts of a docker run command with all configured options. It returns a slice of command parts that can be further customized by the calling method.

func (*DockerRunnerOptions) GetDirectExecutionCommand added in v0.1.5

func (o *DockerRunnerOptions) GetDirectExecutionCommand(cmd string, env []string) string

GetDirectExecutionCommand constructs the docker run command for direct executable execution. This is used to optimize the case where we're just running a single executable without a temp script.

func (*DockerRunnerOptions) GetDockerCommand added in v0.1.5

func (o *DockerRunnerOptions) GetDockerCommand(scriptFile string, env []string) string

GetDockerCommand constructs the docker run command with a script file.

type Runner

type Runner interface {
	Run(ctx context.Context, shell string, command string, env []string, params map[string]interface{}, tmpfile bool) (string, error)
	CheckImplicitRequirements() error
}

Runner is an interface for running commands

func NewRunner

func NewRunner(runnerType RunnerType, options RunnerOptions, logger *common.Logger) (Runner, error)

NewRunner creates a new Runner based on the given type

type RunnerExec

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

RunnerExec implements the Runner interface

func NewRunnerExec

func NewRunnerExec(options RunnerOptions, logger *common.Logger) (*RunnerExec, error)

NewRunnerExec creates a new ExecRunner with the provided logger If logger is nil, a default logger is created

func (*RunnerExec) CheckImplicitRequirements added in v0.1.4

func (r *RunnerExec) CheckImplicitRequirements() error

CheckImplicitRequirements checks if the runner meets its implicit requirements Exec runner has no special requirements

func (*RunnerExec) Run

func (r *RunnerExec) Run(ctx context.Context, shell string,
	command string,
	env []string, params map[string]interface{},
	tmpfile bool,
) (string, error)

Run executes a command with the given shell and returns the output. It implements the Runner interface.

Note: For Windows native shells (cmd, powershell), the 'tmpfile' parameter is ignored and commands are executed directly to avoid issues with output capturing.

type RunnerExecOptions

type RunnerExecOptions struct {
	Shell string `json:"shell"`
}

RunnerExecOptions is the options for the RunnerExec

func NewRunnerExecOptions

func NewRunnerExecOptions(options RunnerOptions) (RunnerExecOptions, error)

NewRunnerExecOptions creates a new RunnerExecOptions from a RunnerOptions

type RunnerFirejail

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

RunnerFirejail implements the Runner interface using firejail on Linux

func NewRunnerFirejail

func NewRunnerFirejail(options RunnerOptions, logger *common.Logger) (*RunnerFirejail, error)

NewRunnerFirejail creates a new RunnerFirejail with the provided logger If logger is nil, a default logger is created

func (*RunnerFirejail) CheckImplicitRequirements added in v0.1.4

func (r *RunnerFirejail) CheckImplicitRequirements() error

CheckImplicitRequirements checks if the runner meets its implicit requirements Firejail runner requires Linux and the firejail executable

func (*RunnerFirejail) Run

func (r *RunnerFirejail) Run(ctx context.Context,
	shell string, command string,
	env []string, params map[string]interface{}, tmpfile bool,
) (string, error)

Run executes a command inside the firejail sandbox and returns the output It implements the Runner interface

note: tmpfile is ignored for firejail because it's not supported

type RunnerFirejailOptions

type RunnerFirejailOptions struct {
	Shell             string   `json:"shell"`
	AllowNetworking   bool     `json:"allow_networking"`
	AllowUserFolders  bool     `json:"allow_user_folders"`
	AllowReadFolders  []string `json:"allow_read_folders"`
	AllowWriteFolders []string `json:"allow_write_folders"`
	AllowReadFiles    []string `json:"allow_read_files"`
	AllowWriteFiles   []string `json:"allow_write_files"`
	CustomProfile     string   `json:"custom_profile"`
}

RunnerFirejailOptions is the options for the RunnerFirejail

func NewRunnerFirejailOptions

func NewRunnerFirejailOptions(options RunnerOptions) (RunnerFirejailOptions, error)

NewRunnerFirejailOptions creates a new RunnerFirejailOptions from a RunnerOptions

type RunnerOptions

type RunnerOptions map[string]interface{}

RunnerOptions is a map of options for the runner

func (RunnerOptions) ToJSON

func (ro RunnerOptions) ToJSON() (string, error)

type RunnerSandboxExec

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

RunnerSandboxExec implements the Runner interface using macOS sandbox-exec

func NewRunnerSandboxExec

func NewRunnerSandboxExec(options RunnerOptions, logger *common.Logger) (*RunnerSandboxExec, error)

NewRunnerSandboxExec creates a new RunnerSandboxExec with the provided logger If logger is nil, a default logger is created

func (*RunnerSandboxExec) CheckImplicitRequirements added in v0.1.4

func (r *RunnerSandboxExec) CheckImplicitRequirements() error

CheckImplicitRequirements checks if the runner meets its implicit requirements SandboxExec runner requires macOS and the sandbox-exec executable

func (*RunnerSandboxExec) Run

func (r *RunnerSandboxExec) Run(ctx context.Context, shell string, command string, env []string, params map[string]interface{}, tmpfile bool) (string, error)

Run executes a command inside the macOS sandbox and returns the output It implements the Runner interface

note: tmpfile is ignored for sandbox because it's not supported

type RunnerSandboxExecOptions

type RunnerSandboxExecOptions struct {
	Shell             string   `json:"shell"`
	AllowNetworking   bool     `json:"allow_networking"`
	AllowUserFolders  bool     `json:"allow_user_folders"`
	AllowReadFolders  []string `json:"allow_read_folders"`
	AllowWriteFolders []string `json:"allow_write_folders"`
	AllowReadFiles    []string `json:"allow_read_files"`
	AllowWriteFiles   []string `json:"allow_write_files"`
	CustomProfile     string   `json:"custom_profile"`
}

RunnerSandboxExecOptions is the options for the RunnerSandboxExec

func NewRunnerSandboxExecOptions

func NewRunnerSandboxExecOptions(options RunnerOptions) (RunnerSandboxExecOptions, error)

NewRunnerSandboxExecOptions creates a new RunnerSandboxExecOptions from a RunnerOptions

type RunnerType

type RunnerType string

RunnerType is an identifier for the type of runner to use. Each runner has its own set of implicit requirements that are checked automatically, so users don't need to explicitly specify common requirements in their tool configurations.

const (
	// RunnerTypeExec is the standard command execution runner with no additional requirements
	RunnerTypeExec RunnerType = "exec"

	// RunnerTypeSandboxExec is the macOS-specific sandbox-exec runner
	// Implicit requirements: OS=darwin, executables=[sandbox-exec]
	RunnerTypeSandboxExec RunnerType = "sandbox-exec"

	// RunnerTypeFirejail is the Linux-specific firejail runner
	// Implicit requirements: OS=linux, executables=[firejail]
	RunnerTypeFirejail RunnerType = "firejail"

	// RunnerTypeDocker is the Docker-based runner
	// Implicit requirements: executables=[docker]
	RunnerTypeDocker RunnerType = "docker"
)

Jump to

Keyboard shortcuts

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