mcpserver

package
v0.6.0 Latest Latest
Warning

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

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

Documentation

Overview

Package mcpserver exposes CLI commands as MCP tools over stdio or Streamable HTTP.

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. With stdio, stdout is the MCP channel, so nothing else may write to it.

Index

Constants

View Source
const Path = "/mcp"

Path is the URL path of the Streamable HTTP endpoint.

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 Handler added in v0.6.0

func Handler(opts Options, token string) http.Handler

Handler returns the HTTP handler of one server for all clients, so that the commands that change the workspace still run one at a time. go-sdk rejects requests to a loopback address with another Host header (DNS rebinding). The handler also rejects browser requests from another origin.

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.

func ServeHTTP added in v0.6.0

func ServeHTTP(ctx context.Context, l net.Listener, opts Options, hopts HTTPOptions) error

ServeHTTP serves the server on l until ctx is done. Then it waits a short time for open requests and closes the listener.

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 HTTPOptions added in v0.6.0

type HTTPOptions struct {
	// Token is the bearer token that every request must carry. Empty means no
	// authentication.
	Token string
	// TLS serves HTTPS when it is not nil.
	TLS *tls.Config
}

HTTPOptions configure the Streamable HTTP transport.

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