variants

package
v0.0.0-...-b95923d Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HintValue

func HintValue[T any](h VariantHints, key string) (T, bool)

HintValue retrieves a typed value from the Hints map for the given key. It returns the zero value and false if the key is missing, the map is nil, or the stored value is not assignable to T.

func NewStreamableHTTPHandler

func NewStreamableHTTPHandler(vs *Server, opts *mcp.StreamableHTTPOptions) *mcp.StreamableHTTPHandler

NewStreamableHTTPHandler returns a new mcp.StreamableHTTPHandler for serving multiple concurrent clients over HTTP. It mirrors mcp.NewStreamableHTTPHandler.

handler := variants.NewStreamableHTTPHandler(vs, nil)
http.ListenAndServe(":8080", handler)

Types

type DeprecationInfo

type DeprecationInfo struct {
	// Message is a human-readable message explaining why this variant is
	// deprecated and how to migrate.
	Message string `json:"message"`

	// Replacement is the suggested replacement variant identifier.
	Replacement string `json:"replacement,omitempty"`

	// RemovalDate is an optional ISO 8601 date when this variant is planned
	// to be removed. Servers SHOULD continue to support the variant until
	// that date.
	RemovalDate string `json:"removalDate,omitempty"`
}

DeprecationInfo provides migration guidance for deprecated variants.

type HintKey

type HintKey = string

HintKey is a well-known key for use in VariantHints.Hints and ServerVariant.Hints.

const (
	// HintModelFamily identifies the target model family/provider.
	// Common values: "anthropic", "openai", "google", "meta", "local", "any".
	HintModelFamily HintKey = "modelFamily"

	// HintUseCase identifies the intended usage scenario.
	// Common values: "autonomous-agent", "human-assistant", "ide", "api",
	// "chat", "planning", "execution".
	HintUseCase HintKey = "useCase"

	// HintContextSize indicates the desired verbosity / token efficiency.
	// Common values: "compact", "standard", "verbose".
	HintContextSize HintKey = "contextSize"

	// HintRenderingCapabilities describes the client's rendering support.
	// Common values: "rich", "markdown", "text-only".
	HintRenderingCapabilities HintKey = "renderingCapabilities"

	// HintLanguageOptimization indicates natural language optimization.
	// Common values: "en", "multilingual", "code-focused".
	HintLanguageOptimization HintKey = "languageOptimization"
)

Well-known hint keys defined by the SEP (Common Hint Vocabulary).

type RankingFunc

type RankingFunc func(ctx context.Context, hints VariantHints, variants []ServerVariant) []ServerVariant

RankingFunc is called during initialization to rank available variants based on the client's hints. It receives the client-provided hints and the full set of registered variants, and must return them sorted by relevance with the highest-priority variant first. The first variant in the returned slice is the recommended default and will be used when a client does not explicitly select a variant via _meta.

Each ServerVariant carries its Priority field (set via WithVariant), which the ranking function may use as a baseline signal alongside client hints.

Note: The default (first) variant is also used when the client does not support variants at all.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a variant-aware MCP server that multiplexes across multiple mcp.Server instances, each associated with a ServerVariant. During initialization, clients provide VariantHints and the server responds with a ranked list of available variants. Per-request variant selection is carried in the _meta field.

Server holds only configuration and is safe for concurrent use. In stateful mode (the default), per-session inner connections are created during initialize and scoped to the front session's lifetime. In stateless mode (via NewStreamableHTTPHandler with Stateless option), a single set of shared connections is created at construction and reused across all requests.

func NewServer

func NewServer(impl *mcp.Implementation) *Server

NewServer creates a new variant-aware server with no registered variants.

The first argument must not be nil.

func (*Server) Close

func (s *Server) Close() error

Close releases resources held by all registered backends and, in stateless mode, tears down the shared inner connections.

func (*Server) RankedVariants

func (s *Server) RankedVariants(ctx context.Context, hints VariantHints) []ServerVariant

RankedVariants returns the registered variants ranked according to the configured RankingFunc (or the default priority-based ranking if none is set).

func (*Server) Run

