Documentation
¶
Overview ¶
Package toolset implements declared toolsets: the selector list an agent sends in the X-Muster-Toolset header (or passes to filter_tools) that bounds which of the gateway's tools its meta-tools can see and call.
A toolset is evaluated per request, statelessly, as the third filter on the per-session catalogue — after server-authentication visibility and workflow availability. It never widens access: the catalogue it filters is already the caller's own.
Inline grammar (header, filter_tools argument): preset:<name>, server:<name>, workflow:<name>, tool:<name> with exact names, at most MaxInlineSelectors. toolset:<name> is reserved; label: exists inside presets only. Patterns, excludes, the read-only predicate and composition live in presets, which are muster configuration (toolsetPresets) with read-only, none and full built in.
The package has one scope-source seam, SourceFromRequest: the header is the first source of a request's toolset, an actor claim from a future agent identity the second. Parsing, resolution and the refusal do not change with the source.
Index ¶
- Constants
- func CommonDescription(tools []mcp.Tool) string
- func ContextWithBound(ctx context.Context, selectors []string) context.Context
- func ContextWithSource(ctx context.Context, src Source) context.Context
- func FamilyMembers(description string) []string
- func Filter(tools []mcp.Tool, res Resolution) []mcp.Tool
- func FromScope(scopes []string) []string
- func HTTPContextFunc(ctx context.Context, r *http.Request) context.Context
- func InputSchemaFingerprint(tool mcp.Tool) string
- func Scoped(ctx context.Context) bool
- func SetFamilyMembers(tool *mcp.Tool, instanceArg string, servers []string)
- func SetToolOrigin(tool *mcp.Tool, origin ToolOrigin)
- type Entry
- type Info
- type Kind
- type OriginKind
- type Preset
- type PresetsConfig
- type Registry
- func (r *Registry) Check(ts Toolset) error
- func (r *Registry) Get(name string) (Preset, bool)
- func (r *Registry) List() []Info
- func (r *Registry) MaySelect(ts Toolset, u Unlisted, labels ServerLabels) bool
- func (r *Registry) Names() []string
- func (r *Registry) Resolve(ts Toolset, entries []Entry) (Resolution, error)
- func (r *Registry) ResolveWith(ts Toolset, entries []Entry, labels ServerLabels) (Resolution, error)
- type Resolution
- type Rule
- type Selector
- type ServerLabels
- type Source
- type ToolGroup
- type ToolOrigin
- type Toolset
- type Unlisted
Constants ¶
const ( PresetReadOnly = "read-only" PresetNone = "none" PresetFull = "full" )
Built-in preset names. They are always defined and cannot be redefined by configuration, so a toolset naming one of them can never hit the unknown-preset error.
const ( KindPreset = "preset" KindServer = "server" KindWorkflow = "workflow" KindTool = "tool" )
Inline selector kinds.
const HeaderName = "X-Muster-Toolset"
HeaderName is the request header that carries an agent's declared toolset.
const MaxInlineBytes = 8 << 10
MaxInlineBytes leaves room for authentication in an HTTP/2 header list.
const MaxInlineSelectors = 128
MaxInlineSelectors caps the number of selectors a toolset may carry inline (in the header or the filter_tools argument). Larger selections are presets.
const MetaKeyLabels = "muster.giantswarm.io/labels"
MetaKeyLabels mirrors api.MetaKeyLabels for tests in this package; the aggregator stashes discovery labels under it.
const MetaKeyOrigin = "muster.giantswarm.io/origin"
MetaKeyOrigin is the mcp.Tool._meta.AdditionalFields key under which the aggregator records where an exposed tool comes from (ToolOrigin). Like the discovery labels key it is an in-process annotation: the meta-tools project it into their own response fields (server, kind) and it is never sent to a client as _meta.
Variables ¶
This section is empty.
Functions ¶
func CommonDescription ¶ added in v5.50.1
CommonDescription returns the description most of the tools carry. A tie goes to the longest description, as a newer member tends to describe a tool more fully, then to the alphabetically first, so the choice is stable.
func ContextWithBound ¶ added in v5.49.0
ContextWithBound stores the toolset bound to the caller's Session, the selectors of its Session binding as stored. nil binds none (the request stays unscoped unless it declares a header).
func ContextWithSource ¶
ContextWithSource stores the request's toolset source in ctx.
func FamilyMembers ¶ added in v5.44.1
FamilyMembers returns the members a family-grouped tool's description trailer names, in trailer order, or nil when the description carries no trailer.
func Filter ¶
func Filter(tools []mcp.Tool, res Resolution) []mcp.Tool
Filter returns the tools whose names the resolution selected, in input order. A family tool selected on some of its members only is narrowed to them: its instance argument, description and origin name those members alone, so a resolution of the filtered tools cannot select the others.
func FromScope ¶ added in v5.58.0
FromScope picks the selectors out of the scope tokens of a token's scope claim (RFC 8693 §4.2): the scope tokens of the form kind:name whose kind is a selector kind, the reserved ones included so ParseInline refuses them. Other scope tokens, such as openid or email, are not selectors and are skipped.
func HTTPContextFunc ¶
HTTPContextFunc has the shape of mcp-go's server.HTTPContextFunc / SSEContextFunc: it stashes the request's toolset source in the context every tool handler receives. Evaluation is per request by construction: the header is never remembered on the MCP session.
func InputSchemaFingerprint ¶ added in v5.50.1
InputSchemaFingerprint returns a canonical JSON encoding of the arguments the tool's input schema accepts, raw or structured: the annotation keywords (description, title, examples, $comment) are dropped at every level, so two versions of a server that only reword a tool's argument descriptions compare equal, and key order does not matter.
func Scoped ¶ added in v5.49.0
Scoped reports whether the request is bounded by any toolset: one bound to its Session or one it declares.
func SetFamilyMembers ¶ added in v5.37.1
SetFamilyMembers advertises servers as the members a family-grouped tool runs on: a required string parameter named instanceArg whose enum lists them, and a description trailer naming them. It replaces what a previous call advertised. Properties and Required are deep-copied — including nested object properties and array items — so the per-server cached tool schema is not mutated by callers that walk the result.
func SetToolOrigin ¶
func SetToolOrigin(tool *mcp.Tool, origin ToolOrigin)
SetToolOrigin records origin on tool. The tool's _meta is cloned before it is written to, because exposed tools are shallow copies of the downstream tools the registry keeps and share their Meta pointer.
Types ¶
type Entry ¶
type Entry struct {
// Name is the exposed tool name (x_<server>_<tool>, workflow_<name>, core_*).
Name string
// Kind is tool, workflow or core.
Kind Kind
// Server is the owning MCPServer (the family name for a family tool).
// Empty for workflows and core tools.
Server string
// Servers lists the member servers of a family tool.
Servers []string
// InstanceArg is the argument a call to a family tool selects its member
// with; empty for every other entry.
InstanceArg string
// ReadOnly is the tool's readOnlyHint annotation (for a workflow the
// derived hint the aggregator computed from its step tools).
ReadOnly bool
}
Entry is what toolset resolution knows about one exposed tool.
func EntriesFromTools ¶
EntriesFromTools projects exposed tools to resolution entries. Kind and server come from the origin the aggregator stashed on the tool; tools without one are classified by name prefix, which is how the aggregator itself tells core and workflow tools apart.
type Info ¶
type Info struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
BuiltIn bool `json:"built_in"`
}
Info describes a preset for discovery (filter_tools include_presets).
type OriginKind ¶
type OriginKind string
OriginKind classifies an exposed tool by what serves it.
const ( // OriginKindTool is a tool served by a registered MCP server // (x_<server>_<tool> or x_<family>_<tool>). OriginKindTool OriginKind = "tool" // OriginKindWorkflow is a workflow execution tool (workflow_<name>). OriginKindWorkflow OriginKind = "workflow" // OriginKindCore is one of muster's own core_* tools. OriginKindCore OriginKind = "core" )
type Preset ¶
type Preset struct {
Description string `yaml:"description,omitempty" json:"description,omitempty"`
Icon string `yaml:"icon,omitempty" json:"icon,omitempty"`
Include []Rule `yaml:"include" json:"include"`
Exclude []Rule `yaml:"exclude,omitempty" json:"exclude,omitempty"`
}
Preset is a named, described selection of tools: the union of its include rules minus its exclude rules. Icon is an optional data: URI (svg, png, jpeg or webp, at most 65536 characters), validated and stored sanitised like an MCPServer's status.icon.
type PresetsConfig ¶
PresetsConfig is the `toolsetPresets:` block of muster's configuration.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds the built-in presets and the configured ones, validated.
func BuiltIns ¶
func BuiltIns() *Registry
BuiltIns returns a registry with only the built-in presets.
func NewRegistry ¶
func NewRegistry(cfg PresetsConfig) (*Registry, error)
NewRegistry validates cfg and returns the registry of built-in plus configured presets. Every validation error names the preset (and rule) so the operator can fix the values; the first error stops startup.
func (*Registry) Check ¶
Check verifies that every preset the toolset names is known. It is what makes an unknown preset an error on every meta-tool call, before any catalogue is consulted.
func (*Registry) MaySelect ¶ added in v5.37.1
func (r *Registry) MaySelect(ts Toolset, u Unlisted, labels ServerLabels) bool
MaySelect reports whether ts could select the unlisted tool (or, with an empty Name, some tool of the server) once the session signs in and the tool is listed. A readOnly rule may or may not match an annotation that is not known yet: it counts as a possible include and never as an exclude, so MaySelect is true whenever the resolution after sign-in could hold the tool, and false only when no listing can put it in the toolset.
func (*Registry) Names ¶
Names lists every known preset: built-ins first, then configured presets sorted by name. This is the order error messages and List use.
func (*Registry) Resolve ¶
func (r *Registry) Resolve(ts Toolset, entries []Entry) (Resolution, error)
Resolve evaluates ts against entries without a label lookup: label rules select nothing. See ResolveWith.
func (*Registry) ResolveWith ¶
func (r *Registry) ResolveWith(ts Toolset, entries []Entry, labels ServerLabels) (Resolution, error)
ResolveWith evaluates ts against entries. Inline selectors are unioned; each preset resolves to its includes minus its excludes, recursively for composed presets. Every preset the toolset names must exist (Check) or the unknown-preset error is returned. labels resolves an MCPServer name to its resource labels for label: rules and is consulted lazily — only when such a rule is evaluated — so a request whose presets carry no label rule never pays for the lookup; nil means no labels are known.
A family tool is evaluated once per member, as that member serves it (Entry.Member): a rule naming one member selects or excludes the tool on that member alone. The tool is selected when the toolset selects it on at least one member, and only those members are inside the toolset.
type Resolution ¶
type Resolution struct {
// Selected holds the exposed names of the tools the toolset resolves to.
Selected map[string]struct{}
// Unmatched lists the inline selectors (as written) that selected no
// tool in this catalogue. preset:none is empty by definition and is never
// reported.
Unmatched []string
// Servers holds every server that contributes at least one selected tool:
// for a family tool, the members the toolset selects it on. Resource and
// prompt accessors use it to decide which servers are inside the toolset.
Servers map[string]struct{}
// contains filtered or unexported fields
}
Resolution is the outcome of evaluating a toolset against a catalogue.
func (Resolution) CallInside ¶ added in v5.37.1
CallInside reports whether a call to the exposed tool name with args stays inside the toolset. For a family tool the call runs on the member its instance argument selects, so that member must be one the toolset selects the tool on: server:<member> selects one member, while server:<family>, tool:, pattern: and readOnly rules select every member. member is the member the call selects, "" for a solo tool or a call without the argument (the aggregator refuses that call as missing its argument).
func (Resolution) Contains ¶
func (r Resolution) Contains(name string) bool
Contains reports whether the exposed tool name is inside the resolution.
func (Resolution) ContainsServer ¶
func (r Resolution) ContainsServer(server string) bool
ContainsServer reports whether a server contributes to the resolution.
func (Resolution) Names ¶
func (r Resolution) Names() []string
Names returns the selected exposed names, sorted.
type Rule ¶
type Rule struct {
Tool string `yaml:"tool,omitempty" json:"tool,omitempty"`
Pattern string `yaml:"pattern,omitempty" json:"pattern,omitempty"`
Server string `yaml:"server,omitempty" json:"server,omitempty"`
Workflow string `yaml:"workflow,omitempty" json:"workflow,omitempty"`
ReadOnly *bool `yaml:"readOnly,omitempty" json:"readOnly,omitempty"`
Preset string `yaml:"preset,omitempty" json:"preset,omitempty"`
Label string `yaml:"label,omitempty" json:"label,omitempty"`
}
Rule is one selector inside a preset. Exactly one field is set.
- tool: <exact exposed name>
- pattern: <glob on the exposed name> (e.g. core_*, x_mcp-kubernetes_*)
- server: <name> (every tool of that server; a family name selects the family surface)
- workflow: <name> (the workflow's execution tool)
- readOnly: true (every tool annotated read-only, including the derived workflow hint)
- preset: <name> (composition; include only)
- label: <key>=<value> | <key> (every tool of an MCPServer whose resource carries that label; #1168)
type ServerLabels ¶
ServerLabels resolves an MCPServer name to its resource labels (nil when the server is unknown). Label: preset rules consult it lazily, per request, so a server gaining or losing a label changes the resolution on the next request without a restart (#1168).
type Source ¶
Source is where a request's toolset came from, before parsing. Present is false when the request declared no toolset at all (today's unscoped behaviour); a present but empty value is a declared, invalid toolset.
func SourceFromContext ¶
SourceFromContext returns the toolset source stored by ContextWithSource.
func SourceFromRequest ¶
SourceFromRequest reads the toolset the request declares in the X-Muster-Toolset header. A request of a bound Session also carries the toolset of its Session binding (ContextWithBound); the header then only narrows it.
type ToolGroup ¶ added in v5.50.1
ToolGroup is a set of family members and their contribution of one tool, Servers[i] contributing Tools[i].
func SplitFamilyTool ¶ added in v5.50.1
SplitFamilyTool splits the members contributing one family tool by the arguments their input schema accepts (InputSchemaFingerprint). The largest schema group keeps the family name (a tie goes to the group of the alphabetically first server, so the choice is stable across listings); rest holds the members of every other schema group, which fall back to per-server names. Descriptions take no part: members whose tool differs only in its own or its arguments' descriptions are one group.
type ToolOrigin ¶
type ToolOrigin struct {
// Kind classifies the tool: tool, workflow or core.
Kind OriginKind
// Server is the owning MCPServer for a server tool. For a family-grouped
// tool it is the family name, which is what the exposed name carries.
// Empty for workflows and core tools.
Server string
// Servers lists the member servers providing a family-grouped tool, in
// sorted order. Empty for solo tools, workflows and core tools.
Servers []string
// InstanceArg is the argument a call to a family-grouped tool selects its
// member with. Empty for every other tool.
InstanceArg string
}
ToolOrigin records where an exposed tool comes from. It is stashed on the mcp.Tool by the aggregator when the exposed catalogue is assembled and read by the meta-tools (server / kind in list_tools, filter_tools, describe_tool) and by toolset resolution (server:<name> selectors).
func ToolOriginOf ¶
func ToolOriginOf(tool mcp.Tool) (ToolOrigin, bool)
ToolOriginOf returns the origin recorded on tool, if any.
type Toolset ¶
Toolset is a parsed inline toolset: the selectors as given (trimmed) and their parsed form. Raw is kept for echoing and for naming the toolset in errors exactly as the caller wrote it.
func BoundFromContext ¶ added in v5.49.0
BoundFromContext parses the toolset bound to the caller's Session. present is false when the Session binds none. A bound toolset that fails to parse returns the error, which callers surface on every meta-tool call.
func FromContext ¶
FromContext parses the toolset the request declared in its header, without the bound one (BoundFromContext). present is false when the request carried none. A present toolset that fails to parse returns the error, which callers surface on every meta-tool call.
func ParseHeader ¶
ParseHeader parses the value of the X-Muster-Toolset header: a comma-separated list of inline selectors with whitespace around each selector trimmed. The caller decides whether the header was present; a present-but-empty value is an error here, never "no toolset".
func ParseInline ¶
ParseInline parses a list of inline selectors (the filter_tools "toolset" argument, or a header already split on commas). Every error names the toolset and the offending selector so the caller can relay it verbatim.
type Unlisted ¶ added in v5.37.1
type Unlisted struct {
// Server is the MCPServer that would serve the tool.
Server string
// Family is the server's family name when its tools are grouped under
// it, as the family's listed tools carry it in Entry.Server.
Family string
// Prefix is the exposed prefix every tool of the server carries.
Prefix string
// Name is the exposed tool name. Empty asks about any tool of the server.
Name string
// Owns reports whether an exposed name is one of the server's tools; it
// answers tool selectors and rules when Name is empty.
Owns func(exposedName string) bool
}
Unlisted describes a server tool the session cannot list yet because it has not signed in to the server: what is known of it before sign-in is its name, its server and the server's labels, never its annotations.