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
- func Args(t Tool, raw json.RawMessage) ([]string, error)
- func Handler(opts Options, token string) http.Handler
- func InputSchema(t Tool) map[string]any
- func New(opts Options) *mcp.Server
- func Serve(ctx context.Context, opts Options) error
- func ServeHTTP(ctx context.Context, l net.Listener, opts Options, hopts HTTPOptions) error
- type Arg
- type Flag
- type HTTPOptions
- type Kind
- type Options
- type Runner
- type Tool
Constants ¶
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
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 ¶
InputSchema returns the JSON schema of the tool parameters.
Types ¶
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 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 ¶
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.