func (s *Server) Run(ctx context.Context, t mcp.Transport) error

Run starts the variant server on the given transport (e.g., stdio). For multi-client HTTP support, use NewStreamableHTTPHandler instead.

func (*Server) Variants

func (s *Server) Variants() []ServerVariant

Variants returns a copy of all registered ServerVariant values in registration order.

func (*Server) WithHTTPVariant

func (s *Server) WithHTTPVariant(v ServerVariant, mcpServer *mcp.Server, priority int) *Server

WithHTTPVariant registers a ServerVariant backed by an mcp.Server exposed over HTTP. Not yet implemented.

func (*Server) WithRanking

func (s *Server) WithRanking(fn RankingFunc) *Server

WithRanking sets a custom ranking function used to order variants based on client hints during initialization. The function should return variants sorted by relevance, with the most appropriate variant first. If nil, variants are ordered by their priority value (lowest first).

Returns the receiver for chaining.

func (*Server) WithRemoteVariant

func (s *Server) WithRemoteVariant(v ServerVariant, endpoint string, priority int) *Server

WithRemoteVariant registers a ServerVariant backed by a remote MCP server at the given endpoint URL. Not yet implemented.

func (*Server) WithVariant

func (s *Server) WithVariant(v ServerVariant, mcpServer *mcp.Server, priority int) *Server

WithVariant registers a ServerVariant backed by the given mcp.Server. priority determines the default ordering when no RankingFunc is set; lower values indicate higher importance (0 = highest priority). By default, the variant with the lowest priority value will appear first in the list and serve as the recommended default for clients. This behavior can be overridden by providing a custom RankingFunc.

Variant IDs must be unique; registering a duplicate panics.

type ServerVariant

type ServerVariant struct {
	// ID is a unique identifier for this variant. Freeform string that servers
	// define. Examples: "claude-optimized", "gpt-optimized", "compact",
	// "agent-plan".
	//
	// Each variant's ID MUST be unique within availableVariants.
	ID string `json:"id"`

	// Description is a human-readable description of this variant, suitable
	// for display to users or for LLM reasoning about variant selection.
	//
	// SHOULD include:
	//   - Target use case or model family
	//   - Key characteristics or optimizations
	//   - Trade-offs compared to other variants
	Description string `json:"description"`

	// Hints are key-value pairs providing structured metadata for intelligent
	// variant selection. Clients and LLMs can use these hints to
	// programmatically filter and rank variants.
	//
	// Unknown hint keys MUST be ignored by clients and servers.
	Hints map[string]string `json:"hints,omitempty"`

	// Status is the stability status of this variant.
	// Defaults to Stable if empty.
	Status VariantStatus `json:"status,omitempty"`

	// DeprecationInfo provides migration guidance when Status is Deprecated.
	DeprecationInfo *DeprecationInfo `json:"deprecationInfo,omitempty"`
	// contains filtered or unexported fields
}

ServerVariant describes a server capability variant that clients can select. Each variant represents a distinct configuration of all server capabilities (tools, resources, prompts, subscriptions).

func (ServerVariant) Priority

func (v ServerVariant) Priority() int

Priority returns the variant's priority value. Lower values indicate higher importance (0 = highest priority).

type VariantHints

type VariantHints struct {
	// Description is a human-readable description of the client's context and
	// requirements.
	Description string `json:"description,omitempty"`

	// Hints are key-value pairs providing structured metadata for variant
	// selection. Values can be a single string or an array of strings (in
	// order of preference).
	//
	// Unknown hint keys MUST be ignored by clients and servers.
	Hints map[string]any `json:"hints,omitempty"`
}

VariantHints are structured hints provided by the client to help the server rank available variants.

type VariantStatus

type VariantStatus string

VariantStatus represents the stability status of a server variant.

const (
	// Stable indicates a production-ready variant recommended for general use.
	Stable VariantStatus = "stable"
	// Experimental indicates a variant that may change without notice; use for testing.
	Experimental VariantStatus = "experimental"
	// Deprecated indicates a variant that will be removed in a future release.
	Deprecated VariantStatus = "deprecated"
)

Jump to

Keyboard shortcuts

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