Help us shape the future of Go! Take the 2026 Go Developer Survey

mcpgen

package
v1.4.15 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package mcpgen generates the MCP surface from the machine registry (contract:cli.mcp-tools v2, EPIC P7-SURF D5).

Purpose: one MCP tool per exposed document command of the registry (core, subcommand and installed plugin commands), so the tool list is data, not code. The input schema is the machine request schema, the output schema is the command's committed data schema, annotations come from the side effect, and every call runs through the invoker and its gate.

Inputs: a *cmdregistry.Registry, an Invoker, a schema lookup, the compat mode.

Outputs: server.ServerTool values (Tools), resources (Resources) and server options (ServerOptions).

Constraints: v1.4 mode keeps the 26 legacy tools primary and adds generated tools only under names no legacy tool uses; v1.5 mode registers generated tools only, where the 13 legacy names equal to a generated name become that one tool with a dual-shape input and the other 13 are deprecated aliases (legacy_aliases.yaml). No name is ever registered twice. This package imports invoke, cmdregistry, canon, errs, output and mcp-go, never internal/plugin.

Layer: L2.

Index

Constants

View Source
const (
	KindMerged  = "merged"
	KindAlias   = "alias"
	KindRemoved = "removed"
)

Row kinds.

View Source
const (
	URIRegistry = "nself://registry"
	URIErrors   = "nself://errors"
	URICatalog  = "nself://plugins/catalog"
	URISchemas  = "nself://schemas/"
)

URIs of the resources this package serves.

View Source
const (
	ServerName    = "nSelf MCP Server"
	ServerVersion = "2.0.0"
)

ServerName and ServerVersion identify the server in the initialize result.

View Source
const DeprecatedPrefix = "Deprecated: "

DeprecatedPrefix starts every alias description and every legacy property.

Variables

This section is empty.

Functions

func ErrorEnvelope

func ErrorEnvelope(command string, err error) []byte

ErrorEnvelope renders err as a v1 error envelope for command.

func ErrorResult

func ErrorResult(command string, err error, deps []output.Deprecation) *mcp.CallToolResult

ErrorResult is the tool result for an error raised before or instead of a run.

func PluginPathOK

func PluginPathOK(path string) bool

PluginPathOK reports whether every word of a plugin command path (without the leading "nself ") is a valid plugin command name.

func Resources

Resources builds the resources and templates. The legacy URIs are included only in v1.5 mode: in v1.4 mode the hand handlers keep serving them.

func Result

func Result(stdout []byte, deps []output.Deprecation) *mcp.CallToolResult

Result maps envelope bytes to a tool result, adding deps to meta.deprecations.

func ServerOptions

func ServerOptions() []server.ServerOption

ServerOptions are the extra options for server.NewMCPServer.

func Tools

func Tools(o Options) ([]server.ServerTool, error)

Tools builds the tools for the mode, sorted by name.

func ValidToolName

func ValidToolName(name string) bool

ValidToolName reports whether name may be a tool name.

Types

type FlagMap

type FlagMap struct {
	Flag  string `yaml:"flag"`
	Param string `yaml:"param"`
	As    string `yaml:"as"` // "" | seconds
}

FlagMap maps a legacy param onto a flag of the target command.

type Invoker

type Invoker func(ctx context.Context, path string, req invoke.Request) (invoke.Result, error)

Invoker runs one registry command; cmd/commands wires invoke.Invoke with the project directory, the confirmation Guard and the audit surface.

type Options

type Options struct {
	Registry *cmdregistry.Registry
	Invoke   Invoker
	// Schema returns a committed schema by its path relative to schemas/.
	Schema func(name string) ([]byte, bool)
	// V15 selects v1.5 mode: generated tools and aliases only.
	V15 bool
	// Warn receives one line for each plugin command skipped because its tool
	// name is invalid or taken; core conflicts fail Tools instead.
	Warn func(string)
}

Options configures Tools.

type Param

type Param struct {
	Name        string `yaml:"name"`
	Type        string `yaml:"type"` // string | integer | number | boolean
	Description string `yaml:"description"`
	Default     any    `yaml:"default"`
	// AllowEmpty keeps an empty string as a value (config set may clear a key);
	// otherwise an empty string counts as not given, as the hand tools did.
	AllowEmpty bool `yaml:"allow_empty"`
	// Positive makes a value of zero or less fall back to Default, as the hand
	// tools did (lines <= 0 meant 50).
	Positive bool `yaml:"positive"`
}

Param is one legacy property of a tool.

type Removed

type Removed struct {
	Code    string   `yaml:"code"`
	Message string   `yaml:"message"`
	Use     []string `yaml:"use"`
}

Removed names the refusal a removed tool returns.

type ResourceOptions

type ResourceOptions struct {
	Registry *cmdregistry.Registry
	Invoke   Invoker
	Schema   func(name string) ([]byte, bool)
	// Catalog returns the cached plugin catalogue document, or an error when
	// there is none; it must not touch the network.
	Catalog func() ([]byte, error)
	// Fallback serves a legacy URI whose command is not exposed on machine
	// surfaces yet (for example one with no envelope), so the URI keeps working
	// until the command is converted. Keyed by URI; optional.
	Fallback map[string]server.ResourceHandlerFunc
	V15      bool
}

ResourceOptions configures Resources.

type Row

type Row struct {
	Name        string    `yaml:"name"`
	Kind        string    `yaml:"kind"`
	Target      string    `yaml:"target"`
	Plugin      string    `yaml:"plugin"`
	SideEffect  string    `yaml:"side_effect"`
	Summary     string    `yaml:"summary"`
	Params      []Param   `yaml:"params"`
	Required    []string  `yaml:"required"`
	Args        []string  `yaml:"args"`
	Flags       []FlagMap `yaml:"flags"`
	Unsupported []string  `yaml:"unsupported"`
	Removed     *Removed  `yaml:"removed"`
	// Shortcut marks a row that is not a legacy name: a short, current alias
	// of a generated tool (nself_capabilities). It is registered in both modes,
	// is not deprecated and adds no deprecation note.
	Shortcut bool `yaml:"shortcut"`
}

Row is one legacy tool name.

func (*Row) CheckNoMix

func (r *Row) CheckNoMix(members map[string]json.RawMessage) error

CheckNoMix refuses a call that carries both machine members and legacy properties.

func (*Row) IsLegacyShape

func (r *Row) IsLegacyShape(members map[string]json.RawMessage) bool

IsLegacyShape reports whether the call carries at least one legacy-only property.

func (*Row) ParamNames

func (r *Row) ParamNames() []string

ParamNames lists the legacy property names of the row.

func (*Row) Request

func (r *Row) Request(members map[string]json.RawMessage, allowConfirm bool) (invoke.Request, error)

Request maps members onto a machine request. allowConfirm admits the shape-neutral confirm member (the target has a confirm block).

type Table

type Table struct {
	RemovalAt string
	Rows      []Row
	// contains filtered or unexported fields
}

Table is the loaded legacy-name table.

func LoadTable

func LoadTable() (*Table, error)

LoadTable decodes and validates the embedded table.

func (*Table) Legacy

func (t *Table) Legacy() []Row

Legacy lists the rows that are legacy tool names (every row but the shortcuts).

func (*Table) Lookup

func (t *Table) Lookup(name string) (*Row, bool)

Lookup returns the row for a tool name (legacy or shortcut).

Jump to

Keyboard shortcuts

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