mcpserver

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package mcpserver exposes CLI commands as MCP tools over stdio.

A tool runs one CLI command with --output json and --non-interactive, and returns its JSON result. The input schema of a tool comes from the flags of its command, so the tools and the commands cannot drift apart. Command progress (stderr) becomes MCP progress notifications. Stdout is the MCP channel, so nothing else may write to it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Args

func Args(t Tool, raw json.RawMessage) ([]string, error)

Args builds the CLI arguments of one call: the command, its flags, then "--" and the positional arguments, so that a value can never become a flag.

func InputSchema

func InputSchema(t Tool) map[string]any

InputSchema returns the JSON schema of the tool parameters.

func New

func New(opts Options) *mcp.Server

New returns a server with the tools of opts.

func Serve

func Serve(ctx context.Context, opts Options) error

Serve runs the server on stdin and stdout until the client disconnects.

Types

type Arg

type Arg struct {
	Name        string
	Description string
	Required    bool
	Variadic    bool // takes a list
}

Arg is a positional argument of the command.

type Flag

type Flag struct {
	Name    string
	Type    string // pflag type: bool, string, int, int64, duration, stringSlice, intSlice
	Default string
	Usage   string
	Values  []string // allowed values, when fixed
}

Flag is a command flag that the tool accepts as a parameter.

type Kind

type Kind int

Kind tells what a tool may change.

const (
	ReadOnly    Kind = iota // reads only
	Write                   // changes files or services, keeps the data
	Destructive             // can delete data or stop services
)

type Options

type Options struct {
	Name         string
	Version      string
	Instructions string
	Tools        []Tool
	Run          Runner
	// Prefix comes before the command of every call, for example --dir <folder>.
	Prefix []string
	// AllowDestructive registers the destructive tools. Without it they do not exist.
	AllowDestructive bool
	// Log receives the stderr of every command.
	Log io.Writer
	// MaxResultBytes cuts longer results, so that one result cannot fill the
	// context of a small model. 0 means no limit.
	MaxResultBytes int
}

Options configure the server.

type Runner

type Runner func(ctx context.Context, args []string, progress io.Writer) (code int, stdout []byte)

Runner runs the CLI with args. Progress receives stderr. It returns the exit code and stdout.

type Tool

type Tool struct {
	Name        string
	Title       string
	Description string
	Command     []string // for example ["documents", "get"]
	Kind        Kind
	Flags       []Flag
	Args        []Arg
	// Defaults are flag values used when the caller does not give the parameter.
	Defaults map[string]string
	// Extra arguments always passed, for example --yes.
	Extra []string
}

Tool is one CLI command exposed as an MCP tool.

Jump to

Keyboard shortcuts

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