Documentation
¶
Overview ¶
Package router provides request routing for Virtual MCP Server.
This package routes MCP protocol requests (tools, resources, prompts) to appropriate backend workloads. It supports session affinity and pluggable routing strategies for load balancing.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrToolNotFound indicates the requested tool doesn't exist. ErrToolNotFound = fmt.Errorf("tool not found") // ErrResourceNotFound indicates the requested resource doesn't exist. ErrResourceNotFound = fmt.Errorf("resource not found") // ErrPromptNotFound indicates the requested prompt doesn't exist. ErrPromptNotFound = fmt.Errorf("prompt not found") // ErrNoHealthyBackends indicates no healthy backends are available. ErrNoHealthyBackends = fmt.Errorf("no healthy backends available") ErrBackendUnavailable = fmt.Errorf("backend unavailable") )
Common routing errors.
Functions ¶
This section is empty.
Types ¶
type Router ¶
type Router interface {
// RouteTool resolves a tool name to its backend target.
// Returns ErrToolNotFound if the tool doesn't exist in any backend.
RouteTool(ctx context.Context, toolName string) (*vmcp.BackendTarget, error)
// RouteResource resolves a resource URI to its backend target.
// Returns ErrResourceNotFound if the resource doesn't exist in any backend.
RouteResource(ctx context.Context, uri string) (*vmcp.BackendTarget, error)
// RoutePrompt resolves a prompt name to its backend target.
// Returns ErrPromptNotFound if the prompt doesn't exist in any backend.
RoutePrompt(ctx context.Context, name string) (*vmcp.BackendTarget, error)
}
Router routes MCP protocol requests to appropriate backend workloads. Implementations must be safe for concurrent use.
With lazy discovery, the router retrieves capabilities from the request context rather than maintaining cached state. This enables per-request routing decisions based on real-time backend availability.
func NewDefaultRouter ¶ added in v0.5.2
func NewDefaultRouter() Router
NewDefaultRouter creates a new default router instance.
type RoutingStrategy ¶
type RoutingStrategy interface {
// SelectBackend chooses a backend from available candidates.
// Returns ErrNoHealthyBackends if no backends are available.
SelectBackend(ctx context.Context, candidates []*vmcp.BackendTarget) (*vmcp.BackendTarget, error)
}
RoutingStrategy defines how requests are routed when multiple backends can handle the same request (e.g., replicas for load balancing).
type SessionAffinityProvider ¶
type SessionAffinityProvider interface {
// GetBackendForSession returns the backend for a given session ID.
// Returns nil if no affinity exists for this session.
GetBackendForSession(ctx context.Context, sessionID string) (*vmcp.BackendTarget, error)
// SetBackendForSession establishes session affinity.
SetBackendForSession(ctx context.Context, sessionID string, target *vmcp.BackendTarget) error
// RemoveSession clears session affinity.
RemoveSession(ctx context.Context, sessionID string) error
}
SessionAffinityProvider manages session-to-backend mappings. This ensures requests from the same MCP session are routed to the same backend.