optimizer

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package optimizer provides the Optimizer interface for intelligent tool discovery and invocation in the Virtual MCP Server.

When the optimizer is enabled, vMCP exposes only two tools to clients:

  • find_tool: Semantic search over available tools
  • call_tool: Dynamic invocation of any backend tool

This reduces token usage by avoiding the need to send all tool definitions to the LLM, instead allowing it to discover relevant tools on demand.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewOptimizerFactory added in v0.10.1

func NewOptimizerFactory(cfg *Config) (
	func(context.Context, []server.ServerTool) (Optimizer, error),
	func(context.Context) error,
	error,
)

NewOptimizerFactory creates the embedding client and SQLite tool store from the given OptimizerConfig, then returns an OptimizerFactory and a cleanup function that closes the store. The caller must invoke the cleanup function during shutdown to release resources.

Types

type CallToolInput

type CallToolInput struct {
	// ToolName is the name of the tool to invoke.
	ToolName string `json:"tool_name" description:"Name of the tool to call"`

	// Parameters are the arguments to pass to the tool.
	Parameters map[string]any `json:"parameters" description:"Parameters to pass to the tool"`
}

CallToolInput contains the parameters for calling a tool.

type Config added in v0.10.1

type Config = types.OptimizerConfig

Config defines configuration options for the Optimizer. It is defined in the internal/types package and aliased here so that external consumers continue to use optimizer.Config.

func GetAndValidateConfig added in v0.10.1

func GetAndValidateConfig(cfg *vmcpconfig.OptimizerConfig) (*Config, error)

GetAndValidateConfig validates the CRD-compatible OptimizerConfig and converts it to the internal optimizer.Config with parsed, typed values. Returns (nil, nil) if cfg is nil.

type FindToolInput

type FindToolInput struct {
	// ToolDescription is a natural language description of the tool to find.
	ToolDescription string `json:"tool_description" description:"Natural language description of the tool to find"`

	// ToolKeywords is an optional list of keywords to narrow the search.
	ToolKeywords []string `json:"tool_keywords,omitempty" description:"Optional keywords to narrow search"`
}

FindToolInput contains the parameters for finding tools.

type FindToolOutput

type FindToolOutput struct {
	// Tools contains the matching tools, ranked by relevance.
	Tools []ToolMatch `json:"tools"`

	// TokenMetrics provides information about token savings from using the optimizer.
	TokenMetrics TokenMetrics `json:"token_metrics"`
}

FindToolOutput contains the results of a tool search.

type Optimizer

type Optimizer interface {
	// FindTool searches for tools matching the given description and keywords.
	// Returns matching tools ranked by relevance.
	FindTool(ctx context.Context, input FindToolInput) (*FindToolOutput, error)

	// CallTool invokes a tool by name with the given parameters.
	// Returns the tool's result or an error if the tool is not found or execution fails.
	// Returns the MCP CallToolResult directly from the underlying tool handler.
	CallTool(ctx context.Context, input CallToolInput) (*mcp.CallToolResult, error)
}

Optimizer defines the interface for intelligent tool discovery and invocation.

The default implementation delegates search to a ToolStore (SQLite FTS5 with optional embedding-based semantic search) and scopes results to the tools registered for each session.

type TokenMetrics

type TokenMetrics = tokencounter.TokenMetrics

TokenMetrics provides information about token usage optimization. It is defined in the internal/tokencounter package and aliased here so that external consumers continue to use optimizer.TokenMetrics.

type ToolMatch

type ToolMatch = types.ToolMatch

ToolMatch represents a tool that matched the search criteria. It is defined in the internal/types package and aliased here so that external consumers continue to use optimizer.ToolMatch.

Directories

Path Synopsis
internal
similarity
Package similarity provides vector distance functions for semantic search.
Package similarity provides vector distance functions for semantic search.
tokencounter
Package tokencounter provides token estimation for MCP tool definitions.
Package tokencounter provides token estimation for MCP tool definitions.
toolstore
Package toolstore implements a SQLite-based ToolStore for search over MCP tool metadata.
Package toolstore implements a SQLite-based ToolStore for search over MCP tool metadata.
types
Package types defines shared types used across optimizer sub-packages.
Package types defines shared types used across optimizer sub-packages.
types/mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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