command

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommandExecutionTool = mcp.NewTool("executeCommand",
	mcp.WithDescription(`Execute a command on the user's system. Concatenations with binary operators like "&&" or "||" are not supported.`),
	mcp.WithString("command",
		mcp.Required(),
		mcp.Description("The shell-like command to execute."),
	),
	mcp.WithString("working_directory",
		mcp.Description("The working directory for the command."),
	),
	mcp.WithObject("environment",
		mcp.Description("Additional environment variables to pass to the command."),
		mcp.AdditionalProperties(map[string]any{"additionalProperties": true}),
	),
	mcp.WithBoolean("out",
		mcp.Description("Whether to disable standard output. Defaults to false."),
		mcp.DefaultBool(false),
	),
	mcp.WithBoolean("err",
		mcp.Description("Whether to disable standard error. Defaults to false."),
		mcp.DefaultBool(false),
	),
	mcp.WithNumber("first",
		mcp.Description("The number of bytes to read from the beginning of the output. -1 means no limit."),
		mcp.DefaultNumber(float64(defaultCommandExecutionArguments.FirstBytes)),
	),
	mcp.WithNumber("last",
		mcp.Description("The number of bytes to read from the end of the output. -1 means no limit."),
		mcp.DefaultNumber(float64(defaultCommandExecutionArguments.LastBytes)),
	),
)
View Source
var CommandExecutionToolHandler = func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
	pArgs := defaultCommandExecutionArguments

	r, w := io.Pipe()
	go func() {
		defer w.Close()

		json.NewEncoder(w).Encode(request.Params.Arguments)
	}()

	err := json.NewDecoder(r).Decode(&pArgs)
	if err != nil {
		return nil, fmt.Errorf("error parsing arguments: %w", err)
	}

	if pArgs.Command == "" {
		return nil, fmt.Errorf("missing parameter: 'command'")
	}

	cmdDesc := CommandDescriptor{
		CommandLine:           pArgs.Command,
		AdditionalEnvironment: pArgs.Environment,
		WorkingDirectory:      pArgs.WorkingDirectory,
		Output: &OutputSettings{
			DisableStdOut: pArgs.DisableOut,
			DisableStdErr: pArgs.DisableErr,
			FirstNBytes:   pArgs.FirstBytes,
			LastNBytes:    pArgs.LastBytes,
		},
	}

	raw, err := cmdDesc.Run(ctx)
	return mcp.NewToolResultText(string(raw)), err
}

Functions

This section is empty.

Types

type CommandDescriptor

type CommandDescriptor struct {
	CommandLine           string            `json:"command,omitempty"`
	Name                  string            `json:"name,omitempty"`
	Arguments             []string          `json:"arguments,omitempty"`
	Environment           map[string]string `json:"env"`
	AdditionalEnvironment map[string]string `json:"additionalEnv"`
	WorkingDirectory      string            `json:"workingDir"`
	Output                *OutputSettings   `json:"output,omitempty"`
}

func (CommandDescriptor) Run

func (c CommandDescriptor) Run(ctx context.Context) ([]byte, error)

type CommandExecutionArguments

type CommandExecutionArguments struct {
	Command          string            `json:"command"`
	WorkingDirectory string            `json:"working_directory"`
	Environment      map[string]string `json:"environment"`

	DisableOut bool `json:"out"`
	DisableErr bool `json:"err"`
	FirstBytes int  `json:"first"`
	LastBytes  int  `json:"last"`
}

type OutputSettings added in v0.26.0

type OutputSettings struct {
	DisableStdOut bool `json:"disableStdOut"`
	DisableStdErr bool `json:"disableStdErr"`
	FirstNBytes   int  `json:"firstNBytes"`
	LastNBytes    int  `json:"lastNBytes"`
}

Jump to

Keyboard shortcuts

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